blob: 3b77684c62a407b87a15ef2ee1b3006e88ae1704 [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
Chris Lattner458b2aa2010-07-29 02:16:43 +0000291 ABIArgInfo classifyReturnType(QualType RetTy) const;
292 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000293
Chris Lattner458b2aa2010-07-29 02:16:43 +0000294 virtual void computeInfo(CGFunctionInfo &FI,
Chris Lattner1d7c9f72010-06-29 01:08:48 +0000295 const llvm::Type *const *PrefTypes,
296 unsigned NumPrefTypes) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000297 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000298 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
299 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +0000300 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000301 }
302
303 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
304 CodeGenFunction &CGF) const;
305};
306
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000307class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
308public:
Chris Lattner2b037972010-07-29 02:01:43 +0000309 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
310 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000311};
312
313llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
314 CodeGenFunction &CGF) const {
315 return 0;
316}
317
Chris Lattner458b2aa2010-07-29 02:16:43 +0000318ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
Chris Lattner9723d6c2010-03-11 18:19:55 +0000319 if (CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000320 return ABIArgInfo::getIndirect(0);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000321
Chris Lattner9723d6c2010-03-11 18:19:55 +0000322 // Treat an enum type as its underlying type.
323 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
324 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000325
Chris Lattner9723d6c2010-03-11 18:19:55 +0000326 return (Ty->isPromotableIntegerType() ?
327 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000328}
329
Chris Lattner0cf24192010-06-28 20:05:43 +0000330//===----------------------------------------------------------------------===//
331// X86-32 ABI Implementation
332//===----------------------------------------------------------------------===//
333
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000334/// X86_32ABIInfo - The X86-32 ABI information.
335class X86_32ABIInfo : public ABIInfo {
David Chisnallde3a0692009-08-17 23:08:21 +0000336 bool IsDarwinVectorABI;
337 bool IsSmallStructInRegABI;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000338
339 static bool isRegisterSize(unsigned Size) {
340 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
341 }
342
343 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
344
Daniel Dunbar557893d2010-04-21 19:10:51 +0000345 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
346 /// such that the argument will be passed in memory.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000347 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbar557893d2010-04-21 19:10:51 +0000348
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000349public:
Chris Lattner2b037972010-07-29 02:01:43 +0000350
Chris Lattner458b2aa2010-07-29 02:16:43 +0000351 ABIArgInfo classifyReturnType(QualType RetTy) const;
352 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000353
Chris Lattner458b2aa2010-07-29 02:16:43 +0000354 virtual void computeInfo(CGFunctionInfo &FI,
Chris Lattner1d7c9f72010-06-29 01:08:48 +0000355 const llvm::Type *const *PrefTypes,
356 unsigned NumPrefTypes) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000357 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000358 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
359 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +0000360 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000361 }
362
363 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
364 CodeGenFunction &CGF) const;
365
Chris Lattner2b037972010-07-29 02:01:43 +0000366 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
367 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p) {}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000368};
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000369
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000370class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
371public:
Chris Lattner2b037972010-07-29 02:01:43 +0000372 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
373 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p)) {}
Charles Davis4ea31ab2010-02-13 15:54:06 +0000374
375 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
376 CodeGen::CodeGenModule &CGM) const;
John McCallbeec5a02010-03-06 00:35:14 +0000377
378 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
379 // Darwin uses different dwarf register numbers for EH.
380 if (CGM.isTargetDarwin()) return 5;
381
382 return 4;
383 }
384
385 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
386 llvm::Value *Address) const;
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000387};
388
389}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000390
391/// shouldReturnTypeInRegister - Determine if the given type should be
392/// passed in a register (for the Darwin ABI).
393bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
394 ASTContext &Context) {
395 uint64_t Size = Context.getTypeSize(Ty);
396
397 // Type must be register sized.
398 if (!isRegisterSize(Size))
399 return false;
400
401 if (Ty->isVectorType()) {
402 // 64- and 128- bit vectors inside structures are not returned in
403 // registers.
404 if (Size == 64 || Size == 128)
405 return false;
406
407 return true;
408 }
409
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000410 // If this is a builtin, pointer, enum, complex type, member pointer, or
411 // member function pointer it is ok.
Daniel Dunbar6b45b672010-05-14 03:40:53 +0000412 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000413 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000414 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000415 return true;
416
417 // Arrays are treated like records.
418 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
419 return shouldReturnTypeInRegister(AT->getElementType(), Context);
420
421 // Otherwise, it must be a record type.
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000422 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000423 if (!RT) return false;
424
Anders Carlsson40446e82010-01-27 03:25:19 +0000425 // FIXME: Traverse bases here too.
426
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000427 // Structure types are passed in register if all fields would be
428 // passed in a register.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000429 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
430 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000431 const FieldDecl *FD = *i;
432
433 // Empty fields are ignored.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000434 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000435 continue;
436
437 // Check fields recursively.
438 if (!shouldReturnTypeInRegister(FD->getType(), Context))
439 return false;
440 }
441
442 return true;
443}
444
Chris Lattner458b2aa2010-07-29 02:16:43 +0000445ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const {
446 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000447 return ABIArgInfo::getIgnore();
Chris Lattner458b2aa2010-07-29 02:16:43 +0000448
449 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000450 // On Darwin, some vectors are returned in registers.
David Chisnallde3a0692009-08-17 23:08:21 +0000451 if (IsDarwinVectorABI) {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000452 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000453
454 // 128-bit vectors are a special case; they are returned in
455 // registers and we need to make sure to pick a type the LLVM
456 // backend will like.
457 if (Size == 128)
Owen Anderson41a75022009-08-13 21:57:51 +0000458 return ABIArgInfo::getCoerce(llvm::VectorType::get(
Chris Lattner458b2aa2010-07-29 02:16:43 +0000459 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000460
461 // Always return in register if it fits in a general purpose
462 // register, or if it is 64 bits and has a single element.
463 if ((Size == 8 || Size == 16 || Size == 32) ||
464 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner458b2aa2010-07-29 02:16:43 +0000465 return ABIArgInfo::getCoerce(llvm::IntegerType::get(getVMContext(),
466 Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000467
468 return ABIArgInfo::getIndirect(0);
469 }
470
471 return ABIArgInfo::getDirect();
Chris Lattner458b2aa2010-07-29 02:16:43 +0000472 }
473
474 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlsson40446e82010-01-27 03:25:19 +0000475 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson5789c492009-10-20 22:07:59 +0000476 // Structures with either a non-trivial destructor or a non-trivial
477 // copy constructor are always indirect.
478 if (hasNonTrivialDestructorOrCopyConstructor(RT))
479 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
480
481 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000482 if (RT->getDecl()->hasFlexibleArrayMember())
483 return ABIArgInfo::getIndirect(0);
Anders Carlsson5789c492009-10-20 22:07:59 +0000484 }
485
David Chisnallde3a0692009-08-17 23:08:21 +0000486 // If specified, structs and unions are always indirect.
487 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000488 return ABIArgInfo::getIndirect(0);
489
490 // Classify "single element" structs as their element type.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000491 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) {
John McCall9dd450b2009-09-21 23:43:11 +0000492 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000493 if (BT->isIntegerType()) {
494 // We need to use the size of the structure, padding
495 // bit-fields can adjust that to be larger than the single
496 // element type.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000497 uint64_t Size = getContext().getTypeSize(RetTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000498 return ABIArgInfo::getCoerce(
Chris Lattner458b2aa2010-07-29 02:16:43 +0000499 llvm::IntegerType::get(getVMContext(), (unsigned)Size));
500 }
501
502 if (BT->getKind() == BuiltinType::Float) {
503 assert(getContext().getTypeSize(RetTy) ==
504 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000505 "Unexpect single element structure size!");
Chris Lattner458b2aa2010-07-29 02:16:43 +0000506 return ABIArgInfo::getCoerce(llvm::Type::getFloatTy(getVMContext()));
507 }
508
509 if (BT->getKind() == BuiltinType::Double) {
510 assert(getContext().getTypeSize(RetTy) ==
511 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000512 "Unexpect single element structure size!");
Chris Lattner458b2aa2010-07-29 02:16:43 +0000513 return ABIArgInfo::getCoerce(llvm::Type::getDoubleTy(getVMContext()));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000514 }
515 } else if (SeltTy->isPointerType()) {
516 // FIXME: It would be really nice if this could come out as the proper
517 // pointer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000518 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(getVMContext());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000519 return ABIArgInfo::getCoerce(PtrTy);
520 } else if (SeltTy->isVectorType()) {
521 // 64- and 128-bit vectors are never returned in a
522 // register when inside a structure.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000523 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000524 if (Size == 64 || Size == 128)
525 return ABIArgInfo::getIndirect(0);
526
Chris Lattner458b2aa2010-07-29 02:16:43 +0000527 return classifyReturnType(QualType(SeltTy, 0));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000528 }
529 }
530
531 // Small structures which are register sized are generally returned
532 // in a register.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000533 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) {
534 uint64_t Size = getContext().getTypeSize(RetTy);
535 return ABIArgInfo::getCoerce(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000536 }
537
538 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000539 }
Chris Lattner458b2aa2010-07-29 02:16:43 +0000540
541 // Treat an enum type as its underlying type.
542 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
543 RetTy = EnumTy->getDecl()->getIntegerType();
544
545 return (RetTy->isPromotableIntegerType() ?
546 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000547}
548
Chris Lattner458b2aa2010-07-29 02:16:43 +0000549ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +0000550 if (!ByVal)
551 return ABIArgInfo::getIndirect(0, false);
552
553 // Compute the byval alignment. We trust the back-end to honor the
554 // minimum ABI alignment for byval, to make cleaner IR.
555 const unsigned MinABIAlign = 4;
Chris Lattner458b2aa2010-07-29 02:16:43 +0000556 unsigned Align = getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar53fac692010-04-21 19:49:55 +0000557 if (Align > MinABIAlign)
558 return ABIArgInfo::getIndirect(Align);
559 return ABIArgInfo::getIndirect(0);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000560}
561
Chris Lattner458b2aa2010-07-29 02:16:43 +0000562ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000563 // FIXME: Set alignment on indirect arguments.
564 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
565 // Structures with flexible arrays are always indirect.
Anders Carlsson40446e82010-01-27 03:25:19 +0000566 if (const RecordType *RT = Ty->getAs<RecordType>()) {
567 // Structures with either a non-trivial destructor or a non-trivial
568 // copy constructor are always indirect.
569 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Chris Lattner458b2aa2010-07-29 02:16:43 +0000570 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000571
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000572 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattner458b2aa2010-07-29 02:16:43 +0000573 return getIndirectResult(Ty);
Anders Carlsson40446e82010-01-27 03:25:19 +0000574 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000575
576 // Ignore empty structs.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000577 if (Ty->isStructureType() && getContext().getTypeSize(Ty) == 0)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000578 return ABIArgInfo::getIgnore();
579
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000580 // Expand small (<= 128-bit) record types when we know that the stack layout
581 // of those arguments will match the struct. This is important because the
582 // LLVM backend isn't smart enough to remove byval, which inhibits many
583 // optimizations.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000584 if (getContext().getTypeSize(Ty) <= 4*32 &&
585 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000586 return ABIArgInfo::getExpand();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000587
Chris Lattner458b2aa2010-07-29 02:16:43 +0000588 return getIndirectResult(Ty);
589 }
590
591 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
592 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000593
Chris Lattner458b2aa2010-07-29 02:16:43 +0000594 return (Ty->isPromotableIntegerType() ?
595 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000596}
597
598llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
599 CodeGenFunction &CGF) const {
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000600 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +0000601 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000602
603 CGBuilderTy &Builder = CGF.Builder;
604 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
605 "ap");
606 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
607 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000608 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000609 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
610
611 uint64_t Offset =
612 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
613 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +0000614 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000615 "ap.next");
616 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
617
618 return AddrTyped;
619}
620
Charles Davis4ea31ab2010-02-13 15:54:06 +0000621void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
622 llvm::GlobalValue *GV,
623 CodeGen::CodeGenModule &CGM) const {
624 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
625 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
626 // Get the LLVM function.
627 llvm::Function *Fn = cast<llvm::Function>(GV);
628
629 // Now add the 'alignstack' attribute with a value of 16.
630 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
631 }
632 }
633}
634
John McCallbeec5a02010-03-06 00:35:14 +0000635bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
636 CodeGen::CodeGenFunction &CGF,
637 llvm::Value *Address) const {
638 CodeGen::CGBuilderTy &Builder = CGF.Builder;
639 llvm::LLVMContext &Context = CGF.getLLVMContext();
640
641 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
642 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
643
644 // 0-7 are the eight integer registers; the order is different
645 // on Darwin (for EH), but the range is the same.
646 // 8 is %eip.
John McCall943fae92010-05-27 06:19:26 +0000647 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCallbeec5a02010-03-06 00:35:14 +0000648
649 if (CGF.CGM.isTargetDarwin()) {
650 // 12-16 are st(0..4). Not sure why we stop at 4.
651 // These have size 16, which is sizeof(long double) on
652 // platforms with 8-byte alignment for that type.
653 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCall943fae92010-05-27 06:19:26 +0000654 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
John McCallbeec5a02010-03-06 00:35:14 +0000655
656 } else {
657 // 9 is %eflags, which doesn't get a size on Darwin for some
658 // reason.
659 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
660
661 // 11-16 are st(0..5). Not sure why we stop at 5.
662 // These have size 12, which is sizeof(long double) on
663 // platforms with 4-byte alignment for that type.
664 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCall943fae92010-05-27 06:19:26 +0000665 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
666 }
John McCallbeec5a02010-03-06 00:35:14 +0000667
668 return false;
669}
670
Chris Lattner0cf24192010-06-28 20:05:43 +0000671//===----------------------------------------------------------------------===//
672// X86-64 ABI Implementation
673//===----------------------------------------------------------------------===//
674
675
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000676namespace {
677/// X86_64ABIInfo - The X86_64 ABI information.
678class X86_64ABIInfo : public ABIInfo {
679 enum Class {
680 Integer = 0,
681 SSE,
682 SSEUp,
683 X87,
684 X87Up,
685 ComplexX87,
686 NoClass,
687 Memory
688 };
689
690 /// merge - Implement the X86_64 ABI merging algorithm.
691 ///
692 /// Merge an accumulating classification \arg Accum with a field
693 /// classification \arg Field.
694 ///
695 /// \param Accum - The accumulating classification. This should
696 /// always be either NoClass or the result of a previous merge
697 /// call. In addition, this should never be Memory (the caller
698 /// should just return Memory for the aggregate).
Chris Lattnerd776fb12010-06-28 21:43:59 +0000699 static Class merge(Class Accum, Class Field);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000700
701 /// classify - Determine the x86_64 register classes in which the
702 /// given type T should be passed.
703 ///
704 /// \param Lo - The classification for the parts of the type
705 /// residing in the low word of the containing object.
706 ///
707 /// \param Hi - The classification for the parts of the type
708 /// residing in the high word of the containing object.
709 ///
710 /// \param OffsetBase - The bit offset of this type in the
711 /// containing object. Some parameters are classified different
712 /// depending on whether they straddle an eightbyte boundary.
713 ///
714 /// If a word is unused its result will be NoClass; if a type should
715 /// be passed in Memory then at least the classification of \arg Lo
716 /// will be Memory.
717 ///
718 /// The \arg Lo class will be NoClass iff the argument is ignored.
719 ///
720 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
721 /// also be ComplexX87.
Chris Lattner22a931e2010-06-29 06:01:59 +0000722 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000723
724 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
725 /// to coerce to, chose the best way to pass Ty in the same place
726 /// that \arg CoerceTo would be passed, but while keeping the
727 /// emitted code as simple as possible.
728 ///
729 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
730 /// the ways we might want to pass things, instead of constructing an LLVM
731 /// type. This makes this code more explicit, and it makes it clearer that we
732 /// are also doing this for correctness in the case of passing scalar types.
733 ABIArgInfo getCoerceResult(QualType Ty,
Chris Lattner22a931e2010-06-29 06:01:59 +0000734 const llvm::Type *CoerceTo) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000735
736 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar53fac692010-04-21 19:49:55 +0000737 /// such that the argument will be returned in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000738 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar53fac692010-04-21 19:49:55 +0000739
740 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000741 /// such that the argument will be passed in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000742 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000743
Chris Lattner458b2aa2010-07-29 02:16:43 +0000744 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000745
746 ABIArgInfo classifyArgumentType(QualType Ty,
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000747 unsigned &neededInt,
Chris Lattner399d22a2010-06-29 01:14:09 +0000748 unsigned &neededSSE,
749 const llvm::Type *PrefType) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000750
751public:
Chris Lattner2b037972010-07-29 02:01:43 +0000752 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Chris Lattner22a931e2010-06-29 06:01:59 +0000753
Chris Lattner458b2aa2010-07-29 02:16:43 +0000754 virtual void computeInfo(CGFunctionInfo &FI,
Chris Lattner1d7c9f72010-06-29 01:08:48 +0000755 const llvm::Type *const *PrefTypes,
756 unsigned NumPrefTypes) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000757
758 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
759 CodeGenFunction &CGF) const;
760};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000761
762class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
763public:
Chris Lattner2b037972010-07-29 02:01:43 +0000764 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
765 : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {}
John McCallbeec5a02010-03-06 00:35:14 +0000766
767 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
768 return 7;
769 }
770
771 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
772 llvm::Value *Address) const {
773 CodeGen::CGBuilderTy &Builder = CGF.Builder;
774 llvm::LLVMContext &Context = CGF.getLLVMContext();
775
776 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
777 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
778
John McCall943fae92010-05-27 06:19:26 +0000779 // 0-15 are the 16 integer registers.
780 // 16 is %rip.
781 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCallbeec5a02010-03-06 00:35:14 +0000782
783 return false;
784 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000785};
786
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000787}
788
Chris Lattnerd776fb12010-06-28 21:43:59 +0000789X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000790 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
791 // classified recursively so that always two fields are
792 // considered. The resulting class is calculated according to
793 // the classes of the fields in the eightbyte:
794 //
795 // (a) If both classes are equal, this is the resulting class.
796 //
797 // (b) If one of the classes is NO_CLASS, the resulting class is
798 // the other class.
799 //
800 // (c) If one of the classes is MEMORY, the result is the MEMORY
801 // class.
802 //
803 // (d) If one of the classes is INTEGER, the result is the
804 // INTEGER.
805 //
806 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
807 // MEMORY is used as class.
808 //
809 // (f) Otherwise class SSE is used.
810
811 // Accum should never be memory (we should have returned) or
812 // ComplexX87 (because this cannot be passed in a structure).
813 assert((Accum != Memory && Accum != ComplexX87) &&
814 "Invalid accumulated classification during merge.");
815 if (Accum == Field || Field == NoClass)
816 return Accum;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000817 if (Field == Memory)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000818 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000819 if (Accum == NoClass)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000820 return Field;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000821 if (Accum == Integer || Field == Integer)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000822 return Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000823 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
824 Accum == X87 || Accum == X87Up)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000825 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000826 return SSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000827}
828
Chris Lattner5c740f12010-06-30 19:14:05 +0000829void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000830 Class &Lo, Class &Hi) const {
831 // FIXME: This code can be simplified by introducing a simple value class for
832 // Class pairs with appropriate constructor methods for the various
833 // situations.
834
835 // FIXME: Some of the split computations are wrong; unaligned vectors
836 // shouldn't be passed in registers for example, so there is no chance they
837 // can straddle an eightbyte. Verify & simplify.
838
839 Lo = Hi = NoClass;
840
841 Class &Current = OffsetBase < 64 ? Lo : Hi;
842 Current = Memory;
843
John McCall9dd450b2009-09-21 23:43:11 +0000844 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000845 BuiltinType::Kind k = BT->getKind();
846
847 if (k == BuiltinType::Void) {
848 Current = NoClass;
849 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
850 Lo = Integer;
851 Hi = Integer;
852 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
853 Current = Integer;
854 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
855 Current = SSE;
856 } else if (k == BuiltinType::LongDouble) {
857 Lo = X87;
858 Hi = X87Up;
859 }
860 // FIXME: _Decimal32 and _Decimal64 are SSE.
861 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattnerd776fb12010-06-28 21:43:59 +0000862 return;
863 }
864
865 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000866 // Classify the underlying integer type.
Chris Lattner22a931e2010-06-29 06:01:59 +0000867 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattnerd776fb12010-06-28 21:43:59 +0000868 return;
869 }
870
871 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000872 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000873 return;
874 }
875
876 if (Ty->isMemberPointerType()) {
Daniel Dunbar36d4d152010-05-15 00:00:37 +0000877 if (Ty->isMemberFunctionPointerType())
878 Lo = Hi = Integer;
879 else
880 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000881 return;
882 }
883
884 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +0000885 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000886 if (Size == 32) {
887 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
888 // float> as integer.
889 Current = Integer;
890
891 // If this type crosses an eightbyte boundary, it should be
892 // split.
893 uint64_t EB_Real = (OffsetBase) / 64;
894 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
895 if (EB_Real != EB_Imag)
896 Hi = Lo;
897 } else if (Size == 64) {
898 // gcc passes <1 x double> in memory. :(
899 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
900 return;
901
902 // gcc passes <1 x long long> as INTEGER.
903 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
904 Current = Integer;
905 else
906 Current = SSE;
907
908 // If this type crosses an eightbyte boundary, it should be
909 // split.
910 if (OffsetBase && OffsetBase != 64)
911 Hi = Lo;
912 } else if (Size == 128) {
913 Lo = SSE;
914 Hi = SSEUp;
915 }
Chris Lattnerd776fb12010-06-28 21:43:59 +0000916 return;
917 }
918
919 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +0000920 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000921
Chris Lattner2b037972010-07-29 02:01:43 +0000922 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregorb90df602010-06-16 00:17:44 +0000923 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000924 if (Size <= 64)
925 Current = Integer;
926 else if (Size <= 128)
927 Lo = Hi = Integer;
Chris Lattner2b037972010-07-29 02:01:43 +0000928 } else if (ET == getContext().FloatTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000929 Current = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +0000930 else if (ET == getContext().DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000931 Lo = Hi = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +0000932 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000933 Current = ComplexX87;
934
935 // If this complex type crosses an eightbyte boundary then it
936 // should be split.
937 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattner2b037972010-07-29 02:01:43 +0000938 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000939 if (Hi == NoClass && EB_Real != EB_Imag)
940 Hi = Lo;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000941
942 return;
943 }
944
Chris Lattner2b037972010-07-29 02:01:43 +0000945 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000946 // Arrays are treated like structures.
947
Chris Lattner2b037972010-07-29 02:01:43 +0000948 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000949
950 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
951 // than two eightbytes, ..., it has class MEMORY.
952 if (Size > 128)
953 return;
954
955 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
956 // fields, it has class MEMORY.
957 //
958 // Only need to check alignment of array base.
Chris Lattner2b037972010-07-29 02:01:43 +0000959 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000960 return;
961
962 // Otherwise implement simplified merge. We could be smarter about
963 // this, but it isn't worth it and would be harder to verify.
964 Current = NoClass;
Chris Lattner2b037972010-07-29 02:01:43 +0000965 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000966 uint64_t ArraySize = AT->getSize().getZExtValue();
967 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
968 Class FieldLo, FieldHi;
Chris Lattner22a931e2010-06-29 06:01:59 +0000969 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000970 Lo = merge(Lo, FieldLo);
971 Hi = merge(Hi, FieldHi);
972 if (Lo == Memory || Hi == Memory)
973 break;
974 }
975
976 // Do post merger cleanup (see below). Only case we worry about is Memory.
977 if (Hi == Memory)
978 Lo = Memory;
979 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattnerd776fb12010-06-28 21:43:59 +0000980 return;
981 }
982
983 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +0000984 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000985
986 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
987 // than two eightbytes, ..., it has class MEMORY.
988 if (Size > 128)
989 return;
990
Anders Carlsson20759ad2009-09-16 15:53:40 +0000991 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
992 // copy constructor or a non-trivial destructor, it is passed by invisible
993 // reference.
994 if (hasNonTrivialDestructorOrCopyConstructor(RT))
995 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +0000996
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000997 const RecordDecl *RD = RT->getDecl();
998
999 // Assume variable sized types are passed in memory.
1000 if (RD->hasFlexibleArrayMember())
1001 return;
1002
Chris Lattner2b037972010-07-29 02:01:43 +00001003 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001004
1005 // Reset Lo class, this will be recomputed.
1006 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001007
1008 // If this is a C++ record, classify the bases first.
1009 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1010 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1011 e = CXXRD->bases_end(); i != e; ++i) {
1012 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1013 "Unexpected base class!");
1014 const CXXRecordDecl *Base =
1015 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1016
1017 // Classify this field.
1018 //
1019 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1020 // single eightbyte, each is classified separately. Each eightbyte gets
1021 // initialized to class NO_CLASS.
1022 Class FieldLo, FieldHi;
1023 uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
Chris Lattner22a931e2010-06-29 06:01:59 +00001024 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001025 Lo = merge(Lo, FieldLo);
1026 Hi = merge(Hi, FieldHi);
1027 if (Lo == Memory || Hi == Memory)
1028 break;
1029 }
Daniel Dunbar3780f0b2009-12-22 01:19:25 +00001030
1031 // If this record has no fields but isn't empty, classify as INTEGER.
1032 if (RD->field_empty() && Size)
1033 Current = Integer;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001034 }
1035
1036 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001037 unsigned idx = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001038 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1039 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001040 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1041 bool BitField = i->isBitField();
1042
1043 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1044 // fields, it has class MEMORY.
1045 //
1046 // Note, skip this test for bit-fields, see below.
Chris Lattner2b037972010-07-29 02:01:43 +00001047 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001048 Lo = Memory;
1049 return;
1050 }
1051
1052 // Classify this field.
1053 //
1054 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1055 // exceeds a single eightbyte, each is classified
1056 // separately. Each eightbyte gets initialized to class
1057 // NO_CLASS.
1058 Class FieldLo, FieldHi;
1059
1060 // Bit-fields require special handling, they do not force the
1061 // structure to be passed in memory even if unaligned, and
1062 // therefore they can straddle an eightbyte.
1063 if (BitField) {
1064 // Ignore padding bit-fields.
1065 if (i->isUnnamedBitfield())
1066 continue;
1067
1068 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Chris Lattner2b037972010-07-29 02:01:43 +00001069 uint64_t Size =
1070 i->getBitWidth()->EvaluateAsInt(getContext()).getZExtValue();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001071
1072 uint64_t EB_Lo = Offset / 64;
1073 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1074 FieldLo = FieldHi = NoClass;
1075 if (EB_Lo) {
1076 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1077 FieldLo = NoClass;
1078 FieldHi = Integer;
1079 } else {
1080 FieldLo = Integer;
1081 FieldHi = EB_Hi ? Integer : NoClass;
1082 }
1083 } else
Chris Lattner22a931e2010-06-29 06:01:59 +00001084 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001085 Lo = merge(Lo, FieldLo);
1086 Hi = merge(Hi, FieldHi);
1087 if (Lo == Memory || Hi == Memory)
1088 break;
1089 }
1090
1091 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1092 //
1093 // (a) If one of the classes is MEMORY, the whole argument is
1094 // passed in memory.
1095 //
1096 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
1097
1098 // The first of these conditions is guaranteed by how we implement
1099 // the merge (just bail).
1100 //
1101 // The second condition occurs in the case of unions; for example
1102 // union { _Complex double; unsigned; }.
1103 if (Hi == Memory)
1104 Lo = Memory;
1105 if (Hi == SSEUp && Lo != SSE)
1106 Hi = SSE;
1107 }
1108}
1109
1110ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
Chris Lattner22a931e2010-06-29 06:01:59 +00001111 const llvm::Type *CoerceTo) const {
Chris Lattner4c1e4842010-07-28 22:15:08 +00001112 // If this is a pointer passed as a pointer, just pass it directly.
1113 if ((isa<llvm::PointerType>(CoerceTo) || CoerceTo->isIntegerTy(64)) &&
1114 Ty->hasPointerRepresentation())
1115 return ABIArgInfo::getExtend();
1116
1117 if (isa<llvm::IntegerType>(CoerceTo)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001118 // Integer and pointer types will end up in a general purpose
1119 // register.
Douglas Gregora71cc152010-02-02 20:10:50 +00001120
1121 // Treat an enum type as its underlying type.
1122 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1123 Ty = EnumTy->getDecl()->getIntegerType();
1124
Chris Lattner4c1e4842010-07-28 22:15:08 +00001125 if (Ty->isIntegralOrEnumerationType())
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001126 return (Ty->isPromotableIntegerType() ?
1127 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001128
Chris Lattnerfa20e952010-06-26 21:52:32 +00001129 } else if (CoerceTo->isDoubleTy()) {
John McCall8ee376f2010-02-24 07:14:12 +00001130 assert(Ty.isCanonical() && "should always have a canonical type here");
1131 assert(!Ty.hasQualifiers() && "should never have a qualified type here");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001132
1133 // Float and double end up in a single SSE reg.
Chris Lattner2b037972010-07-29 02:01:43 +00001134 if (Ty == getContext().FloatTy || Ty == getContext().DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001135 return ABIArgInfo::getDirect();
1136
Chris Lattnera7d81ab2010-06-28 19:56:59 +00001137 // If this is a 32-bit structure that is passed as a double, then it will be
1138 // passed in the low 32-bits of the XMM register, which is the same as how a
1139 // float is passed. Coerce to a float instead of a double.
Chris Lattner2b037972010-07-29 02:01:43 +00001140 if (getContext().getTypeSizeInChars(Ty).getQuantity() == 4)
Chris Lattnera7d81ab2010-06-28 19:56:59 +00001141 CoerceTo = llvm::Type::getFloatTy(CoerceTo->getContext());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001142 }
1143
1144 return ABIArgInfo::getCoerce(CoerceTo);
1145}
1146
Chris Lattner22a931e2010-06-29 06:01:59 +00001147ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001148 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1149 // place naturally.
1150 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1151 // Treat an enum type as its underlying type.
1152 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1153 Ty = EnumTy->getDecl()->getIntegerType();
1154
1155 return (Ty->isPromotableIntegerType() ?
1156 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1157 }
1158
1159 return ABIArgInfo::getIndirect(0);
1160}
1161
Chris Lattner22a931e2010-06-29 06:01:59 +00001162ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001163 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1164 // place naturally.
Douglas Gregora71cc152010-02-02 20:10:50 +00001165 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1166 // Treat an enum type as its underlying type.
1167 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1168 Ty = EnumTy->getDecl()->getIntegerType();
1169
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001170 return (Ty->isPromotableIntegerType() ?
1171 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001172 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001173
Daniel Dunbar53fac692010-04-21 19:49:55 +00001174 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1175 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson20759ad2009-09-16 15:53:40 +00001176
Daniel Dunbar53fac692010-04-21 19:49:55 +00001177 // Compute the byval alignment. We trust the back-end to honor the
1178 // minimum ABI alignment for byval, to make cleaner IR.
1179 const unsigned MinABIAlign = 8;
Chris Lattner2b037972010-07-29 02:01:43 +00001180 unsigned Align = getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar53fac692010-04-21 19:49:55 +00001181 if (Align > MinABIAlign)
1182 return ABIArgInfo::getIndirect(Align);
1183 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001184}
1185
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001186/// Get8ByteTypeAtOffset - The ABI specifies that a value should be passed in an
1187/// 8-byte GPR. This means that we either have a scalar or we are talking about
1188/// the high or low part of an up-to-16-byte struct. This routine picks the
1189/// best LLVM IR type to represent this, which may be i64 or may be anything
1190/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1191/// etc).
1192///
1193/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1194/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1195/// the 8-byte value references. PrefType may be null.
1196///
1197/// SourceTy is the source level type for the entire argument. SourceOffset is
1198/// an offset into this that we're processing (which is always either 0 or 8).
1199///
Chris Lattner22a931e2010-06-29 06:01:59 +00001200static const llvm::Type *Get8ByteTypeAtOffset(const llvm::Type *PrefType,
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001201 unsigned IROffset,
1202 QualType SourceTy,
1203 unsigned SourceOffset,
1204 const llvm::TargetData &TD,
1205 llvm::LLVMContext &VMContext,
1206 ASTContext &Context) {
Chris Lattner22a931e2010-06-29 06:01:59 +00001207 // Pointers are always 8-bytes at offset 0.
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001208 if (IROffset == 0 && PrefType && isa<llvm::PointerType>(PrefType))
Chris Lattner22a931e2010-06-29 06:01:59 +00001209 return PrefType;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001210
Chris Lattner22a931e2010-06-29 06:01:59 +00001211 // TODO: 1/2/4/8 byte integers are also interesting, but we have to know that
1212 // the "hole" is not used in the containing struct (just undef padding).
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001213
1214 if (const llvm::StructType *STy =
1215 dyn_cast_or_null<llvm::StructType>(PrefType)) {
1216 // If this is a struct, recurse into the field at the specified offset.
1217 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1218 if (IROffset < SL->getSizeInBytes()) {
1219 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1220 IROffset -= SL->getElementOffset(FieldIdx);
1221
1222 return Get8ByteTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1223 SourceTy, SourceOffset, TD,VMContext,Context);
1224 }
1225 }
Chris Lattner22a931e2010-06-29 06:01:59 +00001226
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001227 // Okay, we don't have any better idea of what to pass, so we pass this in an
1228 // integer register that isn't too big to fit the rest of the struct.
1229 uint64_t TySizeInBytes = Context.getTypeSizeInChars(SourceTy).getQuantity();
1230
1231 // It is always safe to classify this as an integer type up to i64 that
1232 // isn't larger than the structure.
1233 switch (unsigned(TySizeInBytes-SourceOffset)) {
1234 case 1: return llvm::Type::getInt8Ty(VMContext);
1235 case 2: return llvm::Type::getInt16Ty(VMContext);
1236 case 3:
1237 case 4: return llvm::Type::getInt32Ty(VMContext);
1238 default: return llvm::Type::getInt64Ty(VMContext);
1239 }
Chris Lattner22a931e2010-06-29 06:01:59 +00001240}
1241
Chris Lattner31faff52010-07-28 23:06:14 +00001242ABIArgInfo X86_64ABIInfo::
Chris Lattner458b2aa2010-07-29 02:16:43 +00001243classifyReturnType(QualType RetTy) const {
Chris Lattner31faff52010-07-28 23:06:14 +00001244 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1245 // classification algorithm.
1246 X86_64ABIInfo::Class Lo, Hi;
1247 classify(RetTy, 0, Lo, Hi);
1248
1249 // Check some invariants.
1250 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1251 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1252 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1253
1254 const llvm::Type *ResType = 0;
1255 switch (Lo) {
1256 case NoClass:
1257 return ABIArgInfo::getIgnore();
1258
1259 case SSEUp:
1260 case X87Up:
1261 assert(0 && "Invalid classification for lo word.");
1262
1263 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1264 // hidden argument.
1265 case Memory:
1266 return getIndirectReturnResult(RetTy);
1267
1268 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1269 // available register of the sequence %rax, %rdx is used.
1270 case Integer:
Chris Lattner2b037972010-07-29 02:01:43 +00001271 ResType = Get8ByteTypeAtOffset(0, 0, RetTy, 0, getTargetData(),
Chris Lattner458b2aa2010-07-29 02:16:43 +00001272 getVMContext(), getContext());
Chris Lattner31faff52010-07-28 23:06:14 +00001273 break;
1274
1275 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1276 // available SSE register of the sequence %xmm0, %xmm1 is used.
1277 case SSE:
Chris Lattner2b037972010-07-29 02:01:43 +00001278 ResType = llvm::Type::getDoubleTy(getVMContext());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001279 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001280
1281 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1282 // returned on the X87 stack in %st0 as 80-bit x87 number.
1283 case X87:
Chris Lattner2b037972010-07-29 02:01:43 +00001284 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001285 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001286
1287 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1288 // part of the value is returned in %st0 and the imaginary part in
1289 // %st1.
1290 case ComplexX87:
1291 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner458b2aa2010-07-29 02:16:43 +00001292 ResType = llvm::StructType::get(getVMContext(),
Chris Lattner2b037972010-07-29 02:01:43 +00001293 llvm::Type::getX86_FP80Ty(getVMContext()),
1294 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner31faff52010-07-28 23:06:14 +00001295 NULL);
1296 break;
1297 }
1298
1299 switch (Hi) {
1300 // Memory was handled previously and X87 should
1301 // never occur as a hi class.
1302 case Memory:
1303 case X87:
1304 assert(0 && "Invalid classification for hi word.");
1305
1306 case ComplexX87: // Previously handled.
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001307 case NoClass:
1308 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001309
1310 case Integer: {
1311 const llvm::Type *HiType =
Chris Lattner458b2aa2010-07-29 02:16:43 +00001312 Get8ByteTypeAtOffset(0, 8, RetTy, 8, getTargetData(), getVMContext(),
Chris Lattner2b037972010-07-29 02:01:43 +00001313 getContext());
Chris Lattner458b2aa2010-07-29 02:16:43 +00001314 ResType = llvm::StructType::get(getVMContext(), ResType, HiType, NULL);
Chris Lattner31faff52010-07-28 23:06:14 +00001315 break;
1316 }
1317 case SSE:
Chris Lattner458b2aa2010-07-29 02:16:43 +00001318 ResType = llvm::StructType::get(getVMContext(), ResType,
1319 llvm::Type::getDoubleTy(getVMContext()),
1320 NULL);
Chris Lattner31faff52010-07-28 23:06:14 +00001321 break;
1322
1323 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1324 // is passed in the upper half of the last used SSE register.
1325 //
1326 // SSEUP should always be preceeded by SSE, just widen.
1327 case SSEUp:
1328 assert(Lo == SSE && "Unexpected SSEUp classification.");
Chris Lattner458b2aa2010-07-29 02:16:43 +00001329 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
Chris Lattner31faff52010-07-28 23:06:14 +00001330 break;
1331
1332 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1333 // returned together with the previous X87 value in %st0.
1334 case X87Up:
1335 // If X87Up is preceeded by X87, we don't need to do
1336 // anything. However, in some cases with unions it may not be
1337 // preceeded by X87. In such situations we follow gcc and pass the
1338 // extra bits in an SSE reg.
1339 if (Lo != X87)
Chris Lattner458b2aa2010-07-29 02:16:43 +00001340 ResType = llvm::StructType::get(getVMContext(), ResType,
1341 llvm::Type::getDoubleTy(getVMContext()),
1342 NULL);
Chris Lattner31faff52010-07-28 23:06:14 +00001343 break;
1344 }
1345
1346 return getCoerceResult(RetTy, ResType);
1347}
1348
Chris Lattner458b2aa2010-07-29 02:16:43 +00001349ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Chris Lattner399d22a2010-06-29 01:14:09 +00001350 unsigned &neededSSE,
1351 const llvm::Type *PrefType)const{
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001352 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001353 classify(Ty, 0, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001354
1355 // Check some invariants.
1356 // FIXME: Enforce these by construction.
1357 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1358 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1359 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1360
1361 neededInt = 0;
1362 neededSSE = 0;
1363 const llvm::Type *ResType = 0;
1364 switch (Lo) {
1365 case NoClass:
1366 return ABIArgInfo::getIgnore();
1367
1368 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1369 // on the stack.
1370 case Memory:
1371
1372 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1373 // COMPLEX_X87, it is passed in memory.
1374 case X87:
1375 case ComplexX87:
Chris Lattner22a931e2010-06-29 06:01:59 +00001376 return getIndirectResult(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001377
1378 case SSEUp:
1379 case X87Up:
1380 assert(0 && "Invalid classification for lo word.");
1381
1382 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1383 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1384 // and %r9 is used.
1385 case Integer:
Chris Lattner22a931e2010-06-29 06:01:59 +00001386 ++neededInt;
1387
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001388 // Pick an 8-byte type based on the preferred type.
Chris Lattner2b037972010-07-29 02:01:43 +00001389 ResType = Get8ByteTypeAtOffset(PrefType, 0, Ty, 0, getTargetData(),
Chris Lattner458b2aa2010-07-29 02:16:43 +00001390 getVMContext(), getContext());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001391 break;
1392
1393 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1394 // available SSE register is used, the registers are taken in the
1395 // order from %xmm0 to %xmm7.
1396 case SSE:
1397 ++neededSSE;
Chris Lattner458b2aa2010-07-29 02:16:43 +00001398 ResType = llvm::Type::getDoubleTy(getVMContext());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001399 break;
1400 }
1401
1402 switch (Hi) {
1403 // Memory was handled previously, ComplexX87 and X87 should
1404 // never occur as hi classes, and X87Up must be preceed by X87,
1405 // which is passed in memory.
1406 case Memory:
1407 case X87:
1408 case ComplexX87:
1409 assert(0 && "Invalid classification for hi word.");
1410 break;
1411
1412 case NoClass: break;
Chris Lattner22a931e2010-06-29 06:01:59 +00001413
1414 case Integer: {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001415 ++neededInt;
Chris Lattner22a931e2010-06-29 06:01:59 +00001416
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001417 // Pick an 8-byte type based on the preferred type.
1418 const llvm::Type *HiType =
Chris Lattner2b037972010-07-29 02:01:43 +00001419 Get8ByteTypeAtOffset(PrefType, 8, Ty, 8, getTargetData(),
Chris Lattner458b2aa2010-07-29 02:16:43 +00001420 getVMContext(), getContext());
1421 ResType = llvm::StructType::get(getVMContext(), ResType, HiType, NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001422 break;
Chris Lattner22a931e2010-06-29 06:01:59 +00001423 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001424
1425 // X87Up generally doesn't occur here (long double is passed in
1426 // memory), except in situations involving unions.
1427 case X87Up:
1428 case SSE:
Chris Lattner458b2aa2010-07-29 02:16:43 +00001429 ResType = llvm::StructType::get(getVMContext(), ResType,
1430 llvm::Type::getDoubleTy(getVMContext()),
1431 NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001432 ++neededSSE;
1433 break;
1434
1435 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1436 // eightbyte is passed in the upper half of the last used SSE
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00001437 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001438 case SSEUp:
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00001439 assert(Lo == SSE && "Unexpected SSEUp classification");
Chris Lattner458b2aa2010-07-29 02:16:43 +00001440 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00001441
1442 // If the preferred type is a 16-byte vector, prefer to pass it.
1443 if (const llvm::VectorType *VT =
1444 dyn_cast_or_null<llvm::VectorType>(PrefType)) {
1445 const llvm::Type *EltTy = VT->getElementType();
1446 if (VT->getBitWidth() == 128 &&
1447 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1448 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1449 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1450 EltTy->isIntegerTy(128)))
1451 ResType = PrefType;
1452 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001453 break;
1454 }
1455
Chris Lattner22a931e2010-06-29 06:01:59 +00001456 return getCoerceResult(Ty, ResType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001457}
1458
Chris Lattner458b2aa2010-07-29 02:16:43 +00001459void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI,
Chris Lattner1d7c9f72010-06-29 01:08:48 +00001460 const llvm::Type *const *PrefTypes,
1461 unsigned NumPrefTypes) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00001462 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001463
1464 // Keep track of the number of assigned registers.
1465 unsigned freeIntRegs = 6, freeSSERegs = 8;
1466
1467 // If the return value is indirect, then the hidden argument is consuming one
1468 // integer register.
1469 if (FI.getReturnInfo().isIndirect())
1470 --freeIntRegs;
1471
1472 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1473 // get assigned (in left-to-right order) for passing as follows...
1474 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1475 it != ie; ++it) {
Chris Lattner399d22a2010-06-29 01:14:09 +00001476 // If the client specified a preferred IR type to use, pass it down to
1477 // classifyArgumentType.
1478 const llvm::Type *PrefType = 0;
1479 if (NumPrefTypes) {
1480 PrefType = *PrefTypes++;
1481 --NumPrefTypes;
1482 }
1483
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001484 unsigned neededInt, neededSSE;
Chris Lattner458b2aa2010-07-29 02:16:43 +00001485 it->info = classifyArgumentType(it->type, neededInt, neededSSE, PrefType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001486
1487 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1488 // eightbyte of an argument, the whole argument is passed on the
1489 // stack. If registers have already been assigned for some
1490 // eightbytes of such an argument, the assignments get reverted.
1491 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1492 freeIntRegs -= neededInt;
1493 freeSSERegs -= neededSSE;
1494 } else {
Chris Lattner22a931e2010-06-29 06:01:59 +00001495 it->info = getIndirectResult(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001496 }
1497 }
1498}
1499
1500static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1501 QualType Ty,
1502 CodeGenFunction &CGF) {
1503 llvm::Value *overflow_arg_area_p =
1504 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1505 llvm::Value *overflow_arg_area =
1506 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1507
1508 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1509 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1510 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1511 if (Align > 8) {
1512 // Note that we follow the ABI & gcc here, even though the type
1513 // could in theory have an alignment greater than 16. This case
1514 // shouldn't ever matter in practice.
1515
1516 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson41a75022009-08-13 21:57:51 +00001517 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001518 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001519 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1520 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner5e016ae2010-06-27 07:15:29 +00001521 CGF.Int64Ty);
1522 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001523 overflow_arg_area =
1524 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1525 overflow_arg_area->getType(),
1526 "overflow_arg_area.align");
1527 }
1528
1529 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1530 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1531 llvm::Value *Res =
1532 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001533 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001534
1535 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1536 // l->overflow_arg_area + sizeof(type).
1537 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1538 // an 8 byte boundary.
1539
1540 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00001541 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001542 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001543 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1544 "overflow_arg_area.next");
1545 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1546
1547 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1548 return Res;
1549}
1550
1551llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1552 CodeGenFunction &CGF) const {
Owen Anderson170229f2009-07-14 23:10:40 +00001553 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump11289f42009-09-09 15:08:12 +00001554
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001555 // Assume that va_list type is correct; should be pointer to LLVM type:
1556 // struct {
1557 // i32 gp_offset;
1558 // i32 fp_offset;
1559 // i8* overflow_arg_area;
1560 // i8* reg_save_area;
1561 // };
1562 unsigned neededInt, neededSSE;
Chris Lattner9723d6c2010-03-11 18:19:55 +00001563
1564 Ty = CGF.getContext().getCanonicalType(Ty);
Chris Lattner458b2aa2010-07-29 02:16:43 +00001565 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE, 0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001566
1567 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1568 // in the registers. If not go to step 7.
1569 if (!neededInt && !neededSSE)
1570 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1571
1572 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1573 // general purpose registers needed to pass type and num_fp to hold
1574 // the number of floating point registers needed.
1575
1576 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1577 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1578 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1579 //
1580 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1581 // register save space).
1582
1583 llvm::Value *InRegs = 0;
1584 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1585 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1586 if (neededInt) {
1587 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1588 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattnerd776fb12010-06-28 21:43:59 +00001589 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
1590 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001591 }
1592
1593 if (neededSSE) {
1594 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1595 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1596 llvm::Value *FitsInFP =
Chris Lattnerd776fb12010-06-28 21:43:59 +00001597 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
1598 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001599 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1600 }
1601
1602 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1603 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1604 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1605 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1606
1607 // Emit code to load the value if it was passed in registers.
1608
1609 CGF.EmitBlock(InRegBlock);
1610
1611 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1612 // an offset of l->gp_offset and/or l->fp_offset. This may require
1613 // copying to a temporary location in case the parameter is passed
1614 // in different register classes or requires an alignment greater
1615 // than 8 for general purpose registers and 16 for XMM registers.
1616 //
1617 // FIXME: This really results in shameful code when we end up needing to
1618 // collect arguments from different places; often what should result in a
1619 // simple assembling of a structure from scattered addresses has many more
1620 // loads than necessary. Can we clean this up?
1621 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1622 llvm::Value *RegAddr =
1623 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1624 "reg_save_area");
1625 if (neededInt && neededSSE) {
1626 // FIXME: Cleanup.
1627 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1628 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1629 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1630 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1631 const llvm::Type *TyLo = ST->getElementType(0);
1632 const llvm::Type *TyHi = ST->getElementType(1);
Duncan Sands998f9d92010-02-15 16:14:01 +00001633 assert((TyLo->isFloatingPointTy() ^ TyHi->isFloatingPointTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001634 "Unexpected ABI info for mixed regs");
Owen Anderson9793f0e2009-07-29 22:16:19 +00001635 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1636 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001637 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1638 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sands998f9d92010-02-15 16:14:01 +00001639 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1640 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001641 llvm::Value *V =
1642 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1643 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1644 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1645 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1646
Owen Anderson170229f2009-07-14 23:10:40 +00001647 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001648 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001649 } else if (neededInt) {
1650 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1651 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001652 llvm::PointerType::getUnqual(LTy));
Chris Lattner0cf24192010-06-28 20:05:43 +00001653 } else if (neededSSE == 1) {
1654 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1655 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1656 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001657 } else {
Chris Lattner0cf24192010-06-28 20:05:43 +00001658 assert(neededSSE == 2 && "Invalid number of needed registers!");
1659 // SSE registers are spaced 16 bytes apart in the register save
1660 // area, we need to collect the two eightbytes together.
1661 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattnerd776fb12010-06-28 21:43:59 +00001662 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner0cf24192010-06-28 20:05:43 +00001663 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
1664 const llvm::Type *DblPtrTy =
1665 llvm::PointerType::getUnqual(DoubleTy);
1666 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1667 DoubleTy, NULL);
1668 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1669 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1670 DblPtrTy));
1671 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1672 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1673 DblPtrTy));
1674 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1675 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1676 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001677 }
1678
1679 // AMD64-ABI 3.5.7p5: Step 5. Set:
1680 // l->gp_offset = l->gp_offset + num_gp * 8
1681 // l->fp_offset = l->fp_offset + num_fp * 16.
1682 if (neededInt) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00001683 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001684 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1685 gp_offset_p);
1686 }
1687 if (neededSSE) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00001688 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001689 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1690 fp_offset_p);
1691 }
1692 CGF.EmitBranch(ContBlock);
1693
1694 // Emit code to load the value if it was passed in memory.
1695
1696 CGF.EmitBlock(InMemBlock);
1697 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1698
1699 // Return the appropriate result.
1700
1701 CGF.EmitBlock(ContBlock);
1702 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1703 "vaarg.addr");
1704 ResAddr->reserveOperandSpace(2);
1705 ResAddr->addIncoming(RegAddr, InRegBlock);
1706 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001707 return ResAddr;
1708}
1709
Chris Lattner0cf24192010-06-28 20:05:43 +00001710
1711
1712//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001713// PIC16 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00001714//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001715
1716namespace {
1717
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001718class PIC16ABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +00001719public:
1720 PIC16ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
1721
Chris Lattner458b2aa2010-07-29 02:16:43 +00001722 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001723
Chris Lattner458b2aa2010-07-29 02:16:43 +00001724 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001725
Chris Lattner458b2aa2010-07-29 02:16:43 +00001726 virtual void computeInfo(CGFunctionInfo &FI,
Chris Lattner1d7c9f72010-06-29 01:08:48 +00001727 const llvm::Type *const *PrefTypes,
1728 unsigned NumPrefTypes) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00001729 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001730 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1731 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +00001732 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001733 }
1734
1735 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1736 CodeGenFunction &CGF) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001737};
1738
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001739class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
1740public:
Chris Lattner2b037972010-07-29 02:01:43 +00001741 PIC16TargetCodeGenInfo(CodeGenTypes &CGT)
1742 : TargetCodeGenInfo(new PIC16ABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001743};
1744
Daniel Dunbard59655c2009-09-12 00:59:49 +00001745}
1746
Chris Lattner458b2aa2010-07-29 02:16:43 +00001747ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001748 if (RetTy->isVoidType()) {
1749 return ABIArgInfo::getIgnore();
1750 } else {
1751 return ABIArgInfo::getDirect();
1752 }
1753}
1754
Chris Lattner458b2aa2010-07-29 02:16:43 +00001755ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001756 return ABIArgInfo::getDirect();
1757}
1758
1759llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00001760 CodeGenFunction &CGF) const {
Chris Lattnerc0e8a592010-04-06 17:29:22 +00001761 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00001762 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1763
1764 CGBuilderTy &Builder = CGF.Builder;
1765 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1766 "ap");
1767 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1768 llvm::Type *PTy =
1769 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1770 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1771
1772 uint64_t Offset = CGF.getContext().getTypeSize(Ty) / 8;
1773
1774 llvm::Value *NextAddr =
1775 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1776 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
1777 "ap.next");
1778 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1779
1780 return AddrTyped;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001781}
1782
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00001783
John McCallea8d8bb2010-03-11 00:10:12 +00001784// PowerPC-32
1785
1786namespace {
1787class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
1788public:
Chris Lattner2b037972010-07-29 02:01:43 +00001789 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
1790
John McCallea8d8bb2010-03-11 00:10:12 +00001791 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1792 // This is recovered from gcc output.
1793 return 1; // r1 is the dedicated stack pointer
1794 }
1795
1796 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1797 llvm::Value *Address) const;
1798};
1799
1800}
1801
1802bool
1803PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1804 llvm::Value *Address) const {
1805 // This is calculated from the LLVM and GCC tables and verified
1806 // against gcc output. AFAIK all ABIs use the same encoding.
1807
1808 CodeGen::CGBuilderTy &Builder = CGF.Builder;
1809 llvm::LLVMContext &Context = CGF.getLLVMContext();
1810
1811 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
1812 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
1813 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
1814 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
1815
1816 // 0-31: r0-31, the 4-byte general-purpose registers
John McCall943fae92010-05-27 06:19:26 +00001817 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallea8d8bb2010-03-11 00:10:12 +00001818
1819 // 32-63: fp0-31, the 8-byte floating-point registers
John McCall943fae92010-05-27 06:19:26 +00001820 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallea8d8bb2010-03-11 00:10:12 +00001821
1822 // 64-76 are various 4-byte special-purpose registers:
1823 // 64: mq
1824 // 65: lr
1825 // 66: ctr
1826 // 67: ap
1827 // 68-75 cr0-7
1828 // 76: xer
John McCall943fae92010-05-27 06:19:26 +00001829 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallea8d8bb2010-03-11 00:10:12 +00001830
1831 // 77-108: v0-31, the 16-byte vector registers
John McCall943fae92010-05-27 06:19:26 +00001832 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallea8d8bb2010-03-11 00:10:12 +00001833
1834 // 109: vrsave
1835 // 110: vscr
1836 // 111: spe_acc
1837 // 112: spefscr
1838 // 113: sfp
John McCall943fae92010-05-27 06:19:26 +00001839 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallea8d8bb2010-03-11 00:10:12 +00001840
1841 return false;
1842}
1843
1844
Chris Lattner0cf24192010-06-28 20:05:43 +00001845//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001846// ARM ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00001847//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001848
1849namespace {
1850
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001851class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00001852public:
1853 enum ABIKind {
1854 APCS = 0,
1855 AAPCS = 1,
1856 AAPCS_VFP
1857 };
1858
1859private:
1860 ABIKind Kind;
1861
1862public:
Chris Lattner2b037972010-07-29 02:01:43 +00001863 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar020daa92009-09-12 01:00:39 +00001864
1865private:
1866 ABIKind getABIKind() const { return Kind; }
1867
Chris Lattner458b2aa2010-07-29 02:16:43 +00001868 ABIArgInfo classifyReturnType(QualType RetTy) const;
1869 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001870
Chris Lattner458b2aa2010-07-29 02:16:43 +00001871 virtual void computeInfo(CGFunctionInfo &FI,
Chris Lattner1d7c9f72010-06-29 01:08:48 +00001872 const llvm::Type *const *PrefTypes,
1873 unsigned NumPrefTypes) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001874
1875 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1876 CodeGenFunction &CGF) const;
1877};
1878
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001879class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
1880public:
Chris Lattner2b037972010-07-29 02:01:43 +00001881 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
1882 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00001883
1884 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1885 return 13;
1886 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001887};
1888
Daniel Dunbard59655c2009-09-12 00:59:49 +00001889}
1890
Chris Lattner458b2aa2010-07-29 02:16:43 +00001891void ARMABIInfo::computeInfo(CGFunctionInfo &FI,
Chris Lattner1d7c9f72010-06-29 01:08:48 +00001892 const llvm::Type *const *PrefTypes,
1893 unsigned NumPrefTypes) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00001894 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001895 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattner458b2aa2010-07-29 02:16:43 +00001896 it != ie; ++it)
1897 it->info = classifyArgumentType(it->type);
Daniel Dunbar020daa92009-09-12 01:00:39 +00001898
Chris Lattner458b2aa2010-07-29 02:16:43 +00001899 const llvm::Triple &Triple(getContext().Target.getTriple());
Rafael Espindolaa92c4422010-06-16 16:13:39 +00001900 llvm::CallingConv::ID DefaultCC;
Rafael Espindola23a8a062010-06-16 19:01:17 +00001901 if (Triple.getEnvironmentName() == "gnueabi" ||
1902 Triple.getEnvironmentName() == "eabi")
Rafael Espindolaa92c4422010-06-16 16:13:39 +00001903 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola23a8a062010-06-16 19:01:17 +00001904 else
1905 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindolaa92c4422010-06-16 16:13:39 +00001906
Daniel Dunbar020daa92009-09-12 01:00:39 +00001907 switch (getABIKind()) {
1908 case APCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00001909 if (DefaultCC != llvm::CallingConv::ARM_APCS)
1910 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00001911 break;
1912
1913 case AAPCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00001914 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
1915 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00001916 break;
1917
1918 case AAPCS_VFP:
1919 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
1920 break;
1921 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001922}
1923
Chris Lattner458b2aa2010-07-29 02:16:43 +00001924ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
Douglas Gregora71cc152010-02-02 20:10:50 +00001925 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1926 // Treat an enum type as its underlying type.
1927 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1928 Ty = EnumTy->getDecl()->getIntegerType();
1929
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001930 return (Ty->isPromotableIntegerType() ?
1931 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001932 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001933
Daniel Dunbar09d33622009-09-14 21:54:03 +00001934 // Ignore empty records.
Chris Lattner458b2aa2010-07-29 02:16:43 +00001935 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar09d33622009-09-14 21:54:03 +00001936 return ABIArgInfo::getIgnore();
1937
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00001938 // Structures with either a non-trivial destructor or a non-trivial
1939 // copy constructor are always indirect.
1940 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1941 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
1942
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001943 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1944 // backend doesn't support byval.
1945 // FIXME: This doesn't handle alignment > 64 bits.
1946 const llvm::Type* ElemTy;
1947 unsigned SizeRegs;
Chris Lattner458b2aa2010-07-29 02:16:43 +00001948 if (getContext().getTypeAlign(Ty) > 32) {
1949 ElemTy = llvm::Type::getInt64Ty(getVMContext());
1950 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001951 } else {
Chris Lattner458b2aa2010-07-29 02:16:43 +00001952 ElemTy = llvm::Type::getInt32Ty(getVMContext());
1953 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001954 }
1955 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson9793f0e2009-07-29 22:16:19 +00001956 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Chris Lattner458b2aa2010-07-29 02:16:43 +00001957 const llvm::Type* STy = llvm::StructType::get(getVMContext(), LLVMFields,
1958 true);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001959 return ABIArgInfo::getCoerce(STy);
1960}
1961
Chris Lattner458b2aa2010-07-29 02:16:43 +00001962static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001963 llvm::LLVMContext &VMContext) {
1964 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
1965 // is called integer-like if its size is less than or equal to one word, and
1966 // the offset of each of its addressable sub-fields is zero.
1967
1968 uint64_t Size = Context.getTypeSize(Ty);
1969
1970 // Check that the type fits in a word.
1971 if (Size > 32)
1972 return false;
1973
1974 // FIXME: Handle vector types!
1975 if (Ty->isVectorType())
1976 return false;
1977
Daniel Dunbard53bac72009-09-14 02:20:34 +00001978 // Float types are never treated as "integer like".
1979 if (Ty->isRealFloatingType())
1980 return false;
1981
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001982 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00001983 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001984 return true;
1985
Daniel Dunbar96ebba52010-02-01 23:31:26 +00001986 // Small complex integer types are "integer like".
1987 if (const ComplexType *CT = Ty->getAs<ComplexType>())
1988 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001989
1990 // Single element and zero sized arrays should be allowed, by the definition
1991 // above, but they are not.
1992
1993 // Otherwise, it must be a record type.
1994 const RecordType *RT = Ty->getAs<RecordType>();
1995 if (!RT) return false;
1996
1997 // Ignore records with flexible arrays.
1998 const RecordDecl *RD = RT->getDecl();
1999 if (RD->hasFlexibleArrayMember())
2000 return false;
2001
2002 // Check that all sub-fields are at offset 0, and are themselves "integer
2003 // like".
2004 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2005
2006 bool HadField = false;
2007 unsigned idx = 0;
2008 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2009 i != e; ++i, ++idx) {
2010 const FieldDecl *FD = *i;
2011
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002012 // Bit-fields are not addressable, we only need to verify they are "integer
2013 // like". We still have to disallow a subsequent non-bitfield, for example:
2014 // struct { int : 0; int x }
2015 // is non-integer like according to gcc.
2016 if (FD->isBitField()) {
2017 if (!RD->isUnion())
2018 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002019
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002020 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2021 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002022
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002023 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002024 }
2025
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002026 // Check if this field is at offset 0.
2027 if (Layout.getFieldOffset(idx) != 0)
2028 return false;
2029
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002030 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2031 return false;
2032
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002033 // Only allow at most one field in a structure. This doesn't match the
2034 // wording above, but follows gcc in situations with a field following an
2035 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002036 if (!RD->isUnion()) {
2037 if (HadField)
2038 return false;
2039
2040 HadField = true;
2041 }
2042 }
2043
2044 return true;
2045}
2046
Chris Lattner458b2aa2010-07-29 02:16:43 +00002047ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002048 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002049 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002050
Douglas Gregora71cc152010-02-02 20:10:50 +00002051 if (!CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2052 // Treat an enum type as its underlying type.
2053 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2054 RetTy = EnumTy->getDecl()->getIntegerType();
2055
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002056 return (RetTy->isPromotableIntegerType() ?
2057 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002058 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002059
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002060 // Structures with either a non-trivial destructor or a non-trivial
2061 // copy constructor are always indirect.
2062 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2063 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2064
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002065 // Are we following APCS?
2066 if (getABIKind() == APCS) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002067 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002068 return ABIArgInfo::getIgnore();
2069
Daniel Dunbareedf1512010-02-01 23:31:19 +00002070 // Complex types are all returned as packed integers.
2071 //
2072 // FIXME: Consider using 2 x vector types if the back end handles them
2073 // correctly.
2074 if (RetTy->isAnyComplexType())
Chris Lattner458b2aa2010-07-29 02:16:43 +00002075 return ABIArgInfo::getCoerce(llvm::IntegerType::get(getVMContext(),
2076 getContext().getTypeSize(RetTy)));
Daniel Dunbareedf1512010-02-01 23:31:19 +00002077
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002078 // Integer like structures are returned in r0.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002079 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002080 // Return in the smallest viable integer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002081 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002082 if (Size <= 8)
Chris Lattner458b2aa2010-07-29 02:16:43 +00002083 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002084 if (Size <= 16)
Chris Lattner458b2aa2010-07-29 02:16:43 +00002085 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(getVMContext()));
2086 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002087 }
2088
2089 // Otherwise return in memory.
2090 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002091 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002092
2093 // Otherwise this is an AAPCS variant.
2094
Chris Lattner458b2aa2010-07-29 02:16:43 +00002095 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002096 return ABIArgInfo::getIgnore();
2097
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002098 // Aggregates <= 4 bytes are returned in r0; other aggregates
2099 // are returned indirectly.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002100 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002101 if (Size <= 32) {
2102 // Return in the smallest viable integer type.
2103 if (Size <= 8)
Chris Lattner458b2aa2010-07-29 02:16:43 +00002104 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002105 if (Size <= 16)
Chris Lattner458b2aa2010-07-29 02:16:43 +00002106 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(getVMContext()));
2107 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002108 }
2109
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002110 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002111}
2112
2113llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00002114 CodeGenFunction &CGF) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002115 // FIXME: Need to handle alignment
Benjamin Kramerabd5b902009-10-13 10:07:13 +00002116 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002117 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002118
2119 CGBuilderTy &Builder = CGF.Builder;
2120 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2121 "ap");
2122 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2123 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00002124 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002125 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2126
2127 uint64_t Offset =
2128 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2129 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +00002130 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002131 "ap.next");
2132 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2133
2134 return AddrTyped;
2135}
2136
Chris Lattner458b2aa2010-07-29 02:16:43 +00002137ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
2138 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002139 return ABIArgInfo::getIgnore();
Douglas Gregora71cc152010-02-02 20:10:50 +00002140
Chris Lattner458b2aa2010-07-29 02:16:43 +00002141 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
2142 return ABIArgInfo::getIndirect(0);
2143
2144 // Treat an enum type as its underlying type.
2145 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2146 RetTy = EnumTy->getDecl()->getIntegerType();
2147
2148 return (RetTy->isPromotableIntegerType() ?
2149 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002150}
2151
Chris Lattner0cf24192010-06-28 20:05:43 +00002152//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002153// SystemZ ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002154//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002155
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002156namespace {
Daniel Dunbard59655c2009-09-12 00:59:49 +00002157
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002158class SystemZABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +00002159public:
2160 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2161
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002162 bool isPromotableIntegerType(QualType Ty) const;
2163
Chris Lattner458b2aa2010-07-29 02:16:43 +00002164 ABIArgInfo classifyReturnType(QualType RetTy) const;
2165 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002166
Chris Lattner458b2aa2010-07-29 02:16:43 +00002167 virtual void computeInfo(CGFunctionInfo &FI,
Chris Lattner1d7c9f72010-06-29 01:08:48 +00002168 const llvm::Type *const *PrefTypes,
2169 unsigned NumPrefTypes) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002170 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002171 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2172 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +00002173 it->info = classifyArgumentType(it->type);
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002174 }
2175
2176 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2177 CodeGenFunction &CGF) const;
2178};
Daniel Dunbard59655c2009-09-12 00:59:49 +00002179
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002180class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2181public:
Chris Lattner2b037972010-07-29 02:01:43 +00002182 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
2183 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002184};
2185
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002186}
2187
2188bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2189 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall9dd450b2009-09-21 23:43:11 +00002190 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002191 switch (BT->getKind()) {
2192 case BuiltinType::Bool:
2193 case BuiltinType::Char_S:
2194 case BuiltinType::Char_U:
2195 case BuiltinType::SChar:
2196 case BuiltinType::UChar:
2197 case BuiltinType::Short:
2198 case BuiltinType::UShort:
2199 case BuiltinType::Int:
2200 case BuiltinType::UInt:
2201 return true;
2202 default:
2203 return false;
2204 }
2205 return false;
2206}
2207
2208llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2209 CodeGenFunction &CGF) const {
2210 // FIXME: Implement
2211 return 0;
2212}
2213
2214
Chris Lattner458b2aa2010-07-29 02:16:43 +00002215ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
2216 if (RetTy->isVoidType())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002217 return ABIArgInfo::getIgnore();
Chris Lattner458b2aa2010-07-29 02:16:43 +00002218 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002219 return ABIArgInfo::getIndirect(0);
Chris Lattner458b2aa2010-07-29 02:16:43 +00002220
2221 return (isPromotableIntegerType(RetTy) ?
2222 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002223}
2224
Chris Lattner458b2aa2010-07-29 02:16:43 +00002225ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
2226 if (CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002227 return ABIArgInfo::getIndirect(0);
Chris Lattner458b2aa2010-07-29 02:16:43 +00002228
2229 return (isPromotableIntegerType(Ty) ?
2230 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002231}
2232
Chris Lattner0cf24192010-06-28 20:05:43 +00002233//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002234// MSP430 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002235//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002236
2237namespace {
2238
2239class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2240public:
Chris Lattner2b037972010-07-29 02:01:43 +00002241 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
2242 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002243 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2244 CodeGen::CodeGenModule &M) const;
2245};
2246
2247}
2248
2249void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2250 llvm::GlobalValue *GV,
2251 CodeGen::CodeGenModule &M) const {
2252 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2253 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2254 // Handle 'interrupt' attribute:
2255 llvm::Function *F = cast<llvm::Function>(GV);
2256
2257 // Step 1: Set ISR calling convention.
2258 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2259
2260 // Step 2: Add attributes goodness.
2261 F->addFnAttr(llvm::Attribute::NoInline);
2262
2263 // Step 3: Emit ISR vector alias.
2264 unsigned Num = attr->getNumber() + 0xffe0;
2265 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2266 "vector_" +
2267 llvm::LowercaseString(llvm::utohexstr(Num)),
2268 GV, &M.getModule());
2269 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002270 }
2271}
2272
Chris Lattner0cf24192010-06-28 20:05:43 +00002273//===----------------------------------------------------------------------===//
John McCall943fae92010-05-27 06:19:26 +00002274// MIPS ABI Implementation. This works for both little-endian and
2275// big-endian variants.
Chris Lattner0cf24192010-06-28 20:05:43 +00002276//===----------------------------------------------------------------------===//
2277
John McCall943fae92010-05-27 06:19:26 +00002278namespace {
2279class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
2280public:
Chris Lattner2b037972010-07-29 02:01:43 +00002281 MIPSTargetCodeGenInfo(CodeGenTypes &CGT)
2282 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
John McCall943fae92010-05-27 06:19:26 +00002283
2284 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2285 return 29;
2286 }
2287
2288 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2289 llvm::Value *Address) const;
2290};
2291}
2292
2293bool
2294MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2295 llvm::Value *Address) const {
2296 // This information comes from gcc's implementation, which seems to
2297 // as canonical as it gets.
2298
2299 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2300 llvm::LLVMContext &Context = CGF.getLLVMContext();
2301
2302 // Everything on MIPS is 4 bytes. Double-precision FP registers
2303 // are aliased to pairs of single-precision FP registers.
2304 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2305 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2306
2307 // 0-31 are the general purpose registers, $0 - $31.
2308 // 32-63 are the floating-point registers, $f0 - $f31.
2309 // 64 and 65 are the multiply/divide registers, $hi and $lo.
2310 // 66 is the (notional, I think) register for signal-handler return.
2311 AssignToArrayRange(Builder, Address, Four8, 0, 65);
2312
2313 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
2314 // They are one bit wide and ignored here.
2315
2316 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
2317 // (coprocessor 1 is the FP unit)
2318 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
2319 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
2320 // 176-181 are the DSP accumulator registers.
2321 AssignToArrayRange(Builder, Address, Four8, 80, 181);
2322
2323 return false;
2324}
2325
2326
Chris Lattner2b037972010-07-29 02:01:43 +00002327const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002328 if (TheTargetCodeGenInfo)
2329 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002330
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002331 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2332 // free it.
Daniel Dunbare3532f82009-08-24 08:52:16 +00002333
Chris Lattner22a931e2010-06-29 06:01:59 +00002334 const llvm::Triple &Triple = getContext().Target.getTriple();
Daniel Dunbar40165182009-08-24 09:10:05 +00002335 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00002336 default:
Chris Lattner2b037972010-07-29 02:01:43 +00002337 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002338
John McCall943fae92010-05-27 06:19:26 +00002339 case llvm::Triple::mips:
2340 case llvm::Triple::mipsel:
Chris Lattner2b037972010-07-29 02:01:43 +00002341 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types));
John McCall943fae92010-05-27 06:19:26 +00002342
Daniel Dunbard59655c2009-09-12 00:59:49 +00002343 case llvm::Triple::arm:
2344 case llvm::Triple::thumb:
Daniel Dunbar020daa92009-09-12 01:00:39 +00002345 // FIXME: We want to know the float calling convention as well.
Daniel Dunbarb4091a92009-09-14 00:35:03 +00002346 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002347 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002348 new ARMTargetCodeGenInfo(Types, ARMABIInfo::APCS));
Daniel Dunbar020daa92009-09-12 01:00:39 +00002349
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002350 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002351 new ARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002352
2353 case llvm::Triple::pic16:
Chris Lattner2b037972010-07-29 02:01:43 +00002354 return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002355
John McCallea8d8bb2010-03-11 00:10:12 +00002356 case llvm::Triple::ppc:
Chris Lattner2b037972010-07-29 02:01:43 +00002357 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallea8d8bb2010-03-11 00:10:12 +00002358
Daniel Dunbard59655c2009-09-12 00:59:49 +00002359 case llvm::Triple::systemz:
Chris Lattner2b037972010-07-29 02:01:43 +00002360 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002361
2362 case llvm::Triple::msp430:
Chris Lattner2b037972010-07-29 02:01:43 +00002363 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002364
Daniel Dunbar40165182009-08-24 09:10:05 +00002365 case llvm::Triple::x86:
Daniel Dunbar40165182009-08-24 09:10:05 +00002366 switch (Triple.getOS()) {
Edward O'Callaghan462e4ab2009-10-20 17:22:50 +00002367 case llvm::Triple::Darwin:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002368 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002369 new X86_32TargetCodeGenInfo(Types, true, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002370 case llvm::Triple::Cygwin:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002371 case llvm::Triple::MinGW32:
2372 case llvm::Triple::MinGW64:
Edward O'Callaghan437ec1e2009-10-21 11:58:24 +00002373 case llvm::Triple::AuroraUX:
2374 case llvm::Triple::DragonFly:
David Chisnall2c5bef22009-09-03 01:48:05 +00002375 case llvm::Triple::FreeBSD:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002376 case llvm::Triple::OpenBSD:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002377 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002378 new X86_32TargetCodeGenInfo(Types, false, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002379
2380 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002381 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002382 new X86_32TargetCodeGenInfo(Types, false, false));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002383 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002384
Daniel Dunbare3532f82009-08-24 08:52:16 +00002385 case llvm::Triple::x86_64:
Chris Lattner2b037972010-07-29 02:01:43 +00002386 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002387 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002388}