blob: 1e94dbd86669220e9850083f9b8e691586f3e5bf [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"
Chris Lattner9c254f02010-06-29 06:01:59 +000020#include "llvm/Target/TargetData.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000021#include "llvm/ADT/StringExtras.h"
Daniel Dunbar2c0843f2009-08-24 08:52:16 +000022#include "llvm/ADT/Triple.h"
Daniel Dunbar28df7a52009-12-03 09:13:49 +000023#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000024using namespace clang;
25using namespace CodeGen;
26
John McCallaeeb7012010-05-27 06:19:26 +000027static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
28 llvm::Value *Array,
29 llvm::Value *Value,
30 unsigned FirstIndex,
31 unsigned LastIndex) {
32 // Alternatively, we could emit this as a loop in the source.
33 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
34 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
35 Builder.CreateStore(Value, Cell);
36 }
37}
38
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000039ABIInfo::~ABIInfo() {}
40
Chris Lattnerea044322010-07-29 02:01:43 +000041ASTContext &ABIInfo::getContext() const {
42 return CGT.getContext();
43}
44
45llvm::LLVMContext &ABIInfo::getVMContext() const {
46 return CGT.getLLVMContext();
47}
48
49const llvm::TargetData &ABIInfo::getTargetData() const {
50 return CGT.getTargetData();
51}
52
53
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000054void ABIArgInfo::dump() const {
Daniel Dunbar28df7a52009-12-03 09:13:49 +000055 llvm::raw_ostream &OS = llvm::errs();
56 OS << "(ABIArgInfo Kind=";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000057 switch (TheKind) {
58 case Direct:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000059 OS << "Direct";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000060 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000061 case Extend:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000062 OS << "Extend";
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000063 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000064 case Ignore:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000065 OS << "Ignore";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000066 break;
67 case Coerce:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000068 OS << "Coerce Type=";
69 getCoerceToType()->print(OS);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000070 break;
71 case Indirect:
Daniel Dunbardc6d5742010-04-21 19:10:51 +000072 OS << "Indirect Align=" << getIndirectAlign()
73 << " Byal=" << getIndirectByVal();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000074 break;
75 case Expand:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000076 OS << "Expand";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000077 break;
78 }
Daniel Dunbar28df7a52009-12-03 09:13:49 +000079 OS << ")\n";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000080}
81
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000082TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
83
Daniel Dunbar98303b92009-09-13 08:03:58 +000084static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000085
86/// isEmptyField - Return true iff a the field is "empty", that is it
87/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +000088static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
89 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000090 if (FD->isUnnamedBitfield())
91 return true;
92
93 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000094
Daniel Dunbar98303b92009-09-13 08:03:58 +000095 // Constant arrays of empty records count as empty, strip them off.
96 if (AllowArrays)
97 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
98 FT = AT->getElementType();
99
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000100 const RecordType *RT = FT->getAs<RecordType>();
101 if (!RT)
102 return false;
103
104 // C++ record fields are never empty, at least in the Itanium ABI.
105 //
106 // FIXME: We should use a predicate for whether this behavior is true in the
107 // current ABI.
108 if (isa<CXXRecordDecl>(RT->getDecl()))
109 return false;
110
Daniel Dunbar98303b92009-09-13 08:03:58 +0000111 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000112}
113
114/// isEmptyRecord - Return true iff a structure contains only empty
115/// fields. Note that a structure with a flexible array member is not
116/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000117static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000118 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000119 if (!RT)
120 return 0;
121 const RecordDecl *RD = RT->getDecl();
122 if (RD->hasFlexibleArrayMember())
123 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000124
125 // If this is a C++ record, check the bases first.
126 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
127 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
128 e = CXXRD->bases_end(); i != e; ++i)
129 if (!isEmptyRecord(Context, i->getType(), true))
130 return false;
131
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000132 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
133 i != e; ++i)
Daniel Dunbar98303b92009-09-13 08:03:58 +0000134 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000135 return false;
136 return true;
137}
138
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000139/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
140/// a non-trivial destructor or a non-trivial copy constructor.
141static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
142 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
143 if (!RD)
144 return false;
145
146 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
147}
148
149/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
150/// a record type with either a non-trivial destructor or a non-trivial copy
151/// constructor.
152static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
153 const RecordType *RT = T->getAs<RecordType>();
154 if (!RT)
155 return false;
156
157 return hasNonTrivialDestructorOrCopyConstructor(RT);
158}
159
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000160/// isSingleElementStruct - Determine if a structure is a "single
161/// element struct", i.e. it has exactly one non-empty field or
162/// exactly one field which is itself a single element
163/// struct. Structures with flexible array members are never
164/// considered single element structs.
165///
166/// \return The field declaration for the single non-empty field, if
167/// it exists.
168static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
169 const RecordType *RT = T->getAsStructureType();
170 if (!RT)
171 return 0;
172
173 const RecordDecl *RD = RT->getDecl();
174 if (RD->hasFlexibleArrayMember())
175 return 0;
176
177 const Type *Found = 0;
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000178
179 // If this is a C++ record, check the bases first.
180 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
181 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
182 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000183 // Ignore empty records.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000184 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000185 continue;
186
187 // If we already found an element then this isn't a single-element struct.
188 if (Found)
189 return 0;
190
191 // If this is non-empty and not a single element struct, the composite
192 // cannot be a single element struct.
193 Found = isSingleElementStruct(i->getType(), Context);
194 if (!Found)
195 return 0;
196 }
197 }
198
199 // Check for single element.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000200 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
201 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000202 const FieldDecl *FD = *i;
203 QualType FT = FD->getType();
204
205 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000206 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000207 continue;
208
209 // If we already found an element then this isn't a single-element
210 // struct.
211 if (Found)
212 return 0;
213
214 // Treat single element arrays as the element.
215 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
216 if (AT->getSize().getZExtValue() != 1)
217 break;
218 FT = AT->getElementType();
219 }
220
221 if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
222 Found = FT.getTypePtr();
223 } else {
224 Found = isSingleElementStruct(FT, Context);
225 if (!Found)
226 return 0;
227 }
228 }
229
230 return Found;
231}
232
233static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbara1842d32010-05-14 03:40:53 +0000234 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000235 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
236 !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000237 return false;
238
239 uint64_t Size = Context.getTypeSize(Ty);
240 return Size == 32 || Size == 64;
241}
242
Daniel Dunbar53012f42009-11-09 01:33:53 +0000243/// canExpandIndirectArgument - Test whether an argument type which is to be
244/// passed indirectly (on the stack) would have the equivalent layout if it was
245/// expanded into separate arguments. If so, we prefer to do the latter to avoid
246/// inhibiting optimizations.
247///
248// FIXME: This predicate is missing many cases, currently it just follows
249// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
250// should probably make this smarter, or better yet make the LLVM backend
251// capable of handling it.
252static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
253 // We can only expand structure types.
254 const RecordType *RT = Ty->getAs<RecordType>();
255 if (!RT)
256 return false;
257
258 // We can only expand (C) structures.
259 //
260 // FIXME: This needs to be generalized to handle classes as well.
261 const RecordDecl *RD = RT->getDecl();
262 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
263 return false;
264
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000265 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
266 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000267 const FieldDecl *FD = *i;
268
269 if (!is32Or64BitBasicType(FD->getType(), Context))
270 return false;
271
272 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
273 // how to expand them yet, and the predicate for telling if a bitfield still
274 // counts as "basic" is more complicated than what we were doing previously.
275 if (FD->isBitField())
276 return false;
277 }
278
279 return true;
280}
281
282namespace {
283/// DefaultABIInfo - The default implementation for ABI specific
284/// details. This implementation provides information which results in
285/// self-consistent and sensible LLVM IR generation, but does not
286/// conform to any particular ABI.
287class DefaultABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +0000288public:
289 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
290
Chris Lattnera3c109b2010-07-29 02:16:43 +0000291 ABIArgInfo classifyReturnType(QualType RetTy) const;
292 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000293
Chris Lattneree5dcd02010-07-29 02:31:05 +0000294 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000295 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000296 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
297 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000298 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000299 }
300
301 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
302 CodeGenFunction &CGF) const;
303};
304
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000305class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
306public:
Chris Lattnerea044322010-07-29 02:01:43 +0000307 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
308 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000309};
310
311llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
312 CodeGenFunction &CGF) const {
313 return 0;
314}
315
Chris Lattnera3c109b2010-07-29 02:16:43 +0000316ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
Chris Lattnera14db752010-03-11 18:19:55 +0000317 if (CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000318 return ABIArgInfo::getIndirect(0);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000319
Chris Lattnera14db752010-03-11 18:19:55 +0000320 // Treat an enum type as its underlying type.
321 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
322 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000323
Chris Lattnera14db752010-03-11 18:19:55 +0000324 return (Ty->isPromotableIntegerType() ?
325 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000326}
327
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000328//===----------------------------------------------------------------------===//
329// X86-32 ABI Implementation
330//===----------------------------------------------------------------------===//
331
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000332/// X86_32ABIInfo - The X86-32 ABI information.
333class X86_32ABIInfo : public ABIInfo {
David Chisnall1e4249c2009-08-17 23:08:21 +0000334 bool IsDarwinVectorABI;
335 bool IsSmallStructInRegABI;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000336
337 static bool isRegisterSize(unsigned Size) {
338 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
339 }
340
341 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
342
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000343 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
344 /// such that the argument will be passed in memory.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000345 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000346
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000347public:
Chris Lattnerea044322010-07-29 02:01:43 +0000348
Chris Lattnera3c109b2010-07-29 02:16:43 +0000349 ABIArgInfo classifyReturnType(QualType RetTy) const;
350 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000351
Chris Lattneree5dcd02010-07-29 02:31:05 +0000352 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000353 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000354 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
355 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000356 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000357 }
358
359 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
360 CodeGenFunction &CGF) const;
361
Chris Lattnerea044322010-07-29 02:01:43 +0000362 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
363 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000364};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000365
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000366class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
367public:
Chris Lattnerea044322010-07-29 02:01:43 +0000368 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
369 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000370
371 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
372 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000373
374 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
375 // Darwin uses different dwarf register numbers for EH.
376 if (CGM.isTargetDarwin()) return 5;
377
378 return 4;
379 }
380
381 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
382 llvm::Value *Address) const;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000383};
384
385}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000386
387/// shouldReturnTypeInRegister - Determine if the given type should be
388/// passed in a register (for the Darwin ABI).
389bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
390 ASTContext &Context) {
391 uint64_t Size = Context.getTypeSize(Ty);
392
393 // Type must be register sized.
394 if (!isRegisterSize(Size))
395 return false;
396
397 if (Ty->isVectorType()) {
398 // 64- and 128- bit vectors inside structures are not returned in
399 // registers.
400 if (Size == 64 || Size == 128)
401 return false;
402
403 return true;
404 }
405
Daniel Dunbar77115232010-05-15 00:00:30 +0000406 // If this is a builtin, pointer, enum, complex type, member pointer, or
407 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000408 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000409 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000410 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000411 return true;
412
413 // Arrays are treated like records.
414 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
415 return shouldReturnTypeInRegister(AT->getElementType(), Context);
416
417 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000418 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000419 if (!RT) return false;
420
Anders Carlssona8874232010-01-27 03:25:19 +0000421 // FIXME: Traverse bases here too.
422
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000423 // Structure types are passed in register if all fields would be
424 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000425 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
426 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000427 const FieldDecl *FD = *i;
428
429 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000430 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000431 continue;
432
433 // Check fields recursively.
434 if (!shouldReturnTypeInRegister(FD->getType(), Context))
435 return false;
436 }
437
438 return true;
439}
440
Chris Lattnera3c109b2010-07-29 02:16:43 +0000441ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const {
442 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000443 return ABIArgInfo::getIgnore();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000444
445 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000446 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000447 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000448 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000449
450 // 128-bit vectors are a special case; they are returned in
451 // registers and we need to make sure to pick a type the LLVM
452 // backend will like.
453 if (Size == 128)
Owen Anderson0032b272009-08-13 21:57:51 +0000454 return ABIArgInfo::getCoerce(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000455 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000456
457 // Always return in register if it fits in a general purpose
458 // register, or if it is 64 bits and has a single element.
459 if ((Size == 8 || Size == 16 || Size == 32) ||
460 (Size == 64 && VT->getNumElements() == 1))
Chris Lattnera3c109b2010-07-29 02:16:43 +0000461 return ABIArgInfo::getCoerce(llvm::IntegerType::get(getVMContext(),
462 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000463
464 return ABIArgInfo::getIndirect(0);
465 }
466
467 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000468 }
469
470 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000471 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000472 // Structures with either a non-trivial destructor or a non-trivial
473 // copy constructor are always indirect.
474 if (hasNonTrivialDestructorOrCopyConstructor(RT))
475 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
476
477 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000478 if (RT->getDecl()->hasFlexibleArrayMember())
479 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000480 }
481
David Chisnall1e4249c2009-08-17 23:08:21 +0000482 // If specified, structs and unions are always indirect.
483 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000484 return ABIArgInfo::getIndirect(0);
485
486 // Classify "single element" structs as their element type.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000487 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) {
John McCall183700f2009-09-21 23:43:11 +0000488 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000489 if (BT->isIntegerType()) {
490 // We need to use the size of the structure, padding
491 // bit-fields can adjust that to be larger than the single
492 // element type.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000493 uint64_t Size = getContext().getTypeSize(RetTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000494 return ABIArgInfo::getCoerce(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000495 llvm::IntegerType::get(getVMContext(), (unsigned)Size));
496 }
497
498 if (BT->getKind() == BuiltinType::Float) {
499 assert(getContext().getTypeSize(RetTy) ==
500 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000501 "Unexpect single element structure size!");
Chris Lattnera3c109b2010-07-29 02:16:43 +0000502 return ABIArgInfo::getCoerce(llvm::Type::getFloatTy(getVMContext()));
503 }
504
505 if (BT->getKind() == BuiltinType::Double) {
506 assert(getContext().getTypeSize(RetTy) ==
507 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000508 "Unexpect single element structure size!");
Chris Lattnera3c109b2010-07-29 02:16:43 +0000509 return ABIArgInfo::getCoerce(llvm::Type::getDoubleTy(getVMContext()));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000510 }
511 } else if (SeltTy->isPointerType()) {
512 // FIXME: It would be really nice if this could come out as the proper
513 // pointer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000514 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(getVMContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000515 return ABIArgInfo::getCoerce(PtrTy);
516 } else if (SeltTy->isVectorType()) {
517 // 64- and 128-bit vectors are never returned in a
518 // register when inside a structure.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000519 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000520 if (Size == 64 || Size == 128)
521 return ABIArgInfo::getIndirect(0);
522
Chris Lattnera3c109b2010-07-29 02:16:43 +0000523 return classifyReturnType(QualType(SeltTy, 0));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000524 }
525 }
526
527 // Small structures which are register sized are generally returned
528 // in a register.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000529 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) {
530 uint64_t Size = getContext().getTypeSize(RetTy);
531 return ABIArgInfo::getCoerce(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000532 }
533
534 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000535 }
Chris Lattnera3c109b2010-07-29 02:16:43 +0000536
537 // Treat an enum type as its underlying type.
538 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
539 RetTy = EnumTy->getDecl()->getIntegerType();
540
541 return (RetTy->isPromotableIntegerType() ?
542 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000543}
544
Chris Lattnera3c109b2010-07-29 02:16:43 +0000545ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000546 if (!ByVal)
547 return ABIArgInfo::getIndirect(0, false);
548
549 // Compute the byval alignment. We trust the back-end to honor the
550 // minimum ABI alignment for byval, to make cleaner IR.
551 const unsigned MinABIAlign = 4;
Chris Lattnera3c109b2010-07-29 02:16:43 +0000552 unsigned Align = getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000553 if (Align > MinABIAlign)
554 return ABIArgInfo::getIndirect(Align);
555 return ABIArgInfo::getIndirect(0);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000556}
557
Chris Lattnera3c109b2010-07-29 02:16:43 +0000558ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) 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))
Chris Lattnera3c109b2010-07-29 02:16:43 +0000566 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000567
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000568 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattnera3c109b2010-07-29 02:16:43 +0000569 return getIndirectResult(Ty);
Anders Carlssona8874232010-01-27 03:25:19 +0000570 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000571
572 // Ignore empty structs.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000573 if (Ty->isStructureType() && getContext().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.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000580 if (getContext().getTypeSize(Ty) <= 4*32 &&
581 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar53012f42009-11-09 01:33:53 +0000582 return ABIArgInfo::getExpand();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000583
Chris Lattnera3c109b2010-07-29 02:16:43 +0000584 return getIndirectResult(Ty);
585 }
586
587 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
588 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000589
Chris Lattnera3c109b2010-07-29 02:16:43 +0000590 return (Ty->isPromotableIntegerType() ?
591 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000592}
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.
Chris Lattner9c254f02010-06-29 06:01:59 +0000718 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000719
Chris Lattner5868ca22010-07-29 04:41:05 +0000720 const llvm::Type *Get8ByteTypeAtOffset(const llvm::Type *IRType,
721 unsigned IROffset, QualType SourceTy,
Chris Lattner44f0fd22010-07-29 02:20:19 +0000722 unsigned SourceOffset) const;
723
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000724 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
725 /// to coerce to, chose the best way to pass Ty in the same place
726 /// that \arg CoerceTo would be passed, but while keeping the
727 /// emitted code as simple as possible.
728 ///
729 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
730 /// the ways we might want to pass things, instead of constructing an LLVM
731 /// type. This makes this code more explicit, and it makes it clearer that we
732 /// are also doing this for correctness in the case of passing scalar types.
733 ABIArgInfo getCoerceResult(QualType Ty,
Chris Lattner9c254f02010-06-29 06:01:59 +0000734 const llvm::Type *CoerceTo) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000735
736 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000737 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000738 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000739
740 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000741 /// such that the argument will be passed in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000742 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000743
Chris Lattnera3c109b2010-07-29 02:16:43 +0000744 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000745
Chris Lattner5868ca22010-07-29 04:41:05 +0000746 ABIArgInfo classifyArgumentType(QualType Ty, unsigned &neededInt,
747 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000748
749public:
Chris Lattnerea044322010-07-29 02:01:43 +0000750 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Chris Lattner9c254f02010-06-29 06:01:59 +0000751
Chris Lattneree5dcd02010-07-29 02:31:05 +0000752 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000753
754 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
755 CodeGenFunction &CGF) const;
756};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000757
758class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
759public:
Chris Lattnerea044322010-07-29 02:01:43 +0000760 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
761 : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {}
John McCall6374c332010-03-06 00:35:14 +0000762
763 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
764 return 7;
765 }
766
767 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
768 llvm::Value *Address) const {
769 CodeGen::CGBuilderTy &Builder = CGF.Builder;
770 llvm::LLVMContext &Context = CGF.getLLVMContext();
771
772 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
773 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
774
John McCallaeeb7012010-05-27 06:19:26 +0000775 // 0-15 are the 16 integer registers.
776 // 16 is %rip.
777 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +0000778
779 return false;
780 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000781};
782
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000783}
784
Chris Lattner1090a9b2010-06-28 21:43:59 +0000785X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000786 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
787 // classified recursively so that always two fields are
788 // considered. The resulting class is calculated according to
789 // the classes of the fields in the eightbyte:
790 //
791 // (a) If both classes are equal, this is the resulting class.
792 //
793 // (b) If one of the classes is NO_CLASS, the resulting class is
794 // the other class.
795 //
796 // (c) If one of the classes is MEMORY, the result is the MEMORY
797 // class.
798 //
799 // (d) If one of the classes is INTEGER, the result is the
800 // INTEGER.
801 //
802 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
803 // MEMORY is used as class.
804 //
805 // (f) Otherwise class SSE is used.
806
807 // Accum should never be memory (we should have returned) or
808 // ComplexX87 (because this cannot be passed in a structure).
809 assert((Accum != Memory && Accum != ComplexX87) &&
810 "Invalid accumulated classification during merge.");
811 if (Accum == Field || Field == NoClass)
812 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000813 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000814 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000815 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000816 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000817 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000818 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000819 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
820 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000821 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000822 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000823}
824
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000825void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000826 Class &Lo, Class &Hi) const {
827 // FIXME: This code can be simplified by introducing a simple value class for
828 // Class pairs with appropriate constructor methods for the various
829 // situations.
830
831 // FIXME: Some of the split computations are wrong; unaligned vectors
832 // shouldn't be passed in registers for example, so there is no chance they
833 // can straddle an eightbyte. Verify & simplify.
834
835 Lo = Hi = NoClass;
836
837 Class &Current = OffsetBase < 64 ? Lo : Hi;
838 Current = Memory;
839
John McCall183700f2009-09-21 23:43:11 +0000840 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000841 BuiltinType::Kind k = BT->getKind();
842
843 if (k == BuiltinType::Void) {
844 Current = NoClass;
845 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
846 Lo = Integer;
847 Hi = Integer;
848 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
849 Current = Integer;
850 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
851 Current = SSE;
852 } else if (k == BuiltinType::LongDouble) {
853 Lo = X87;
854 Hi = X87Up;
855 }
856 // FIXME: _Decimal32 and _Decimal64 are SSE.
857 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +0000858 return;
859 }
860
861 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000862 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +0000863 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +0000864 return;
865 }
866
867 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000868 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000869 return;
870 }
871
872 if (Ty->isMemberPointerType()) {
Daniel Dunbar67d438d2010-05-15 00:00:37 +0000873 if (Ty->isMemberFunctionPointerType())
874 Lo = Hi = Integer;
875 else
876 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000877 return;
878 }
879
880 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +0000881 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000882 if (Size == 32) {
883 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
884 // float> as integer.
885 Current = Integer;
886
887 // If this type crosses an eightbyte boundary, it should be
888 // split.
889 uint64_t EB_Real = (OffsetBase) / 64;
890 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
891 if (EB_Real != EB_Imag)
892 Hi = Lo;
893 } else if (Size == 64) {
894 // gcc passes <1 x double> in memory. :(
895 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
896 return;
897
898 // gcc passes <1 x long long> as INTEGER.
899 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
900 Current = Integer;
901 else
902 Current = SSE;
903
904 // If this type crosses an eightbyte boundary, it should be
905 // split.
906 if (OffsetBase && OffsetBase != 64)
907 Hi = Lo;
908 } else if (Size == 128) {
909 Lo = SSE;
910 Hi = SSEUp;
911 }
Chris Lattner1090a9b2010-06-28 21:43:59 +0000912 return;
913 }
914
915 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +0000916 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000917
Chris Lattnerea044322010-07-29 02:01:43 +0000918 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000919 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000920 if (Size <= 64)
921 Current = Integer;
922 else if (Size <= 128)
923 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +0000924 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000925 Current = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +0000926 else if (ET == getContext().DoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000927 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +0000928 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000929 Current = ComplexX87;
930
931 // If this complex type crosses an eightbyte boundary then it
932 // should be split.
933 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +0000934 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000935 if (Hi == NoClass && EB_Real != EB_Imag)
936 Hi = Lo;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000937
938 return;
939 }
940
Chris Lattnerea044322010-07-29 02:01:43 +0000941 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000942 // Arrays are treated like structures.
943
Chris Lattnerea044322010-07-29 02:01:43 +0000944 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000945
946 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
947 // than two eightbytes, ..., it has class MEMORY.
948 if (Size > 128)
949 return;
950
951 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
952 // fields, it has class MEMORY.
953 //
954 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +0000955 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000956 return;
957
958 // Otherwise implement simplified merge. We could be smarter about
959 // this, but it isn't worth it and would be harder to verify.
960 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +0000961 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000962 uint64_t ArraySize = AT->getSize().getZExtValue();
963 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
964 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +0000965 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000966 Lo = merge(Lo, FieldLo);
967 Hi = merge(Hi, FieldHi);
968 if (Lo == Memory || Hi == Memory)
969 break;
970 }
971
972 // Do post merger cleanup (see below). Only case we worry about is Memory.
973 if (Hi == Memory)
974 Lo = Memory;
975 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +0000976 return;
977 }
978
979 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +0000980 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000981
982 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
983 // than two eightbytes, ..., it has class MEMORY.
984 if (Size > 128)
985 return;
986
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000987 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
988 // copy constructor or a non-trivial destructor, it is passed by invisible
989 // reference.
990 if (hasNonTrivialDestructorOrCopyConstructor(RT))
991 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +0000992
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000993 const RecordDecl *RD = RT->getDecl();
994
995 // Assume variable sized types are passed in memory.
996 if (RD->hasFlexibleArrayMember())
997 return;
998
Chris Lattnerea044322010-07-29 02:01:43 +0000999 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001000
1001 // Reset Lo class, this will be recomputed.
1002 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001003
1004 // If this is a C++ record, classify the bases first.
1005 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1006 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1007 e = CXXRD->bases_end(); i != e; ++i) {
1008 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1009 "Unexpected base class!");
1010 const CXXRecordDecl *Base =
1011 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1012
1013 // Classify this field.
1014 //
1015 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1016 // single eightbyte, each is classified separately. Each eightbyte gets
1017 // initialized to class NO_CLASS.
1018 Class FieldLo, FieldHi;
1019 uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
Chris Lattner9c254f02010-06-29 06:01:59 +00001020 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001021 Lo = merge(Lo, FieldLo);
1022 Hi = merge(Hi, FieldHi);
1023 if (Lo == Memory || Hi == Memory)
1024 break;
1025 }
Daniel Dunbar4971ff82009-12-22 01:19:25 +00001026
1027 // If this record has no fields but isn't empty, classify as INTEGER.
1028 if (RD->field_empty() && Size)
1029 Current = Integer;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001030 }
1031
1032 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001033 unsigned idx = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001034 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1035 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001036 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1037 bool BitField = i->isBitField();
1038
1039 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1040 // fields, it has class MEMORY.
1041 //
1042 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001043 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001044 Lo = Memory;
1045 return;
1046 }
1047
1048 // Classify this field.
1049 //
1050 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1051 // exceeds a single eightbyte, each is classified
1052 // separately. Each eightbyte gets initialized to class
1053 // NO_CLASS.
1054 Class FieldLo, FieldHi;
1055
1056 // Bit-fields require special handling, they do not force the
1057 // structure to be passed in memory even if unaligned, and
1058 // therefore they can straddle an eightbyte.
1059 if (BitField) {
1060 // Ignore padding bit-fields.
1061 if (i->isUnnamedBitfield())
1062 continue;
1063
1064 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Chris Lattnerea044322010-07-29 02:01:43 +00001065 uint64_t Size =
1066 i->getBitWidth()->EvaluateAsInt(getContext()).getZExtValue();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001067
1068 uint64_t EB_Lo = Offset / 64;
1069 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1070 FieldLo = FieldHi = NoClass;
1071 if (EB_Lo) {
1072 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1073 FieldLo = NoClass;
1074 FieldHi = Integer;
1075 } else {
1076 FieldLo = Integer;
1077 FieldHi = EB_Hi ? Integer : NoClass;
1078 }
1079 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001080 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001081 Lo = merge(Lo, FieldLo);
1082 Hi = merge(Hi, FieldHi);
1083 if (Lo == Memory || Hi == Memory)
1084 break;
1085 }
1086
1087 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1088 //
1089 // (a) If one of the classes is MEMORY, the whole argument is
1090 // passed in memory.
1091 //
1092 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
1093
1094 // The first of these conditions is guaranteed by how we implement
1095 // the merge (just bail).
1096 //
1097 // The second condition occurs in the case of unions; for example
1098 // union { _Complex double; unsigned; }.
1099 if (Hi == Memory)
1100 Lo = Memory;
1101 if (Hi == SSEUp && Lo != SSE)
1102 Hi = SSE;
1103 }
1104}
1105
1106ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
Chris Lattner9c254f02010-06-29 06:01:59 +00001107 const llvm::Type *CoerceTo) const {
Chris Lattner1daf8082010-07-28 22:15:08 +00001108 // If this is a pointer passed as a pointer, just pass it directly.
1109 if ((isa<llvm::PointerType>(CoerceTo) || CoerceTo->isIntegerTy(64)) &&
1110 Ty->hasPointerRepresentation())
1111 return ABIArgInfo::getExtend();
1112
1113 if (isa<llvm::IntegerType>(CoerceTo)) {
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
Chris Lattner1daf8082010-07-28 22:15:08 +00001121 if (Ty->isIntegralOrEnumerationType())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001122 return (Ty->isPromotableIntegerType() ?
1123 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Chris Lattner0b30c672010-07-28 23:12:33 +00001124
Chris Lattner7f215c12010-06-26 21:52:32 +00001125 } else if (CoerceTo->isDoubleTy()) {
John McCall0b0ef0a2010-02-24 07:14:12 +00001126 assert(Ty.isCanonical() && "should always have a canonical type here");
1127 assert(!Ty.hasQualifiers() && "should never have a qualified type here");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001128
1129 // Float and double end up in a single SSE reg.
Chris Lattnerea044322010-07-29 02:01:43 +00001130 if (Ty == getContext().FloatTy || Ty == getContext().DoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001131 return ABIArgInfo::getDirect();
1132
Chris Lattnerfaf23b72010-06-28 19:56:59 +00001133 // If this is a 32-bit structure that is passed as a double, then it will be
1134 // passed in the low 32-bits of the XMM register, which is the same as how a
1135 // float is passed. Coerce to a float instead of a double.
Chris Lattnerea044322010-07-29 02:01:43 +00001136 if (getContext().getTypeSizeInChars(Ty).getQuantity() == 4)
Chris Lattnerfaf23b72010-06-28 19:56:59 +00001137 CoerceTo = llvm::Type::getFloatTy(CoerceTo->getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001138 }
1139
1140 return ABIArgInfo::getCoerce(CoerceTo);
1141}
1142
Chris Lattner9c254f02010-06-29 06:01:59 +00001143ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001144 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1145 // place naturally.
1146 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1147 // Treat an enum type as its underlying type.
1148 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1149 Ty = EnumTy->getDecl()->getIntegerType();
1150
1151 return (Ty->isPromotableIntegerType() ?
1152 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1153 }
1154
1155 return ABIArgInfo::getIndirect(0);
1156}
1157
Chris Lattner9c254f02010-06-29 06:01:59 +00001158ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001159 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1160 // place naturally.
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001161 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1162 // Treat an enum type as its underlying type.
1163 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1164 Ty = EnumTy->getDecl()->getIntegerType();
1165
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001166 return (Ty->isPromotableIntegerType() ?
1167 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001168 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001169
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001170 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1171 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001172
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001173 // Compute the byval alignment. We trust the back-end to honor the
1174 // minimum ABI alignment for byval, to make cleaner IR.
1175 const unsigned MinABIAlign = 8;
Chris Lattnerea044322010-07-29 02:01:43 +00001176 unsigned Align = getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001177 if (Align > MinABIAlign)
1178 return ABIArgInfo::getIndirect(Align);
1179 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001180}
1181
Chris Lattner49382de2010-07-28 22:44:07 +00001182/// Get8ByteTypeAtOffset - The ABI specifies that a value should be passed in an
1183/// 8-byte GPR. This means that we either have a scalar or we are talking about
1184/// the high or low part of an up-to-16-byte struct. This routine picks the
1185/// best LLVM IR type to represent this, which may be i64 or may be anything
1186/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1187/// etc).
1188///
1189/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1190/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1191/// the 8-byte value references. PrefType may be null.
1192///
1193/// SourceTy is the source level type for the entire argument. SourceOffset is
1194/// an offset into this that we're processing (which is always either 0 or 8).
1195///
Chris Lattner44f0fd22010-07-29 02:20:19 +00001196const llvm::Type *X86_64ABIInfo::
Chris Lattner5868ca22010-07-29 04:41:05 +00001197Get8ByteTypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
Chris Lattner44f0fd22010-07-29 02:20:19 +00001198 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattner9c254f02010-06-29 06:01:59 +00001199 // Pointers are always 8-bytes at offset 0.
Chris Lattner5868ca22010-07-29 04:41:05 +00001200 if (IROffset == 0 && IRType && isa<llvm::PointerType>(IRType))
1201 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001202
Chris Lattner9c254f02010-06-29 06:01:59 +00001203 // TODO: 1/2/4/8 byte integers are also interesting, but we have to know that
1204 // the "hole" is not used in the containing struct (just undef padding).
Chris Lattner49382de2010-07-28 22:44:07 +00001205
Chris Lattner5868ca22010-07-29 04:41:05 +00001206 if (const llvm::StructType *STy = dyn_cast_or_null<llvm::StructType>(IRType)){
Chris Lattner49382de2010-07-28 22:44:07 +00001207 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner44f0fd22010-07-29 02:20:19 +00001208 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001209 if (IROffset < SL->getSizeInBytes()) {
1210 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1211 IROffset -= SL->getElementOffset(FieldIdx);
1212
1213 return Get8ByteTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
Chris Lattner44f0fd22010-07-29 02:20:19 +00001214 SourceTy, SourceOffset);
Chris Lattner49382de2010-07-28 22:44:07 +00001215 }
1216 }
Chris Lattner9c254f02010-06-29 06:01:59 +00001217
Chris Lattner49382de2010-07-28 22:44:07 +00001218 // Okay, we don't have any better idea of what to pass, so we pass this in an
1219 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner44f0fd22010-07-29 02:20:19 +00001220 uint64_t TySizeInBytes =
1221 getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00001222
1223 // It is always safe to classify this as an integer type up to i64 that
1224 // isn't larger than the structure.
1225 switch (unsigned(TySizeInBytes-SourceOffset)) {
Chris Lattner44f0fd22010-07-29 02:20:19 +00001226 case 1: return llvm::Type::getInt8Ty(getVMContext());
1227 case 2: return llvm::Type::getInt16Ty(getVMContext());
Chris Lattner49382de2010-07-28 22:44:07 +00001228 case 3:
Chris Lattner44f0fd22010-07-29 02:20:19 +00001229 case 4: return llvm::Type::getInt32Ty(getVMContext());
1230 default: return llvm::Type::getInt64Ty(getVMContext());
Chris Lattner49382de2010-07-28 22:44:07 +00001231 }
Chris Lattner9c254f02010-06-29 06:01:59 +00001232}
1233
Chris Lattner519f68c2010-07-28 23:06:14 +00001234ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00001235classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00001236 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1237 // classification algorithm.
1238 X86_64ABIInfo::Class Lo, Hi;
1239 classify(RetTy, 0, Lo, Hi);
1240
1241 // Check some invariants.
1242 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1243 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1244 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1245
1246 const llvm::Type *ResType = 0;
1247 switch (Lo) {
1248 case NoClass:
1249 return ABIArgInfo::getIgnore();
1250
1251 case SSEUp:
1252 case X87Up:
1253 assert(0 && "Invalid classification for lo word.");
1254
1255 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1256 // hidden argument.
1257 case Memory:
1258 return getIndirectReturnResult(RetTy);
1259
1260 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1261 // available register of the sequence %rax, %rdx is used.
1262 case Integer:
Chris Lattner44f0fd22010-07-29 02:20:19 +00001263 ResType = Get8ByteTypeAtOffset(0, 0, RetTy, 0);
Chris Lattner519f68c2010-07-28 23:06:14 +00001264 break;
1265
1266 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1267 // available SSE register of the sequence %xmm0, %xmm1 is used.
1268 case SSE:
Chris Lattnerea044322010-07-29 02:01:43 +00001269 ResType = llvm::Type::getDoubleTy(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00001270 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001271
1272 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1273 // returned on the X87 stack in %st0 as 80-bit x87 number.
1274 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00001275 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00001276 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001277
1278 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1279 // part of the value is returned in %st0 and the imaginary part in
1280 // %st1.
1281 case ComplexX87:
1282 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattnera3c109b2010-07-29 02:16:43 +00001283 ResType = llvm::StructType::get(getVMContext(),
Chris Lattnerea044322010-07-29 02:01:43 +00001284 llvm::Type::getX86_FP80Ty(getVMContext()),
1285 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00001286 NULL);
1287 break;
1288 }
1289
1290 switch (Hi) {
1291 // Memory was handled previously and X87 should
1292 // never occur as a hi class.
1293 case Memory:
1294 case X87:
1295 assert(0 && "Invalid classification for hi word.");
1296
1297 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00001298 case NoClass:
1299 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001300
1301 case Integer: {
Chris Lattner44f0fd22010-07-29 02:20:19 +00001302 const llvm::Type *HiType = Get8ByteTypeAtOffset(0, 8, RetTy, 8);
Chris Lattnera3c109b2010-07-29 02:16:43 +00001303 ResType = llvm::StructType::get(getVMContext(), ResType, HiType, NULL);
Chris Lattner519f68c2010-07-28 23:06:14 +00001304 break;
1305 }
1306 case SSE:
Chris Lattnera3c109b2010-07-29 02:16:43 +00001307 ResType = llvm::StructType::get(getVMContext(), ResType,
1308 llvm::Type::getDoubleTy(getVMContext()),
1309 NULL);
Chris Lattner519f68c2010-07-28 23:06:14 +00001310 break;
1311
1312 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1313 // is passed in the upper half of the last used SSE register.
1314 //
1315 // SSEUP should always be preceeded by SSE, just widen.
1316 case SSEUp:
1317 assert(Lo == SSE && "Unexpected SSEUp classification.");
Chris Lattnera3c109b2010-07-29 02:16:43 +00001318 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
Chris Lattner519f68c2010-07-28 23:06:14 +00001319 break;
1320
1321 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1322 // returned together with the previous X87 value in %st0.
1323 case X87Up:
1324 // If X87Up is preceeded by X87, we don't need to do
1325 // anything. However, in some cases with unions it may not be
1326 // preceeded by X87. In such situations we follow gcc and pass the
1327 // extra bits in an SSE reg.
1328 if (Lo != X87)
Chris Lattnera3c109b2010-07-29 02:16:43 +00001329 ResType = llvm::StructType::get(getVMContext(), ResType,
1330 llvm::Type::getDoubleTy(getVMContext()),
1331 NULL);
Chris Lattner519f68c2010-07-28 23:06:14 +00001332 break;
1333 }
1334
1335 return getCoerceResult(RetTy, ResType);
1336}
1337
Chris Lattnera3c109b2010-07-29 02:16:43 +00001338ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Chris Lattner5868ca22010-07-29 04:41:05 +00001339 unsigned &neededSSE) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001340 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001341 classify(Ty, 0, Lo, Hi);
Chris Lattner5868ca22010-07-29 04:41:05 +00001342
1343
1344 // Determine the preferred IR type to use and pass it down to
1345 // classifyArgumentType.
1346 const llvm::Type *IRType = 0;
1347
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001348 // Check some invariants.
1349 // FIXME: Enforce these by construction.
1350 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1351 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1352 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1353
1354 neededInt = 0;
1355 neededSSE = 0;
1356 const llvm::Type *ResType = 0;
1357 switch (Lo) {
1358 case NoClass:
1359 return ABIArgInfo::getIgnore();
1360
1361 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1362 // on the stack.
1363 case Memory:
1364
1365 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1366 // COMPLEX_X87, it is passed in memory.
1367 case X87:
1368 case ComplexX87:
Chris Lattner9c254f02010-06-29 06:01:59 +00001369 return getIndirectResult(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001370
1371 case SSEUp:
1372 case X87Up:
1373 assert(0 && "Invalid classification for lo word.");
1374
1375 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1376 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1377 // and %r9 is used.
1378 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00001379 ++neededInt;
Chris Lattner5868ca22010-07-29 04:41:05 +00001380
1381 if (IRType == 0)
1382 IRType = CGT.ConvertTypeRecursive(Ty);
1383
Chris Lattner49382de2010-07-28 22:44:07 +00001384 // Pick an 8-byte type based on the preferred type.
Chris Lattner5868ca22010-07-29 04:41:05 +00001385 ResType = Get8ByteTypeAtOffset(IRType, 0, Ty, 0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001386 break;
1387
1388 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1389 // available SSE register is used, the registers are taken in the
1390 // order from %xmm0 to %xmm7.
1391 case SSE:
1392 ++neededSSE;
Chris Lattnera3c109b2010-07-29 02:16:43 +00001393 ResType = llvm::Type::getDoubleTy(getVMContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001394 break;
1395 }
1396
1397 switch (Hi) {
1398 // Memory was handled previously, ComplexX87 and X87 should
1399 // never occur as hi classes, and X87Up must be preceed by X87,
1400 // which is passed in memory.
1401 case Memory:
1402 case X87:
1403 case ComplexX87:
1404 assert(0 && "Invalid classification for hi word.");
1405 break;
1406
1407 case NoClass: break;
Chris Lattner9c254f02010-06-29 06:01:59 +00001408
1409 case Integer: {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001410 ++neededInt;
Chris Lattner9c254f02010-06-29 06:01:59 +00001411
Chris Lattner5868ca22010-07-29 04:41:05 +00001412 if (IRType == 0)
1413 IRType = CGT.ConvertTypeRecursive(Ty);
1414
Chris Lattner49382de2010-07-28 22:44:07 +00001415 // Pick an 8-byte type based on the preferred type.
Chris Lattner5868ca22010-07-29 04:41:05 +00001416 const llvm::Type *HiType = Get8ByteTypeAtOffset(IRType, 8, Ty, 8);
Chris Lattnera3c109b2010-07-29 02:16:43 +00001417 ResType = llvm::StructType::get(getVMContext(), ResType, HiType, NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001418 break;
Chris Lattner9c254f02010-06-29 06:01:59 +00001419 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001420
1421 // X87Up generally doesn't occur here (long double is passed in
1422 // memory), except in situations involving unions.
1423 case X87Up:
1424 case SSE:
Chris Lattnera3c109b2010-07-29 02:16:43 +00001425 ResType = llvm::StructType::get(getVMContext(), ResType,
1426 llvm::Type::getDoubleTy(getVMContext()),
1427 NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001428 ++neededSSE;
1429 break;
1430
1431 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1432 // eightbyte is passed in the upper half of the last used SSE
Chris Lattnerab5722e2010-07-28 23:47:21 +00001433 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001434 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00001435 assert(Lo == SSE && "Unexpected SSEUp classification");
Chris Lattnera3c109b2010-07-29 02:16:43 +00001436 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
Chris Lattnerab5722e2010-07-28 23:47:21 +00001437
Chris Lattner5868ca22010-07-29 04:41:05 +00001438 if (IRType == 0)
1439 IRType = CGT.ConvertTypeRecursive(Ty);
1440
Chris Lattnerab5722e2010-07-28 23:47:21 +00001441 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner5868ca22010-07-29 04:41:05 +00001442 if (const llvm::VectorType *VT =dyn_cast_or_null<llvm::VectorType>(IRType)){
Chris Lattnerab5722e2010-07-28 23:47:21 +00001443 const llvm::Type *EltTy = VT->getElementType();
1444 if (VT->getBitWidth() == 128 &&
1445 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1446 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1447 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1448 EltTy->isIntegerTy(128)))
Chris Lattner5868ca22010-07-29 04:41:05 +00001449 ResType = IRType;
Chris Lattnerab5722e2010-07-28 23:47:21 +00001450 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001451 break;
1452 }
1453
Chris Lattner9c254f02010-06-29 06:01:59 +00001454 return getCoerceResult(Ty, ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001455}
1456
Chris Lattneree5dcd02010-07-29 02:31:05 +00001457void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattner5868ca22010-07-29 04:41:05 +00001458
1459 // Pass preferred type into classifyReturnType.
Chris Lattnera3c109b2010-07-29 02:16:43 +00001460 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001461
1462 // Keep track of the number of assigned registers.
1463 unsigned freeIntRegs = 6, freeSSERegs = 8;
1464
1465 // If the return value is indirect, then the hidden argument is consuming one
1466 // integer register.
1467 if (FI.getReturnInfo().isIndirect())
1468 --freeIntRegs;
1469
1470 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1471 // get assigned (in left-to-right order) for passing as follows...
1472 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1473 it != ie; ++it) {
1474 unsigned neededInt, neededSSE;
Chris Lattner5868ca22010-07-29 04:41:05 +00001475 it->info = classifyArgumentType(it->type, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001476
1477 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1478 // eightbyte of an argument, the whole argument is passed on the
1479 // stack. If registers have already been assigned for some
1480 // eightbytes of such an argument, the assignments get reverted.
1481 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1482 freeIntRegs -= neededInt;
1483 freeSSERegs -= neededSSE;
1484 } else {
Chris Lattner9c254f02010-06-29 06:01:59 +00001485 it->info = getIndirectResult(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001486 }
1487 }
1488}
1489
1490static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1491 QualType Ty,
1492 CodeGenFunction &CGF) {
1493 llvm::Value *overflow_arg_area_p =
1494 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1495 llvm::Value *overflow_arg_area =
1496 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1497
1498 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1499 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1500 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1501 if (Align > 8) {
1502 // Note that we follow the ABI & gcc here, even though the type
1503 // could in theory have an alignment greater than 16. This case
1504 // shouldn't ever matter in practice.
1505
1506 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson0032b272009-08-13 21:57:51 +00001507 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001508 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001509 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1510 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00001511 CGF.Int64Ty);
1512 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001513 overflow_arg_area =
1514 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1515 overflow_arg_area->getType(),
1516 "overflow_arg_area.align");
1517 }
1518
1519 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1520 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1521 llvm::Value *Res =
1522 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001523 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001524
1525 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1526 // l->overflow_arg_area + sizeof(type).
1527 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1528 // an 8 byte boundary.
1529
1530 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00001531 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001532 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001533 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1534 "overflow_arg_area.next");
1535 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1536
1537 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1538 return Res;
1539}
1540
1541llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1542 CodeGenFunction &CGF) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001543 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001544
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001545 // Assume that va_list type is correct; should be pointer to LLVM type:
1546 // struct {
1547 // i32 gp_offset;
1548 // i32 fp_offset;
1549 // i8* overflow_arg_area;
1550 // i8* reg_save_area;
1551 // };
1552 unsigned neededInt, neededSSE;
Chris Lattnera14db752010-03-11 18:19:55 +00001553
1554 Ty = CGF.getContext().getCanonicalType(Ty);
Chris Lattner5868ca22010-07-29 04:41:05 +00001555 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001556
1557 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1558 // in the registers. If not go to step 7.
1559 if (!neededInt && !neededSSE)
1560 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1561
1562 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1563 // general purpose registers needed to pass type and num_fp to hold
1564 // the number of floating point registers needed.
1565
1566 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1567 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1568 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1569 //
1570 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1571 // register save space).
1572
1573 llvm::Value *InRegs = 0;
1574 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1575 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1576 if (neededInt) {
1577 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1578 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001579 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
1580 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001581 }
1582
1583 if (neededSSE) {
1584 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1585 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1586 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00001587 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
1588 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001589 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1590 }
1591
1592 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1593 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1594 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1595 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1596
1597 // Emit code to load the value if it was passed in registers.
1598
1599 CGF.EmitBlock(InRegBlock);
1600
1601 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1602 // an offset of l->gp_offset and/or l->fp_offset. This may require
1603 // copying to a temporary location in case the parameter is passed
1604 // in different register classes or requires an alignment greater
1605 // than 8 for general purpose registers and 16 for XMM registers.
1606 //
1607 // FIXME: This really results in shameful code when we end up needing to
1608 // collect arguments from different places; often what should result in a
1609 // simple assembling of a structure from scattered addresses has many more
1610 // loads than necessary. Can we clean this up?
1611 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1612 llvm::Value *RegAddr =
1613 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1614 "reg_save_area");
1615 if (neededInt && neededSSE) {
1616 // FIXME: Cleanup.
1617 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1618 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1619 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1620 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1621 const llvm::Type *TyLo = ST->getElementType(0);
1622 const llvm::Type *TyHi = ST->getElementType(1);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00001623 assert((TyLo->isFloatingPointTy() ^ TyHi->isFloatingPointTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001624 "Unexpected ABI info for mixed regs");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001625 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1626 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001627 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1628 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00001629 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1630 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001631 llvm::Value *V =
1632 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1633 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1634 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1635 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1636
Owen Andersona1cf15f2009-07-14 23:10:40 +00001637 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001638 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001639 } else if (neededInt) {
1640 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1641 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001642 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001643 } else if (neededSSE == 1) {
1644 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1645 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1646 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001647 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001648 assert(neededSSE == 2 && "Invalid number of needed registers!");
1649 // SSE registers are spaced 16 bytes apart in the register save
1650 // area, we need to collect the two eightbytes together.
1651 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001652 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001653 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
1654 const llvm::Type *DblPtrTy =
1655 llvm::PointerType::getUnqual(DoubleTy);
1656 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1657 DoubleTy, NULL);
1658 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1659 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1660 DblPtrTy));
1661 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1662 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1663 DblPtrTy));
1664 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1665 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1666 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001667 }
1668
1669 // AMD64-ABI 3.5.7p5: Step 5. Set:
1670 // l->gp_offset = l->gp_offset + num_gp * 8
1671 // l->fp_offset = l->fp_offset + num_fp * 16.
1672 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00001673 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001674 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1675 gp_offset_p);
1676 }
1677 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00001678 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001679 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1680 fp_offset_p);
1681 }
1682 CGF.EmitBranch(ContBlock);
1683
1684 // Emit code to load the value if it was passed in memory.
1685
1686 CGF.EmitBlock(InMemBlock);
1687 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1688
1689 // Return the appropriate result.
1690
1691 CGF.EmitBlock(ContBlock);
1692 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1693 "vaarg.addr");
1694 ResAddr->reserveOperandSpace(2);
1695 ResAddr->addIncoming(RegAddr, InRegBlock);
1696 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001697 return ResAddr;
1698}
1699
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001700
1701
1702//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001703// PIC16 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001704//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001705
1706namespace {
1707
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001708class PIC16ABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +00001709public:
1710 PIC16ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
1711
Chris Lattnera3c109b2010-07-29 02:16:43 +00001712 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001713
Chris Lattnera3c109b2010-07-29 02:16:43 +00001714 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001715
Chris Lattneree5dcd02010-07-29 02:31:05 +00001716 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00001717 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001718 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1719 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +00001720 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001721 }
1722
1723 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1724 CodeGenFunction &CGF) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001725};
1726
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001727class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
1728public:
Chris Lattnerea044322010-07-29 02:01:43 +00001729 PIC16TargetCodeGenInfo(CodeGenTypes &CGT)
1730 : TargetCodeGenInfo(new PIC16ABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001731};
1732
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001733}
1734
Chris Lattnera3c109b2010-07-29 02:16:43 +00001735ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001736 if (RetTy->isVoidType()) {
1737 return ABIArgInfo::getIgnore();
1738 } else {
1739 return ABIArgInfo::getDirect();
1740 }
1741}
1742
Chris Lattnera3c109b2010-07-29 02:16:43 +00001743ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001744 return ABIArgInfo::getDirect();
1745}
1746
1747llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00001748 CodeGenFunction &CGF) const {
Chris Lattner52d9ae32010-04-06 17:29:22 +00001749 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Sanjiv Guptaa446ecd2010-02-17 02:25:52 +00001750 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1751
1752 CGBuilderTy &Builder = CGF.Builder;
1753 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1754 "ap");
1755 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1756 llvm::Type *PTy =
1757 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1758 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1759
1760 uint64_t Offset = CGF.getContext().getTypeSize(Ty) / 8;
1761
1762 llvm::Value *NextAddr =
1763 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1764 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
1765 "ap.next");
1766 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1767
1768 return AddrTyped;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001769}
1770
Sanjiv Guptaa446ecd2010-02-17 02:25:52 +00001771
John McCallec853ba2010-03-11 00:10:12 +00001772// PowerPC-32
1773
1774namespace {
1775class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
1776public:
Chris Lattnerea044322010-07-29 02:01:43 +00001777 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
1778
John McCallec853ba2010-03-11 00:10:12 +00001779 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1780 // This is recovered from gcc output.
1781 return 1; // r1 is the dedicated stack pointer
1782 }
1783
1784 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1785 llvm::Value *Address) const;
1786};
1787
1788}
1789
1790bool
1791PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1792 llvm::Value *Address) const {
1793 // This is calculated from the LLVM and GCC tables and verified
1794 // against gcc output. AFAIK all ABIs use the same encoding.
1795
1796 CodeGen::CGBuilderTy &Builder = CGF.Builder;
1797 llvm::LLVMContext &Context = CGF.getLLVMContext();
1798
1799 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
1800 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
1801 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
1802 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
1803
1804 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00001805 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00001806
1807 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00001808 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00001809
1810 // 64-76 are various 4-byte special-purpose registers:
1811 // 64: mq
1812 // 65: lr
1813 // 66: ctr
1814 // 67: ap
1815 // 68-75 cr0-7
1816 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00001817 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00001818
1819 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00001820 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00001821
1822 // 109: vrsave
1823 // 110: vscr
1824 // 111: spe_acc
1825 // 112: spefscr
1826 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00001827 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00001828
1829 return false;
1830}
1831
1832
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001833//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001834// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001835//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001836
1837namespace {
1838
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001839class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001840public:
1841 enum ABIKind {
1842 APCS = 0,
1843 AAPCS = 1,
1844 AAPCS_VFP
1845 };
1846
1847private:
1848 ABIKind Kind;
1849
1850public:
Chris Lattnerea044322010-07-29 02:01:43 +00001851 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001852
1853private:
1854 ABIKind getABIKind() const { return Kind; }
1855
Chris Lattnera3c109b2010-07-29 02:16:43 +00001856 ABIArgInfo classifyReturnType(QualType RetTy) const;
1857 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001858
Chris Lattneree5dcd02010-07-29 02:31:05 +00001859 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001860
1861 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1862 CodeGenFunction &CGF) const;
1863};
1864
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001865class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
1866public:
Chris Lattnerea044322010-07-29 02:01:43 +00001867 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
1868 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00001869
1870 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1871 return 13;
1872 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001873};
1874
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001875}
1876
Chris Lattneree5dcd02010-07-29 02:31:05 +00001877void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00001878 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001879 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattnera3c109b2010-07-29 02:16:43 +00001880 it != ie; ++it)
1881 it->info = classifyArgumentType(it->type);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001882
Chris Lattnera3c109b2010-07-29 02:16:43 +00001883 const llvm::Triple &Triple(getContext().Target.getTriple());
Rafael Espindola25117ab2010-06-16 16:13:39 +00001884 llvm::CallingConv::ID DefaultCC;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00001885 if (Triple.getEnvironmentName() == "gnueabi" ||
1886 Triple.getEnvironmentName() == "eabi")
Rafael Espindola25117ab2010-06-16 16:13:39 +00001887 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00001888 else
1889 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00001890
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001891 switch (getABIKind()) {
1892 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00001893 if (DefaultCC != llvm::CallingConv::ARM_APCS)
1894 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001895 break;
1896
1897 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00001898 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
1899 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001900 break;
1901
1902 case AAPCS_VFP:
1903 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
1904 break;
1905 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001906}
1907
Chris Lattnera3c109b2010-07-29 02:16:43 +00001908ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001909 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1910 // Treat an enum type as its underlying type.
1911 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1912 Ty = EnumTy->getDecl()->getIntegerType();
1913
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001914 return (Ty->isPromotableIntegerType() ?
1915 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001916 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00001917
Daniel Dunbar42025572009-09-14 21:54:03 +00001918 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00001919 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00001920 return ABIArgInfo::getIgnore();
1921
Rafael Espindola0eb1d972010-06-08 02:42:08 +00001922 // Structures with either a non-trivial destructor or a non-trivial
1923 // copy constructor are always indirect.
1924 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1925 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
1926
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001927 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1928 // backend doesn't support byval.
1929 // FIXME: This doesn't handle alignment > 64 bits.
1930 const llvm::Type* ElemTy;
1931 unsigned SizeRegs;
Chris Lattnera3c109b2010-07-29 02:16:43 +00001932 if (getContext().getTypeAlign(Ty) > 32) {
1933 ElemTy = llvm::Type::getInt64Ty(getVMContext());
1934 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001935 } else {
Chris Lattnera3c109b2010-07-29 02:16:43 +00001936 ElemTy = llvm::Type::getInt32Ty(getVMContext());
1937 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001938 }
1939 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson96e0fc72009-07-29 22:16:19 +00001940 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Chris Lattnera3c109b2010-07-29 02:16:43 +00001941 const llvm::Type* STy = llvm::StructType::get(getVMContext(), LLVMFields,
1942 true);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001943 return ABIArgInfo::getCoerce(STy);
1944}
1945
Chris Lattnera3c109b2010-07-29 02:16:43 +00001946static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00001947 llvm::LLVMContext &VMContext) {
1948 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
1949 // is called integer-like if its size is less than or equal to one word, and
1950 // the offset of each of its addressable sub-fields is zero.
1951
1952 uint64_t Size = Context.getTypeSize(Ty);
1953
1954 // Check that the type fits in a word.
1955 if (Size > 32)
1956 return false;
1957
1958 // FIXME: Handle vector types!
1959 if (Ty->isVectorType())
1960 return false;
1961
Daniel Dunbarb0d58192009-09-14 02:20:34 +00001962 // Float types are never treated as "integer like".
1963 if (Ty->isRealFloatingType())
1964 return false;
1965
Daniel Dunbar98303b92009-09-13 08:03:58 +00001966 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00001967 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00001968 return true;
1969
Daniel Dunbar45815812010-02-01 23:31:26 +00001970 // Small complex integer types are "integer like".
1971 if (const ComplexType *CT = Ty->getAs<ComplexType>())
1972 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00001973
1974 // Single element and zero sized arrays should be allowed, by the definition
1975 // above, but they are not.
1976
1977 // Otherwise, it must be a record type.
1978 const RecordType *RT = Ty->getAs<RecordType>();
1979 if (!RT) return false;
1980
1981 // Ignore records with flexible arrays.
1982 const RecordDecl *RD = RT->getDecl();
1983 if (RD->hasFlexibleArrayMember())
1984 return false;
1985
1986 // Check that all sub-fields are at offset 0, and are themselves "integer
1987 // like".
1988 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1989
1990 bool HadField = false;
1991 unsigned idx = 0;
1992 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1993 i != e; ++i, ++idx) {
1994 const FieldDecl *FD = *i;
1995
Daniel Dunbar679855a2010-01-29 03:22:29 +00001996 // Bit-fields are not addressable, we only need to verify they are "integer
1997 // like". We still have to disallow a subsequent non-bitfield, for example:
1998 // struct { int : 0; int x }
1999 // is non-integer like according to gcc.
2000 if (FD->isBitField()) {
2001 if (!RD->isUnion())
2002 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002003
Daniel Dunbar679855a2010-01-29 03:22:29 +00002004 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2005 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002006
Daniel Dunbar679855a2010-01-29 03:22:29 +00002007 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002008 }
2009
Daniel Dunbar679855a2010-01-29 03:22:29 +00002010 // Check if this field is at offset 0.
2011 if (Layout.getFieldOffset(idx) != 0)
2012 return false;
2013
Daniel Dunbar98303b92009-09-13 08:03:58 +00002014 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2015 return false;
2016
Daniel Dunbar679855a2010-01-29 03:22:29 +00002017 // Only allow at most one field in a structure. This doesn't match the
2018 // wording above, but follows gcc in situations with a field following an
2019 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00002020 if (!RD->isUnion()) {
2021 if (HadField)
2022 return false;
2023
2024 HadField = true;
2025 }
2026 }
2027
2028 return true;
2029}
2030
Chris Lattnera3c109b2010-07-29 02:16:43 +00002031ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002032 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002033 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00002034
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002035 if (!CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2036 // Treat an enum type as its underlying type.
2037 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2038 RetTy = EnumTy->getDecl()->getIntegerType();
2039
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002040 return (RetTy->isPromotableIntegerType() ?
2041 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002042 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002043
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002044 // Structures with either a non-trivial destructor or a non-trivial
2045 // copy constructor are always indirect.
2046 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2047 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2048
Daniel Dunbar98303b92009-09-13 08:03:58 +00002049 // Are we following APCS?
2050 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002051 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00002052 return ABIArgInfo::getIgnore();
2053
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002054 // Complex types are all returned as packed integers.
2055 //
2056 // FIXME: Consider using 2 x vector types if the back end handles them
2057 // correctly.
2058 if (RetTy->isAnyComplexType())
Chris Lattnera3c109b2010-07-29 02:16:43 +00002059 return ABIArgInfo::getCoerce(llvm::IntegerType::get(getVMContext(),
2060 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002061
Daniel Dunbar98303b92009-09-13 08:03:58 +00002062 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002063 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002064 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002065 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002066 if (Size <= 8)
Chris Lattnera3c109b2010-07-29 02:16:43 +00002067 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002068 if (Size <= 16)
Chris Lattnera3c109b2010-07-29 02:16:43 +00002069 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(getVMContext()));
2070 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002071 }
2072
2073 // Otherwise return in memory.
2074 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002075 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002076
2077 // Otherwise this is an AAPCS variant.
2078
Chris Lattnera3c109b2010-07-29 02:16:43 +00002079 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00002080 return ABIArgInfo::getIgnore();
2081
Daniel Dunbar98303b92009-09-13 08:03:58 +00002082 // Aggregates <= 4 bytes are returned in r0; other aggregates
2083 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002084 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00002085 if (Size <= 32) {
2086 // Return in the smallest viable integer type.
2087 if (Size <= 8)
Chris Lattnera3c109b2010-07-29 02:16:43 +00002088 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002089 if (Size <= 16)
Chris Lattnera3c109b2010-07-29 02:16:43 +00002090 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(getVMContext()));
2091 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002092 }
2093
Daniel Dunbar98303b92009-09-13 08:03:58 +00002094 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002095}
2096
2097llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00002098 CodeGenFunction &CGF) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002099 // FIXME: Need to handle alignment
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00002100 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002101 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002102
2103 CGBuilderTy &Builder = CGF.Builder;
2104 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2105 "ap");
2106 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2107 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00002108 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002109 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2110
2111 uint64_t Offset =
2112 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2113 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00002114 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002115 "ap.next");
2116 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2117
2118 return AddrTyped;
2119}
2120
Chris Lattnera3c109b2010-07-29 02:16:43 +00002121ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
2122 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002123 return ABIArgInfo::getIgnore();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002124
Chris Lattnera3c109b2010-07-29 02:16:43 +00002125 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
2126 return ABIArgInfo::getIndirect(0);
2127
2128 // Treat an enum type as its underlying type.
2129 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2130 RetTy = EnumTy->getDecl()->getIntegerType();
2131
2132 return (RetTy->isPromotableIntegerType() ?
2133 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002134}
2135
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002136//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002137// SystemZ ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002138//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002139
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002140namespace {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002141
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002142class SystemZABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +00002143public:
2144 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2145
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002146 bool isPromotableIntegerType(QualType Ty) const;
2147
Chris Lattnera3c109b2010-07-29 02:16:43 +00002148 ABIArgInfo classifyReturnType(QualType RetTy) const;
2149 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002150
Chris Lattneree5dcd02010-07-29 02:31:05 +00002151 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002152 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002153 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2154 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +00002155 it->info = classifyArgumentType(it->type);
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002156 }
2157
2158 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2159 CodeGenFunction &CGF) const;
2160};
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002161
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002162class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2163public:
Chris Lattnerea044322010-07-29 02:01:43 +00002164 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
2165 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002166};
2167
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002168}
2169
2170bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2171 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall183700f2009-09-21 23:43:11 +00002172 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002173 switch (BT->getKind()) {
2174 case BuiltinType::Bool:
2175 case BuiltinType::Char_S:
2176 case BuiltinType::Char_U:
2177 case BuiltinType::SChar:
2178 case BuiltinType::UChar:
2179 case BuiltinType::Short:
2180 case BuiltinType::UShort:
2181 case BuiltinType::Int:
2182 case BuiltinType::UInt:
2183 return true;
2184 default:
2185 return false;
2186 }
2187 return false;
2188}
2189
2190llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2191 CodeGenFunction &CGF) const {
2192 // FIXME: Implement
2193 return 0;
2194}
2195
2196
Chris Lattnera3c109b2010-07-29 02:16:43 +00002197ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
2198 if (RetTy->isVoidType())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002199 return ABIArgInfo::getIgnore();
Chris Lattnera3c109b2010-07-29 02:16:43 +00002200 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002201 return ABIArgInfo::getIndirect(0);
Chris Lattnera3c109b2010-07-29 02:16:43 +00002202
2203 return (isPromotableIntegerType(RetTy) ?
2204 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002205}
2206
Chris Lattnera3c109b2010-07-29 02:16:43 +00002207ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
2208 if (CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002209 return ABIArgInfo::getIndirect(0);
Chris Lattnera3c109b2010-07-29 02:16:43 +00002210
2211 return (isPromotableIntegerType(Ty) ?
2212 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002213}
2214
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002215//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002216// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002217//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002218
2219namespace {
2220
2221class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2222public:
Chris Lattnerea044322010-07-29 02:01:43 +00002223 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
2224 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002225 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2226 CodeGen::CodeGenModule &M) const;
2227};
2228
2229}
2230
2231void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2232 llvm::GlobalValue *GV,
2233 CodeGen::CodeGenModule &M) const {
2234 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2235 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2236 // Handle 'interrupt' attribute:
2237 llvm::Function *F = cast<llvm::Function>(GV);
2238
2239 // Step 1: Set ISR calling convention.
2240 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2241
2242 // Step 2: Add attributes goodness.
2243 F->addFnAttr(llvm::Attribute::NoInline);
2244
2245 // Step 3: Emit ISR vector alias.
2246 unsigned Num = attr->getNumber() + 0xffe0;
2247 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2248 "vector_" +
2249 llvm::LowercaseString(llvm::utohexstr(Num)),
2250 GV, &M.getModule());
2251 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002252 }
2253}
2254
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002255//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00002256// MIPS ABI Implementation. This works for both little-endian and
2257// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002258//===----------------------------------------------------------------------===//
2259
John McCallaeeb7012010-05-27 06:19:26 +00002260namespace {
2261class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
2262public:
Chris Lattnerea044322010-07-29 02:01:43 +00002263 MIPSTargetCodeGenInfo(CodeGenTypes &CGT)
2264 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
John McCallaeeb7012010-05-27 06:19:26 +00002265
2266 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2267 return 29;
2268 }
2269
2270 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2271 llvm::Value *Address) const;
2272};
2273}
2274
2275bool
2276MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2277 llvm::Value *Address) const {
2278 // This information comes from gcc's implementation, which seems to
2279 // as canonical as it gets.
2280
2281 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2282 llvm::LLVMContext &Context = CGF.getLLVMContext();
2283
2284 // Everything on MIPS is 4 bytes. Double-precision FP registers
2285 // are aliased to pairs of single-precision FP registers.
2286 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2287 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2288
2289 // 0-31 are the general purpose registers, $0 - $31.
2290 // 32-63 are the floating-point registers, $f0 - $f31.
2291 // 64 and 65 are the multiply/divide registers, $hi and $lo.
2292 // 66 is the (notional, I think) register for signal-handler return.
2293 AssignToArrayRange(Builder, Address, Four8, 0, 65);
2294
2295 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
2296 // They are one bit wide and ignored here.
2297
2298 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
2299 // (coprocessor 1 is the FP unit)
2300 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
2301 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
2302 // 176-181 are the DSP accumulator registers.
2303 AssignToArrayRange(Builder, Address, Four8, 80, 181);
2304
2305 return false;
2306}
2307
2308
Chris Lattnerea044322010-07-29 02:01:43 +00002309const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002310 if (TheTargetCodeGenInfo)
2311 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002312
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002313 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2314 // free it.
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002315
Chris Lattner9c254f02010-06-29 06:01:59 +00002316 const llvm::Triple &Triple = getContext().Target.getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002317 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002318 default:
Chris Lattnerea044322010-07-29 02:01:43 +00002319 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002320
John McCallaeeb7012010-05-27 06:19:26 +00002321 case llvm::Triple::mips:
2322 case llvm::Triple::mipsel:
Chris Lattnerea044322010-07-29 02:01:43 +00002323 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types));
John McCallaeeb7012010-05-27 06:19:26 +00002324
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002325 case llvm::Triple::arm:
2326 case llvm::Triple::thumb:
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002327 // FIXME: We want to know the float calling convention as well.
Daniel Dunbar018ba5a2009-09-14 00:35:03 +00002328 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002329 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00002330 new ARMTargetCodeGenInfo(Types, ARMABIInfo::APCS));
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002331
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002332 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00002333 new ARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002334
2335 case llvm::Triple::pic16:
Chris Lattnerea044322010-07-29 02:01:43 +00002336 return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002337
John McCallec853ba2010-03-11 00:10:12 +00002338 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00002339 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00002340
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002341 case llvm::Triple::systemz:
Chris Lattnerea044322010-07-29 02:01:43 +00002342 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002343
2344 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00002345 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002346
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002347 case llvm::Triple::x86:
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002348 switch (Triple.getOS()) {
Edward O'Callaghan7ee68bd2009-10-20 17:22:50 +00002349 case llvm::Triple::Darwin:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002350 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00002351 new X86_32TargetCodeGenInfo(Types, true, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002352 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002353 case llvm::Triple::MinGW32:
2354 case llvm::Triple::MinGW64:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00002355 case llvm::Triple::AuroraUX:
2356 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00002357 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002358 case llvm::Triple::OpenBSD:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002359 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00002360 new X86_32TargetCodeGenInfo(Types, false, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002361
2362 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002363 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00002364 new X86_32TargetCodeGenInfo(Types, false, false));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002365 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002366
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002367 case llvm::Triple::x86_64:
Chris Lattnerea044322010-07-29 02:01:43 +00002368 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002369 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002370}