blob: a36f9a5579656fb05c2604ec9142b7d95b76d98a [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
Chris Lattnerc11301c2010-07-29 02:20:19 +0000724 const llvm::Type *Get8ByteTypeAtOffset(const llvm::Type *PrefType,
725 unsigned IROffset,
726 QualType SourceTy,
727 unsigned SourceOffset) const;
728
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000729 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
730 /// to coerce to, chose the best way to pass Ty in the same place
731 /// that \arg CoerceTo would be passed, but while keeping the
732 /// emitted code as simple as possible.
733 ///
734 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
735 /// the ways we might want to pass things, instead of constructing an LLVM
736 /// type. This makes this code more explicit, and it makes it clearer that we
737 /// are also doing this for correctness in the case of passing scalar types.
738 ABIArgInfo getCoerceResult(QualType Ty,
Chris Lattner22a931e2010-06-29 06:01:59 +0000739 const llvm::Type *CoerceTo) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000740
741 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar53fac692010-04-21 19:49:55 +0000742 /// such that the argument will be returned in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000743 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar53fac692010-04-21 19:49:55 +0000744
745 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000746 /// such that the argument will be passed in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000747 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000748
Chris Lattner458b2aa2010-07-29 02:16:43 +0000749 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000750
751 ABIArgInfo classifyArgumentType(QualType Ty,
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000752 unsigned &neededInt,
Chris Lattner399d22a2010-06-29 01:14:09 +0000753 unsigned &neededSSE,
754 const llvm::Type *PrefType) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000755
756public:
Chris Lattner2b037972010-07-29 02:01:43 +0000757 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Chris Lattner22a931e2010-06-29 06:01:59 +0000758
Chris Lattner458b2aa2010-07-29 02:16:43 +0000759 virtual void computeInfo(CGFunctionInfo &FI,
Chris Lattner1d7c9f72010-06-29 01:08:48 +0000760 const llvm::Type *const *PrefTypes,
761 unsigned NumPrefTypes) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000762
763 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
764 CodeGenFunction &CGF) const;
765};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000766
767class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
768public:
Chris Lattner2b037972010-07-29 02:01:43 +0000769 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
770 : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {}
John McCallbeec5a02010-03-06 00:35:14 +0000771
772 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
773 return 7;
774 }
775
776 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
777 llvm::Value *Address) const {
778 CodeGen::CGBuilderTy &Builder = CGF.Builder;
779 llvm::LLVMContext &Context = CGF.getLLVMContext();
780
781 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
782 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
783
John McCall943fae92010-05-27 06:19:26 +0000784 // 0-15 are the 16 integer registers.
785 // 16 is %rip.
786 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCallbeec5a02010-03-06 00:35:14 +0000787
788 return false;
789 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000790};
791
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000792}
793
Chris Lattnerd776fb12010-06-28 21:43:59 +0000794X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000795 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
796 // classified recursively so that always two fields are
797 // considered. The resulting class is calculated according to
798 // the classes of the fields in the eightbyte:
799 //
800 // (a) If both classes are equal, this is the resulting class.
801 //
802 // (b) If one of the classes is NO_CLASS, the resulting class is
803 // the other class.
804 //
805 // (c) If one of the classes is MEMORY, the result is the MEMORY
806 // class.
807 //
808 // (d) If one of the classes is INTEGER, the result is the
809 // INTEGER.
810 //
811 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
812 // MEMORY is used as class.
813 //
814 // (f) Otherwise class SSE is used.
815
816 // Accum should never be memory (we should have returned) or
817 // ComplexX87 (because this cannot be passed in a structure).
818 assert((Accum != Memory && Accum != ComplexX87) &&
819 "Invalid accumulated classification during merge.");
820 if (Accum == Field || Field == NoClass)
821 return Accum;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000822 if (Field == Memory)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000823 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000824 if (Accum == NoClass)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000825 return Field;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000826 if (Accum == Integer || Field == Integer)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000827 return Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000828 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
829 Accum == X87 || Accum == X87Up)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000830 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000831 return SSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000832}
833
Chris Lattner5c740f12010-06-30 19:14:05 +0000834void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000835 Class &Lo, Class &Hi) const {
836 // FIXME: This code can be simplified by introducing a simple value class for
837 // Class pairs with appropriate constructor methods for the various
838 // situations.
839
840 // FIXME: Some of the split computations are wrong; unaligned vectors
841 // shouldn't be passed in registers for example, so there is no chance they
842 // can straddle an eightbyte. Verify & simplify.
843
844 Lo = Hi = NoClass;
845
846 Class &Current = OffsetBase < 64 ? Lo : Hi;
847 Current = Memory;
848
John McCall9dd450b2009-09-21 23:43:11 +0000849 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000850 BuiltinType::Kind k = BT->getKind();
851
852 if (k == BuiltinType::Void) {
853 Current = NoClass;
854 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
855 Lo = Integer;
856 Hi = Integer;
857 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
858 Current = Integer;
859 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
860 Current = SSE;
861 } else if (k == BuiltinType::LongDouble) {
862 Lo = X87;
863 Hi = X87Up;
864 }
865 // FIXME: _Decimal32 and _Decimal64 are SSE.
866 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattnerd776fb12010-06-28 21:43:59 +0000867 return;
868 }
869
870 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000871 // Classify the underlying integer type.
Chris Lattner22a931e2010-06-29 06:01:59 +0000872 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattnerd776fb12010-06-28 21:43:59 +0000873 return;
874 }
875
876 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000877 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000878 return;
879 }
880
881 if (Ty->isMemberPointerType()) {
Daniel Dunbar36d4d152010-05-15 00:00:37 +0000882 if (Ty->isMemberFunctionPointerType())
883 Lo = Hi = Integer;
884 else
885 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000886 return;
887 }
888
889 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +0000890 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000891 if (Size == 32) {
892 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
893 // float> as integer.
894 Current = Integer;
895
896 // If this type crosses an eightbyte boundary, it should be
897 // split.
898 uint64_t EB_Real = (OffsetBase) / 64;
899 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
900 if (EB_Real != EB_Imag)
901 Hi = Lo;
902 } else if (Size == 64) {
903 // gcc passes <1 x double> in memory. :(
904 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
905 return;
906
907 // gcc passes <1 x long long> as INTEGER.
908 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
909 Current = Integer;
910 else
911 Current = SSE;
912
913 // If this type crosses an eightbyte boundary, it should be
914 // split.
915 if (OffsetBase && OffsetBase != 64)
916 Hi = Lo;
917 } else if (Size == 128) {
918 Lo = SSE;
919 Hi = SSEUp;
920 }
Chris Lattnerd776fb12010-06-28 21:43:59 +0000921 return;
922 }
923
924 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +0000925 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000926
Chris Lattner2b037972010-07-29 02:01:43 +0000927 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregorb90df602010-06-16 00:17:44 +0000928 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000929 if (Size <= 64)
930 Current = Integer;
931 else if (Size <= 128)
932 Lo = Hi = Integer;
Chris Lattner2b037972010-07-29 02:01:43 +0000933 } else if (ET == getContext().FloatTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000934 Current = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +0000935 else if (ET == getContext().DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000936 Lo = Hi = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +0000937 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000938 Current = ComplexX87;
939
940 // If this complex type crosses an eightbyte boundary then it
941 // should be split.
942 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattner2b037972010-07-29 02:01:43 +0000943 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000944 if (Hi == NoClass && EB_Real != EB_Imag)
945 Hi = Lo;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000946
947 return;
948 }
949
Chris Lattner2b037972010-07-29 02:01:43 +0000950 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000951 // Arrays are treated like structures.
952
Chris Lattner2b037972010-07-29 02:01:43 +0000953 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000954
955 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
956 // than two eightbytes, ..., it has class MEMORY.
957 if (Size > 128)
958 return;
959
960 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
961 // fields, it has class MEMORY.
962 //
963 // Only need to check alignment of array base.
Chris Lattner2b037972010-07-29 02:01:43 +0000964 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000965 return;
966
967 // Otherwise implement simplified merge. We could be smarter about
968 // this, but it isn't worth it and would be harder to verify.
969 Current = NoClass;
Chris Lattner2b037972010-07-29 02:01:43 +0000970 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000971 uint64_t ArraySize = AT->getSize().getZExtValue();
972 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
973 Class FieldLo, FieldHi;
Chris Lattner22a931e2010-06-29 06:01:59 +0000974 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000975 Lo = merge(Lo, FieldLo);
976 Hi = merge(Hi, FieldHi);
977 if (Lo == Memory || Hi == Memory)
978 break;
979 }
980
981 // Do post merger cleanup (see below). Only case we worry about is Memory.
982 if (Hi == Memory)
983 Lo = Memory;
984 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattnerd776fb12010-06-28 21:43:59 +0000985 return;
986 }
987
988 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +0000989 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000990
991 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
992 // than two eightbytes, ..., it has class MEMORY.
993 if (Size > 128)
994 return;
995
Anders Carlsson20759ad2009-09-16 15:53:40 +0000996 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
997 // copy constructor or a non-trivial destructor, it is passed by invisible
998 // reference.
999 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1000 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001001
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001002 const RecordDecl *RD = RT->getDecl();
1003
1004 // Assume variable sized types are passed in memory.
1005 if (RD->hasFlexibleArrayMember())
1006 return;
1007
Chris Lattner2b037972010-07-29 02:01:43 +00001008 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001009
1010 // Reset Lo class, this will be recomputed.
1011 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001012
1013 // If this is a C++ record, classify the bases first.
1014 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1015 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1016 e = CXXRD->bases_end(); i != e; ++i) {
1017 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1018 "Unexpected base class!");
1019 const CXXRecordDecl *Base =
1020 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1021
1022 // Classify this field.
1023 //
1024 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1025 // single eightbyte, each is classified separately. Each eightbyte gets
1026 // initialized to class NO_CLASS.
1027 Class FieldLo, FieldHi;
1028 uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
Chris Lattner22a931e2010-06-29 06:01:59 +00001029 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001030 Lo = merge(Lo, FieldLo);
1031 Hi = merge(Hi, FieldHi);
1032 if (Lo == Memory || Hi == Memory)
1033 break;
1034 }
Daniel Dunbar3780f0b2009-12-22 01:19:25 +00001035
1036 // If this record has no fields but isn't empty, classify as INTEGER.
1037 if (RD->field_empty() && Size)
1038 Current = Integer;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001039 }
1040
1041 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001042 unsigned idx = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001043 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1044 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001045 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1046 bool BitField = i->isBitField();
1047
1048 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1049 // fields, it has class MEMORY.
1050 //
1051 // Note, skip this test for bit-fields, see below.
Chris Lattner2b037972010-07-29 02:01:43 +00001052 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001053 Lo = Memory;
1054 return;
1055 }
1056
1057 // Classify this field.
1058 //
1059 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1060 // exceeds a single eightbyte, each is classified
1061 // separately. Each eightbyte gets initialized to class
1062 // NO_CLASS.
1063 Class FieldLo, FieldHi;
1064
1065 // Bit-fields require special handling, they do not force the
1066 // structure to be passed in memory even if unaligned, and
1067 // therefore they can straddle an eightbyte.
1068 if (BitField) {
1069 // Ignore padding bit-fields.
1070 if (i->isUnnamedBitfield())
1071 continue;
1072
1073 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Chris Lattner2b037972010-07-29 02:01:43 +00001074 uint64_t Size =
1075 i->getBitWidth()->EvaluateAsInt(getContext()).getZExtValue();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001076
1077 uint64_t EB_Lo = Offset / 64;
1078 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1079 FieldLo = FieldHi = NoClass;
1080 if (EB_Lo) {
1081 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1082 FieldLo = NoClass;
1083 FieldHi = Integer;
1084 } else {
1085 FieldLo = Integer;
1086 FieldHi = EB_Hi ? Integer : NoClass;
1087 }
1088 } else
Chris Lattner22a931e2010-06-29 06:01:59 +00001089 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001090 Lo = merge(Lo, FieldLo);
1091 Hi = merge(Hi, FieldHi);
1092 if (Lo == Memory || Hi == Memory)
1093 break;
1094 }
1095
1096 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1097 //
1098 // (a) If one of the classes is MEMORY, the whole argument is
1099 // passed in memory.
1100 //
1101 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
1102
1103 // The first of these conditions is guaranteed by how we implement
1104 // the merge (just bail).
1105 //
1106 // The second condition occurs in the case of unions; for example
1107 // union { _Complex double; unsigned; }.
1108 if (Hi == Memory)
1109 Lo = Memory;
1110 if (Hi == SSEUp && Lo != SSE)
1111 Hi = SSE;
1112 }
1113}
1114
1115ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
Chris Lattner22a931e2010-06-29 06:01:59 +00001116 const llvm::Type *CoerceTo) const {
Chris Lattner4c1e4842010-07-28 22:15:08 +00001117 // If this is a pointer passed as a pointer, just pass it directly.
1118 if ((isa<llvm::PointerType>(CoerceTo) || CoerceTo->isIntegerTy(64)) &&
1119 Ty->hasPointerRepresentation())
1120 return ABIArgInfo::getExtend();
1121
1122 if (isa<llvm::IntegerType>(CoerceTo)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001123 // Integer and pointer types will end up in a general purpose
1124 // register.
Douglas Gregora71cc152010-02-02 20:10:50 +00001125
1126 // Treat an enum type as its underlying type.
1127 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1128 Ty = EnumTy->getDecl()->getIntegerType();
1129
Chris Lattner4c1e4842010-07-28 22:15:08 +00001130 if (Ty->isIntegralOrEnumerationType())
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001131 return (Ty->isPromotableIntegerType() ?
1132 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001133
Chris Lattnerfa20e952010-06-26 21:52:32 +00001134 } else if (CoerceTo->isDoubleTy()) {
John McCall8ee376f2010-02-24 07:14:12 +00001135 assert(Ty.isCanonical() && "should always have a canonical type here");
1136 assert(!Ty.hasQualifiers() && "should never have a qualified type here");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001137
1138 // Float and double end up in a single SSE reg.
Chris Lattner2b037972010-07-29 02:01:43 +00001139 if (Ty == getContext().FloatTy || Ty == getContext().DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001140 return ABIArgInfo::getDirect();
1141
Chris Lattnera7d81ab2010-06-28 19:56:59 +00001142 // If this is a 32-bit structure that is passed as a double, then it will be
1143 // passed in the low 32-bits of the XMM register, which is the same as how a
1144 // float is passed. Coerce to a float instead of a double.
Chris Lattner2b037972010-07-29 02:01:43 +00001145 if (getContext().getTypeSizeInChars(Ty).getQuantity() == 4)
Chris Lattnera7d81ab2010-06-28 19:56:59 +00001146 CoerceTo = llvm::Type::getFloatTy(CoerceTo->getContext());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001147 }
1148
1149 return ABIArgInfo::getCoerce(CoerceTo);
1150}
1151
Chris Lattner22a931e2010-06-29 06:01:59 +00001152ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001153 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1154 // place naturally.
1155 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1156 // Treat an enum type as its underlying type.
1157 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1158 Ty = EnumTy->getDecl()->getIntegerType();
1159
1160 return (Ty->isPromotableIntegerType() ?
1161 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1162 }
1163
1164 return ABIArgInfo::getIndirect(0);
1165}
1166
Chris Lattner22a931e2010-06-29 06:01:59 +00001167ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001168 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1169 // place naturally.
Douglas Gregora71cc152010-02-02 20:10:50 +00001170 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1171 // Treat an enum type as its underlying type.
1172 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1173 Ty = EnumTy->getDecl()->getIntegerType();
1174
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001175 return (Ty->isPromotableIntegerType() ?
1176 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001177 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001178
Daniel Dunbar53fac692010-04-21 19:49:55 +00001179 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1180 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson20759ad2009-09-16 15:53:40 +00001181
Daniel Dunbar53fac692010-04-21 19:49:55 +00001182 // Compute the byval alignment. We trust the back-end to honor the
1183 // minimum ABI alignment for byval, to make cleaner IR.
1184 const unsigned MinABIAlign = 8;
Chris Lattner2b037972010-07-29 02:01:43 +00001185 unsigned Align = getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar53fac692010-04-21 19:49:55 +00001186 if (Align > MinABIAlign)
1187 return ABIArgInfo::getIndirect(Align);
1188 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001189}
1190
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001191/// Get8ByteTypeAtOffset - The ABI specifies that a value should be passed in an
1192/// 8-byte GPR. This means that we either have a scalar or we are talking about
1193/// the high or low part of an up-to-16-byte struct. This routine picks the
1194/// best LLVM IR type to represent this, which may be i64 or may be anything
1195/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1196/// etc).
1197///
1198/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1199/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1200/// the 8-byte value references. PrefType may be null.
1201///
1202/// SourceTy is the source level type for the entire argument. SourceOffset is
1203/// an offset into this that we're processing (which is always either 0 or 8).
1204///
Chris Lattnerc11301c2010-07-29 02:20:19 +00001205const llvm::Type *X86_64ABIInfo::
1206Get8ByteTypeAtOffset(const llvm::Type *PrefType, unsigned IROffset,
1207 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattner22a931e2010-06-29 06:01:59 +00001208 // Pointers are always 8-bytes at offset 0.
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001209 if (IROffset == 0 && PrefType && isa<llvm::PointerType>(PrefType))
Chris Lattner22a931e2010-06-29 06:01:59 +00001210 return PrefType;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001211
Chris Lattner22a931e2010-06-29 06:01:59 +00001212 // TODO: 1/2/4/8 byte integers are also interesting, but we have to know that
1213 // the "hole" is not used in the containing struct (just undef padding).
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001214
1215 if (const llvm::StructType *STy =
1216 dyn_cast_or_null<llvm::StructType>(PrefType)) {
1217 // If this is a struct, recurse into the field at the specified offset.
Chris Lattnerc11301c2010-07-29 02:20:19 +00001218 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001219 if (IROffset < SL->getSizeInBytes()) {
1220 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1221 IROffset -= SL->getElementOffset(FieldIdx);
1222
1223 return Get8ByteTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
Chris Lattnerc11301c2010-07-29 02:20:19 +00001224 SourceTy, SourceOffset);
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001225 }
1226 }
Chris Lattner22a931e2010-06-29 06:01:59 +00001227
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001228 // Okay, we don't have any better idea of what to pass, so we pass this in an
1229 // integer register that isn't too big to fit the rest of the struct.
Chris Lattnerc11301c2010-07-29 02:20:19 +00001230 uint64_t TySizeInBytes =
1231 getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001232
1233 // It is always safe to classify this as an integer type up to i64 that
1234 // isn't larger than the structure.
1235 switch (unsigned(TySizeInBytes-SourceOffset)) {
Chris Lattnerc11301c2010-07-29 02:20:19 +00001236 case 1: return llvm::Type::getInt8Ty(getVMContext());
1237 case 2: return llvm::Type::getInt16Ty(getVMContext());
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001238 case 3:
Chris Lattnerc11301c2010-07-29 02:20:19 +00001239 case 4: return llvm::Type::getInt32Ty(getVMContext());
1240 default: return llvm::Type::getInt64Ty(getVMContext());
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001241 }
Chris Lattner22a931e2010-06-29 06:01:59 +00001242}
1243
Chris Lattner31faff52010-07-28 23:06:14 +00001244ABIArgInfo X86_64ABIInfo::
Chris Lattner458b2aa2010-07-29 02:16:43 +00001245classifyReturnType(QualType RetTy) const {
Chris Lattner31faff52010-07-28 23:06:14 +00001246 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1247 // classification algorithm.
1248 X86_64ABIInfo::Class Lo, Hi;
1249 classify(RetTy, 0, Lo, Hi);
1250
1251 // Check some invariants.
1252 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1253 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1254 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1255
1256 const llvm::Type *ResType = 0;
1257 switch (Lo) {
1258 case NoClass:
1259 return ABIArgInfo::getIgnore();
1260
1261 case SSEUp:
1262 case X87Up:
1263 assert(0 && "Invalid classification for lo word.");
1264
1265 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1266 // hidden argument.
1267 case Memory:
1268 return getIndirectReturnResult(RetTy);
1269
1270 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1271 // available register of the sequence %rax, %rdx is used.
1272 case Integer:
Chris Lattnerc11301c2010-07-29 02:20:19 +00001273 ResType = Get8ByteTypeAtOffset(0, 0, RetTy, 0);
Chris Lattner31faff52010-07-28 23:06:14 +00001274 break;
1275
1276 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1277 // available SSE register of the sequence %xmm0, %xmm1 is used.
1278 case SSE:
Chris Lattner2b037972010-07-29 02:01:43 +00001279 ResType = llvm::Type::getDoubleTy(getVMContext());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001280 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001281
1282 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1283 // returned on the X87 stack in %st0 as 80-bit x87 number.
1284 case X87:
Chris Lattner2b037972010-07-29 02:01:43 +00001285 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001286 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001287
1288 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1289 // part of the value is returned in %st0 and the imaginary part in
1290 // %st1.
1291 case ComplexX87:
1292 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner458b2aa2010-07-29 02:16:43 +00001293 ResType = llvm::StructType::get(getVMContext(),
Chris Lattner2b037972010-07-29 02:01:43 +00001294 llvm::Type::getX86_FP80Ty(getVMContext()),
1295 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner31faff52010-07-28 23:06:14 +00001296 NULL);
1297 break;
1298 }
1299
1300 switch (Hi) {
1301 // Memory was handled previously and X87 should
1302 // never occur as a hi class.
1303 case Memory:
1304 case X87:
1305 assert(0 && "Invalid classification for hi word.");
1306
1307 case ComplexX87: // Previously handled.
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001308 case NoClass:
1309 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001310
1311 case Integer: {
Chris Lattnerc11301c2010-07-29 02:20:19 +00001312 const llvm::Type *HiType = Get8ByteTypeAtOffset(0, 8, RetTy, 8);
Chris Lattner458b2aa2010-07-29 02:16:43 +00001313 ResType = llvm::StructType::get(getVMContext(), ResType, HiType, NULL);
Chris Lattner31faff52010-07-28 23:06:14 +00001314 break;
1315 }
1316 case SSE:
Chris Lattner458b2aa2010-07-29 02:16:43 +00001317 ResType = llvm::StructType::get(getVMContext(), ResType,
1318 llvm::Type::getDoubleTy(getVMContext()),
1319 NULL);
Chris Lattner31faff52010-07-28 23:06:14 +00001320 break;
1321
1322 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1323 // is passed in the upper half of the last used SSE register.
1324 //
1325 // SSEUP should always be preceeded by SSE, just widen.
1326 case SSEUp:
1327 assert(Lo == SSE && "Unexpected SSEUp classification.");
Chris Lattner458b2aa2010-07-29 02:16:43 +00001328 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
Chris Lattner31faff52010-07-28 23:06:14 +00001329 break;
1330
1331 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1332 // returned together with the previous X87 value in %st0.
1333 case X87Up:
1334 // If X87Up is preceeded by X87, we don't need to do
1335 // anything. However, in some cases with unions it may not be
1336 // preceeded by X87. In such situations we follow gcc and pass the
1337 // extra bits in an SSE reg.
1338 if (Lo != X87)
Chris Lattner458b2aa2010-07-29 02:16:43 +00001339 ResType = llvm::StructType::get(getVMContext(), ResType,
1340 llvm::Type::getDoubleTy(getVMContext()),
1341 NULL);
Chris Lattner31faff52010-07-28 23:06:14 +00001342 break;
1343 }
1344
1345 return getCoerceResult(RetTy, ResType);
1346}
1347
Chris Lattner458b2aa2010-07-29 02:16:43 +00001348ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Chris Lattner399d22a2010-06-29 01:14:09 +00001349 unsigned &neededSSE,
1350 const llvm::Type *PrefType)const{
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001351 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001352 classify(Ty, 0, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001353
1354 // Check some invariants.
1355 // FIXME: Enforce these by construction.
1356 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1357 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1358 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1359
1360 neededInt = 0;
1361 neededSSE = 0;
1362 const llvm::Type *ResType = 0;
1363 switch (Lo) {
1364 case NoClass:
1365 return ABIArgInfo::getIgnore();
1366
1367 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1368 // on the stack.
1369 case Memory:
1370
1371 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1372 // COMPLEX_X87, it is passed in memory.
1373 case X87:
1374 case ComplexX87:
Chris Lattner22a931e2010-06-29 06:01:59 +00001375 return getIndirectResult(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001376
1377 case SSEUp:
1378 case X87Up:
1379 assert(0 && "Invalid classification for lo word.");
1380
1381 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1382 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1383 // and %r9 is used.
1384 case Integer:
Chris Lattner22a931e2010-06-29 06:01:59 +00001385 ++neededInt;
1386
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001387 // Pick an 8-byte type based on the preferred type.
Chris Lattnerc11301c2010-07-29 02:20:19 +00001388 ResType = Get8ByteTypeAtOffset(PrefType, 0, Ty, 0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001389 break;
1390
1391 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1392 // available SSE register is used, the registers are taken in the
1393 // order from %xmm0 to %xmm7.
1394 case SSE:
1395 ++neededSSE;
Chris Lattner458b2aa2010-07-29 02:16:43 +00001396 ResType = llvm::Type::getDoubleTy(getVMContext());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001397 break;
1398 }
1399
1400 switch (Hi) {
1401 // Memory was handled previously, ComplexX87 and X87 should
1402 // never occur as hi classes, and X87Up must be preceed by X87,
1403 // which is passed in memory.
1404 case Memory:
1405 case X87:
1406 case ComplexX87:
1407 assert(0 && "Invalid classification for hi word.");
1408 break;
1409
1410 case NoClass: break;
Chris Lattner22a931e2010-06-29 06:01:59 +00001411
1412 case Integer: {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001413 ++neededInt;
Chris Lattner22a931e2010-06-29 06:01:59 +00001414
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001415 // Pick an 8-byte type based on the preferred type.
Chris Lattnerc11301c2010-07-29 02:20:19 +00001416 const llvm::Type *HiType = Get8ByteTypeAtOffset(PrefType, 8, Ty, 8);
Chris Lattner458b2aa2010-07-29 02:16:43 +00001417 ResType = llvm::StructType::get(getVMContext(), ResType, HiType, NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001418 break;
Chris Lattner22a931e2010-06-29 06:01:59 +00001419 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001420
1421 // X87Up generally doesn't occur here (long double is passed in
1422 // memory), except in situations involving unions.
1423 case X87Up:
1424 case SSE:
Chris Lattner458b2aa2010-07-29 02:16:43 +00001425 ResType = llvm::StructType::get(getVMContext(), ResType,
1426 llvm::Type::getDoubleTy(getVMContext()),
1427 NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001428 ++neededSSE;
1429 break;
1430
1431 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1432 // eightbyte is passed in the upper half of the last used SSE
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00001433 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001434 case SSEUp:
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00001435 assert(Lo == SSE && "Unexpected SSEUp classification");
Chris Lattner458b2aa2010-07-29 02:16:43 +00001436 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00001437
1438 // If the preferred type is a 16-byte vector, prefer to pass it.
1439 if (const llvm::VectorType *VT =
1440 dyn_cast_or_null<llvm::VectorType>(PrefType)) {
1441 const llvm::Type *EltTy = VT->getElementType();
1442 if (VT->getBitWidth() == 128 &&
1443 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1444 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1445 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1446 EltTy->isIntegerTy(128)))
1447 ResType = PrefType;
1448 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001449 break;
1450 }
1451
Chris Lattner22a931e2010-06-29 06:01:59 +00001452 return getCoerceResult(Ty, ResType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001453}
1454
Chris Lattner458b2aa2010-07-29 02:16:43 +00001455void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI,
Chris Lattner1d7c9f72010-06-29 01:08:48 +00001456 const llvm::Type *const *PrefTypes,
1457 unsigned NumPrefTypes) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00001458 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001459
1460 // Keep track of the number of assigned registers.
1461 unsigned freeIntRegs = 6, freeSSERegs = 8;
1462
1463 // If the return value is indirect, then the hidden argument is consuming one
1464 // integer register.
1465 if (FI.getReturnInfo().isIndirect())
1466 --freeIntRegs;
1467
1468 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1469 // get assigned (in left-to-right order) for passing as follows...
1470 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1471 it != ie; ++it) {
Chris Lattner399d22a2010-06-29 01:14:09 +00001472 // If the client specified a preferred IR type to use, pass it down to
1473 // classifyArgumentType.
1474 const llvm::Type *PrefType = 0;
1475 if (NumPrefTypes) {
1476 PrefType = *PrefTypes++;
1477 --NumPrefTypes;
1478 }
1479
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001480 unsigned neededInt, neededSSE;
Chris Lattner458b2aa2010-07-29 02:16:43 +00001481 it->info = classifyArgumentType(it->type, neededInt, neededSSE, PrefType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001482
1483 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1484 // eightbyte of an argument, the whole argument is passed on the
1485 // stack. If registers have already been assigned for some
1486 // eightbytes of such an argument, the assignments get reverted.
1487 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1488 freeIntRegs -= neededInt;
1489 freeSSERegs -= neededSSE;
1490 } else {
Chris Lattner22a931e2010-06-29 06:01:59 +00001491 it->info = getIndirectResult(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001492 }
1493 }
1494}
1495
1496static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1497 QualType Ty,
1498 CodeGenFunction &CGF) {
1499 llvm::Value *overflow_arg_area_p =
1500 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1501 llvm::Value *overflow_arg_area =
1502 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1503
1504 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1505 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1506 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1507 if (Align > 8) {
1508 // Note that we follow the ABI & gcc here, even though the type
1509 // could in theory have an alignment greater than 16. This case
1510 // shouldn't ever matter in practice.
1511
1512 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson41a75022009-08-13 21:57:51 +00001513 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001514 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001515 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1516 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner5e016ae2010-06-27 07:15:29 +00001517 CGF.Int64Ty);
1518 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001519 overflow_arg_area =
1520 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1521 overflow_arg_area->getType(),
1522 "overflow_arg_area.align");
1523 }
1524
1525 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1526 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1527 llvm::Value *Res =
1528 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001529 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001530
1531 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1532 // l->overflow_arg_area + sizeof(type).
1533 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1534 // an 8 byte boundary.
1535
1536 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00001537 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001538 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001539 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1540 "overflow_arg_area.next");
1541 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1542
1543 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1544 return Res;
1545}
1546
1547llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1548 CodeGenFunction &CGF) const {
Owen Anderson170229f2009-07-14 23:10:40 +00001549 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump11289f42009-09-09 15:08:12 +00001550
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001551 // Assume that va_list type is correct; should be pointer to LLVM type:
1552 // struct {
1553 // i32 gp_offset;
1554 // i32 fp_offset;
1555 // i8* overflow_arg_area;
1556 // i8* reg_save_area;
1557 // };
1558 unsigned neededInt, neededSSE;
Chris Lattner9723d6c2010-03-11 18:19:55 +00001559
1560 Ty = CGF.getContext().getCanonicalType(Ty);
Chris Lattner458b2aa2010-07-29 02:16:43 +00001561 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE, 0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001562
1563 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1564 // in the registers. If not go to step 7.
1565 if (!neededInt && !neededSSE)
1566 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1567
1568 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1569 // general purpose registers needed to pass type and num_fp to hold
1570 // the number of floating point registers needed.
1571
1572 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1573 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1574 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1575 //
1576 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1577 // register save space).
1578
1579 llvm::Value *InRegs = 0;
1580 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1581 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1582 if (neededInt) {
1583 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1584 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattnerd776fb12010-06-28 21:43:59 +00001585 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
1586 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001587 }
1588
1589 if (neededSSE) {
1590 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1591 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1592 llvm::Value *FitsInFP =
Chris Lattnerd776fb12010-06-28 21:43:59 +00001593 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
1594 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001595 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1596 }
1597
1598 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1599 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1600 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1601 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1602
1603 // Emit code to load the value if it was passed in registers.
1604
1605 CGF.EmitBlock(InRegBlock);
1606
1607 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1608 // an offset of l->gp_offset and/or l->fp_offset. This may require
1609 // copying to a temporary location in case the parameter is passed
1610 // in different register classes or requires an alignment greater
1611 // than 8 for general purpose registers and 16 for XMM registers.
1612 //
1613 // FIXME: This really results in shameful code when we end up needing to
1614 // collect arguments from different places; often what should result in a
1615 // simple assembling of a structure from scattered addresses has many more
1616 // loads than necessary. Can we clean this up?
1617 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1618 llvm::Value *RegAddr =
1619 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1620 "reg_save_area");
1621 if (neededInt && neededSSE) {
1622 // FIXME: Cleanup.
1623 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1624 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1625 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1626 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1627 const llvm::Type *TyLo = ST->getElementType(0);
1628 const llvm::Type *TyHi = ST->getElementType(1);
Duncan Sands998f9d92010-02-15 16:14:01 +00001629 assert((TyLo->isFloatingPointTy() ^ TyHi->isFloatingPointTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001630 "Unexpected ABI info for mixed regs");
Owen Anderson9793f0e2009-07-29 22:16:19 +00001631 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1632 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001633 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1634 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sands998f9d92010-02-15 16:14:01 +00001635 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1636 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001637 llvm::Value *V =
1638 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1639 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1640 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1641 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1642
Owen Anderson170229f2009-07-14 23:10:40 +00001643 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001644 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001645 } else if (neededInt) {
1646 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1647 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001648 llvm::PointerType::getUnqual(LTy));
Chris Lattner0cf24192010-06-28 20:05:43 +00001649 } else if (neededSSE == 1) {
1650 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1651 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1652 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001653 } else {
Chris Lattner0cf24192010-06-28 20:05:43 +00001654 assert(neededSSE == 2 && "Invalid number of needed registers!");
1655 // SSE registers are spaced 16 bytes apart in the register save
1656 // area, we need to collect the two eightbytes together.
1657 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattnerd776fb12010-06-28 21:43:59 +00001658 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner0cf24192010-06-28 20:05:43 +00001659 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
1660 const llvm::Type *DblPtrTy =
1661 llvm::PointerType::getUnqual(DoubleTy);
1662 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1663 DoubleTy, NULL);
1664 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1665 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1666 DblPtrTy));
1667 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1668 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1669 DblPtrTy));
1670 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1671 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1672 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001673 }
1674
1675 // AMD64-ABI 3.5.7p5: Step 5. Set:
1676 // l->gp_offset = l->gp_offset + num_gp * 8
1677 // l->fp_offset = l->fp_offset + num_fp * 16.
1678 if (neededInt) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00001679 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001680 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1681 gp_offset_p);
1682 }
1683 if (neededSSE) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00001684 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001685 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1686 fp_offset_p);
1687 }
1688 CGF.EmitBranch(ContBlock);
1689
1690 // Emit code to load the value if it was passed in memory.
1691
1692 CGF.EmitBlock(InMemBlock);
1693 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1694
1695 // Return the appropriate result.
1696
1697 CGF.EmitBlock(ContBlock);
1698 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1699 "vaarg.addr");
1700 ResAddr->reserveOperandSpace(2);
1701 ResAddr->addIncoming(RegAddr, InRegBlock);
1702 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001703 return ResAddr;
1704}
1705
Chris Lattner0cf24192010-06-28 20:05:43 +00001706
1707
1708//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001709// PIC16 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00001710//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001711
1712namespace {
1713
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001714class PIC16ABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +00001715public:
1716 PIC16ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
1717
Chris Lattner458b2aa2010-07-29 02:16:43 +00001718 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001719
Chris Lattner458b2aa2010-07-29 02:16:43 +00001720 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001721
Chris Lattner458b2aa2010-07-29 02:16:43 +00001722 virtual void computeInfo(CGFunctionInfo &FI,
Chris Lattner1d7c9f72010-06-29 01:08:48 +00001723 const llvm::Type *const *PrefTypes,
1724 unsigned NumPrefTypes) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00001725 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001726 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1727 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +00001728 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001729 }
1730
1731 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1732 CodeGenFunction &CGF) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001733};
1734
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001735class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
1736public:
Chris Lattner2b037972010-07-29 02:01:43 +00001737 PIC16TargetCodeGenInfo(CodeGenTypes &CGT)
1738 : TargetCodeGenInfo(new PIC16ABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001739};
1740
Daniel Dunbard59655c2009-09-12 00:59:49 +00001741}
1742
Chris Lattner458b2aa2010-07-29 02:16:43 +00001743ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001744 if (RetTy->isVoidType()) {
1745 return ABIArgInfo::getIgnore();
1746 } else {
1747 return ABIArgInfo::getDirect();
1748 }
1749}
1750
Chris Lattner458b2aa2010-07-29 02:16:43 +00001751ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001752 return ABIArgInfo::getDirect();
1753}
1754
1755llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00001756 CodeGenFunction &CGF) const {
Chris Lattnerc0e8a592010-04-06 17:29:22 +00001757 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00001758 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1759
1760 CGBuilderTy &Builder = CGF.Builder;
1761 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1762 "ap");
1763 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1764 llvm::Type *PTy =
1765 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1766 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1767
1768 uint64_t Offset = CGF.getContext().getTypeSize(Ty) / 8;
1769
1770 llvm::Value *NextAddr =
1771 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1772 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
1773 "ap.next");
1774 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1775
1776 return AddrTyped;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001777}
1778
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00001779
John McCallea8d8bb2010-03-11 00:10:12 +00001780// PowerPC-32
1781
1782namespace {
1783class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
1784public:
Chris Lattner2b037972010-07-29 02:01:43 +00001785 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
1786
John McCallea8d8bb2010-03-11 00:10:12 +00001787 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1788 // This is recovered from gcc output.
1789 return 1; // r1 is the dedicated stack pointer
1790 }
1791
1792 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1793 llvm::Value *Address) const;
1794};
1795
1796}
1797
1798bool
1799PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1800 llvm::Value *Address) const {
1801 // This is calculated from the LLVM and GCC tables and verified
1802 // against gcc output. AFAIK all ABIs use the same encoding.
1803
1804 CodeGen::CGBuilderTy &Builder = CGF.Builder;
1805 llvm::LLVMContext &Context = CGF.getLLVMContext();
1806
1807 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
1808 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
1809 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
1810 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
1811
1812 // 0-31: r0-31, the 4-byte general-purpose registers
John McCall943fae92010-05-27 06:19:26 +00001813 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallea8d8bb2010-03-11 00:10:12 +00001814
1815 // 32-63: fp0-31, the 8-byte floating-point registers
John McCall943fae92010-05-27 06:19:26 +00001816 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallea8d8bb2010-03-11 00:10:12 +00001817
1818 // 64-76 are various 4-byte special-purpose registers:
1819 // 64: mq
1820 // 65: lr
1821 // 66: ctr
1822 // 67: ap
1823 // 68-75 cr0-7
1824 // 76: xer
John McCall943fae92010-05-27 06:19:26 +00001825 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallea8d8bb2010-03-11 00:10:12 +00001826
1827 // 77-108: v0-31, the 16-byte vector registers
John McCall943fae92010-05-27 06:19:26 +00001828 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallea8d8bb2010-03-11 00:10:12 +00001829
1830 // 109: vrsave
1831 // 110: vscr
1832 // 111: spe_acc
1833 // 112: spefscr
1834 // 113: sfp
John McCall943fae92010-05-27 06:19:26 +00001835 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallea8d8bb2010-03-11 00:10:12 +00001836
1837 return false;
1838}
1839
1840
Chris Lattner0cf24192010-06-28 20:05:43 +00001841//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001842// ARM ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00001843//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001844
1845namespace {
1846
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001847class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00001848public:
1849 enum ABIKind {
1850 APCS = 0,
1851 AAPCS = 1,
1852 AAPCS_VFP
1853 };
1854
1855private:
1856 ABIKind Kind;
1857
1858public:
Chris Lattner2b037972010-07-29 02:01:43 +00001859 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar020daa92009-09-12 01:00:39 +00001860
1861private:
1862 ABIKind getABIKind() const { return Kind; }
1863
Chris Lattner458b2aa2010-07-29 02:16:43 +00001864 ABIArgInfo classifyReturnType(QualType RetTy) const;
1865 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001866
Chris Lattner458b2aa2010-07-29 02:16:43 +00001867 virtual void computeInfo(CGFunctionInfo &FI,
Chris Lattner1d7c9f72010-06-29 01:08:48 +00001868 const llvm::Type *const *PrefTypes,
1869 unsigned NumPrefTypes) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001870
1871 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1872 CodeGenFunction &CGF) const;
1873};
1874
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001875class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
1876public:
Chris Lattner2b037972010-07-29 02:01:43 +00001877 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
1878 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00001879
1880 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1881 return 13;
1882 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001883};
1884
Daniel Dunbard59655c2009-09-12 00:59:49 +00001885}
1886
Chris Lattner458b2aa2010-07-29 02:16:43 +00001887void ARMABIInfo::computeInfo(CGFunctionInfo &FI,
Chris Lattner1d7c9f72010-06-29 01:08:48 +00001888 const llvm::Type *const *PrefTypes,
1889 unsigned NumPrefTypes) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00001890 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001891 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattner458b2aa2010-07-29 02:16:43 +00001892 it != ie; ++it)
1893 it->info = classifyArgumentType(it->type);
Daniel Dunbar020daa92009-09-12 01:00:39 +00001894
Chris Lattner458b2aa2010-07-29 02:16:43 +00001895 const llvm::Triple &Triple(getContext().Target.getTriple());
Rafael Espindolaa92c4422010-06-16 16:13:39 +00001896 llvm::CallingConv::ID DefaultCC;
Rafael Espindola23a8a062010-06-16 19:01:17 +00001897 if (Triple.getEnvironmentName() == "gnueabi" ||
1898 Triple.getEnvironmentName() == "eabi")
Rafael Espindolaa92c4422010-06-16 16:13:39 +00001899 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola23a8a062010-06-16 19:01:17 +00001900 else
1901 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindolaa92c4422010-06-16 16:13:39 +00001902
Daniel Dunbar020daa92009-09-12 01:00:39 +00001903 switch (getABIKind()) {
1904 case APCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00001905 if (DefaultCC != llvm::CallingConv::ARM_APCS)
1906 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00001907 break;
1908
1909 case AAPCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00001910 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
1911 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00001912 break;
1913
1914 case AAPCS_VFP:
1915 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
1916 break;
1917 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001918}
1919
Chris Lattner458b2aa2010-07-29 02:16:43 +00001920ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
Douglas Gregora71cc152010-02-02 20:10:50 +00001921 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1922 // Treat an enum type as its underlying type.
1923 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1924 Ty = EnumTy->getDecl()->getIntegerType();
1925
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001926 return (Ty->isPromotableIntegerType() ?
1927 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001928 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001929
Daniel Dunbar09d33622009-09-14 21:54:03 +00001930 // Ignore empty records.
Chris Lattner458b2aa2010-07-29 02:16:43 +00001931 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar09d33622009-09-14 21:54:03 +00001932 return ABIArgInfo::getIgnore();
1933
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00001934 // Structures with either a non-trivial destructor or a non-trivial
1935 // copy constructor are always indirect.
1936 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1937 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
1938
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001939 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1940 // backend doesn't support byval.
1941 // FIXME: This doesn't handle alignment > 64 bits.
1942 const llvm::Type* ElemTy;
1943 unsigned SizeRegs;
Chris Lattner458b2aa2010-07-29 02:16:43 +00001944 if (getContext().getTypeAlign(Ty) > 32) {
1945 ElemTy = llvm::Type::getInt64Ty(getVMContext());
1946 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001947 } else {
Chris Lattner458b2aa2010-07-29 02:16:43 +00001948 ElemTy = llvm::Type::getInt32Ty(getVMContext());
1949 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001950 }
1951 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson9793f0e2009-07-29 22:16:19 +00001952 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Chris Lattner458b2aa2010-07-29 02:16:43 +00001953 const llvm::Type* STy = llvm::StructType::get(getVMContext(), LLVMFields,
1954 true);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001955 return ABIArgInfo::getCoerce(STy);
1956}
1957
Chris Lattner458b2aa2010-07-29 02:16:43 +00001958static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001959 llvm::LLVMContext &VMContext) {
1960 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
1961 // is called integer-like if its size is less than or equal to one word, and
1962 // the offset of each of its addressable sub-fields is zero.
1963
1964 uint64_t Size = Context.getTypeSize(Ty);
1965
1966 // Check that the type fits in a word.
1967 if (Size > 32)
1968 return false;
1969
1970 // FIXME: Handle vector types!
1971 if (Ty->isVectorType())
1972 return false;
1973
Daniel Dunbard53bac72009-09-14 02:20:34 +00001974 // Float types are never treated as "integer like".
1975 if (Ty->isRealFloatingType())
1976 return false;
1977
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001978 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00001979 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001980 return true;
1981
Daniel Dunbar96ebba52010-02-01 23:31:26 +00001982 // Small complex integer types are "integer like".
1983 if (const ComplexType *CT = Ty->getAs<ComplexType>())
1984 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001985
1986 // Single element and zero sized arrays should be allowed, by the definition
1987 // above, but they are not.
1988
1989 // Otherwise, it must be a record type.
1990 const RecordType *RT = Ty->getAs<RecordType>();
1991 if (!RT) return false;
1992
1993 // Ignore records with flexible arrays.
1994 const RecordDecl *RD = RT->getDecl();
1995 if (RD->hasFlexibleArrayMember())
1996 return false;
1997
1998 // Check that all sub-fields are at offset 0, and are themselves "integer
1999 // like".
2000 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2001
2002 bool HadField = false;
2003 unsigned idx = 0;
2004 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2005 i != e; ++i, ++idx) {
2006 const FieldDecl *FD = *i;
2007
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002008 // Bit-fields are not addressable, we only need to verify they are "integer
2009 // like". We still have to disallow a subsequent non-bitfield, for example:
2010 // struct { int : 0; int x }
2011 // is non-integer like according to gcc.
2012 if (FD->isBitField()) {
2013 if (!RD->isUnion())
2014 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002015
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002016 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2017 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002018
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002019 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002020 }
2021
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002022 // Check if this field is at offset 0.
2023 if (Layout.getFieldOffset(idx) != 0)
2024 return false;
2025
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002026 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2027 return false;
2028
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002029 // Only allow at most one field in a structure. This doesn't match the
2030 // wording above, but follows gcc in situations with a field following an
2031 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002032 if (!RD->isUnion()) {
2033 if (HadField)
2034 return false;
2035
2036 HadField = true;
2037 }
2038 }
2039
2040 return true;
2041}
2042
Chris Lattner458b2aa2010-07-29 02:16:43 +00002043ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002044 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002045 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002046
Douglas Gregora71cc152010-02-02 20:10:50 +00002047 if (!CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2048 // Treat an enum type as its underlying type.
2049 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2050 RetTy = EnumTy->getDecl()->getIntegerType();
2051
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002052 return (RetTy->isPromotableIntegerType() ?
2053 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002054 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002055
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002056 // Structures with either a non-trivial destructor or a non-trivial
2057 // copy constructor are always indirect.
2058 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2059 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2060
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002061 // Are we following APCS?
2062 if (getABIKind() == APCS) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002063 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002064 return ABIArgInfo::getIgnore();
2065
Daniel Dunbareedf1512010-02-01 23:31:19 +00002066 // Complex types are all returned as packed integers.
2067 //
2068 // FIXME: Consider using 2 x vector types if the back end handles them
2069 // correctly.
2070 if (RetTy->isAnyComplexType())
Chris Lattner458b2aa2010-07-29 02:16:43 +00002071 return ABIArgInfo::getCoerce(llvm::IntegerType::get(getVMContext(),
2072 getContext().getTypeSize(RetTy)));
Daniel Dunbareedf1512010-02-01 23:31:19 +00002073
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002074 // Integer like structures are returned in r0.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002075 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002076 // Return in the smallest viable integer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002077 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002078 if (Size <= 8)
Chris Lattner458b2aa2010-07-29 02:16:43 +00002079 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002080 if (Size <= 16)
Chris Lattner458b2aa2010-07-29 02:16:43 +00002081 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(getVMContext()));
2082 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002083 }
2084
2085 // Otherwise return in memory.
2086 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002087 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002088
2089 // Otherwise this is an AAPCS variant.
2090
Chris Lattner458b2aa2010-07-29 02:16:43 +00002091 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002092 return ABIArgInfo::getIgnore();
2093
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002094 // Aggregates <= 4 bytes are returned in r0; other aggregates
2095 // are returned indirectly.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002096 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002097 if (Size <= 32) {
2098 // Return in the smallest viable integer type.
2099 if (Size <= 8)
Chris Lattner458b2aa2010-07-29 02:16:43 +00002100 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002101 if (Size <= 16)
Chris Lattner458b2aa2010-07-29 02:16:43 +00002102 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(getVMContext()));
2103 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002104 }
2105
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002106 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002107}
2108
2109llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00002110 CodeGenFunction &CGF) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002111 // FIXME: Need to handle alignment
Benjamin Kramerabd5b902009-10-13 10:07:13 +00002112 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002113 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002114
2115 CGBuilderTy &Builder = CGF.Builder;
2116 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2117 "ap");
2118 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2119 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00002120 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002121 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2122
2123 uint64_t Offset =
2124 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2125 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +00002126 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002127 "ap.next");
2128 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2129
2130 return AddrTyped;
2131}
2132
Chris Lattner458b2aa2010-07-29 02:16:43 +00002133ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
2134 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002135 return ABIArgInfo::getIgnore();
Douglas Gregora71cc152010-02-02 20:10:50 +00002136
Chris Lattner458b2aa2010-07-29 02:16:43 +00002137 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
2138 return ABIArgInfo::getIndirect(0);
2139
2140 // Treat an enum type as its underlying type.
2141 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2142 RetTy = EnumTy->getDecl()->getIntegerType();
2143
2144 return (RetTy->isPromotableIntegerType() ?
2145 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002146}
2147
Chris Lattner0cf24192010-06-28 20:05:43 +00002148//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002149// SystemZ ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002150//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002151
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002152namespace {
Daniel Dunbard59655c2009-09-12 00:59:49 +00002153
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002154class SystemZABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +00002155public:
2156 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2157
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002158 bool isPromotableIntegerType(QualType Ty) const;
2159
Chris Lattner458b2aa2010-07-29 02:16:43 +00002160 ABIArgInfo classifyReturnType(QualType RetTy) const;
2161 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002162
Chris Lattner458b2aa2010-07-29 02:16:43 +00002163 virtual void computeInfo(CGFunctionInfo &FI,
Chris Lattner1d7c9f72010-06-29 01:08:48 +00002164 const llvm::Type *const *PrefTypes,
2165 unsigned NumPrefTypes) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002166 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002167 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2168 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +00002169 it->info = classifyArgumentType(it->type);
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002170 }
2171
2172 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2173 CodeGenFunction &CGF) const;
2174};
Daniel Dunbard59655c2009-09-12 00:59:49 +00002175
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002176class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2177public:
Chris Lattner2b037972010-07-29 02:01:43 +00002178 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
2179 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002180};
2181
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002182}
2183
2184bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2185 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall9dd450b2009-09-21 23:43:11 +00002186 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002187 switch (BT->getKind()) {
2188 case BuiltinType::Bool:
2189 case BuiltinType::Char_S:
2190 case BuiltinType::Char_U:
2191 case BuiltinType::SChar:
2192 case BuiltinType::UChar:
2193 case BuiltinType::Short:
2194 case BuiltinType::UShort:
2195 case BuiltinType::Int:
2196 case BuiltinType::UInt:
2197 return true;
2198 default:
2199 return false;
2200 }
2201 return false;
2202}
2203
2204llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2205 CodeGenFunction &CGF) const {
2206 // FIXME: Implement
2207 return 0;
2208}
2209
2210
Chris Lattner458b2aa2010-07-29 02:16:43 +00002211ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
2212 if (RetTy->isVoidType())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002213 return ABIArgInfo::getIgnore();
Chris Lattner458b2aa2010-07-29 02:16:43 +00002214 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002215 return ABIArgInfo::getIndirect(0);
Chris Lattner458b2aa2010-07-29 02:16:43 +00002216
2217 return (isPromotableIntegerType(RetTy) ?
2218 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002219}
2220
Chris Lattner458b2aa2010-07-29 02:16:43 +00002221ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
2222 if (CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002223 return ABIArgInfo::getIndirect(0);
Chris Lattner458b2aa2010-07-29 02:16:43 +00002224
2225 return (isPromotableIntegerType(Ty) ?
2226 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002227}
2228
Chris Lattner0cf24192010-06-28 20:05:43 +00002229//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002230// MSP430 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002231//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002232
2233namespace {
2234
2235class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2236public:
Chris Lattner2b037972010-07-29 02:01:43 +00002237 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
2238 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002239 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2240 CodeGen::CodeGenModule &M) const;
2241};
2242
2243}
2244
2245void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2246 llvm::GlobalValue *GV,
2247 CodeGen::CodeGenModule &M) const {
2248 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2249 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2250 // Handle 'interrupt' attribute:
2251 llvm::Function *F = cast<llvm::Function>(GV);
2252
2253 // Step 1: Set ISR calling convention.
2254 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2255
2256 // Step 2: Add attributes goodness.
2257 F->addFnAttr(llvm::Attribute::NoInline);
2258
2259 // Step 3: Emit ISR vector alias.
2260 unsigned Num = attr->getNumber() + 0xffe0;
2261 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2262 "vector_" +
2263 llvm::LowercaseString(llvm::utohexstr(Num)),
2264 GV, &M.getModule());
2265 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002266 }
2267}
2268
Chris Lattner0cf24192010-06-28 20:05:43 +00002269//===----------------------------------------------------------------------===//
John McCall943fae92010-05-27 06:19:26 +00002270// MIPS ABI Implementation. This works for both little-endian and
2271// big-endian variants.
Chris Lattner0cf24192010-06-28 20:05:43 +00002272//===----------------------------------------------------------------------===//
2273
John McCall943fae92010-05-27 06:19:26 +00002274namespace {
2275class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
2276public:
Chris Lattner2b037972010-07-29 02:01:43 +00002277 MIPSTargetCodeGenInfo(CodeGenTypes &CGT)
2278 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
John McCall943fae92010-05-27 06:19:26 +00002279
2280 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2281 return 29;
2282 }
2283
2284 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2285 llvm::Value *Address) const;
2286};
2287}
2288
2289bool
2290MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2291 llvm::Value *Address) const {
2292 // This information comes from gcc's implementation, which seems to
2293 // as canonical as it gets.
2294
2295 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2296 llvm::LLVMContext &Context = CGF.getLLVMContext();
2297
2298 // Everything on MIPS is 4 bytes. Double-precision FP registers
2299 // are aliased to pairs of single-precision FP registers.
2300 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2301 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2302
2303 // 0-31 are the general purpose registers, $0 - $31.
2304 // 32-63 are the floating-point registers, $f0 - $f31.
2305 // 64 and 65 are the multiply/divide registers, $hi and $lo.
2306 // 66 is the (notional, I think) register for signal-handler return.
2307 AssignToArrayRange(Builder, Address, Four8, 0, 65);
2308
2309 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
2310 // They are one bit wide and ignored here.
2311
2312 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
2313 // (coprocessor 1 is the FP unit)
2314 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
2315 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
2316 // 176-181 are the DSP accumulator registers.
2317 AssignToArrayRange(Builder, Address, Four8, 80, 181);
2318
2319 return false;
2320}
2321
2322
Chris Lattner2b037972010-07-29 02:01:43 +00002323const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002324 if (TheTargetCodeGenInfo)
2325 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002326
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002327 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2328 // free it.
Daniel Dunbare3532f82009-08-24 08:52:16 +00002329
Chris Lattner22a931e2010-06-29 06:01:59 +00002330 const llvm::Triple &Triple = getContext().Target.getTriple();
Daniel Dunbar40165182009-08-24 09:10:05 +00002331 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00002332 default:
Chris Lattner2b037972010-07-29 02:01:43 +00002333 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002334
John McCall943fae92010-05-27 06:19:26 +00002335 case llvm::Triple::mips:
2336 case llvm::Triple::mipsel:
Chris Lattner2b037972010-07-29 02:01:43 +00002337 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types));
John McCall943fae92010-05-27 06:19:26 +00002338
Daniel Dunbard59655c2009-09-12 00:59:49 +00002339 case llvm::Triple::arm:
2340 case llvm::Triple::thumb:
Daniel Dunbar020daa92009-09-12 01:00:39 +00002341 // FIXME: We want to know the float calling convention as well.
Daniel Dunbarb4091a92009-09-14 00:35:03 +00002342 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002343 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002344 new ARMTargetCodeGenInfo(Types, ARMABIInfo::APCS));
Daniel Dunbar020daa92009-09-12 01:00:39 +00002345
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002346 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002347 new ARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002348
2349 case llvm::Triple::pic16:
Chris Lattner2b037972010-07-29 02:01:43 +00002350 return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002351
John McCallea8d8bb2010-03-11 00:10:12 +00002352 case llvm::Triple::ppc:
Chris Lattner2b037972010-07-29 02:01:43 +00002353 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallea8d8bb2010-03-11 00:10:12 +00002354
Daniel Dunbard59655c2009-09-12 00:59:49 +00002355 case llvm::Triple::systemz:
Chris Lattner2b037972010-07-29 02:01:43 +00002356 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002357
2358 case llvm::Triple::msp430:
Chris Lattner2b037972010-07-29 02:01:43 +00002359 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002360
Daniel Dunbar40165182009-08-24 09:10:05 +00002361 case llvm::Triple::x86:
Daniel Dunbar40165182009-08-24 09:10:05 +00002362 switch (Triple.getOS()) {
Edward O'Callaghan462e4ab2009-10-20 17:22:50 +00002363 case llvm::Triple::Darwin:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002364 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002365 new X86_32TargetCodeGenInfo(Types, true, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002366 case llvm::Triple::Cygwin:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002367 case llvm::Triple::MinGW32:
2368 case llvm::Triple::MinGW64:
Edward O'Callaghan437ec1e2009-10-21 11:58:24 +00002369 case llvm::Triple::AuroraUX:
2370 case llvm::Triple::DragonFly:
David Chisnall2c5bef22009-09-03 01:48:05 +00002371 case llvm::Triple::FreeBSD:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002372 case llvm::Triple::OpenBSD:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002373 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002374 new X86_32TargetCodeGenInfo(Types, false, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002375
2376 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002377 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002378 new X86_32TargetCodeGenInfo(Types, false, false));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002379 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002380
Daniel Dunbare3532f82009-08-24 08:52:16 +00002381 case llvm::Triple::x86_64:
Chris Lattner2b037972010-07-29 02:01:43 +00002382 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002383 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002384}