blob: 61a74265469a498418d5aabd649433aacec4ec69 [file] [log] [blame]
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikov244360d2009-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 Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000016#include "ABIInfo.h"
17#include "CodeGenFunction.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000018#include "clang/AST/RecordLayout.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000019#include "llvm/Type.h"
Chris Lattner22a931e2010-06-29 06:01:59 +000020#include "llvm/Target/TargetData.h"
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000021#include "llvm/ADT/StringExtras.h"
Daniel Dunbare3532f82009-08-24 08:52:16 +000022#include "llvm/ADT/Triple.h"
Daniel Dunbar7230fa52009-12-03 09:13:49 +000023#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000024using namespace clang;
25using namespace CodeGen;
26
John McCall943fae92010-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 Korobeynikov244360d2009-06-05 22:08:42 +000039ABIInfo::~ABIInfo() {}
40
Chris Lattner2b037972010-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 Korobeynikov244360d2009-06-05 22:08:42 +000054void ABIArgInfo::dump() const {
Daniel Dunbar7230fa52009-12-03 09:13:49 +000055 llvm::raw_ostream &OS = llvm::errs();
56 OS << "(ABIArgInfo Kind=";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000057 switch (TheKind) {
58 case Direct:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000059 OS << "Direct";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000060 break;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000061 case Extend:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000062 OS << "Extend";
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000063 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +000064 case Ignore:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000065 OS << "Ignore";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000066 break;
67 case Coerce:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000068 OS << "Coerce Type=";
69 getCoerceToType()->print(OS);
Anton Korobeynikov244360d2009-06-05 22:08:42 +000070 break;
71 case Indirect:
Daniel Dunbar557893d2010-04-21 19:10:51 +000072 OS << "Indirect Align=" << getIndirectAlign()
73 << " Byal=" << getIndirectByVal();
Anton Korobeynikov244360d2009-06-05 22:08:42 +000074 break;
75 case Expand:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000076 OS << "Expand";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000077 break;
78 }
Daniel Dunbar7230fa52009-12-03 09:13:49 +000079 OS << ")\n";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000080}
81
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000082TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
83
Daniel Dunbar626f1d82009-09-13 08:03:58 +000084static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikov244360d2009-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 Dunbar626f1d82009-09-13 08:03:58 +000088static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
89 bool AllowArrays) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +000090 if (FD->isUnnamedBitfield())
91 return true;
92
93 QualType FT = FD->getType();
Anton Korobeynikov244360d2009-06-05 22:08:42 +000094
Daniel Dunbar626f1d82009-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 Dunbarcd20ce12010-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 Dunbar626f1d82009-09-13 08:03:58 +0000111 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikov244360d2009-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 Dunbar626f1d82009-09-13 08:03:58 +0000117static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000118 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikov244360d2009-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 Dunbarcd20ce12010-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 Kyrtzidiscfbfe782009-06-30 02:36:12 +0000132 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
133 i != e; ++i)
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000134 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000135 return false;
136 return true;
137}
138
Anders Carlsson20759ad2009-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 Korobeynikov244360d2009-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 Dunbar12ebb472010-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 Dunbar12ebb472010-05-11 21:15:36 +0000183 // Ignore empty records.
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000184 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar12ebb472010-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 Kyrtzidiscfbfe782009-06-30 02:36:12 +0000200 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
201 i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000202 const FieldDecl *FD = *i;
203 QualType FT = FD->getType();
204
205 // Ignore empty fields.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000206 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-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 Dunbar6b45b672010-05-14 03:40:53 +0000234 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000235 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
236 !Ty->isBlockPointerType())
Anton Korobeynikov244360d2009-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 Dunbar11c08c82009-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 Kyrtzidiscfbfe782009-06-30 02:36:12 +0000265 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
266 i != e; ++i) {
Anton Korobeynikov244360d2009-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 Lattner2b037972010-07-29 02:01:43 +0000288public:
289 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
290
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000291 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000292 ASTContext &Context,
293 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000294
295 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000296 ASTContext &Context,
297 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000298
Owen Anderson170229f2009-07-14 23:10:40 +0000299 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
Chris Lattner1d7c9f72010-06-29 01:08:48 +0000300 llvm::LLVMContext &VMContext,
301 const llvm::Type *const *PrefTypes,
302 unsigned NumPrefTypes) const {
Owen Anderson170229f2009-07-14 23:10:40 +0000303 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
304 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000305 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
306 it != ie; ++it)
Owen Anderson170229f2009-07-14 23:10:40 +0000307 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000308 }
309
310 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
311 CodeGenFunction &CGF) const;
312};
313
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000314class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
315public:
Chris Lattner2b037972010-07-29 02:01:43 +0000316 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
317 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000318};
319
320llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
321 CodeGenFunction &CGF) const {
322 return 0;
323}
324
325ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
326 ASTContext &Context,
327 llvm::LLVMContext &VMContext) const {
Chris Lattner9723d6c2010-03-11 18:19:55 +0000328 if (CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000329 return ABIArgInfo::getIndirect(0);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000330
Chris Lattner9723d6c2010-03-11 18:19:55 +0000331 // Treat an enum type as its underlying type.
332 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
333 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000334
Chris Lattner9723d6c2010-03-11 18:19:55 +0000335 return (Ty->isPromotableIntegerType() ?
336 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000337}
338
Chris Lattner0cf24192010-06-28 20:05:43 +0000339//===----------------------------------------------------------------------===//
340// X86-32 ABI Implementation
341//===----------------------------------------------------------------------===//
342
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000343/// X86_32ABIInfo - The X86-32 ABI information.
344class X86_32ABIInfo : public ABIInfo {
David Chisnallde3a0692009-08-17 23:08:21 +0000345 bool IsDarwinVectorABI;
346 bool IsSmallStructInRegABI;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000347
348 static bool isRegisterSize(unsigned Size) {
349 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
350 }
351
352 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
353
Daniel Dunbar557893d2010-04-21 19:10:51 +0000354 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
355 /// such that the argument will be passed in memory.
356 ABIArgInfo getIndirectResult(QualType Ty, ASTContext &Context,
357 bool ByVal = true) const;
358
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000359public:
Chris Lattner2b037972010-07-29 02:01:43 +0000360 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
361
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000362 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000363 ASTContext &Context,
364 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000365
366 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000367 ASTContext &Context,
368 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000369
Owen Anderson170229f2009-07-14 23:10:40 +0000370 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
Chris Lattner1d7c9f72010-06-29 01:08:48 +0000371 llvm::LLVMContext &VMContext,
372 const llvm::Type *const *PrefTypes,
373 unsigned NumPrefTypes) const {
Owen Anderson170229f2009-07-14 23:10:40 +0000374 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
375 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000376 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
377 it != ie; ++it)
Owen Anderson170229f2009-07-14 23:10:40 +0000378 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000379 }
380
381 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
382 CodeGenFunction &CGF) const;
383
Chris Lattner2b037972010-07-29 02:01:43 +0000384 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
385 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p) {}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000386};
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000387
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000388class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
389public:
Chris Lattner2b037972010-07-29 02:01:43 +0000390 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
391 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p)) {}
Charles Davis4ea31ab2010-02-13 15:54:06 +0000392
393 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
394 CodeGen::CodeGenModule &CGM) const;
John McCallbeec5a02010-03-06 00:35:14 +0000395
396 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
397 // Darwin uses different dwarf register numbers for EH.
398 if (CGM.isTargetDarwin()) return 5;
399
400 return 4;
401 }
402
403 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
404 llvm::Value *Address) const;
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000405};
406
407}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000408
409/// shouldReturnTypeInRegister - Determine if the given type should be
410/// passed in a register (for the Darwin ABI).
411bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
412 ASTContext &Context) {
413 uint64_t Size = Context.getTypeSize(Ty);
414
415 // Type must be register sized.
416 if (!isRegisterSize(Size))
417 return false;
418
419 if (Ty->isVectorType()) {
420 // 64- and 128- bit vectors inside structures are not returned in
421 // registers.
422 if (Size == 64 || Size == 128)
423 return false;
424
425 return true;
426 }
427
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000428 // If this is a builtin, pointer, enum, complex type, member pointer, or
429 // member function pointer it is ok.
Daniel Dunbar6b45b672010-05-14 03:40:53 +0000430 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000431 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000432 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000433 return true;
434
435 // Arrays are treated like records.
436 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
437 return shouldReturnTypeInRegister(AT->getElementType(), Context);
438
439 // Otherwise, it must be a record type.
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000440 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000441 if (!RT) return false;
442
Anders Carlsson40446e82010-01-27 03:25:19 +0000443 // FIXME: Traverse bases here too.
444
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000445 // Structure types are passed in register if all fields would be
446 // passed in a register.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000447 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
448 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000449 const FieldDecl *FD = *i;
450
451 // Empty fields are ignored.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000452 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000453 continue;
454
455 // Check fields recursively.
456 if (!shouldReturnTypeInRegister(FD->getType(), Context))
457 return false;
458 }
459
460 return true;
461}
462
463ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000464 ASTContext &Context,
465 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000466 if (RetTy->isVoidType()) {
467 return ABIArgInfo::getIgnore();
John McCall9dd450b2009-09-21 23:43:11 +0000468 } else if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000469 // On Darwin, some vectors are returned in registers.
David Chisnallde3a0692009-08-17 23:08:21 +0000470 if (IsDarwinVectorABI) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000471 uint64_t Size = Context.getTypeSize(RetTy);
472
473 // 128-bit vectors are a special case; they are returned in
474 // registers and we need to make sure to pick a type the LLVM
475 // backend will like.
476 if (Size == 128)
Owen Anderson41a75022009-08-13 21:57:51 +0000477 return ABIArgInfo::getCoerce(llvm::VectorType::get(
478 llvm::Type::getInt64Ty(VMContext), 2));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000479
480 // Always return in register if it fits in a general purpose
481 // register, or if it is 64 bits and has a single element.
482 if ((Size == 8 || Size == 16 || Size == 32) ||
483 (Size == 64 && VT->getNumElements() == 1))
Owen Anderson41a75022009-08-13 21:57:51 +0000484 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000485
486 return ABIArgInfo::getIndirect(0);
487 }
488
489 return ABIArgInfo::getDirect();
490 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlsson40446e82010-01-27 03:25:19 +0000491 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson5789c492009-10-20 22:07:59 +0000492 // Structures with either a non-trivial destructor or a non-trivial
493 // copy constructor are always indirect.
494 if (hasNonTrivialDestructorOrCopyConstructor(RT))
495 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
496
497 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000498 if (RT->getDecl()->hasFlexibleArrayMember())
499 return ABIArgInfo::getIndirect(0);
Anders Carlsson5789c492009-10-20 22:07:59 +0000500 }
501
David Chisnallde3a0692009-08-17 23:08:21 +0000502 // If specified, structs and unions are always indirect.
503 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000504 return ABIArgInfo::getIndirect(0);
505
506 // Classify "single element" structs as their element type.
507 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
John McCall9dd450b2009-09-21 23:43:11 +0000508 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000509 if (BT->isIntegerType()) {
510 // We need to use the size of the structure, padding
511 // bit-fields can adjust that to be larger than the single
512 // element type.
513 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000514 return ABIArgInfo::getCoerce(
Owen Anderson41a75022009-08-13 21:57:51 +0000515 llvm::IntegerType::get(VMContext, (unsigned) Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000516 } else if (BT->getKind() == BuiltinType::Float) {
517 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
518 "Unexpect single element structure size!");
Owen Anderson41a75022009-08-13 21:57:51 +0000519 return ABIArgInfo::getCoerce(llvm::Type::getFloatTy(VMContext));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000520 } else if (BT->getKind() == BuiltinType::Double) {
521 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
522 "Unexpect single element structure size!");
Owen Anderson41a75022009-08-13 21:57:51 +0000523 return ABIArgInfo::getCoerce(llvm::Type::getDoubleTy(VMContext));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000524 }
525 } else if (SeltTy->isPointerType()) {
526 // FIXME: It would be really nice if this could come out as the proper
527 // pointer type.
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000528 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000529 return ABIArgInfo::getCoerce(PtrTy);
530 } else if (SeltTy->isVectorType()) {
531 // 64- and 128-bit vectors are never returned in a
532 // register when inside a structure.
533 uint64_t Size = Context.getTypeSize(RetTy);
534 if (Size == 64 || Size == 128)
535 return ABIArgInfo::getIndirect(0);
536
Owen Anderson170229f2009-07-14 23:10:40 +0000537 return classifyReturnType(QualType(SeltTy, 0), Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000538 }
539 }
540
541 // Small structures which are register sized are generally returned
542 // in a register.
543 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context)) {
544 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson41a75022009-08-13 21:57:51 +0000545 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000546 }
547
548 return ABIArgInfo::getIndirect(0);
549 } else {
Douglas Gregora71cc152010-02-02 20:10:50 +0000550 // Treat an enum type as its underlying type.
551 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
552 RetTy = EnumTy->getDecl()->getIntegerType();
553
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000554 return (RetTy->isPromotableIntegerType() ?
555 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000556 }
557}
558
Daniel Dunbar557893d2010-04-21 19:10:51 +0000559ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty,
560 ASTContext &Context,
561 bool ByVal) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +0000562 if (!ByVal)
563 return ABIArgInfo::getIndirect(0, false);
564
565 // Compute the byval alignment. We trust the back-end to honor the
566 // minimum ABI alignment for byval, to make cleaner IR.
567 const unsigned MinABIAlign = 4;
568 unsigned Align = Context.getTypeAlign(Ty) / 8;
569 if (Align > MinABIAlign)
570 return ABIArgInfo::getIndirect(Align);
571 return ABIArgInfo::getIndirect(0);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000572}
573
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000574ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +0000575 ASTContext &Context,
576 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000577 // FIXME: Set alignment on indirect arguments.
578 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
579 // Structures with flexible arrays are always indirect.
Anders Carlsson40446e82010-01-27 03:25:19 +0000580 if (const RecordType *RT = Ty->getAs<RecordType>()) {
581 // Structures with either a non-trivial destructor or a non-trivial
582 // copy constructor are always indirect.
583 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Daniel Dunbar557893d2010-04-21 19:10:51 +0000584 return getIndirectResult(Ty, Context, /*ByVal=*/false);
585
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000586 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbar557893d2010-04-21 19:10:51 +0000587 return getIndirectResult(Ty, Context);
Anders Carlsson40446e82010-01-27 03:25:19 +0000588 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000589
590 // Ignore empty structs.
Eli Friedman3192cc82009-06-13 21:37:10 +0000591 if (Ty->isStructureType() && Context.getTypeSize(Ty) == 0)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000592 return ABIArgInfo::getIgnore();
593
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000594 // Expand small (<= 128-bit) record types when we know that the stack layout
595 // of those arguments will match the struct. This is important because the
596 // LLVM backend isn't smart enough to remove byval, which inhibits many
597 // optimizations.
598 if (Context.getTypeSize(Ty) <= 4*32 &&
599 canExpandIndirectArgument(Ty, Context))
600 return ABIArgInfo::getExpand();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000601
Daniel Dunbar557893d2010-04-21 19:10:51 +0000602 return getIndirectResult(Ty, Context);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000603 } else {
Douglas Gregora71cc152010-02-02 20:10:50 +0000604 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
605 Ty = EnumTy->getDecl()->getIntegerType();
606
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000607 return (Ty->isPromotableIntegerType() ?
608 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000609 }
610}
611
612llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
613 CodeGenFunction &CGF) const {
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000614 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +0000615 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000616
617 CGBuilderTy &Builder = CGF.Builder;
618 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
619 "ap");
620 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
621 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000622 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000623 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
624
625 uint64_t Offset =
626 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
627 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +0000628 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000629 "ap.next");
630 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
631
632 return AddrTyped;
633}
634
Charles Davis4ea31ab2010-02-13 15:54:06 +0000635void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
636 llvm::GlobalValue *GV,
637 CodeGen::CodeGenModule &CGM) const {
638 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
639 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
640 // Get the LLVM function.
641 llvm::Function *Fn = cast<llvm::Function>(GV);
642
643 // Now add the 'alignstack' attribute with a value of 16.
644 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
645 }
646 }
647}
648
John McCallbeec5a02010-03-06 00:35:14 +0000649bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
650 CodeGen::CodeGenFunction &CGF,
651 llvm::Value *Address) const {
652 CodeGen::CGBuilderTy &Builder = CGF.Builder;
653 llvm::LLVMContext &Context = CGF.getLLVMContext();
654
655 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
656 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
657
658 // 0-7 are the eight integer registers; the order is different
659 // on Darwin (for EH), but the range is the same.
660 // 8 is %eip.
John McCall943fae92010-05-27 06:19:26 +0000661 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCallbeec5a02010-03-06 00:35:14 +0000662
663 if (CGF.CGM.isTargetDarwin()) {
664 // 12-16 are st(0..4). Not sure why we stop at 4.
665 // These have size 16, which is sizeof(long double) on
666 // platforms with 8-byte alignment for that type.
667 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCall943fae92010-05-27 06:19:26 +0000668 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
John McCallbeec5a02010-03-06 00:35:14 +0000669
670 } else {
671 // 9 is %eflags, which doesn't get a size on Darwin for some
672 // reason.
673 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
674
675 // 11-16 are st(0..5). Not sure why we stop at 5.
676 // These have size 12, which is sizeof(long double) on
677 // platforms with 4-byte alignment for that type.
678 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCall943fae92010-05-27 06:19:26 +0000679 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
680 }
John McCallbeec5a02010-03-06 00:35:14 +0000681
682 return false;
683}
684
Chris Lattner0cf24192010-06-28 20:05:43 +0000685//===----------------------------------------------------------------------===//
686// X86-64 ABI Implementation
687//===----------------------------------------------------------------------===//
688
689
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000690namespace {
691/// X86_64ABIInfo - The X86_64 ABI information.
692class X86_64ABIInfo : public ABIInfo {
693 enum Class {
694 Integer = 0,
695 SSE,
696 SSEUp,
697 X87,
698 X87Up,
699 ComplexX87,
700 NoClass,
701 Memory
702 };
703
704 /// merge - Implement the X86_64 ABI merging algorithm.
705 ///
706 /// Merge an accumulating classification \arg Accum with a field
707 /// classification \arg Field.
708 ///
709 /// \param Accum - The accumulating classification. This should
710 /// always be either NoClass or the result of a previous merge
711 /// call. In addition, this should never be Memory (the caller
712 /// should just return Memory for the aggregate).
Chris Lattnerd776fb12010-06-28 21:43:59 +0000713 static Class merge(Class Accum, Class Field);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000714
715 /// classify - Determine the x86_64 register classes in which the
716 /// given type T should be passed.
717 ///
718 /// \param Lo - The classification for the parts of the type
719 /// residing in the low word of the containing object.
720 ///
721 /// \param Hi - The classification for the parts of the type
722 /// residing in the high word of the containing object.
723 ///
724 /// \param OffsetBase - The bit offset of this type in the
725 /// containing object. Some parameters are classified different
726 /// depending on whether they straddle an eightbyte boundary.
727 ///
728 /// If a word is unused its result will be NoClass; if a type should
729 /// be passed in Memory then at least the classification of \arg Lo
730 /// will be Memory.
731 ///
732 /// The \arg Lo class will be NoClass iff the argument is ignored.
733 ///
734 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
735 /// also be ComplexX87.
Chris Lattner22a931e2010-06-29 06:01:59 +0000736 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000737
738 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
739 /// to coerce to, chose the best way to pass Ty in the same place
740 /// that \arg CoerceTo would be passed, but while keeping the
741 /// emitted code as simple as possible.
742 ///
743 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
744 /// the ways we might want to pass things, instead of constructing an LLVM
745 /// type. This makes this code more explicit, and it makes it clearer that we
746 /// are also doing this for correctness in the case of passing scalar types.
747 ABIArgInfo getCoerceResult(QualType Ty,
Chris Lattner22a931e2010-06-29 06:01:59 +0000748 const llvm::Type *CoerceTo) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000749
750 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar53fac692010-04-21 19:49:55 +0000751 /// such that the argument will be returned in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000752 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar53fac692010-04-21 19:49:55 +0000753
754 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000755 /// such that the argument will be passed in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000756 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000757
758 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000759 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000760
761 ABIArgInfo classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +0000762 llvm::LLVMContext &VMContext,
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000763 unsigned &neededInt,
Chris Lattner399d22a2010-06-29 01:14:09 +0000764 unsigned &neededSSE,
765 const llvm::Type *PrefType) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000766
767public:
Chris Lattner2b037972010-07-29 02:01:43 +0000768 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Chris Lattner22a931e2010-06-29 06:01:59 +0000769
Owen Anderson170229f2009-07-14 23:10:40 +0000770 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
Chris Lattner1d7c9f72010-06-29 01:08:48 +0000771 llvm::LLVMContext &VMContext,
772 const llvm::Type *const *PrefTypes,
773 unsigned NumPrefTypes) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000774
775 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
776 CodeGenFunction &CGF) const;
777};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000778
779class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
780public:
Chris Lattner2b037972010-07-29 02:01:43 +0000781 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
782 : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {}
John McCallbeec5a02010-03-06 00:35:14 +0000783
784 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
785 return 7;
786 }
787
788 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
789 llvm::Value *Address) const {
790 CodeGen::CGBuilderTy &Builder = CGF.Builder;
791 llvm::LLVMContext &Context = CGF.getLLVMContext();
792
793 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
794 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
795
John McCall943fae92010-05-27 06:19:26 +0000796 // 0-15 are the 16 integer registers.
797 // 16 is %rip.
798 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCallbeec5a02010-03-06 00:35:14 +0000799
800 return false;
801 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000802};
803
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000804}
805
Chris Lattnerd776fb12010-06-28 21:43:59 +0000806X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000807 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
808 // classified recursively so that always two fields are
809 // considered. The resulting class is calculated according to
810 // the classes of the fields in the eightbyte:
811 //
812 // (a) If both classes are equal, this is the resulting class.
813 //
814 // (b) If one of the classes is NO_CLASS, the resulting class is
815 // the other class.
816 //
817 // (c) If one of the classes is MEMORY, the result is the MEMORY
818 // class.
819 //
820 // (d) If one of the classes is INTEGER, the result is the
821 // INTEGER.
822 //
823 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
824 // MEMORY is used as class.
825 //
826 // (f) Otherwise class SSE is used.
827
828 // Accum should never be memory (we should have returned) or
829 // ComplexX87 (because this cannot be passed in a structure).
830 assert((Accum != Memory && Accum != ComplexX87) &&
831 "Invalid accumulated classification during merge.");
832 if (Accum == Field || Field == NoClass)
833 return Accum;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000834 if (Field == Memory)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000835 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000836 if (Accum == NoClass)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000837 return Field;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000838 if (Accum == Integer || Field == Integer)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000839 return Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000840 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
841 Accum == X87 || Accum == X87Up)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000842 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000843 return SSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000844}
845
Chris Lattner5c740f12010-06-30 19:14:05 +0000846void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000847 Class &Lo, Class &Hi) const {
848 // FIXME: This code can be simplified by introducing a simple value class for
849 // Class pairs with appropriate constructor methods for the various
850 // situations.
851
852 // FIXME: Some of the split computations are wrong; unaligned vectors
853 // shouldn't be passed in registers for example, so there is no chance they
854 // can straddle an eightbyte. Verify & simplify.
855
856 Lo = Hi = NoClass;
857
858 Class &Current = OffsetBase < 64 ? Lo : Hi;
859 Current = Memory;
860
John McCall9dd450b2009-09-21 23:43:11 +0000861 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000862 BuiltinType::Kind k = BT->getKind();
863
864 if (k == BuiltinType::Void) {
865 Current = NoClass;
866 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
867 Lo = Integer;
868 Hi = Integer;
869 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
870 Current = Integer;
871 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
872 Current = SSE;
873 } else if (k == BuiltinType::LongDouble) {
874 Lo = X87;
875 Hi = X87Up;
876 }
877 // FIXME: _Decimal32 and _Decimal64 are SSE.
878 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattnerd776fb12010-06-28 21:43:59 +0000879 return;
880 }
881
882 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000883 // Classify the underlying integer type.
Chris Lattner22a931e2010-06-29 06:01:59 +0000884 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattnerd776fb12010-06-28 21:43:59 +0000885 return;
886 }
887
888 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000889 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000890 return;
891 }
892
893 if (Ty->isMemberPointerType()) {
Daniel Dunbar36d4d152010-05-15 00:00:37 +0000894 if (Ty->isMemberFunctionPointerType())
895 Lo = Hi = Integer;
896 else
897 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000898 return;
899 }
900
901 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +0000902 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000903 if (Size == 32) {
904 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
905 // float> as integer.
906 Current = Integer;
907
908 // If this type crosses an eightbyte boundary, it should be
909 // split.
910 uint64_t EB_Real = (OffsetBase) / 64;
911 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
912 if (EB_Real != EB_Imag)
913 Hi = Lo;
914 } else if (Size == 64) {
915 // gcc passes <1 x double> in memory. :(
916 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
917 return;
918
919 // gcc passes <1 x long long> as INTEGER.
920 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
921 Current = Integer;
922 else
923 Current = SSE;
924
925 // If this type crosses an eightbyte boundary, it should be
926 // split.
927 if (OffsetBase && OffsetBase != 64)
928 Hi = Lo;
929 } else if (Size == 128) {
930 Lo = SSE;
931 Hi = SSEUp;
932 }
Chris Lattnerd776fb12010-06-28 21:43:59 +0000933 return;
934 }
935
936 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +0000937 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000938
Chris Lattner2b037972010-07-29 02:01:43 +0000939 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregorb90df602010-06-16 00:17:44 +0000940 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000941 if (Size <= 64)
942 Current = Integer;
943 else if (Size <= 128)
944 Lo = Hi = Integer;
Chris Lattner2b037972010-07-29 02:01:43 +0000945 } else if (ET == getContext().FloatTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000946 Current = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +0000947 else if (ET == getContext().DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000948 Lo = Hi = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +0000949 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000950 Current = ComplexX87;
951
952 // If this complex type crosses an eightbyte boundary then it
953 // should be split.
954 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattner2b037972010-07-29 02:01:43 +0000955 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000956 if (Hi == NoClass && EB_Real != EB_Imag)
957 Hi = Lo;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000958
959 return;
960 }
961
Chris Lattner2b037972010-07-29 02:01:43 +0000962 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000963 // Arrays are treated like structures.
964
Chris Lattner2b037972010-07-29 02:01:43 +0000965 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000966
967 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
968 // than two eightbytes, ..., it has class MEMORY.
969 if (Size > 128)
970 return;
971
972 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
973 // fields, it has class MEMORY.
974 //
975 // Only need to check alignment of array base.
Chris Lattner2b037972010-07-29 02:01:43 +0000976 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000977 return;
978
979 // Otherwise implement simplified merge. We could be smarter about
980 // this, but it isn't worth it and would be harder to verify.
981 Current = NoClass;
Chris Lattner2b037972010-07-29 02:01:43 +0000982 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000983 uint64_t ArraySize = AT->getSize().getZExtValue();
984 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
985 Class FieldLo, FieldHi;
Chris Lattner22a931e2010-06-29 06:01:59 +0000986 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000987 Lo = merge(Lo, FieldLo);
988 Hi = merge(Hi, FieldHi);
989 if (Lo == Memory || Hi == Memory)
990 break;
991 }
992
993 // Do post merger cleanup (see below). Only case we worry about is Memory.
994 if (Hi == Memory)
995 Lo = Memory;
996 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattnerd776fb12010-06-28 21:43:59 +0000997 return;
998 }
999
1000 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001001 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001002
1003 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
1004 // than two eightbytes, ..., it has class MEMORY.
1005 if (Size > 128)
1006 return;
1007
Anders Carlsson20759ad2009-09-16 15:53:40 +00001008 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1009 // copy constructor or a non-trivial destructor, it is passed by invisible
1010 // reference.
1011 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1012 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001013
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001014 const RecordDecl *RD = RT->getDecl();
1015
1016 // Assume variable sized types are passed in memory.
1017 if (RD->hasFlexibleArrayMember())
1018 return;
1019
Chris Lattner2b037972010-07-29 02:01:43 +00001020 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001021
1022 // Reset Lo class, this will be recomputed.
1023 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001024
1025 // If this is a C++ record, classify the bases first.
1026 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1027 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1028 e = CXXRD->bases_end(); i != e; ++i) {
1029 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1030 "Unexpected base class!");
1031 const CXXRecordDecl *Base =
1032 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1033
1034 // Classify this field.
1035 //
1036 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1037 // single eightbyte, each is classified separately. Each eightbyte gets
1038 // initialized to class NO_CLASS.
1039 Class FieldLo, FieldHi;
1040 uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
Chris Lattner22a931e2010-06-29 06:01:59 +00001041 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001042 Lo = merge(Lo, FieldLo);
1043 Hi = merge(Hi, FieldHi);
1044 if (Lo == Memory || Hi == Memory)
1045 break;
1046 }
Daniel Dunbar3780f0b2009-12-22 01:19:25 +00001047
1048 // If this record has no fields but isn't empty, classify as INTEGER.
1049 if (RD->field_empty() && Size)
1050 Current = Integer;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001051 }
1052
1053 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001054 unsigned idx = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001055 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1056 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001057 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1058 bool BitField = i->isBitField();
1059
1060 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1061 // fields, it has class MEMORY.
1062 //
1063 // Note, skip this test for bit-fields, see below.
Chris Lattner2b037972010-07-29 02:01:43 +00001064 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001065 Lo = Memory;
1066 return;
1067 }
1068
1069 // Classify this field.
1070 //
1071 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1072 // exceeds a single eightbyte, each is classified
1073 // separately. Each eightbyte gets initialized to class
1074 // NO_CLASS.
1075 Class FieldLo, FieldHi;
1076
1077 // Bit-fields require special handling, they do not force the
1078 // structure to be passed in memory even if unaligned, and
1079 // therefore they can straddle an eightbyte.
1080 if (BitField) {
1081 // Ignore padding bit-fields.
1082 if (i->isUnnamedBitfield())
1083 continue;
1084
1085 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Chris Lattner2b037972010-07-29 02:01:43 +00001086 uint64_t Size =
1087 i->getBitWidth()->EvaluateAsInt(getContext()).getZExtValue();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001088
1089 uint64_t EB_Lo = Offset / 64;
1090 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1091 FieldLo = FieldHi = NoClass;
1092 if (EB_Lo) {
1093 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1094 FieldLo = NoClass;
1095 FieldHi = Integer;
1096 } else {
1097 FieldLo = Integer;
1098 FieldHi = EB_Hi ? Integer : NoClass;
1099 }
1100 } else
Chris Lattner22a931e2010-06-29 06:01:59 +00001101 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001102 Lo = merge(Lo, FieldLo);
1103 Hi = merge(Hi, FieldHi);
1104 if (Lo == Memory || Hi == Memory)
1105 break;
1106 }
1107
1108 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1109 //
1110 // (a) If one of the classes is MEMORY, the whole argument is
1111 // passed in memory.
1112 //
1113 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
1114
1115 // The first of these conditions is guaranteed by how we implement
1116 // the merge (just bail).
1117 //
1118 // The second condition occurs in the case of unions; for example
1119 // union { _Complex double; unsigned; }.
1120 if (Hi == Memory)
1121 Lo = Memory;
1122 if (Hi == SSEUp && Lo != SSE)
1123 Hi = SSE;
1124 }
1125}
1126
1127ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
Chris Lattner22a931e2010-06-29 06:01:59 +00001128 const llvm::Type *CoerceTo) const {
Chris Lattner4c1e4842010-07-28 22:15:08 +00001129 // If this is a pointer passed as a pointer, just pass it directly.
1130 if ((isa<llvm::PointerType>(CoerceTo) || CoerceTo->isIntegerTy(64)) &&
1131 Ty->hasPointerRepresentation())
1132 return ABIArgInfo::getExtend();
1133
1134 if (isa<llvm::IntegerType>(CoerceTo)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001135 // Integer and pointer types will end up in a general purpose
1136 // register.
Douglas Gregora71cc152010-02-02 20:10:50 +00001137
1138 // Treat an enum type as its underlying type.
1139 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1140 Ty = EnumTy->getDecl()->getIntegerType();
1141
Chris Lattner4c1e4842010-07-28 22:15:08 +00001142 if (Ty->isIntegralOrEnumerationType())
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001143 return (Ty->isPromotableIntegerType() ?
1144 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001145
Chris Lattnerfa20e952010-06-26 21:52:32 +00001146 } else if (CoerceTo->isDoubleTy()) {
John McCall8ee376f2010-02-24 07:14:12 +00001147 assert(Ty.isCanonical() && "should always have a canonical type here");
1148 assert(!Ty.hasQualifiers() && "should never have a qualified type here");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001149
1150 // Float and double end up in a single SSE reg.
Chris Lattner2b037972010-07-29 02:01:43 +00001151 if (Ty == getContext().FloatTy || Ty == getContext().DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001152 return ABIArgInfo::getDirect();
1153
Chris Lattnera7d81ab2010-06-28 19:56:59 +00001154 // If this is a 32-bit structure that is passed as a double, then it will be
1155 // passed in the low 32-bits of the XMM register, which is the same as how a
1156 // float is passed. Coerce to a float instead of a double.
Chris Lattner2b037972010-07-29 02:01:43 +00001157 if (getContext().getTypeSizeInChars(Ty).getQuantity() == 4)
Chris Lattnera7d81ab2010-06-28 19:56:59 +00001158 CoerceTo = llvm::Type::getFloatTy(CoerceTo->getContext());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001159 }
1160
1161 return ABIArgInfo::getCoerce(CoerceTo);
1162}
1163
Chris Lattner22a931e2010-06-29 06:01:59 +00001164ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001165 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1166 // place naturally.
1167 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1168 // Treat an enum type as its underlying type.
1169 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1170 Ty = EnumTy->getDecl()->getIntegerType();
1171
1172 return (Ty->isPromotableIntegerType() ?
1173 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1174 }
1175
1176 return ABIArgInfo::getIndirect(0);
1177}
1178
Chris Lattner22a931e2010-06-29 06:01:59 +00001179ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001180 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1181 // place naturally.
Douglas Gregora71cc152010-02-02 20:10:50 +00001182 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1183 // Treat an enum type as its underlying type.
1184 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1185 Ty = EnumTy->getDecl()->getIntegerType();
1186
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001187 return (Ty->isPromotableIntegerType() ?
1188 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001189 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001190
Daniel Dunbar53fac692010-04-21 19:49:55 +00001191 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1192 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson20759ad2009-09-16 15:53:40 +00001193
Daniel Dunbar53fac692010-04-21 19:49:55 +00001194 // Compute the byval alignment. We trust the back-end to honor the
1195 // minimum ABI alignment for byval, to make cleaner IR.
1196 const unsigned MinABIAlign = 8;
Chris Lattner2b037972010-07-29 02:01:43 +00001197 unsigned Align = getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar53fac692010-04-21 19:49:55 +00001198 if (Align > MinABIAlign)
1199 return ABIArgInfo::getIndirect(Align);
1200 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001201}
1202
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001203/// Get8ByteTypeAtOffset - The ABI specifies that a value should be passed in an
1204/// 8-byte GPR. This means that we either have a scalar or we are talking about
1205/// the high or low part of an up-to-16-byte struct. This routine picks the
1206/// best LLVM IR type to represent this, which may be i64 or may be anything
1207/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1208/// etc).
1209///
1210/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1211/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1212/// the 8-byte value references. PrefType may be null.
1213///
1214/// SourceTy is the source level type for the entire argument. SourceOffset is
1215/// an offset into this that we're processing (which is always either 0 or 8).
1216///
Chris Lattner22a931e2010-06-29 06:01:59 +00001217static const llvm::Type *Get8ByteTypeAtOffset(const llvm::Type *PrefType,
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001218 unsigned IROffset,
1219 QualType SourceTy,
1220 unsigned SourceOffset,
1221 const llvm::TargetData &TD,
1222 llvm::LLVMContext &VMContext,
1223 ASTContext &Context) {
Chris Lattner22a931e2010-06-29 06:01:59 +00001224 // Pointers are always 8-bytes at offset 0.
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001225 if (IROffset == 0 && PrefType && isa<llvm::PointerType>(PrefType))
Chris Lattner22a931e2010-06-29 06:01:59 +00001226 return PrefType;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001227
Chris Lattner22a931e2010-06-29 06:01:59 +00001228 // TODO: 1/2/4/8 byte integers are also interesting, but we have to know that
1229 // the "hole" is not used in the containing struct (just undef padding).
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001230
1231 if (const llvm::StructType *STy =
1232 dyn_cast_or_null<llvm::StructType>(PrefType)) {
1233 // If this is a struct, recurse into the field at the specified offset.
1234 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1235 if (IROffset < SL->getSizeInBytes()) {
1236 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1237 IROffset -= SL->getElementOffset(FieldIdx);
1238
1239 return Get8ByteTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1240 SourceTy, SourceOffset, TD,VMContext,Context);
1241 }
1242 }
Chris Lattner22a931e2010-06-29 06:01:59 +00001243
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001244 // Okay, we don't have any better idea of what to pass, so we pass this in an
1245 // integer register that isn't too big to fit the rest of the struct.
1246 uint64_t TySizeInBytes = Context.getTypeSizeInChars(SourceTy).getQuantity();
1247
1248 // It is always safe to classify this as an integer type up to i64 that
1249 // isn't larger than the structure.
1250 switch (unsigned(TySizeInBytes-SourceOffset)) {
1251 case 1: return llvm::Type::getInt8Ty(VMContext);
1252 case 2: return llvm::Type::getInt16Ty(VMContext);
1253 case 3:
1254 case 4: return llvm::Type::getInt32Ty(VMContext);
1255 default: return llvm::Type::getInt64Ty(VMContext);
1256 }
Chris Lattner22a931e2010-06-29 06:01:59 +00001257}
1258
Chris Lattner31faff52010-07-28 23:06:14 +00001259ABIArgInfo X86_64ABIInfo::
1260classifyReturnType(QualType RetTy, llvm::LLVMContext &VMContext) const {
1261 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1262 // classification algorithm.
1263 X86_64ABIInfo::Class Lo, Hi;
1264 classify(RetTy, 0, Lo, Hi);
1265
1266 // Check some invariants.
1267 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1268 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1269 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1270
1271 const llvm::Type *ResType = 0;
1272 switch (Lo) {
1273 case NoClass:
1274 return ABIArgInfo::getIgnore();
1275
1276 case SSEUp:
1277 case X87Up:
1278 assert(0 && "Invalid classification for lo word.");
1279
1280 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1281 // hidden argument.
1282 case Memory:
1283 return getIndirectReturnResult(RetTy);
1284
1285 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1286 // available register of the sequence %rax, %rdx is used.
1287 case Integer:
Chris Lattner2b037972010-07-29 02:01:43 +00001288 ResType = Get8ByteTypeAtOffset(0, 0, RetTy, 0, getTargetData(),
1289 VMContext, getContext());
Chris Lattner31faff52010-07-28 23:06:14 +00001290 break;
1291
1292 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1293 // available SSE register of the sequence %xmm0, %xmm1 is used.
1294 case SSE:
Chris Lattner2b037972010-07-29 02:01:43 +00001295 ResType = llvm::Type::getDoubleTy(getVMContext());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001296 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001297
1298 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1299 // returned on the X87 stack in %st0 as 80-bit x87 number.
1300 case X87:
Chris Lattner2b037972010-07-29 02:01:43 +00001301 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001302 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001303
1304 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1305 // part of the value is returned in %st0 and the imaginary part in
1306 // %st1.
1307 case ComplexX87:
1308 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
1309 ResType = llvm::StructType::get(VMContext,
Chris Lattner2b037972010-07-29 02:01:43 +00001310 llvm::Type::getX86_FP80Ty(getVMContext()),
1311 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner31faff52010-07-28 23:06:14 +00001312 NULL);
1313 break;
1314 }
1315
1316 switch (Hi) {
1317 // Memory was handled previously and X87 should
1318 // never occur as a hi class.
1319 case Memory:
1320 case X87:
1321 assert(0 && "Invalid classification for hi word.");
1322
1323 case ComplexX87: // Previously handled.
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001324 case NoClass:
1325 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001326
1327 case Integer: {
1328 const llvm::Type *HiType =
Chris Lattner2b037972010-07-29 02:01:43 +00001329 Get8ByteTypeAtOffset(0, 8, RetTy, 8, getTargetData(), VMContext,
1330 getContext());
Chris Lattner31faff52010-07-28 23:06:14 +00001331 ResType = llvm::StructType::get(VMContext, ResType, HiType, NULL);
1332 break;
1333 }
1334 case SSE:
1335 ResType = llvm::StructType::get(VMContext, ResType,
1336 llvm::Type::getDoubleTy(VMContext), NULL);
1337 break;
1338
1339 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1340 // is passed in the upper half of the last used SSE register.
1341 //
1342 // SSEUP should always be preceeded by SSE, just widen.
1343 case SSEUp:
1344 assert(Lo == SSE && "Unexpected SSEUp classification.");
1345 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
1346 break;
1347
1348 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1349 // returned together with the previous X87 value in %st0.
1350 case X87Up:
1351 // If X87Up is preceeded by X87, we don't need to do
1352 // anything. However, in some cases with unions it may not be
1353 // preceeded by X87. In such situations we follow gcc and pass the
1354 // extra bits in an SSE reg.
1355 if (Lo != X87)
1356 ResType = llvm::StructType::get(VMContext, ResType,
1357 llvm::Type::getDoubleTy(VMContext), NULL);
1358 break;
1359 }
1360
1361 return getCoerceResult(RetTy, ResType);
1362}
1363
Chris Lattner22a931e2010-06-29 06:01:59 +00001364ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +00001365 llvm::LLVMContext &VMContext,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001366 unsigned &neededInt,
Chris Lattner399d22a2010-06-29 01:14:09 +00001367 unsigned &neededSSE,
1368 const llvm::Type *PrefType)const{
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001369 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001370 classify(Ty, 0, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001371
1372 // Check some invariants.
1373 // FIXME: Enforce these by construction.
1374 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1375 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1376 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1377
1378 neededInt = 0;
1379 neededSSE = 0;
1380 const llvm::Type *ResType = 0;
1381 switch (Lo) {
1382 case NoClass:
1383 return ABIArgInfo::getIgnore();
1384
1385 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1386 // on the stack.
1387 case Memory:
1388
1389 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1390 // COMPLEX_X87, it is passed in memory.
1391 case X87:
1392 case ComplexX87:
Chris Lattner22a931e2010-06-29 06:01:59 +00001393 return getIndirectResult(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001394
1395 case SSEUp:
1396 case X87Up:
1397 assert(0 && "Invalid classification for lo word.");
1398
1399 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1400 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1401 // and %r9 is used.
1402 case Integer:
Chris Lattner22a931e2010-06-29 06:01:59 +00001403 ++neededInt;
1404
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001405 // Pick an 8-byte type based on the preferred type.
Chris Lattner2b037972010-07-29 02:01:43 +00001406 ResType = Get8ByteTypeAtOffset(PrefType, 0, Ty, 0, getTargetData(),
1407 VMContext, getContext());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001408 break;
1409
1410 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1411 // available SSE register is used, the registers are taken in the
1412 // order from %xmm0 to %xmm7.
1413 case SSE:
1414 ++neededSSE;
Owen Anderson41a75022009-08-13 21:57:51 +00001415 ResType = llvm::Type::getDoubleTy(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001416 break;
1417 }
1418
1419 switch (Hi) {
1420 // Memory was handled previously, ComplexX87 and X87 should
1421 // never occur as hi classes, and X87Up must be preceed by X87,
1422 // which is passed in memory.
1423 case Memory:
1424 case X87:
1425 case ComplexX87:
1426 assert(0 && "Invalid classification for hi word.");
1427 break;
1428
1429 case NoClass: break;
Chris Lattner22a931e2010-06-29 06:01:59 +00001430
1431 case Integer: {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001432 ++neededInt;
Chris Lattner22a931e2010-06-29 06:01:59 +00001433
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001434 // Pick an 8-byte type based on the preferred type.
1435 const llvm::Type *HiType =
Chris Lattner2b037972010-07-29 02:01:43 +00001436 Get8ByteTypeAtOffset(PrefType, 8, Ty, 8, getTargetData(),
1437 VMContext, getContext());
Chris Lattner22a931e2010-06-29 06:01:59 +00001438 ResType = llvm::StructType::get(VMContext, ResType, HiType, NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001439 break;
Chris Lattner22a931e2010-06-29 06:01:59 +00001440 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001441
1442 // X87Up generally doesn't occur here (long double is passed in
1443 // memory), except in situations involving unions.
1444 case X87Up:
1445 case SSE:
Owen Anderson758428f2009-08-05 23:18:46 +00001446 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001447 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001448 ++neededSSE;
1449 break;
1450
1451 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1452 // eightbyte is passed in the upper half of the last used SSE
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00001453 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001454 case SSEUp:
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00001455 assert(Lo == SSE && "Unexpected SSEUp classification");
Owen Anderson41a75022009-08-13 21:57:51 +00001456 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00001457
1458 // If the preferred type is a 16-byte vector, prefer to pass it.
1459 if (const llvm::VectorType *VT =
1460 dyn_cast_or_null<llvm::VectorType>(PrefType)) {
1461 const llvm::Type *EltTy = VT->getElementType();
1462 if (VT->getBitWidth() == 128 &&
1463 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1464 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1465 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1466 EltTy->isIntegerTy(128)))
1467 ResType = PrefType;
1468 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001469 break;
1470 }
1471
Chris Lattner22a931e2010-06-29 06:01:59 +00001472 return getCoerceResult(Ty, ResType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001473}
1474
Owen Anderson170229f2009-07-14 23:10:40 +00001475void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
Chris Lattner1d7c9f72010-06-29 01:08:48 +00001476 llvm::LLVMContext &VMContext,
1477 const llvm::Type *const *PrefTypes,
1478 unsigned NumPrefTypes) const {
Chris Lattner22a931e2010-06-29 06:01:59 +00001479 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001480
1481 // Keep track of the number of assigned registers.
1482 unsigned freeIntRegs = 6, freeSSERegs = 8;
1483
1484 // If the return value is indirect, then the hidden argument is consuming one
1485 // integer register.
1486 if (FI.getReturnInfo().isIndirect())
1487 --freeIntRegs;
1488
1489 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1490 // get assigned (in left-to-right order) for passing as follows...
1491 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1492 it != ie; ++it) {
Chris Lattner399d22a2010-06-29 01:14:09 +00001493 // If the client specified a preferred IR type to use, pass it down to
1494 // classifyArgumentType.
1495 const llvm::Type *PrefType = 0;
1496 if (NumPrefTypes) {
1497 PrefType = *PrefTypes++;
1498 --NumPrefTypes;
1499 }
1500
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001501 unsigned neededInt, neededSSE;
Chris Lattner22a931e2010-06-29 06:01:59 +00001502 it->info = classifyArgumentType(it->type, VMContext,
Chris Lattner399d22a2010-06-29 01:14:09 +00001503 neededInt, neededSSE, PrefType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001504
1505 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1506 // eightbyte of an argument, the whole argument is passed on the
1507 // stack. If registers have already been assigned for some
1508 // eightbytes of such an argument, the assignments get reverted.
1509 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1510 freeIntRegs -= neededInt;
1511 freeSSERegs -= neededSSE;
1512 } else {
Chris Lattner22a931e2010-06-29 06:01:59 +00001513 it->info = getIndirectResult(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001514 }
1515 }
1516}
1517
1518static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1519 QualType Ty,
1520 CodeGenFunction &CGF) {
1521 llvm::Value *overflow_arg_area_p =
1522 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1523 llvm::Value *overflow_arg_area =
1524 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1525
1526 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1527 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1528 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1529 if (Align > 8) {
1530 // Note that we follow the ABI & gcc here, even though the type
1531 // could in theory have an alignment greater than 16. This case
1532 // shouldn't ever matter in practice.
1533
1534 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson41a75022009-08-13 21:57:51 +00001535 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001536 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001537 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1538 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner5e016ae2010-06-27 07:15:29 +00001539 CGF.Int64Ty);
1540 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001541 overflow_arg_area =
1542 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1543 overflow_arg_area->getType(),
1544 "overflow_arg_area.align");
1545 }
1546
1547 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1548 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1549 llvm::Value *Res =
1550 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001551 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001552
1553 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1554 // l->overflow_arg_area + sizeof(type).
1555 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1556 // an 8 byte boundary.
1557
1558 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00001559 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001560 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001561 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1562 "overflow_arg_area.next");
1563 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1564
1565 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1566 return Res;
1567}
1568
1569llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1570 CodeGenFunction &CGF) const {
Owen Anderson170229f2009-07-14 23:10:40 +00001571 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump11289f42009-09-09 15:08:12 +00001572
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001573 // Assume that va_list type is correct; should be pointer to LLVM type:
1574 // struct {
1575 // i32 gp_offset;
1576 // i32 fp_offset;
1577 // i8* overflow_arg_area;
1578 // i8* reg_save_area;
1579 // };
1580 unsigned neededInt, neededSSE;
Chris Lattner9723d6c2010-03-11 18:19:55 +00001581
1582 Ty = CGF.getContext().getCanonicalType(Ty);
Chris Lattner22a931e2010-06-29 06:01:59 +00001583 ABIArgInfo AI = classifyArgumentType(Ty, VMContext, neededInt, neededSSE, 0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001584
1585 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1586 // in the registers. If not go to step 7.
1587 if (!neededInt && !neededSSE)
1588 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1589
1590 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1591 // general purpose registers needed to pass type and num_fp to hold
1592 // the number of floating point registers needed.
1593
1594 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1595 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1596 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1597 //
1598 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1599 // register save space).
1600
1601 llvm::Value *InRegs = 0;
1602 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1603 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1604 if (neededInt) {
1605 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1606 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattnerd776fb12010-06-28 21:43:59 +00001607 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
1608 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001609 }
1610
1611 if (neededSSE) {
1612 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1613 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1614 llvm::Value *FitsInFP =
Chris Lattnerd776fb12010-06-28 21:43:59 +00001615 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
1616 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001617 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1618 }
1619
1620 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1621 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1622 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1623 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1624
1625 // Emit code to load the value if it was passed in registers.
1626
1627 CGF.EmitBlock(InRegBlock);
1628
1629 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1630 // an offset of l->gp_offset and/or l->fp_offset. This may require
1631 // copying to a temporary location in case the parameter is passed
1632 // in different register classes or requires an alignment greater
1633 // than 8 for general purpose registers and 16 for XMM registers.
1634 //
1635 // FIXME: This really results in shameful code when we end up needing to
1636 // collect arguments from different places; often what should result in a
1637 // simple assembling of a structure from scattered addresses has many more
1638 // loads than necessary. Can we clean this up?
1639 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1640 llvm::Value *RegAddr =
1641 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1642 "reg_save_area");
1643 if (neededInt && neededSSE) {
1644 // FIXME: Cleanup.
1645 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1646 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1647 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1648 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1649 const llvm::Type *TyLo = ST->getElementType(0);
1650 const llvm::Type *TyHi = ST->getElementType(1);
Duncan Sands998f9d92010-02-15 16:14:01 +00001651 assert((TyLo->isFloatingPointTy() ^ TyHi->isFloatingPointTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001652 "Unexpected ABI info for mixed regs");
Owen Anderson9793f0e2009-07-29 22:16:19 +00001653 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1654 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001655 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1656 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sands998f9d92010-02-15 16:14:01 +00001657 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1658 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001659 llvm::Value *V =
1660 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1661 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1662 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1663 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1664
Owen Anderson170229f2009-07-14 23:10:40 +00001665 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001666 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001667 } else if (neededInt) {
1668 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1669 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001670 llvm::PointerType::getUnqual(LTy));
Chris Lattner0cf24192010-06-28 20:05:43 +00001671 } else if (neededSSE == 1) {
1672 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1673 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1674 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001675 } else {
Chris Lattner0cf24192010-06-28 20:05:43 +00001676 assert(neededSSE == 2 && "Invalid number of needed registers!");
1677 // SSE registers are spaced 16 bytes apart in the register save
1678 // area, we need to collect the two eightbytes together.
1679 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattnerd776fb12010-06-28 21:43:59 +00001680 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner0cf24192010-06-28 20:05:43 +00001681 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
1682 const llvm::Type *DblPtrTy =
1683 llvm::PointerType::getUnqual(DoubleTy);
1684 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1685 DoubleTy, NULL);
1686 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1687 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1688 DblPtrTy));
1689 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1690 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1691 DblPtrTy));
1692 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1693 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1694 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001695 }
1696
1697 // AMD64-ABI 3.5.7p5: Step 5. Set:
1698 // l->gp_offset = l->gp_offset + num_gp * 8
1699 // l->fp_offset = l->fp_offset + num_fp * 16.
1700 if (neededInt) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00001701 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001702 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1703 gp_offset_p);
1704 }
1705 if (neededSSE) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00001706 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001707 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1708 fp_offset_p);
1709 }
1710 CGF.EmitBranch(ContBlock);
1711
1712 // Emit code to load the value if it was passed in memory.
1713
1714 CGF.EmitBlock(InMemBlock);
1715 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1716
1717 // Return the appropriate result.
1718
1719 CGF.EmitBlock(ContBlock);
1720 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1721 "vaarg.addr");
1722 ResAddr->reserveOperandSpace(2);
1723 ResAddr->addIncoming(RegAddr, InRegBlock);
1724 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001725 return ResAddr;
1726}
1727
Chris Lattner0cf24192010-06-28 20:05:43 +00001728
1729
1730//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001731// PIC16 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00001732//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001733
1734namespace {
1735
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001736class PIC16ABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +00001737public:
1738 PIC16ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
1739
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001740 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001741 ASTContext &Context,
1742 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001743
1744 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001745 ASTContext &Context,
1746 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001747
Owen Anderson170229f2009-07-14 23:10:40 +00001748 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
Chris Lattner1d7c9f72010-06-29 01:08:48 +00001749 llvm::LLVMContext &VMContext,
1750 const llvm::Type *const *PrefTypes,
1751 unsigned NumPrefTypes) const {
Owen Anderson170229f2009-07-14 23:10:40 +00001752 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1753 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001754 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1755 it != ie; ++it)
Owen Anderson170229f2009-07-14 23:10:40 +00001756 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001757 }
1758
1759 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1760 CodeGenFunction &CGF) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001761};
1762
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001763class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
1764public:
Chris Lattner2b037972010-07-29 02:01:43 +00001765 PIC16TargetCodeGenInfo(CodeGenTypes &CGT)
1766 : TargetCodeGenInfo(new PIC16ABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001767};
1768
Daniel Dunbard59655c2009-09-12 00:59:49 +00001769}
1770
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001771ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001772 ASTContext &Context,
1773 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001774 if (RetTy->isVoidType()) {
1775 return ABIArgInfo::getIgnore();
1776 } else {
1777 return ABIArgInfo::getDirect();
1778 }
1779}
1780
1781ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +00001782 ASTContext &Context,
1783 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001784 return ABIArgInfo::getDirect();
1785}
1786
1787llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00001788 CodeGenFunction &CGF) const {
Chris Lattnerc0e8a592010-04-06 17:29:22 +00001789 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00001790 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1791
1792 CGBuilderTy &Builder = CGF.Builder;
1793 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1794 "ap");
1795 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1796 llvm::Type *PTy =
1797 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1798 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1799
1800 uint64_t Offset = CGF.getContext().getTypeSize(Ty) / 8;
1801
1802 llvm::Value *NextAddr =
1803 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1804 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
1805 "ap.next");
1806 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1807
1808 return AddrTyped;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001809}
1810
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00001811
John McCallea8d8bb2010-03-11 00:10:12 +00001812// PowerPC-32
1813
1814namespace {
1815class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
1816public:
Chris Lattner2b037972010-07-29 02:01:43 +00001817 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
1818
John McCallea8d8bb2010-03-11 00:10:12 +00001819 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1820 // This is recovered from gcc output.
1821 return 1; // r1 is the dedicated stack pointer
1822 }
1823
1824 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1825 llvm::Value *Address) const;
1826};
1827
1828}
1829
1830bool
1831PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1832 llvm::Value *Address) const {
1833 // This is calculated from the LLVM and GCC tables and verified
1834 // against gcc output. AFAIK all ABIs use the same encoding.
1835
1836 CodeGen::CGBuilderTy &Builder = CGF.Builder;
1837 llvm::LLVMContext &Context = CGF.getLLVMContext();
1838
1839 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
1840 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
1841 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
1842 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
1843
1844 // 0-31: r0-31, the 4-byte general-purpose registers
John McCall943fae92010-05-27 06:19:26 +00001845 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallea8d8bb2010-03-11 00:10:12 +00001846
1847 // 32-63: fp0-31, the 8-byte floating-point registers
John McCall943fae92010-05-27 06:19:26 +00001848 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallea8d8bb2010-03-11 00:10:12 +00001849
1850 // 64-76 are various 4-byte special-purpose registers:
1851 // 64: mq
1852 // 65: lr
1853 // 66: ctr
1854 // 67: ap
1855 // 68-75 cr0-7
1856 // 76: xer
John McCall943fae92010-05-27 06:19:26 +00001857 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallea8d8bb2010-03-11 00:10:12 +00001858
1859 // 77-108: v0-31, the 16-byte vector registers
John McCall943fae92010-05-27 06:19:26 +00001860 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallea8d8bb2010-03-11 00:10:12 +00001861
1862 // 109: vrsave
1863 // 110: vscr
1864 // 111: spe_acc
1865 // 112: spefscr
1866 // 113: sfp
John McCall943fae92010-05-27 06:19:26 +00001867 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallea8d8bb2010-03-11 00:10:12 +00001868
1869 return false;
1870}
1871
1872
Chris Lattner0cf24192010-06-28 20:05:43 +00001873//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001874// ARM ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00001875//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001876
1877namespace {
1878
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001879class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00001880public:
1881 enum ABIKind {
1882 APCS = 0,
1883 AAPCS = 1,
1884 AAPCS_VFP
1885 };
1886
1887private:
1888 ABIKind Kind;
1889
1890public:
Chris Lattner2b037972010-07-29 02:01:43 +00001891 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar020daa92009-09-12 01:00:39 +00001892
1893private:
1894 ABIKind getABIKind() const { return Kind; }
1895
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001896 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001897 ASTContext &Context,
1898 llvm::LLVMContext &VMCOntext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001899
1900 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001901 ASTContext &Context,
1902 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001903
Owen Anderson170229f2009-07-14 23:10:40 +00001904 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
Chris Lattner1d7c9f72010-06-29 01:08:48 +00001905 llvm::LLVMContext &VMContext,
1906 const llvm::Type *const *PrefTypes,
1907 unsigned NumPrefTypes) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001908
1909 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1910 CodeGenFunction &CGF) const;
1911};
1912
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001913class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
1914public:
Chris Lattner2b037972010-07-29 02:01:43 +00001915 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
1916 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00001917
1918 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1919 return 13;
1920 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001921};
1922
Daniel Dunbard59655c2009-09-12 00:59:49 +00001923}
1924
Owen Anderson170229f2009-07-14 23:10:40 +00001925void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
Chris Lattner1d7c9f72010-06-29 01:08:48 +00001926 llvm::LLVMContext &VMContext,
1927 const llvm::Type *const *PrefTypes,
1928 unsigned NumPrefTypes) const {
Mike Stump11289f42009-09-09 15:08:12 +00001929 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
Owen Anderson170229f2009-07-14 23:10:40 +00001930 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001931 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1932 it != ie; ++it) {
Owen Anderson170229f2009-07-14 23:10:40 +00001933 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001934 }
Daniel Dunbar020daa92009-09-12 01:00:39 +00001935
Rafael Espindolaa92c4422010-06-16 16:13:39 +00001936 const llvm::Triple &Triple(Context.Target.getTriple());
1937 llvm::CallingConv::ID DefaultCC;
Rafael Espindola23a8a062010-06-16 19:01:17 +00001938 if (Triple.getEnvironmentName() == "gnueabi" ||
1939 Triple.getEnvironmentName() == "eabi")
Rafael Espindolaa92c4422010-06-16 16:13:39 +00001940 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola23a8a062010-06-16 19:01:17 +00001941 else
1942 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindolaa92c4422010-06-16 16:13:39 +00001943
Daniel Dunbar020daa92009-09-12 01:00:39 +00001944 switch (getABIKind()) {
1945 case APCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00001946 if (DefaultCC != llvm::CallingConv::ARM_APCS)
1947 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00001948 break;
1949
1950 case AAPCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00001951 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
1952 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00001953 break;
1954
1955 case AAPCS_VFP:
1956 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
1957 break;
1958 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001959}
1960
1961ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +00001962 ASTContext &Context,
1963 llvm::LLVMContext &VMContext) const {
Douglas Gregora71cc152010-02-02 20:10:50 +00001964 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1965 // Treat an enum type as its underlying type.
1966 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1967 Ty = EnumTy->getDecl()->getIntegerType();
1968
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001969 return (Ty->isPromotableIntegerType() ?
1970 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001971 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001972
Daniel Dunbar09d33622009-09-14 21:54:03 +00001973 // Ignore empty records.
1974 if (isEmptyRecord(Context, Ty, true))
1975 return ABIArgInfo::getIgnore();
1976
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00001977 // Structures with either a non-trivial destructor or a non-trivial
1978 // copy constructor are always indirect.
1979 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1980 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
1981
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001982 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1983 // backend doesn't support byval.
1984 // FIXME: This doesn't handle alignment > 64 bits.
1985 const llvm::Type* ElemTy;
1986 unsigned SizeRegs;
1987 if (Context.getTypeAlign(Ty) > 32) {
Owen Anderson41a75022009-08-13 21:57:51 +00001988 ElemTy = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001989 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1990 } else {
Owen Anderson41a75022009-08-13 21:57:51 +00001991 ElemTy = llvm::Type::getInt32Ty(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001992 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1993 }
1994 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson9793f0e2009-07-29 22:16:19 +00001995 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Owen Anderson758428f2009-08-05 23:18:46 +00001996 const llvm::Type* STy = llvm::StructType::get(VMContext, LLVMFields, true);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001997 return ABIArgInfo::getCoerce(STy);
1998}
1999
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002000static bool isIntegerLikeType(QualType Ty,
2001 ASTContext &Context,
2002 llvm::LLVMContext &VMContext) {
2003 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2004 // is called integer-like if its size is less than or equal to one word, and
2005 // the offset of each of its addressable sub-fields is zero.
2006
2007 uint64_t Size = Context.getTypeSize(Ty);
2008
2009 // Check that the type fits in a word.
2010 if (Size > 32)
2011 return false;
2012
2013 // FIXME: Handle vector types!
2014 if (Ty->isVectorType())
2015 return false;
2016
Daniel Dunbard53bac72009-09-14 02:20:34 +00002017 // Float types are never treated as "integer like".
2018 if (Ty->isRealFloatingType())
2019 return false;
2020
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002021 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00002022 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002023 return true;
2024
Daniel Dunbar96ebba52010-02-01 23:31:26 +00002025 // Small complex integer types are "integer like".
2026 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2027 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002028
2029 // Single element and zero sized arrays should be allowed, by the definition
2030 // above, but they are not.
2031
2032 // Otherwise, it must be a record type.
2033 const RecordType *RT = Ty->getAs<RecordType>();
2034 if (!RT) return false;
2035
2036 // Ignore records with flexible arrays.
2037 const RecordDecl *RD = RT->getDecl();
2038 if (RD->hasFlexibleArrayMember())
2039 return false;
2040
2041 // Check that all sub-fields are at offset 0, and are themselves "integer
2042 // like".
2043 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2044
2045 bool HadField = false;
2046 unsigned idx = 0;
2047 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2048 i != e; ++i, ++idx) {
2049 const FieldDecl *FD = *i;
2050
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002051 // Bit-fields are not addressable, we only need to verify they are "integer
2052 // like". We still have to disallow a subsequent non-bitfield, for example:
2053 // struct { int : 0; int x }
2054 // is non-integer like according to gcc.
2055 if (FD->isBitField()) {
2056 if (!RD->isUnion())
2057 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002058
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002059 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2060 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002061
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002062 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002063 }
2064
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002065 // Check if this field is at offset 0.
2066 if (Layout.getFieldOffset(idx) != 0)
2067 return false;
2068
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002069 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2070 return false;
2071
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002072 // Only allow at most one field in a structure. This doesn't match the
2073 // wording above, but follows gcc in situations with a field following an
2074 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002075 if (!RD->isUnion()) {
2076 if (HadField)
2077 return false;
2078
2079 HadField = true;
2080 }
2081 }
2082
2083 return true;
2084}
2085
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002086ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00002087 ASTContext &Context,
2088 llvm::LLVMContext &VMContext) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002089 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002090 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002091
Douglas Gregora71cc152010-02-02 20:10:50 +00002092 if (!CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2093 // Treat an enum type as its underlying type.
2094 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2095 RetTy = EnumTy->getDecl()->getIntegerType();
2096
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002097 return (RetTy->isPromotableIntegerType() ?
2098 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002099 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002100
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002101 // Structures with either a non-trivial destructor or a non-trivial
2102 // copy constructor are always indirect.
2103 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2104 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2105
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002106 // Are we following APCS?
2107 if (getABIKind() == APCS) {
2108 if (isEmptyRecord(Context, RetTy, false))
2109 return ABIArgInfo::getIgnore();
2110
Daniel Dunbareedf1512010-02-01 23:31:19 +00002111 // Complex types are all returned as packed integers.
2112 //
2113 // FIXME: Consider using 2 x vector types if the back end handles them
2114 // correctly.
2115 if (RetTy->isAnyComplexType())
2116 return ABIArgInfo::getCoerce(llvm::IntegerType::get(
2117 VMContext, Context.getTypeSize(RetTy)));
2118
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002119 // Integer like structures are returned in r0.
2120 if (isIntegerLikeType(RetTy, Context, VMContext)) {
2121 // Return in the smallest viable integer type.
2122 uint64_t Size = Context.getTypeSize(RetTy);
2123 if (Size <= 8)
2124 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
2125 if (Size <= 16)
2126 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
2127 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
2128 }
2129
2130 // Otherwise return in memory.
2131 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002132 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002133
2134 // Otherwise this is an AAPCS variant.
2135
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002136 if (isEmptyRecord(Context, RetTy, true))
2137 return ABIArgInfo::getIgnore();
2138
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002139 // Aggregates <= 4 bytes are returned in r0; other aggregates
2140 // are returned indirectly.
2141 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002142 if (Size <= 32) {
2143 // Return in the smallest viable integer type.
2144 if (Size <= 8)
2145 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
2146 if (Size <= 16)
2147 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002148 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002149 }
2150
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002151 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002152}
2153
2154llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00002155 CodeGenFunction &CGF) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002156 // FIXME: Need to handle alignment
Benjamin Kramerabd5b902009-10-13 10:07:13 +00002157 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002158 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002159
2160 CGBuilderTy &Builder = CGF.Builder;
2161 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2162 "ap");
2163 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2164 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00002165 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002166 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2167
2168 uint64_t Offset =
2169 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2170 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +00002171 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002172 "ap.next");
2173 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2174
2175 return AddrTyped;
2176}
2177
2178ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00002179 ASTContext &Context,
2180 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002181 if (RetTy->isVoidType()) {
2182 return ABIArgInfo::getIgnore();
2183 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2184 return ABIArgInfo::getIndirect(0);
2185 } else {
Douglas Gregora71cc152010-02-02 20:10:50 +00002186 // Treat an enum type as its underlying type.
2187 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2188 RetTy = EnumTy->getDecl()->getIntegerType();
2189
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002190 return (RetTy->isPromotableIntegerType() ?
2191 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002192 }
2193}
2194
Chris Lattner0cf24192010-06-28 20:05:43 +00002195//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002196// SystemZ ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002197//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002198
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002199namespace {
Daniel Dunbard59655c2009-09-12 00:59:49 +00002200
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002201class SystemZABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +00002202public:
2203 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2204
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002205 bool isPromotableIntegerType(QualType Ty) const;
2206
2207 ABIArgInfo classifyReturnType(QualType RetTy, ASTContext &Context,
2208 llvm::LLVMContext &VMContext) const;
2209
2210 ABIArgInfo classifyArgumentType(QualType RetTy, ASTContext &Context,
2211 llvm::LLVMContext &VMContext) const;
2212
2213 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
Chris Lattner1d7c9f72010-06-29 01:08:48 +00002214 llvm::LLVMContext &VMContext,
2215 const llvm::Type *const *PrefTypes,
2216 unsigned NumPrefTypes) const {
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002217 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
2218 Context, VMContext);
2219 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2220 it != ie; ++it)
2221 it->info = classifyArgumentType(it->type, Context, VMContext);
2222 }
2223
2224 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2225 CodeGenFunction &CGF) const;
2226};
Daniel Dunbard59655c2009-09-12 00:59:49 +00002227
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002228class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2229public:
Chris Lattner2b037972010-07-29 02:01:43 +00002230 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
2231 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002232};
2233
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002234}
2235
2236bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2237 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall9dd450b2009-09-21 23:43:11 +00002238 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002239 switch (BT->getKind()) {
2240 case BuiltinType::Bool:
2241 case BuiltinType::Char_S:
2242 case BuiltinType::Char_U:
2243 case BuiltinType::SChar:
2244 case BuiltinType::UChar:
2245 case BuiltinType::Short:
2246 case BuiltinType::UShort:
2247 case BuiltinType::Int:
2248 case BuiltinType::UInt:
2249 return true;
2250 default:
2251 return false;
2252 }
2253 return false;
2254}
2255
2256llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2257 CodeGenFunction &CGF) const {
2258 // FIXME: Implement
2259 return 0;
2260}
2261
2262
2263ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy,
2264 ASTContext &Context,
Daniel Dunbard59655c2009-09-12 00:59:49 +00002265 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002266 if (RetTy->isVoidType()) {
2267 return ABIArgInfo::getIgnore();
2268 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2269 return ABIArgInfo::getIndirect(0);
2270 } else {
2271 return (isPromotableIntegerType(RetTy) ?
2272 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2273 }
2274}
2275
2276ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty,
2277 ASTContext &Context,
Daniel Dunbard59655c2009-09-12 00:59:49 +00002278 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002279 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
2280 return ABIArgInfo::getIndirect(0);
2281 } else {
2282 return (isPromotableIntegerType(Ty) ?
2283 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2284 }
2285}
2286
Chris Lattner0cf24192010-06-28 20:05:43 +00002287//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002288// MSP430 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002289//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002290
2291namespace {
2292
2293class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2294public:
Chris Lattner2b037972010-07-29 02:01:43 +00002295 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
2296 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002297 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2298 CodeGen::CodeGenModule &M) const;
2299};
2300
2301}
2302
2303void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2304 llvm::GlobalValue *GV,
2305 CodeGen::CodeGenModule &M) const {
2306 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2307 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2308 // Handle 'interrupt' attribute:
2309 llvm::Function *F = cast<llvm::Function>(GV);
2310
2311 // Step 1: Set ISR calling convention.
2312 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2313
2314 // Step 2: Add attributes goodness.
2315 F->addFnAttr(llvm::Attribute::NoInline);
2316
2317 // Step 3: Emit ISR vector alias.
2318 unsigned Num = attr->getNumber() + 0xffe0;
2319 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2320 "vector_" +
2321 llvm::LowercaseString(llvm::utohexstr(Num)),
2322 GV, &M.getModule());
2323 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002324 }
2325}
2326
Chris Lattner0cf24192010-06-28 20:05:43 +00002327//===----------------------------------------------------------------------===//
John McCall943fae92010-05-27 06:19:26 +00002328// MIPS ABI Implementation. This works for both little-endian and
2329// big-endian variants.
Chris Lattner0cf24192010-06-28 20:05:43 +00002330//===----------------------------------------------------------------------===//
2331
John McCall943fae92010-05-27 06:19:26 +00002332namespace {
2333class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
2334public:
Chris Lattner2b037972010-07-29 02:01:43 +00002335 MIPSTargetCodeGenInfo(CodeGenTypes &CGT)
2336 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
John McCall943fae92010-05-27 06:19:26 +00002337
2338 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2339 return 29;
2340 }
2341
2342 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2343 llvm::Value *Address) const;
2344};
2345}
2346
2347bool
2348MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2349 llvm::Value *Address) const {
2350 // This information comes from gcc's implementation, which seems to
2351 // as canonical as it gets.
2352
2353 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2354 llvm::LLVMContext &Context = CGF.getLLVMContext();
2355
2356 // Everything on MIPS is 4 bytes. Double-precision FP registers
2357 // are aliased to pairs of single-precision FP registers.
2358 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2359 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2360
2361 // 0-31 are the general purpose registers, $0 - $31.
2362 // 32-63 are the floating-point registers, $f0 - $f31.
2363 // 64 and 65 are the multiply/divide registers, $hi and $lo.
2364 // 66 is the (notional, I think) register for signal-handler return.
2365 AssignToArrayRange(Builder, Address, Four8, 0, 65);
2366
2367 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
2368 // They are one bit wide and ignored here.
2369
2370 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
2371 // (coprocessor 1 is the FP unit)
2372 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
2373 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
2374 // 176-181 are the DSP accumulator registers.
2375 AssignToArrayRange(Builder, Address, Four8, 80, 181);
2376
2377 return false;
2378}
2379
2380
Chris Lattner2b037972010-07-29 02:01:43 +00002381const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002382 if (TheTargetCodeGenInfo)
2383 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002384
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002385 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2386 // free it.
Daniel Dunbare3532f82009-08-24 08:52:16 +00002387
Chris Lattner22a931e2010-06-29 06:01:59 +00002388 const llvm::Triple &Triple = getContext().Target.getTriple();
Daniel Dunbar40165182009-08-24 09:10:05 +00002389 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00002390 default:
Chris Lattner2b037972010-07-29 02:01:43 +00002391 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002392
John McCall943fae92010-05-27 06:19:26 +00002393 case llvm::Triple::mips:
2394 case llvm::Triple::mipsel:
Chris Lattner2b037972010-07-29 02:01:43 +00002395 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types));
John McCall943fae92010-05-27 06:19:26 +00002396
Daniel Dunbard59655c2009-09-12 00:59:49 +00002397 case llvm::Triple::arm:
2398 case llvm::Triple::thumb:
Daniel Dunbar020daa92009-09-12 01:00:39 +00002399 // FIXME: We want to know the float calling convention as well.
Daniel Dunbarb4091a92009-09-14 00:35:03 +00002400 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002401 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002402 new ARMTargetCodeGenInfo(Types, ARMABIInfo::APCS));
Daniel Dunbar020daa92009-09-12 01:00:39 +00002403
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002404 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002405 new ARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002406
2407 case llvm::Triple::pic16:
Chris Lattner2b037972010-07-29 02:01:43 +00002408 return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002409
John McCallea8d8bb2010-03-11 00:10:12 +00002410 case llvm::Triple::ppc:
Chris Lattner2b037972010-07-29 02:01:43 +00002411 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallea8d8bb2010-03-11 00:10:12 +00002412
Daniel Dunbard59655c2009-09-12 00:59:49 +00002413 case llvm::Triple::systemz:
Chris Lattner2b037972010-07-29 02:01:43 +00002414 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002415
2416 case llvm::Triple::msp430:
Chris Lattner2b037972010-07-29 02:01:43 +00002417 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002418
Daniel Dunbar40165182009-08-24 09:10:05 +00002419 case llvm::Triple::x86:
Daniel Dunbar40165182009-08-24 09:10:05 +00002420 switch (Triple.getOS()) {
Edward O'Callaghan462e4ab2009-10-20 17:22:50 +00002421 case llvm::Triple::Darwin:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002422 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002423 new X86_32TargetCodeGenInfo(Types, true, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002424 case llvm::Triple::Cygwin:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002425 case llvm::Triple::MinGW32:
2426 case llvm::Triple::MinGW64:
Edward O'Callaghan437ec1e2009-10-21 11:58:24 +00002427 case llvm::Triple::AuroraUX:
2428 case llvm::Triple::DragonFly:
David Chisnall2c5bef22009-09-03 01:48:05 +00002429 case llvm::Triple::FreeBSD:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002430 case llvm::Triple::OpenBSD:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002431 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002432 new X86_32TargetCodeGenInfo(Types, false, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002433
2434 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002435 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002436 new X86_32TargetCodeGenInfo(Types, false, false));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002437 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002438
Daniel Dunbare3532f82009-08-24 08:52:16 +00002439 case llvm::Triple::x86_64:
Chris Lattner2b037972010-07-29 02:01:43 +00002440 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002441 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002442}