blob: 4d221d4e657ed7999a10c76affa4127084791488 [file] [log] [blame]
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000016#include "ABIInfo.h"
17#include "CodeGenFunction.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000018#include "clang/AST/RecordLayout.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000019#include "llvm/Type.h"
Chris Lattner9c254f02010-06-29 06:01:59 +000020#include "llvm/Target/TargetData.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000021#include "llvm/ADT/StringExtras.h"
Daniel Dunbar2c0843f2009-08-24 08:52:16 +000022#include "llvm/ADT/Triple.h"
Daniel Dunbar28df7a52009-12-03 09:13:49 +000023#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000024using namespace clang;
25using namespace CodeGen;
26
John McCallaeeb7012010-05-27 06:19:26 +000027static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
28 llvm::Value *Array,
29 llvm::Value *Value,
30 unsigned FirstIndex,
31 unsigned LastIndex) {
32 // Alternatively, we could emit this as a loop in the source.
33 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
34 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
35 Builder.CreateStore(Value, Cell);
36 }
37}
38
John McCalld608cdb2010-08-22 10:59:02 +000039static bool isAggregateTypeForABI(QualType T) {
40 return CodeGenFunction::hasAggregateLLVMType(T) ||
41 T->isMemberFunctionPointerType();
42}
43
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000044ABIInfo::~ABIInfo() {}
45
Chris Lattnerea044322010-07-29 02:01:43 +000046ASTContext &ABIInfo::getContext() const {
47 return CGT.getContext();
48}
49
50llvm::LLVMContext &ABIInfo::getVMContext() const {
51 return CGT.getLLVMContext();
52}
53
54const llvm::TargetData &ABIInfo::getTargetData() const {
55 return CGT.getTargetData();
56}
57
58
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000059void ABIArgInfo::dump() const {
Daniel Dunbar28df7a52009-12-03 09:13:49 +000060 llvm::raw_ostream &OS = llvm::errs();
61 OS << "(ABIArgInfo Kind=";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000062 switch (TheKind) {
63 case Direct:
Chris Lattner800588f2010-07-29 06:26:06 +000064 OS << "Direct Type=";
65 if (const llvm::Type *Ty = getCoerceToType())
66 Ty->print(OS);
67 else
68 OS << "null";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000069 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000070 case Extend:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000071 OS << "Extend";
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000072 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000073 case Ignore:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000074 OS << "Ignore";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000075 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000076 case Indirect:
Daniel Dunbardc6d5742010-04-21 19:10:51 +000077 OS << "Indirect Align=" << getIndirectAlign()
78 << " Byal=" << getIndirectByVal();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000079 break;
80 case Expand:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000081 OS << "Expand";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000082 break;
83 }
Daniel Dunbar28df7a52009-12-03 09:13:49 +000084 OS << ")\n";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000085}
86
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000087TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
88
Daniel Dunbar98303b92009-09-13 08:03:58 +000089static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000090
91/// isEmptyField - Return true iff a the field is "empty", that is it
92/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +000093static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
94 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000095 if (FD->isUnnamedBitfield())
96 return true;
97
98 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000099
Daniel Dunbar98303b92009-09-13 08:03:58 +0000100 // Constant arrays of empty records count as empty, strip them off.
101 if (AllowArrays)
102 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
103 FT = AT->getElementType();
104
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000105 const RecordType *RT = FT->getAs<RecordType>();
106 if (!RT)
107 return false;
108
109 // C++ record fields are never empty, at least in the Itanium ABI.
110 //
111 // FIXME: We should use a predicate for whether this behavior is true in the
112 // current ABI.
113 if (isa<CXXRecordDecl>(RT->getDecl()))
114 return false;
115
Daniel Dunbar98303b92009-09-13 08:03:58 +0000116 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000117}
118
119/// isEmptyRecord - Return true iff a structure contains only empty
120/// fields. Note that a structure with a flexible array member is not
121/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000122static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000123 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000124 if (!RT)
125 return 0;
126 const RecordDecl *RD = RT->getDecl();
127 if (RD->hasFlexibleArrayMember())
128 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000129
130 // If this is a C++ record, check the bases first.
131 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
132 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
133 e = CXXRD->bases_end(); i != e; ++i)
134 if (!isEmptyRecord(Context, i->getType(), true))
135 return false;
136
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000137 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
138 i != e; ++i)
Daniel Dunbar98303b92009-09-13 08:03:58 +0000139 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000140 return false;
141 return true;
142}
143
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000144/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
145/// a non-trivial destructor or a non-trivial copy constructor.
146static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
147 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
148 if (!RD)
149 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000150
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000151 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
152}
153
154/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
155/// a record type with either a non-trivial destructor or a non-trivial copy
156/// constructor.
157static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
158 const RecordType *RT = T->getAs<RecordType>();
159 if (!RT)
160 return false;
161
162 return hasNonTrivialDestructorOrCopyConstructor(RT);
163}
164
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000165/// isSingleElementStruct - Determine if a structure is a "single
166/// element struct", i.e. it has exactly one non-empty field or
167/// exactly one field which is itself a single element
168/// struct. Structures with flexible array members are never
169/// considered single element structs.
170///
171/// \return The field declaration for the single non-empty field, if
172/// it exists.
173static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
174 const RecordType *RT = T->getAsStructureType();
175 if (!RT)
176 return 0;
177
178 const RecordDecl *RD = RT->getDecl();
179 if (RD->hasFlexibleArrayMember())
180 return 0;
181
182 const Type *Found = 0;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000183
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000184 // If this is a C++ record, check the bases first.
185 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
186 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
187 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000188 // Ignore empty records.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000189 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000190 continue;
191
192 // If we already found an element then this isn't a single-element struct.
193 if (Found)
194 return 0;
195
196 // If this is non-empty and not a single element struct, the composite
197 // cannot be a single element struct.
198 Found = isSingleElementStruct(i->getType(), Context);
199 if (!Found)
200 return 0;
201 }
202 }
203
204 // Check for single element.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000205 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
206 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000207 const FieldDecl *FD = *i;
208 QualType FT = FD->getType();
209
210 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000211 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000212 continue;
213
214 // If we already found an element then this isn't a single-element
215 // struct.
216 if (Found)
217 return 0;
218
219 // Treat single element arrays as the element.
220 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
221 if (AT->getSize().getZExtValue() != 1)
222 break;
223 FT = AT->getElementType();
224 }
225
John McCalld608cdb2010-08-22 10:59:02 +0000226 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000227 Found = FT.getTypePtr();
228 } else {
229 Found = isSingleElementStruct(FT, Context);
230 if (!Found)
231 return 0;
232 }
233 }
234
235 return Found;
236}
237
238static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbara1842d32010-05-14 03:40:53 +0000239 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000240 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
241 !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000242 return false;
243
244 uint64_t Size = Context.getTypeSize(Ty);
245 return Size == 32 || Size == 64;
246}
247
Daniel Dunbar53012f42009-11-09 01:33:53 +0000248/// canExpandIndirectArgument - Test whether an argument type which is to be
249/// passed indirectly (on the stack) would have the equivalent layout if it was
250/// expanded into separate arguments. If so, we prefer to do the latter to avoid
251/// inhibiting optimizations.
252///
253// FIXME: This predicate is missing many cases, currently it just follows
254// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
255// should probably make this smarter, or better yet make the LLVM backend
256// capable of handling it.
257static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
258 // We can only expand structure types.
259 const RecordType *RT = Ty->getAs<RecordType>();
260 if (!RT)
261 return false;
262
263 // We can only expand (C) structures.
264 //
265 // FIXME: This needs to be generalized to handle classes as well.
266 const RecordDecl *RD = RT->getDecl();
267 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
268 return false;
269
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000270 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
271 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000272 const FieldDecl *FD = *i;
273
274 if (!is32Or64BitBasicType(FD->getType(), Context))
275 return false;
276
277 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
278 // how to expand them yet, and the predicate for telling if a bitfield still
279 // counts as "basic" is more complicated than what we were doing previously.
280 if (FD->isBitField())
281 return false;
282 }
283
284 return true;
285}
286
287namespace {
288/// DefaultABIInfo - The default implementation for ABI specific
289/// details. This implementation provides information which results in
290/// self-consistent and sensible LLVM IR generation, but does not
291/// conform to any particular ABI.
292class DefaultABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +0000293public:
294 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000295
Chris Lattnera3c109b2010-07-29 02:16:43 +0000296 ABIArgInfo classifyReturnType(QualType RetTy) const;
297 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000298
Chris Lattneree5dcd02010-07-29 02:31:05 +0000299 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000300 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000301 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
302 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000303 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000304 }
305
306 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
307 CodeGenFunction &CGF) const;
308};
309
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000310class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
311public:
Chris Lattnerea044322010-07-29 02:01:43 +0000312 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
313 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000314};
315
316llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
317 CodeGenFunction &CGF) const {
318 return 0;
319}
320
Chris Lattnera3c109b2010-07-29 02:16:43 +0000321ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +0000322 if (isAggregateTypeForABI(Ty))
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000323 return ABIArgInfo::getIndirect(0);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000324
Chris Lattnera14db752010-03-11 18:19:55 +0000325 // Treat an enum type as its underlying type.
326 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
327 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000328
Chris Lattnera14db752010-03-11 18:19:55 +0000329 return (Ty->isPromotableIntegerType() ?
330 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000331}
332
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000333//===----------------------------------------------------------------------===//
334// X86-32 ABI Implementation
335//===----------------------------------------------------------------------===//
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000336
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000337/// X86_32ABIInfo - The X86-32 ABI information.
338class X86_32ABIInfo : public ABIInfo {
David Chisnall1e4249c2009-08-17 23:08:21 +0000339 bool IsDarwinVectorABI;
340 bool IsSmallStructInRegABI;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000341
342 static bool isRegisterSize(unsigned Size) {
343 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
344 }
345
346 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
347
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000348 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
349 /// such that the argument will be passed in memory.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000350 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000351
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000352public:
Chris Lattnerea044322010-07-29 02:01:43 +0000353
Chris Lattnera3c109b2010-07-29 02:16:43 +0000354 ABIArgInfo classifyReturnType(QualType RetTy) const;
355 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000356
Chris Lattneree5dcd02010-07-29 02:31:05 +0000357 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000358 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000359 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
360 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000361 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000362 }
363
364 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
365 CodeGenFunction &CGF) const;
366
Chris Lattnerea044322010-07-29 02:01:43 +0000367 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
368 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000369};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000370
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000371class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
372public:
Chris Lattnerea044322010-07-29 02:01:43 +0000373 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
374 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000375
376 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
377 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000378
379 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
380 // Darwin uses different dwarf register numbers for EH.
381 if (CGM.isTargetDarwin()) return 5;
382
383 return 4;
384 }
385
386 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
387 llvm::Value *Address) const;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000388};
389
390}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000391
392/// shouldReturnTypeInRegister - Determine if the given type should be
393/// passed in a register (for the Darwin ABI).
394bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
395 ASTContext &Context) {
396 uint64_t Size = Context.getTypeSize(Ty);
397
398 // Type must be register sized.
399 if (!isRegisterSize(Size))
400 return false;
401
402 if (Ty->isVectorType()) {
403 // 64- and 128- bit vectors inside structures are not returned in
404 // registers.
405 if (Size == 64 || Size == 128)
406 return false;
407
408 return true;
409 }
410
Daniel Dunbar77115232010-05-15 00:00:30 +0000411 // If this is a builtin, pointer, enum, complex type, member pointer, or
412 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000413 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000414 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000415 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000416 return true;
417
418 // Arrays are treated like records.
419 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
420 return shouldReturnTypeInRegister(AT->getElementType(), Context);
421
422 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000423 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000424 if (!RT) return false;
425
Anders Carlssona8874232010-01-27 03:25:19 +0000426 // FIXME: Traverse bases here too.
427
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000428 // Structure types are passed in register if all fields would be
429 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000430 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
431 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000432 const FieldDecl *FD = *i;
433
434 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000435 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000436 continue;
437
438 // Check fields recursively.
439 if (!shouldReturnTypeInRegister(FD->getType(), Context))
440 return false;
441 }
442
443 return true;
444}
445
Chris Lattnera3c109b2010-07-29 02:16:43 +0000446ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const {
447 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000448 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000449
Chris Lattnera3c109b2010-07-29 02:16:43 +0000450 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000451 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000452 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000453 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000454
455 // 128-bit vectors are a special case; they are returned in
456 // registers and we need to make sure to pick a type the LLVM
457 // backend will like.
458 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +0000459 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000460 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000461
462 // Always return in register if it fits in a general purpose
463 // register, or if it is 64 bits and has a single element.
464 if ((Size == 8 || Size == 16 || Size == 32) ||
465 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +0000466 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000467 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000468
469 return ABIArgInfo::getIndirect(0);
470 }
471
472 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000473 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000474
John McCalld608cdb2010-08-22 10:59:02 +0000475 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000476 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000477 // Structures with either a non-trivial destructor or a non-trivial
478 // copy constructor are always indirect.
479 if (hasNonTrivialDestructorOrCopyConstructor(RT))
480 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000481
Anders Carlsson40092972009-10-20 22:07:59 +0000482 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000483 if (RT->getDecl()->hasFlexibleArrayMember())
484 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000485 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000486
David Chisnall1e4249c2009-08-17 23:08:21 +0000487 // If specified, structs and unions are always indirect.
488 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000489 return ABIArgInfo::getIndirect(0);
490
491 // Classify "single element" structs as their element type.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000492 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) {
John McCall183700f2009-09-21 23:43:11 +0000493 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000494 if (BT->isIntegerType()) {
495 // We need to use the size of the structure, padding
496 // bit-fields can adjust that to be larger than the single
497 // element type.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000498 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattner800588f2010-07-29 06:26:06 +0000499 return ABIArgInfo::getDirect(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000500 llvm::IntegerType::get(getVMContext(), (unsigned)Size));
501 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000502
Chris Lattnera3c109b2010-07-29 02:16:43 +0000503 if (BT->getKind() == BuiltinType::Float) {
504 assert(getContext().getTypeSize(RetTy) ==
505 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000506 "Unexpect single element structure size!");
Chris Lattner800588f2010-07-29 06:26:06 +0000507 return ABIArgInfo::getDirect(llvm::Type::getFloatTy(getVMContext()));
Chris Lattnera3c109b2010-07-29 02:16:43 +0000508 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000509
Chris Lattnera3c109b2010-07-29 02:16:43 +0000510 if (BT->getKind() == BuiltinType::Double) {
511 assert(getContext().getTypeSize(RetTy) ==
512 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000513 "Unexpect single element structure size!");
Chris Lattner800588f2010-07-29 06:26:06 +0000514 return ABIArgInfo::getDirect(llvm::Type::getDoubleTy(getVMContext()));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000515 }
516 } else if (SeltTy->isPointerType()) {
517 // FIXME: It would be really nice if this could come out as the proper
518 // pointer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000519 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(getVMContext());
Chris Lattner800588f2010-07-29 06:26:06 +0000520 return ABIArgInfo::getDirect(PtrTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000521 } else if (SeltTy->isVectorType()) {
522 // 64- and 128-bit vectors are never returned in a
523 // register when inside a structure.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000524 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000525 if (Size == 64 || Size == 128)
526 return ABIArgInfo::getIndirect(0);
527
Chris Lattnera3c109b2010-07-29 02:16:43 +0000528 return classifyReturnType(QualType(SeltTy, 0));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000529 }
530 }
531
532 // Small structures which are register sized are generally returned
533 // in a register.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000534 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) {
535 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattner800588f2010-07-29 06:26:06 +0000536 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000537 }
538
539 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000540 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000541
Chris Lattnera3c109b2010-07-29 02:16:43 +0000542 // Treat an enum type as its underlying type.
543 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
544 RetTy = EnumTy->getDecl()->getIntegerType();
545
546 return (RetTy->isPromotableIntegerType() ?
547 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000548}
549
Chris Lattnera3c109b2010-07-29 02:16:43 +0000550ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000551 if (!ByVal)
552 return ABIArgInfo::getIndirect(0, false);
553
554 // Compute the byval alignment. We trust the back-end to honor the
555 // minimum ABI alignment for byval, to make cleaner IR.
556 const unsigned MinABIAlign = 4;
Chris Lattnera3c109b2010-07-29 02:16:43 +0000557 unsigned Align = getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000558 if (Align > MinABIAlign)
559 return ABIArgInfo::getIndirect(Align);
560 return ABIArgInfo::getIndirect(0);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000561}
562
Chris Lattnera3c109b2010-07-29 02:16:43 +0000563ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000564 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000565 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000566 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000567 if (const RecordType *RT = Ty->getAs<RecordType>()) {
568 // Structures with either a non-trivial destructor or a non-trivial
569 // copy constructor are always indirect.
570 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Chris Lattnera3c109b2010-07-29 02:16:43 +0000571 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000572
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000573 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattnera3c109b2010-07-29 02:16:43 +0000574 return getIndirectResult(Ty);
Anders Carlssona8874232010-01-27 03:25:19 +0000575 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000576
577 // Ignore empty structs.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000578 if (Ty->isStructureType() && getContext().getTypeSize(Ty) == 0)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000579 return ABIArgInfo::getIgnore();
580
Daniel Dunbar53012f42009-11-09 01:33:53 +0000581 // Expand small (<= 128-bit) record types when we know that the stack layout
582 // of those arguments will match the struct. This is important because the
583 // LLVM backend isn't smart enough to remove byval, which inhibits many
584 // optimizations.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000585 if (getContext().getTypeSize(Ty) <= 4*32 &&
586 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar53012f42009-11-09 01:33:53 +0000587 return ABIArgInfo::getExpand();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000588
Chris Lattnera3c109b2010-07-29 02:16:43 +0000589 return getIndirectResult(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000590 }
591
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000592 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +0000593 // On Darwin, some vectors are passed in memory, we handle this by passing
594 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000595 if (IsDarwinVectorABI) {
596 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000597 if ((Size == 8 || Size == 16 || Size == 32) ||
598 (Size == 64 && VT->getNumElements() == 1))
599 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
600 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000601 }
602
603 return ABIArgInfo::getDirect();
604 }
605
606
Chris Lattnera3c109b2010-07-29 02:16:43 +0000607 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
608 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000609
Chris Lattnera3c109b2010-07-29 02:16:43 +0000610 return (Ty->isPromotableIntegerType() ?
611 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000612}
613
614llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
615 CodeGenFunction &CGF) const {
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000616 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +0000617 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000618
619 CGBuilderTy &Builder = CGF.Builder;
620 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
621 "ap");
622 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
623 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000624 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000625 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
626
627 uint64_t Offset =
628 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
629 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +0000630 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000631 "ap.next");
632 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
633
634 return AddrTyped;
635}
636
Charles Davis74f72932010-02-13 15:54:06 +0000637void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
638 llvm::GlobalValue *GV,
639 CodeGen::CodeGenModule &CGM) const {
640 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
641 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
642 // Get the LLVM function.
643 llvm::Function *Fn = cast<llvm::Function>(GV);
644
645 // Now add the 'alignstack' attribute with a value of 16.
646 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
647 }
648 }
649}
650
John McCall6374c332010-03-06 00:35:14 +0000651bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
652 CodeGen::CodeGenFunction &CGF,
653 llvm::Value *Address) const {
654 CodeGen::CGBuilderTy &Builder = CGF.Builder;
655 llvm::LLVMContext &Context = CGF.getLLVMContext();
656
657 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
658 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000659
John McCall6374c332010-03-06 00:35:14 +0000660 // 0-7 are the eight integer registers; the order is different
661 // on Darwin (for EH), but the range is the same.
662 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +0000663 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +0000664
665 if (CGF.CGM.isTargetDarwin()) {
666 // 12-16 are st(0..4). Not sure why we stop at 4.
667 // These have size 16, which is sizeof(long double) on
668 // platforms with 8-byte alignment for that type.
669 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCallaeeb7012010-05-27 06:19:26 +0000670 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000671
John McCall6374c332010-03-06 00:35:14 +0000672 } else {
673 // 9 is %eflags, which doesn't get a size on Darwin for some
674 // reason.
675 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
676
677 // 11-16 are st(0..5). Not sure why we stop at 5.
678 // These have size 12, which is sizeof(long double) on
679 // platforms with 4-byte alignment for that type.
680 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCallaeeb7012010-05-27 06:19:26 +0000681 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
682 }
John McCall6374c332010-03-06 00:35:14 +0000683
684 return false;
685}
686
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000687//===----------------------------------------------------------------------===//
688// X86-64 ABI Implementation
689//===----------------------------------------------------------------------===//
690
691
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000692namespace {
693/// X86_64ABIInfo - The X86_64 ABI information.
694class X86_64ABIInfo : public ABIInfo {
695 enum Class {
696 Integer = 0,
697 SSE,
698 SSEUp,
699 X87,
700 X87Up,
701 ComplexX87,
702 NoClass,
703 Memory
704 };
705
706 /// merge - Implement the X86_64 ABI merging algorithm.
707 ///
708 /// Merge an accumulating classification \arg Accum with a field
709 /// classification \arg Field.
710 ///
711 /// \param Accum - The accumulating classification. This should
712 /// always be either NoClass or the result of a previous merge
713 /// call. In addition, this should never be Memory (the caller
714 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +0000715 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000716
717 /// classify - Determine the x86_64 register classes in which the
718 /// given type T should be passed.
719 ///
720 /// \param Lo - The classification for the parts of the type
721 /// residing in the low word of the containing object.
722 ///
723 /// \param Hi - The classification for the parts of the type
724 /// residing in the high word of the containing object.
725 ///
726 /// \param OffsetBase - The bit offset of this type in the
727 /// containing object. Some parameters are classified different
728 /// depending on whether they straddle an eightbyte boundary.
729 ///
730 /// If a word is unused its result will be NoClass; if a type should
731 /// be passed in Memory then at least the classification of \arg Lo
732 /// will be Memory.
733 ///
734 /// The \arg Lo class will be NoClass iff the argument is ignored.
735 ///
736 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
737 /// also be ComplexX87.
Chris Lattner9c254f02010-06-29 06:01:59 +0000738 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000739
Chris Lattner0f408f52010-07-29 04:56:46 +0000740 const llvm::Type *Get16ByteVectorType(QualType Ty) const;
Chris Lattner603519d2010-07-29 17:49:08 +0000741 const llvm::Type *GetSSETypeAtOffset(const llvm::Type *IRType,
Chris Lattnerf47c9442010-07-29 18:13:09 +0000742 unsigned IROffset, QualType SourceTy,
743 unsigned SourceOffset) const;
Chris Lattner0d2656d2010-07-29 17:40:35 +0000744 const llvm::Type *GetINTEGERTypeAtOffset(const llvm::Type *IRType,
745 unsigned IROffset, QualType SourceTy,
746 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000747
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000748 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000749 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000750 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000751
752 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000753 /// such that the argument will be passed in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000754 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000755
Chris Lattnera3c109b2010-07-29 02:16:43 +0000756 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000757
Chris Lattner5868ca22010-07-29 04:41:05 +0000758 ABIArgInfo classifyArgumentType(QualType Ty, unsigned &neededInt,
759 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000760
761public:
Chris Lattnerea044322010-07-29 02:01:43 +0000762 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Chris Lattner9c254f02010-06-29 06:01:59 +0000763
Chris Lattneree5dcd02010-07-29 02:31:05 +0000764 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000765
766 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
767 CodeGenFunction &CGF) const;
768};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000769
Chris Lattnerf13721d2010-08-31 16:44:54 +0000770/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
771class WinX86_64ABIInfo : public X86_64ABIInfo {
772public:
773 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : X86_64ABIInfo(CGT) {}
774
775 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
776 CodeGenFunction &CGF) const;
777};
778
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000779class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
780public:
Chris Lattnerea044322010-07-29 02:01:43 +0000781 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
782 : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {}
John McCall6374c332010-03-06 00:35:14 +0000783
784 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
785 return 7;
786 }
787
788 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
789 llvm::Value *Address) const {
790 CodeGen::CGBuilderTy &Builder = CGF.Builder;
791 llvm::LLVMContext &Context = CGF.getLLVMContext();
792
793 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
794 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000795
John McCallaeeb7012010-05-27 06:19:26 +0000796 // 0-15 are the 16 integer registers.
797 // 16 is %rip.
798 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +0000799
800 return false;
801 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000802};
803
Chris Lattnerf13721d2010-08-31 16:44:54 +0000804class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
805public:
806 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
807 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
808
809 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
810 return 7;
811 }
812
813 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
814 llvm::Value *Address) const {
815 CodeGen::CGBuilderTy &Builder = CGF.Builder;
816 llvm::LLVMContext &Context = CGF.getLLVMContext();
817
818 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
819 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
820
821 // 0-15 are the 16 integer registers.
822 // 16 is %rip.
823 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
824
825 return false;
826 }
827};
828
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000829}
830
Chris Lattner1090a9b2010-06-28 21:43:59 +0000831X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000832 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
833 // classified recursively so that always two fields are
834 // considered. The resulting class is calculated according to
835 // the classes of the fields in the eightbyte:
836 //
837 // (a) If both classes are equal, this is the resulting class.
838 //
839 // (b) If one of the classes is NO_CLASS, the resulting class is
840 // the other class.
841 //
842 // (c) If one of the classes is MEMORY, the result is the MEMORY
843 // class.
844 //
845 // (d) If one of the classes is INTEGER, the result is the
846 // INTEGER.
847 //
848 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
849 // MEMORY is used as class.
850 //
851 // (f) Otherwise class SSE is used.
852
853 // Accum should never be memory (we should have returned) or
854 // ComplexX87 (because this cannot be passed in a structure).
855 assert((Accum != Memory && Accum != ComplexX87) &&
856 "Invalid accumulated classification during merge.");
857 if (Accum == Field || Field == NoClass)
858 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000859 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000860 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000861 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000862 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000863 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000864 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000865 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
866 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000867 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000868 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000869}
870
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000871void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000872 Class &Lo, Class &Hi) const {
873 // FIXME: This code can be simplified by introducing a simple value class for
874 // Class pairs with appropriate constructor methods for the various
875 // situations.
876
877 // FIXME: Some of the split computations are wrong; unaligned vectors
878 // shouldn't be passed in registers for example, so there is no chance they
879 // can straddle an eightbyte. Verify & simplify.
880
881 Lo = Hi = NoClass;
882
883 Class &Current = OffsetBase < 64 ? Lo : Hi;
884 Current = Memory;
885
John McCall183700f2009-09-21 23:43:11 +0000886 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000887 BuiltinType::Kind k = BT->getKind();
888
889 if (k == BuiltinType::Void) {
890 Current = NoClass;
891 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
892 Lo = Integer;
893 Hi = Integer;
894 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
895 Current = Integer;
896 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
897 Current = SSE;
898 } else if (k == BuiltinType::LongDouble) {
899 Lo = X87;
900 Hi = X87Up;
901 }
902 // FIXME: _Decimal32 and _Decimal64 are SSE.
903 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +0000904 return;
905 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000906
Chris Lattner1090a9b2010-06-28 21:43:59 +0000907 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000908 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +0000909 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +0000910 return;
911 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000912
Chris Lattner1090a9b2010-06-28 21:43:59 +0000913 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000914 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000915 return;
916 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000917
Chris Lattner1090a9b2010-06-28 21:43:59 +0000918 if (Ty->isMemberPointerType()) {
Daniel Dunbar67d438d2010-05-15 00:00:37 +0000919 if (Ty->isMemberFunctionPointerType())
920 Lo = Hi = Integer;
921 else
922 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000923 return;
924 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000925
Chris Lattner1090a9b2010-06-28 21:43:59 +0000926 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +0000927 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000928 if (Size == 32) {
929 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
930 // float> as integer.
931 Current = Integer;
932
933 // If this type crosses an eightbyte boundary, it should be
934 // split.
935 uint64_t EB_Real = (OffsetBase) / 64;
936 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
937 if (EB_Real != EB_Imag)
938 Hi = Lo;
939 } else if (Size == 64) {
940 // gcc passes <1 x double> in memory. :(
941 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
942 return;
943
944 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +0000945 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +0000946 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
947 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
948 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000949 Current = Integer;
950 else
951 Current = SSE;
952
953 // If this type crosses an eightbyte boundary, it should be
954 // split.
955 if (OffsetBase && OffsetBase != 64)
956 Hi = Lo;
957 } else if (Size == 128) {
958 Lo = SSE;
959 Hi = SSEUp;
960 }
Chris Lattner1090a9b2010-06-28 21:43:59 +0000961 return;
962 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000963
Chris Lattner1090a9b2010-06-28 21:43:59 +0000964 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +0000965 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000966
Chris Lattnerea044322010-07-29 02:01:43 +0000967 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000968 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000969 if (Size <= 64)
970 Current = Integer;
971 else if (Size <= 128)
972 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +0000973 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000974 Current = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +0000975 else if (ET == getContext().DoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000976 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +0000977 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000978 Current = ComplexX87;
979
980 // If this complex type crosses an eightbyte boundary then it
981 // should be split.
982 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +0000983 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000984 if (Hi == NoClass && EB_Real != EB_Imag)
985 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000986
Chris Lattner1090a9b2010-06-28 21:43:59 +0000987 return;
988 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000989
Chris Lattnerea044322010-07-29 02:01:43 +0000990 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000991 // Arrays are treated like structures.
992
Chris Lattnerea044322010-07-29 02:01:43 +0000993 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000994
995 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
996 // than two eightbytes, ..., it has class MEMORY.
997 if (Size > 128)
998 return;
999
1000 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1001 // fields, it has class MEMORY.
1002 //
1003 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001004 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001005 return;
1006
1007 // Otherwise implement simplified merge. We could be smarter about
1008 // this, but it isn't worth it and would be harder to verify.
1009 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001010 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001011 uint64_t ArraySize = AT->getSize().getZExtValue();
1012 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1013 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001014 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001015 Lo = merge(Lo, FieldLo);
1016 Hi = merge(Hi, FieldHi);
1017 if (Lo == Memory || Hi == Memory)
1018 break;
1019 }
1020
1021 // Do post merger cleanup (see below). Only case we worry about is Memory.
1022 if (Hi == Memory)
1023 Lo = Memory;
1024 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001025 return;
1026 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001027
Chris Lattner1090a9b2010-06-28 21:43:59 +00001028 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001029 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001030
1031 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
1032 // than two eightbytes, ..., it has class MEMORY.
1033 if (Size > 128)
1034 return;
1035
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001036 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1037 // copy constructor or a non-trivial destructor, it is passed by invisible
1038 // reference.
1039 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1040 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001041
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001042 const RecordDecl *RD = RT->getDecl();
1043
1044 // Assume variable sized types are passed in memory.
1045 if (RD->hasFlexibleArrayMember())
1046 return;
1047
Chris Lattnerea044322010-07-29 02:01:43 +00001048 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001049
1050 // Reset Lo class, this will be recomputed.
1051 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001052
1053 // If this is a C++ record, classify the bases first.
1054 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1055 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1056 e = CXXRD->bases_end(); i != e; ++i) {
1057 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1058 "Unexpected base class!");
1059 const CXXRecordDecl *Base =
1060 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1061
1062 // Classify this field.
1063 //
1064 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1065 // single eightbyte, each is classified separately. Each eightbyte gets
1066 // initialized to class NO_CLASS.
1067 Class FieldLo, FieldHi;
1068 uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
Chris Lattner9c254f02010-06-29 06:01:59 +00001069 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001070 Lo = merge(Lo, FieldLo);
1071 Hi = merge(Hi, FieldHi);
1072 if (Lo == Memory || Hi == Memory)
1073 break;
1074 }
1075 }
1076
1077 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001078 unsigned idx = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001079 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1080 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001081 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1082 bool BitField = i->isBitField();
1083
1084 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1085 // fields, it has class MEMORY.
1086 //
1087 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001088 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001089 Lo = Memory;
1090 return;
1091 }
1092
1093 // Classify this field.
1094 //
1095 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1096 // exceeds a single eightbyte, each is classified
1097 // separately. Each eightbyte gets initialized to class
1098 // NO_CLASS.
1099 Class FieldLo, FieldHi;
1100
1101 // Bit-fields require special handling, they do not force the
1102 // structure to be passed in memory even if unaligned, and
1103 // therefore they can straddle an eightbyte.
1104 if (BitField) {
1105 // Ignore padding bit-fields.
1106 if (i->isUnnamedBitfield())
1107 continue;
1108
1109 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Chris Lattnerea044322010-07-29 02:01:43 +00001110 uint64_t Size =
1111 i->getBitWidth()->EvaluateAsInt(getContext()).getZExtValue();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001112
1113 uint64_t EB_Lo = Offset / 64;
1114 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1115 FieldLo = FieldHi = NoClass;
1116 if (EB_Lo) {
1117 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1118 FieldLo = NoClass;
1119 FieldHi = Integer;
1120 } else {
1121 FieldLo = Integer;
1122 FieldHi = EB_Hi ? Integer : NoClass;
1123 }
1124 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001125 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001126 Lo = merge(Lo, FieldLo);
1127 Hi = merge(Hi, FieldHi);
1128 if (Lo == Memory || Hi == Memory)
1129 break;
1130 }
1131
1132 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1133 //
1134 // (a) If one of the classes is MEMORY, the whole argument is
1135 // passed in memory.
1136 //
1137 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
1138
1139 // The first of these conditions is guaranteed by how we implement
1140 // the merge (just bail).
1141 //
1142 // The second condition occurs in the case of unions; for example
1143 // union { _Complex double; unsigned; }.
1144 if (Hi == Memory)
1145 Lo = Memory;
1146 if (Hi == SSEUp && Lo != SSE)
1147 Hi = SSE;
1148 }
1149}
1150
Chris Lattner9c254f02010-06-29 06:01:59 +00001151ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001152 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1153 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001154 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001155 // Treat an enum type as its underlying type.
1156 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1157 Ty = EnumTy->getDecl()->getIntegerType();
1158
1159 return (Ty->isPromotableIntegerType() ?
1160 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1161 }
1162
1163 return ABIArgInfo::getIndirect(0);
1164}
1165
Chris Lattner9c254f02010-06-29 06:01:59 +00001166ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001167 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1168 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001169 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001170 // Treat an enum type as its underlying type.
1171 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1172 Ty = EnumTy->getDecl()->getIntegerType();
1173
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001174 return (Ty->isPromotableIntegerType() ?
1175 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001176 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001177
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001178 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1179 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001180
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001181 // Compute the byval alignment. We trust the back-end to honor the
1182 // minimum ABI alignment for byval, to make cleaner IR.
1183 const unsigned MinABIAlign = 8;
Chris Lattnerea044322010-07-29 02:01:43 +00001184 unsigned Align = getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001185 if (Align > MinABIAlign)
1186 return ABIArgInfo::getIndirect(Align);
1187 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001188}
1189
Chris Lattner0f408f52010-07-29 04:56:46 +00001190/// Get16ByteVectorType - The ABI specifies that a value should be passed in an
1191/// full vector XMM register. Pick an LLVM IR type that will be passed as a
1192/// vector register.
1193const llvm::Type *X86_64ABIInfo::Get16ByteVectorType(QualType Ty) const {
Chris Lattner15842bd2010-07-29 05:02:29 +00001194 const llvm::Type *IRType = CGT.ConvertTypeRecursive(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001195
Chris Lattner15842bd2010-07-29 05:02:29 +00001196 // Wrapper structs that just contain vectors are passed just like vectors,
1197 // strip them off if present.
1198 const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
1199 while (STy && STy->getNumElements() == 1) {
1200 IRType = STy->getElementType(0);
1201 STy = dyn_cast<llvm::StructType>(IRType);
1202 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001203
Chris Lattner0f408f52010-07-29 04:56:46 +00001204 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner15842bd2010-07-29 05:02:29 +00001205 if (const llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
Chris Lattner0f408f52010-07-29 04:56:46 +00001206 const llvm::Type *EltTy = VT->getElementType();
1207 if (VT->getBitWidth() == 128 &&
1208 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1209 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1210 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1211 EltTy->isIntegerTy(128)))
1212 return VT;
1213 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001214
Chris Lattner0f408f52010-07-29 04:56:46 +00001215 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1216}
1217
Chris Lattnere2962be2010-07-29 07:30:00 +00001218/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1219/// is known to either be off the end of the specified type or being in
1220/// alignment padding. The user type specified is known to be at most 128 bits
1221/// in size, and have passed through X86_64ABIInfo::classify with a successful
1222/// classification that put one of the two halves in the INTEGER class.
1223///
1224/// It is conservatively correct to return false.
1225static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1226 unsigned EndBit, ASTContext &Context) {
1227 // If the bytes being queried are off the end of the type, there is no user
1228 // data hiding here. This handles analysis of builtins, vectors and other
1229 // types that don't contain interesting padding.
1230 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1231 if (TySize <= StartBit)
1232 return true;
1233
Chris Lattner021c3a32010-07-29 07:43:55 +00001234 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1235 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1236 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1237
1238 // Check each element to see if the element overlaps with the queried range.
1239 for (unsigned i = 0; i != NumElts; ++i) {
1240 // If the element is after the span we care about, then we're done..
1241 unsigned EltOffset = i*EltSize;
1242 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001243
Chris Lattner021c3a32010-07-29 07:43:55 +00001244 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1245 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1246 EndBit-EltOffset, Context))
1247 return false;
1248 }
1249 // If it overlaps no elements, then it is safe to process as padding.
1250 return true;
1251 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001252
Chris Lattnere2962be2010-07-29 07:30:00 +00001253 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1254 const RecordDecl *RD = RT->getDecl();
1255 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001256
Chris Lattnere2962be2010-07-29 07:30:00 +00001257 // If this is a C++ record, check the bases first.
1258 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1259 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1260 e = CXXRD->bases_end(); i != e; ++i) {
1261 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1262 "Unexpected base class!");
1263 const CXXRecordDecl *Base =
1264 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001265
Chris Lattnere2962be2010-07-29 07:30:00 +00001266 // If the base is after the span we care about, ignore it.
1267 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffset(Base);
1268 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001269
Chris Lattnere2962be2010-07-29 07:30:00 +00001270 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1271 if (!BitsContainNoUserData(i->getType(), BaseStart,
1272 EndBit-BaseOffset, Context))
1273 return false;
1274 }
1275 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001276
Chris Lattnere2962be2010-07-29 07:30:00 +00001277 // Verify that no field has data that overlaps the region of interest. Yes
1278 // this could be sped up a lot by being smarter about queried fields,
1279 // however we're only looking at structs up to 16 bytes, so we don't care
1280 // much.
1281 unsigned idx = 0;
1282 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1283 i != e; ++i, ++idx) {
1284 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001285
Chris Lattnere2962be2010-07-29 07:30:00 +00001286 // If we found a field after the region we care about, then we're done.
1287 if (FieldOffset >= EndBit) break;
1288
1289 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1290 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1291 Context))
1292 return false;
1293 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001294
Chris Lattnere2962be2010-07-29 07:30:00 +00001295 // If nothing in this record overlapped the area of interest, then we're
1296 // clean.
1297 return true;
1298 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001299
Chris Lattnere2962be2010-07-29 07:30:00 +00001300 return false;
1301}
1302
Chris Lattner0b362002010-07-29 18:39:32 +00001303/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1304/// float member at the specified offset. For example, {int,{float}} has a
1305/// float at offset 4. It is conservatively correct for this routine to return
1306/// false.
1307static bool ContainsFloatAtOffset(const llvm::Type *IRType, unsigned IROffset,
1308 const llvm::TargetData &TD) {
1309 // Base case if we find a float.
1310 if (IROffset == 0 && IRType->isFloatTy())
1311 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001312
Chris Lattner0b362002010-07-29 18:39:32 +00001313 // If this is a struct, recurse into the field at the specified offset.
1314 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
1315 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1316 unsigned Elt = SL->getElementContainingOffset(IROffset);
1317 IROffset -= SL->getElementOffset(Elt);
1318 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1319 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001320
Chris Lattner0b362002010-07-29 18:39:32 +00001321 // If this is an array, recurse into the field at the specified offset.
1322 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1323 const llvm::Type *EltTy = ATy->getElementType();
1324 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1325 IROffset -= IROffset/EltSize*EltSize;
1326 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1327 }
1328
1329 return false;
1330}
1331
Chris Lattnerf47c9442010-07-29 18:13:09 +00001332
1333/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1334/// low 8 bytes of an XMM register, corresponding to the SSE class.
1335const llvm::Type *X86_64ABIInfo::
1336GetSSETypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
1337 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001338 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001339 // pass as float if the last 4 bytes is just padding. This happens for
1340 // structs that contain 3 floats.
1341 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1342 SourceOffset*8+64, getContext()))
1343 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001344
Chris Lattner0b362002010-07-29 18:39:32 +00001345 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1346 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1347 // case.
1348 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001349 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1350 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001351
Chris Lattnerf47c9442010-07-29 18:13:09 +00001352 return llvm::Type::getDoubleTy(getVMContext());
1353}
1354
1355
Chris Lattner0d2656d2010-07-29 17:40:35 +00001356/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1357/// an 8-byte GPR. This means that we either have a scalar or we are talking
1358/// about the high or low part of an up-to-16-byte struct. This routine picks
1359/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001360/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1361/// etc).
1362///
1363/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1364/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1365/// the 8-byte value references. PrefType may be null.
1366///
1367/// SourceTy is the source level type for the entire argument. SourceOffset is
1368/// an offset into this that we're processing (which is always either 0 or 8).
1369///
Chris Lattner44f0fd22010-07-29 02:20:19 +00001370const llvm::Type *X86_64ABIInfo::
Chris Lattner0d2656d2010-07-29 17:40:35 +00001371GetINTEGERTypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
1372 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001373 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1374 // returning an 8-byte unit starting with it. See if we can safely use it.
1375 if (IROffset == 0) {
1376 // Pointers and int64's always fill the 8-byte unit.
1377 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1378 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001379
Chris Lattnere2962be2010-07-29 07:30:00 +00001380 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1381 // goodness in the source type is just tail padding. This is allowed to
1382 // kick in for struct {double,int} on the int, but not on
1383 // struct{double,int,int} because we wouldn't return the second int. We
1384 // have to do this analysis on the source type because we can't depend on
1385 // unions being lowered a specific way etc.
1386 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1387 IRType->isIntegerTy(32)) {
1388 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001389
Chris Lattnere2962be2010-07-29 07:30:00 +00001390 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1391 SourceOffset*8+64, getContext()))
1392 return IRType;
1393 }
1394 }
Chris Lattner49382de2010-07-28 22:44:07 +00001395
Chris Lattnerfe12d1e2010-07-29 04:51:12 +00001396 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001397 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner44f0fd22010-07-29 02:20:19 +00001398 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001399 if (IROffset < SL->getSizeInBytes()) {
1400 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1401 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001402
Chris Lattner0d2656d2010-07-29 17:40:35 +00001403 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1404 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001405 }
Chris Lattner49382de2010-07-28 22:44:07 +00001406 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001407
Chris Lattner021c3a32010-07-29 07:43:55 +00001408 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1409 const llvm::Type *EltTy = ATy->getElementType();
1410 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1411 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00001412 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1413 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00001414 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001415
Chris Lattner49382de2010-07-28 22:44:07 +00001416 // Okay, we don't have any better idea of what to pass, so we pass this in an
1417 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001418 unsigned TySizeInBytes =
1419 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00001420
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001421 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001422
Chris Lattner49382de2010-07-28 22:44:07 +00001423 // It is always safe to classify this as an integer type up to i64 that
1424 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001425 return llvm::IntegerType::get(getVMContext(),
1426 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00001427}
1428
Chris Lattner66e7b682010-09-01 00:50:20 +00001429
1430/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1431/// be used as elements of a two register pair to pass or return, return a
1432/// first class aggregate to represent them. For example, if the low part of
1433/// a by-value argument should be passed as i32* and the high part as float,
1434/// return {i32*, float}.
1435static const llvm::Type *
1436GetX86_64ByValArgumentPair(const llvm::Type *Lo, const llvm::Type *Hi,
1437 const llvm::TargetData &TD) {
1438 // In order to correctly satisfy the ABI, we need to the high part to start
1439 // at offset 8. If the high and low parts we inferred are both 4-byte types
1440 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1441 // the second element at offset 8. Check for this:
1442 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1443 unsigned HiAlign = TD.getABITypeAlignment(Hi);
1444 unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
1445 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
1446
1447 // To handle this, we have to increase the size of the low part so that the
1448 // second element will start at an 8 byte offset. We can't increase the size
1449 // of the second element because it might make us access off the end of the
1450 // struct.
1451 if (HiStart != 8) {
1452 // There are only two sorts of types the ABI generation code can produce for
1453 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1454 // Promote these to a larger type.
1455 if (Lo->isFloatTy())
1456 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1457 else {
1458 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1459 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1460 }
1461 }
1462
1463 const llvm::StructType *Result =
1464 llvm::StructType::get(Lo->getContext(), Lo, Hi, NULL);
1465
1466
1467 // Verify that the second element is at an 8-byte offset.
1468 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1469 "Invalid x86-64 argument pair!");
1470 return Result;
1471}
1472
Chris Lattner519f68c2010-07-28 23:06:14 +00001473ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00001474classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00001475 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1476 // classification algorithm.
1477 X86_64ABIInfo::Class Lo, Hi;
1478 classify(RetTy, 0, Lo, Hi);
1479
1480 // Check some invariants.
1481 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001482 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1483
1484 const llvm::Type *ResType = 0;
1485 switch (Lo) {
1486 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001487 if (Hi == NoClass)
1488 return ABIArgInfo::getIgnore();
1489 // If the low part is just padding, it takes no register, leave ResType
1490 // null.
1491 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1492 "Unknown missing lo part");
1493 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001494
1495 case SSEUp:
1496 case X87Up:
1497 assert(0 && "Invalid classification for lo word.");
1498
1499 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1500 // hidden argument.
1501 case Memory:
1502 return getIndirectReturnResult(RetTy);
1503
1504 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1505 // available register of the sequence %rax, %rdx is used.
1506 case Integer:
Chris Lattner0d2656d2010-07-29 17:40:35 +00001507 ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0,
1508 RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001509
Chris Lattnereb518b42010-07-29 21:42:50 +00001510 // If we have a sign or zero extended integer, make sure to return Extend
1511 // so that the parameter gets the right LLVM IR attributes.
1512 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1513 // Treat an enum type as its underlying type.
1514 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1515 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001516
Chris Lattnereb518b42010-07-29 21:42:50 +00001517 if (RetTy->isIntegralOrEnumerationType() &&
1518 RetTy->isPromotableIntegerType())
1519 return ABIArgInfo::getExtend();
1520 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001521 break;
1522
1523 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1524 // available SSE register of the sequence %xmm0, %xmm1 is used.
1525 case SSE:
Chris Lattnerf47c9442010-07-29 18:13:09 +00001526 ResType = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00001527 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001528
1529 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1530 // returned on the X87 stack in %st0 as 80-bit x87 number.
1531 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00001532 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00001533 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001534
1535 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1536 // part of the value is returned in %st0 and the imaginary part in
1537 // %st1.
1538 case ComplexX87:
1539 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattnera3c109b2010-07-29 02:16:43 +00001540 ResType = llvm::StructType::get(getVMContext(),
Chris Lattnerea044322010-07-29 02:01:43 +00001541 llvm::Type::getX86_FP80Ty(getVMContext()),
1542 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00001543 NULL);
1544 break;
1545 }
1546
Chris Lattner3db4dde2010-09-01 00:20:33 +00001547 const llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001548 switch (Hi) {
1549 // Memory was handled previously and X87 should
1550 // never occur as a hi class.
1551 case Memory:
1552 case X87:
1553 assert(0 && "Invalid classification for hi word.");
1554
1555 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00001556 case NoClass:
1557 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001558
Chris Lattner3db4dde2010-09-01 00:20:33 +00001559 case Integer:
1560 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy),
1561 8, RetTy, 8);
1562 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1563 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001564 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00001565 case SSE:
1566 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 8, RetTy, 8);
1567 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1568 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001569 break;
1570
1571 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1572 // is passed in the upper half of the last used SSE register.
1573 //
1574 // SSEUP should always be preceeded by SSE, just widen.
1575 case SSEUp:
1576 assert(Lo == SSE && "Unexpected SSEUp classification.");
Chris Lattner0f408f52010-07-29 04:56:46 +00001577 ResType = Get16ByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00001578 break;
1579
1580 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1581 // returned together with the previous X87 value in %st0.
1582 case X87Up:
1583 // If X87Up is preceeded by X87, we don't need to do
1584 // anything. However, in some cases with unions it may not be
1585 // preceeded by X87. In such situations we follow gcc and pass the
1586 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00001587 if (Lo != X87) {
Chris Lattner3db4dde2010-09-01 00:20:33 +00001588 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy),
1589 8, RetTy, 8);
1590 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1591 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00001592 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001593 break;
1594 }
Chris Lattner3db4dde2010-09-01 00:20:33 +00001595
1596 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00001597 // known to pass in the high eightbyte of the result. We do this by forming a
1598 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00001599 if (HighPart)
1600 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner519f68c2010-07-28 23:06:14 +00001601
Chris Lattnereb518b42010-07-29 21:42:50 +00001602 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00001603}
1604
Chris Lattnera3c109b2010-07-29 02:16:43 +00001605ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Chris Lattner5868ca22010-07-29 04:41:05 +00001606 unsigned &neededSSE) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001607 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001608 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001609
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001610 // Check some invariants.
1611 // FIXME: Enforce these by construction.
1612 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001613 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1614
1615 neededInt = 0;
1616 neededSSE = 0;
1617 const llvm::Type *ResType = 0;
1618 switch (Lo) {
1619 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001620 if (Hi == NoClass)
1621 return ABIArgInfo::getIgnore();
1622 // If the low part is just padding, it takes no register, leave ResType
1623 // null.
1624 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1625 "Unknown missing lo part");
1626 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001627
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001628 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1629 // on the stack.
1630 case Memory:
1631
1632 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1633 // COMPLEX_X87, it is passed in memory.
1634 case X87:
1635 case ComplexX87:
Chris Lattner9c254f02010-06-29 06:01:59 +00001636 return getIndirectResult(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001637
1638 case SSEUp:
1639 case X87Up:
1640 assert(0 && "Invalid classification for lo word.");
1641
1642 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1643 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1644 // and %r9 is used.
1645 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00001646 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001647
Chris Lattner49382de2010-07-28 22:44:07 +00001648 // Pick an 8-byte type based on the preferred type.
Chris Lattner0d2656d2010-07-29 17:40:35 +00001649 ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00001650
1651 // If we have a sign or zero extended integer, make sure to return Extend
1652 // so that the parameter gets the right LLVM IR attributes.
1653 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1654 // Treat an enum type as its underlying type.
1655 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1656 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001657
Chris Lattnereb518b42010-07-29 21:42:50 +00001658 if (Ty->isIntegralOrEnumerationType() &&
1659 Ty->isPromotableIntegerType())
1660 return ABIArgInfo::getExtend();
1661 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001662
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001663 break;
1664
1665 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1666 // available SSE register is used, the registers are taken in the
1667 // order from %xmm0 to %xmm7.
1668 case SSE:
1669 ++neededSSE;
Chris Lattnerf47c9442010-07-29 18:13:09 +00001670 ResType = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(Ty), 0, Ty, 0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001671 break;
1672 }
1673
Chris Lattner645406a2010-09-01 00:24:35 +00001674 const llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001675 switch (Hi) {
1676 // Memory was handled previously, ComplexX87 and X87 should
1677 // never occur as hi classes, and X87Up must be preceed by X87,
1678 // which is passed in memory.
1679 case Memory:
1680 case X87:
1681 case ComplexX87:
1682 assert(0 && "Invalid classification for hi word.");
1683 break;
1684
1685 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001686
Chris Lattner645406a2010-09-01 00:24:35 +00001687 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001688 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00001689 // Pick an 8-byte type based on the preferred type.
Chris Lattner645406a2010-09-01 00:24:35 +00001690 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001691
Chris Lattner645406a2010-09-01 00:24:35 +00001692 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1693 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001694 break;
1695
1696 // X87Up generally doesn't occur here (long double is passed in
1697 // memory), except in situations involving unions.
1698 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00001699 case SSE:
1700 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001701
Chris Lattner645406a2010-09-01 00:24:35 +00001702 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1703 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00001704
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001705 ++neededSSE;
1706 break;
1707
1708 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1709 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001710 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001711 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00001712 assert(Lo == SSE && "Unexpected SSEUp classification");
Chris Lattner0f408f52010-07-29 04:56:46 +00001713 ResType = Get16ByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001714 break;
1715 }
1716
Chris Lattner645406a2010-09-01 00:24:35 +00001717 // If a high part was specified, merge it together with the low part. It is
1718 // known to pass in the high eightbyte of the result. We do this by forming a
1719 // first class struct aggregate with the high and low part: {low, high}
1720 if (HighPart)
Chris Lattner66e7b682010-09-01 00:50:20 +00001721 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner645406a2010-09-01 00:24:35 +00001722
Chris Lattnereb518b42010-07-29 21:42:50 +00001723 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001724}
1725
Chris Lattneree5dcd02010-07-29 02:31:05 +00001726void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001727
Chris Lattnera3c109b2010-07-29 02:16:43 +00001728 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001729
1730 // Keep track of the number of assigned registers.
1731 unsigned freeIntRegs = 6, freeSSERegs = 8;
1732
1733 // If the return value is indirect, then the hidden argument is consuming one
1734 // integer register.
1735 if (FI.getReturnInfo().isIndirect())
1736 --freeIntRegs;
1737
1738 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1739 // get assigned (in left-to-right order) for passing as follows...
1740 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1741 it != ie; ++it) {
1742 unsigned neededInt, neededSSE;
Chris Lattner5868ca22010-07-29 04:41:05 +00001743 it->info = classifyArgumentType(it->type, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001744
1745 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1746 // eightbyte of an argument, the whole argument is passed on the
1747 // stack. If registers have already been assigned for some
1748 // eightbytes of such an argument, the assignments get reverted.
1749 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1750 freeIntRegs -= neededInt;
1751 freeSSERegs -= neededSSE;
1752 } else {
Chris Lattner9c254f02010-06-29 06:01:59 +00001753 it->info = getIndirectResult(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001754 }
1755 }
1756}
1757
1758static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1759 QualType Ty,
1760 CodeGenFunction &CGF) {
1761 llvm::Value *overflow_arg_area_p =
1762 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1763 llvm::Value *overflow_arg_area =
1764 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1765
1766 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1767 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1768 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1769 if (Align > 8) {
1770 // Note that we follow the ABI & gcc here, even though the type
1771 // could in theory have an alignment greater than 16. This case
1772 // shouldn't ever matter in practice.
1773
1774 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson0032b272009-08-13 21:57:51 +00001775 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001776 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001777 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1778 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00001779 CGF.Int64Ty);
1780 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001781 overflow_arg_area =
1782 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1783 overflow_arg_area->getType(),
1784 "overflow_arg_area.align");
1785 }
1786
1787 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1788 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1789 llvm::Value *Res =
1790 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001791 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001792
1793 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1794 // l->overflow_arg_area + sizeof(type).
1795 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1796 // an 8 byte boundary.
1797
1798 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00001799 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001800 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001801 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1802 "overflow_arg_area.next");
1803 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1804
1805 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1806 return Res;
1807}
1808
1809llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1810 CodeGenFunction &CGF) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001811 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001812
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001813 // Assume that va_list type is correct; should be pointer to LLVM type:
1814 // struct {
1815 // i32 gp_offset;
1816 // i32 fp_offset;
1817 // i8* overflow_arg_area;
1818 // i8* reg_save_area;
1819 // };
1820 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001821
Chris Lattnera14db752010-03-11 18:19:55 +00001822 Ty = CGF.getContext().getCanonicalType(Ty);
Chris Lattner5868ca22010-07-29 04:41:05 +00001823 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001824
1825 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1826 // in the registers. If not go to step 7.
1827 if (!neededInt && !neededSSE)
1828 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1829
1830 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1831 // general purpose registers needed to pass type and num_fp to hold
1832 // the number of floating point registers needed.
1833
1834 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1835 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1836 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1837 //
1838 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1839 // register save space).
1840
1841 llvm::Value *InRegs = 0;
1842 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1843 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1844 if (neededInt) {
1845 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1846 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001847 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
1848 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001849 }
1850
1851 if (neededSSE) {
1852 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1853 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1854 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00001855 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
1856 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001857 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1858 }
1859
1860 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1861 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1862 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1863 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1864
1865 // Emit code to load the value if it was passed in registers.
1866
1867 CGF.EmitBlock(InRegBlock);
1868
1869 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1870 // an offset of l->gp_offset and/or l->fp_offset. This may require
1871 // copying to a temporary location in case the parameter is passed
1872 // in different register classes or requires an alignment greater
1873 // than 8 for general purpose registers and 16 for XMM registers.
1874 //
1875 // FIXME: This really results in shameful code when we end up needing to
1876 // collect arguments from different places; often what should result in a
1877 // simple assembling of a structure from scattered addresses has many more
1878 // loads than necessary. Can we clean this up?
1879 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1880 llvm::Value *RegAddr =
1881 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1882 "reg_save_area");
1883 if (neededInt && neededSSE) {
1884 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00001885 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001886 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1887 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1888 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1889 const llvm::Type *TyLo = ST->getElementType(0);
1890 const llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00001891 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001892 "Unexpected ABI info for mixed regs");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001893 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1894 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001895 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1896 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00001897 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1898 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001899 llvm::Value *V =
1900 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1901 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1902 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1903 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1904
Owen Andersona1cf15f2009-07-14 23:10:40 +00001905 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001906 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001907 } else if (neededInt) {
1908 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1909 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001910 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001911 } else if (neededSSE == 1) {
1912 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1913 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1914 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001915 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001916 assert(neededSSE == 2 && "Invalid number of needed registers!");
1917 // SSE registers are spaced 16 bytes apart in the register save
1918 // area, we need to collect the two eightbytes together.
1919 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001920 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001921 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
1922 const llvm::Type *DblPtrTy =
1923 llvm::PointerType::getUnqual(DoubleTy);
1924 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1925 DoubleTy, NULL);
1926 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1927 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1928 DblPtrTy));
1929 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1930 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1931 DblPtrTy));
1932 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1933 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1934 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001935 }
1936
1937 // AMD64-ABI 3.5.7p5: Step 5. Set:
1938 // l->gp_offset = l->gp_offset + num_gp * 8
1939 // l->fp_offset = l->fp_offset + num_fp * 16.
1940 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00001941 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001942 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1943 gp_offset_p);
1944 }
1945 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00001946 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001947 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1948 fp_offset_p);
1949 }
1950 CGF.EmitBranch(ContBlock);
1951
1952 // Emit code to load the value if it was passed in memory.
1953
1954 CGF.EmitBlock(InMemBlock);
1955 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1956
1957 // Return the appropriate result.
1958
1959 CGF.EmitBlock(ContBlock);
1960 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1961 "vaarg.addr");
1962 ResAddr->reserveOperandSpace(2);
1963 ResAddr->addIncoming(RegAddr, InRegBlock);
1964 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001965 return ResAddr;
1966}
1967
Chris Lattnerf13721d2010-08-31 16:44:54 +00001968llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1969 CodeGenFunction &CGF) const {
1970 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
1971 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001972
Chris Lattnerf13721d2010-08-31 16:44:54 +00001973 CGBuilderTy &Builder = CGF.Builder;
1974 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1975 "ap");
1976 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1977 llvm::Type *PTy =
1978 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1979 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1980
1981 uint64_t Offset =
1982 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
1983 llvm::Value *NextAddr =
1984 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
1985 "ap.next");
1986 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1987
1988 return AddrTyped;
1989}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001990
1991//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001992// PIC16 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001993//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001994
1995namespace {
1996
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001997class PIC16ABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +00001998public:
1999 PIC16ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002000
Chris Lattnera3c109b2010-07-29 02:16:43 +00002001 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002002
Chris Lattnera3c109b2010-07-29 02:16:43 +00002003 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002004
Chris Lattneree5dcd02010-07-29 02:31:05 +00002005 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002006 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002007 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2008 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +00002009 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002010 }
2011
2012 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2013 CodeGenFunction &CGF) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002014};
2015
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002016class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
2017public:
Chris Lattnerea044322010-07-29 02:01:43 +00002018 PIC16TargetCodeGenInfo(CodeGenTypes &CGT)
2019 : TargetCodeGenInfo(new PIC16ABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002020};
2021
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002022}
2023
Chris Lattnera3c109b2010-07-29 02:16:43 +00002024ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002025 if (RetTy->isVoidType()) {
2026 return ABIArgInfo::getIgnore();
2027 } else {
2028 return ABIArgInfo::getDirect();
2029 }
2030}
2031
Chris Lattnera3c109b2010-07-29 02:16:43 +00002032ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002033 return ABIArgInfo::getDirect();
2034}
2035
2036llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00002037 CodeGenFunction &CGF) const {
Chris Lattner52d9ae32010-04-06 17:29:22 +00002038 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Sanjiv Guptaa446ecd2010-02-17 02:25:52 +00002039 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
2040
2041 CGBuilderTy &Builder = CGF.Builder;
2042 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2043 "ap");
2044 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2045 llvm::Type *PTy =
2046 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2047 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2048
2049 uint64_t Offset = CGF.getContext().getTypeSize(Ty) / 8;
2050
2051 llvm::Value *NextAddr =
2052 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
2053 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
2054 "ap.next");
2055 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2056
2057 return AddrTyped;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002058}
2059
Sanjiv Guptaa446ecd2010-02-17 02:25:52 +00002060
John McCallec853ba2010-03-11 00:10:12 +00002061// PowerPC-32
2062
2063namespace {
2064class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2065public:
Chris Lattnerea044322010-07-29 02:01:43 +00002066 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002067
John McCallec853ba2010-03-11 00:10:12 +00002068 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2069 // This is recovered from gcc output.
2070 return 1; // r1 is the dedicated stack pointer
2071 }
2072
2073 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002074 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002075};
2076
2077}
2078
2079bool
2080PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2081 llvm::Value *Address) const {
2082 // This is calculated from the LLVM and GCC tables and verified
2083 // against gcc output. AFAIK all ABIs use the same encoding.
2084
2085 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2086 llvm::LLVMContext &Context = CGF.getLLVMContext();
2087
2088 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2089 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2090 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2091 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2092
2093 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002094 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002095
2096 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002097 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002098
2099 // 64-76 are various 4-byte special-purpose registers:
2100 // 64: mq
2101 // 65: lr
2102 // 66: ctr
2103 // 67: ap
2104 // 68-75 cr0-7
2105 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002106 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002107
2108 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002109 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002110
2111 // 109: vrsave
2112 // 110: vscr
2113 // 111: spe_acc
2114 // 112: spefscr
2115 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002116 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002117
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002118 return false;
John McCallec853ba2010-03-11 00:10:12 +00002119}
2120
2121
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002122//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002123// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002124//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002125
2126namespace {
2127
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002128class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002129public:
2130 enum ABIKind {
2131 APCS = 0,
2132 AAPCS = 1,
2133 AAPCS_VFP
2134 };
2135
2136private:
2137 ABIKind Kind;
2138
2139public:
Chris Lattnerea044322010-07-29 02:01:43 +00002140 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002141
2142private:
2143 ABIKind getABIKind() const { return Kind; }
2144
Chris Lattnera3c109b2010-07-29 02:16:43 +00002145 ABIArgInfo classifyReturnType(QualType RetTy) const;
2146 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002147
Chris Lattneree5dcd02010-07-29 02:31:05 +00002148 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002149
2150 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2151 CodeGenFunction &CGF) const;
2152};
2153
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002154class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2155public:
Chris Lattnerea044322010-07-29 02:01:43 +00002156 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2157 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00002158
2159 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2160 return 13;
2161 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002162};
2163
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002164}
2165
Chris Lattneree5dcd02010-07-29 02:31:05 +00002166void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002167 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002168 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattnera3c109b2010-07-29 02:16:43 +00002169 it != ie; ++it)
2170 it->info = classifyArgumentType(it->type);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002171
Chris Lattnera3c109b2010-07-29 02:16:43 +00002172 const llvm::Triple &Triple(getContext().Target.getTriple());
Rafael Espindola25117ab2010-06-16 16:13:39 +00002173 llvm::CallingConv::ID DefaultCC;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00002174 if (Triple.getEnvironmentName() == "gnueabi" ||
2175 Triple.getEnvironmentName() == "eabi")
Rafael Espindola25117ab2010-06-16 16:13:39 +00002176 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00002177 else
2178 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00002179
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002180 switch (getABIKind()) {
2181 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002182 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2183 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002184 break;
2185
2186 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002187 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2188 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002189 break;
2190
2191 case AAPCS_VFP:
2192 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
2193 break;
2194 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002195}
2196
Chris Lattnera3c109b2010-07-29 02:16:43 +00002197ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +00002198 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002199 // Treat an enum type as its underlying type.
2200 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2201 Ty = EnumTy->getDecl()->getIntegerType();
2202
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002203 return (Ty->isPromotableIntegerType() ?
2204 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002205 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002206
Daniel Dunbar42025572009-09-14 21:54:03 +00002207 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002208 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00002209 return ABIArgInfo::getIgnore();
2210
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002211 // Structures with either a non-trivial destructor or a non-trivial
2212 // copy constructor are always indirect.
2213 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2214 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2215
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002216 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
2217 // backend doesn't support byval.
2218 // FIXME: This doesn't handle alignment > 64 bits.
2219 const llvm::Type* ElemTy;
2220 unsigned SizeRegs;
Chris Lattnera3c109b2010-07-29 02:16:43 +00002221 if (getContext().getTypeAlign(Ty) > 32) {
2222 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2223 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002224 } else {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002225 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2226 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002227 }
2228 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson96e0fc72009-07-29 22:16:19 +00002229 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Chris Lattnera3c109b2010-07-29 02:16:43 +00002230 const llvm::Type* STy = llvm::StructType::get(getVMContext(), LLVMFields,
2231 true);
Chris Lattner800588f2010-07-29 06:26:06 +00002232 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002233}
2234
Chris Lattnera3c109b2010-07-29 02:16:43 +00002235static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00002236 llvm::LLVMContext &VMContext) {
2237 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2238 // is called integer-like if its size is less than or equal to one word, and
2239 // the offset of each of its addressable sub-fields is zero.
2240
2241 uint64_t Size = Context.getTypeSize(Ty);
2242
2243 // Check that the type fits in a word.
2244 if (Size > 32)
2245 return false;
2246
2247 // FIXME: Handle vector types!
2248 if (Ty->isVectorType())
2249 return false;
2250
Daniel Dunbarb0d58192009-09-14 02:20:34 +00002251 // Float types are never treated as "integer like".
2252 if (Ty->isRealFloatingType())
2253 return false;
2254
Daniel Dunbar98303b92009-09-13 08:03:58 +00002255 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00002256 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00002257 return true;
2258
Daniel Dunbar45815812010-02-01 23:31:26 +00002259 // Small complex integer types are "integer like".
2260 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2261 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002262
2263 // Single element and zero sized arrays should be allowed, by the definition
2264 // above, but they are not.
2265
2266 // Otherwise, it must be a record type.
2267 const RecordType *RT = Ty->getAs<RecordType>();
2268 if (!RT) return false;
2269
2270 // Ignore records with flexible arrays.
2271 const RecordDecl *RD = RT->getDecl();
2272 if (RD->hasFlexibleArrayMember())
2273 return false;
2274
2275 // Check that all sub-fields are at offset 0, and are themselves "integer
2276 // like".
2277 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2278
2279 bool HadField = false;
2280 unsigned idx = 0;
2281 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2282 i != e; ++i, ++idx) {
2283 const FieldDecl *FD = *i;
2284
Daniel Dunbar679855a2010-01-29 03:22:29 +00002285 // Bit-fields are not addressable, we only need to verify they are "integer
2286 // like". We still have to disallow a subsequent non-bitfield, for example:
2287 // struct { int : 0; int x }
2288 // is non-integer like according to gcc.
2289 if (FD->isBitField()) {
2290 if (!RD->isUnion())
2291 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002292
Daniel Dunbar679855a2010-01-29 03:22:29 +00002293 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2294 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002295
Daniel Dunbar679855a2010-01-29 03:22:29 +00002296 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002297 }
2298
Daniel Dunbar679855a2010-01-29 03:22:29 +00002299 // Check if this field is at offset 0.
2300 if (Layout.getFieldOffset(idx) != 0)
2301 return false;
2302
Daniel Dunbar98303b92009-09-13 08:03:58 +00002303 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2304 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002305
Daniel Dunbar679855a2010-01-29 03:22:29 +00002306 // Only allow at most one field in a structure. This doesn't match the
2307 // wording above, but follows gcc in situations with a field following an
2308 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00002309 if (!RD->isUnion()) {
2310 if (HadField)
2311 return false;
2312
2313 HadField = true;
2314 }
2315 }
2316
2317 return true;
2318}
2319
Chris Lattnera3c109b2010-07-29 02:16:43 +00002320ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002321 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002322 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00002323
John McCalld608cdb2010-08-22 10:59:02 +00002324 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002325 // Treat an enum type as its underlying type.
2326 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2327 RetTy = EnumTy->getDecl()->getIntegerType();
2328
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002329 return (RetTy->isPromotableIntegerType() ?
2330 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002331 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002332
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002333 // Structures with either a non-trivial destructor or a non-trivial
2334 // copy constructor are always indirect.
2335 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2336 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2337
Daniel Dunbar98303b92009-09-13 08:03:58 +00002338 // Are we following APCS?
2339 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002340 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00002341 return ABIArgInfo::getIgnore();
2342
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002343 // Complex types are all returned as packed integers.
2344 //
2345 // FIXME: Consider using 2 x vector types if the back end handles them
2346 // correctly.
2347 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00002348 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00002349 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002350
Daniel Dunbar98303b92009-09-13 08:03:58 +00002351 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002352 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002353 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002354 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002355 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002356 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002357 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002358 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2359 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002360 }
2361
2362 // Otherwise return in memory.
2363 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002364 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002365
2366 // Otherwise this is an AAPCS variant.
2367
Chris Lattnera3c109b2010-07-29 02:16:43 +00002368 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00002369 return ABIArgInfo::getIgnore();
2370
Daniel Dunbar98303b92009-09-13 08:03:58 +00002371 // Aggregates <= 4 bytes are returned in r0; other aggregates
2372 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002373 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00002374 if (Size <= 32) {
2375 // Return in the smallest viable integer type.
2376 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002377 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002378 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002379 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2380 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002381 }
2382
Daniel Dunbar98303b92009-09-13 08:03:58 +00002383 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002384}
2385
2386llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00002387 CodeGenFunction &CGF) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002388 // FIXME: Need to handle alignment
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00002389 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002390 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002391
2392 CGBuilderTy &Builder = CGF.Builder;
2393 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2394 "ap");
2395 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2396 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00002397 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002398 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2399
2400 uint64_t Offset =
2401 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2402 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00002403 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002404 "ap.next");
2405 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2406
2407 return AddrTyped;
2408}
2409
Chris Lattnera3c109b2010-07-29 02:16:43 +00002410ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
2411 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002412 return ABIArgInfo::getIgnore();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002413
John McCalld608cdb2010-08-22 10:59:02 +00002414 if (isAggregateTypeForABI(RetTy))
Chris Lattnera3c109b2010-07-29 02:16:43 +00002415 return ABIArgInfo::getIndirect(0);
2416
2417 // Treat an enum type as its underlying type.
2418 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2419 RetTy = EnumTy->getDecl()->getIntegerType();
2420
2421 return (RetTy->isPromotableIntegerType() ?
2422 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002423}
2424
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002425//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002426// SystemZ ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002427//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002428
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002429namespace {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002430
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002431class SystemZABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +00002432public:
2433 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2434
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002435 bool isPromotableIntegerType(QualType Ty) const;
2436
Chris Lattnera3c109b2010-07-29 02:16:43 +00002437 ABIArgInfo classifyReturnType(QualType RetTy) const;
2438 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002439
Chris Lattneree5dcd02010-07-29 02:31:05 +00002440 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002441 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002442 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2443 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +00002444 it->info = classifyArgumentType(it->type);
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002445 }
2446
2447 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2448 CodeGenFunction &CGF) const;
2449};
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002450
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002451class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2452public:
Chris Lattnerea044322010-07-29 02:01:43 +00002453 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
2454 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002455};
2456
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002457}
2458
2459bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2460 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall183700f2009-09-21 23:43:11 +00002461 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002462 switch (BT->getKind()) {
2463 case BuiltinType::Bool:
2464 case BuiltinType::Char_S:
2465 case BuiltinType::Char_U:
2466 case BuiltinType::SChar:
2467 case BuiltinType::UChar:
2468 case BuiltinType::Short:
2469 case BuiltinType::UShort:
2470 case BuiltinType::Int:
2471 case BuiltinType::UInt:
2472 return true;
2473 default:
2474 return false;
2475 }
2476 return false;
2477}
2478
2479llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2480 CodeGenFunction &CGF) const {
2481 // FIXME: Implement
2482 return 0;
2483}
2484
2485
Chris Lattnera3c109b2010-07-29 02:16:43 +00002486ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
2487 if (RetTy->isVoidType())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002488 return ABIArgInfo::getIgnore();
John McCalld608cdb2010-08-22 10:59:02 +00002489 if (isAggregateTypeForABI(RetTy))
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002490 return ABIArgInfo::getIndirect(0);
Chris Lattnera3c109b2010-07-29 02:16:43 +00002491
2492 return (isPromotableIntegerType(RetTy) ?
2493 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002494}
2495
Chris Lattnera3c109b2010-07-29 02:16:43 +00002496ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +00002497 if (isAggregateTypeForABI(Ty))
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002498 return ABIArgInfo::getIndirect(0);
Chris Lattnera3c109b2010-07-29 02:16:43 +00002499
2500 return (isPromotableIntegerType(Ty) ?
2501 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002502}
2503
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002504//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002505// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002506//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002507
2508namespace {
2509
2510class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2511public:
Chris Lattnerea044322010-07-29 02:01:43 +00002512 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
2513 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002514 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2515 CodeGen::CodeGenModule &M) const;
2516};
2517
2518}
2519
2520void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2521 llvm::GlobalValue *GV,
2522 CodeGen::CodeGenModule &M) const {
2523 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2524 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2525 // Handle 'interrupt' attribute:
2526 llvm::Function *F = cast<llvm::Function>(GV);
2527
2528 // Step 1: Set ISR calling convention.
2529 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2530
2531 // Step 2: Add attributes goodness.
2532 F->addFnAttr(llvm::Attribute::NoInline);
2533
2534 // Step 3: Emit ISR vector alias.
2535 unsigned Num = attr->getNumber() + 0xffe0;
2536 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2537 "vector_" +
2538 llvm::LowercaseString(llvm::utohexstr(Num)),
2539 GV, &M.getModule());
2540 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002541 }
2542}
2543
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002544//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00002545// MIPS ABI Implementation. This works for both little-endian and
2546// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002547//===----------------------------------------------------------------------===//
2548
John McCallaeeb7012010-05-27 06:19:26 +00002549namespace {
2550class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
2551public:
Chris Lattnerea044322010-07-29 02:01:43 +00002552 MIPSTargetCodeGenInfo(CodeGenTypes &CGT)
2553 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
John McCallaeeb7012010-05-27 06:19:26 +00002554
2555 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2556 return 29;
2557 }
2558
2559 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002560 llvm::Value *Address) const;
John McCallaeeb7012010-05-27 06:19:26 +00002561};
2562}
2563
2564bool
2565MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2566 llvm::Value *Address) const {
2567 // This information comes from gcc's implementation, which seems to
2568 // as canonical as it gets.
2569
2570 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2571 llvm::LLVMContext &Context = CGF.getLLVMContext();
2572
2573 // Everything on MIPS is 4 bytes. Double-precision FP registers
2574 // are aliased to pairs of single-precision FP registers.
2575 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2576 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2577
2578 // 0-31 are the general purpose registers, $0 - $31.
2579 // 32-63 are the floating-point registers, $f0 - $f31.
2580 // 64 and 65 are the multiply/divide registers, $hi and $lo.
2581 // 66 is the (notional, I think) register for signal-handler return.
2582 AssignToArrayRange(Builder, Address, Four8, 0, 65);
2583
2584 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
2585 // They are one bit wide and ignored here.
2586
2587 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
2588 // (coprocessor 1 is the FP unit)
2589 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
2590 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
2591 // 176-181 are the DSP accumulator registers.
2592 AssignToArrayRange(Builder, Address, Four8, 80, 181);
2593
2594 return false;
2595}
2596
2597
Chris Lattnerea044322010-07-29 02:01:43 +00002598const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002599 if (TheTargetCodeGenInfo)
2600 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002601
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002602 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2603 // free it.
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002604
Chris Lattner9c254f02010-06-29 06:01:59 +00002605 const llvm::Triple &Triple = getContext().Target.getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002606 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002607 default:
Chris Lattnerea044322010-07-29 02:01:43 +00002608 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002609
John McCallaeeb7012010-05-27 06:19:26 +00002610 case llvm::Triple::mips:
2611 case llvm::Triple::mipsel:
Chris Lattnerea044322010-07-29 02:01:43 +00002612 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types));
John McCallaeeb7012010-05-27 06:19:26 +00002613
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002614 case llvm::Triple::arm:
2615 case llvm::Triple::thumb:
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002616 // FIXME: We want to know the float calling convention as well.
Daniel Dunbar018ba5a2009-09-14 00:35:03 +00002617 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002618 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00002619 new ARMTargetCodeGenInfo(Types, ARMABIInfo::APCS));
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002620
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002621 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00002622 new ARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002623
2624 case llvm::Triple::pic16:
Chris Lattnerea044322010-07-29 02:01:43 +00002625 return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002626
John McCallec853ba2010-03-11 00:10:12 +00002627 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00002628 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00002629
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002630 case llvm::Triple::systemz:
Chris Lattnerea044322010-07-29 02:01:43 +00002631 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002632
2633 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00002634 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002635
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002636 case llvm::Triple::x86:
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002637 switch (Triple.getOS()) {
Edward O'Callaghan7ee68bd2009-10-20 17:22:50 +00002638 case llvm::Triple::Darwin:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002639 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00002640 new X86_32TargetCodeGenInfo(Types, true, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002641 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002642 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00002643 case llvm::Triple::AuroraUX:
2644 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00002645 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002646 case llvm::Triple::OpenBSD:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002647 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00002648 new X86_32TargetCodeGenInfo(Types, false, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002649
2650 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002651 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00002652 new X86_32TargetCodeGenInfo(Types, false, false));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002653 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002654
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002655 case llvm::Triple::x86_64:
Chris Lattnerf13721d2010-08-31 16:44:54 +00002656 switch (Triple.getOS()) {
2657 case llvm::Triple::Win32:
2658 case llvm::Triple::MinGW64:
2659 case llvm::Triple::Cygwin:
2660 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
2661 default:
2662 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
2663 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002664 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002665}