blob: 944eae85d51486a45dd30e7b333f8157c6e362b0 [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"
Sandeep Patel34c1af82011-04-05 00:23:47 +000019#include "clang/Frontend/CodeGenOptions.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000020#include "llvm/Type.h"
Chris Lattner9c254f02010-06-29 06:01:59 +000021#include "llvm/Target/TargetData.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 {
Chris Lattner5f9e2722011-07-23 10:55:15 +000060 raw_ostream &OS = llvm::errs();
Daniel Dunbar28df7a52009-12-03 09:13:49 +000061 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=";
Chris Lattner2acc6e32011-07-18 04:24:23 +000065 if (llvm::Type *Ty = getCoerceToType())
Chris Lattner800588f2010-07-29 06:26:06 +000066 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()
Joerg Sonnenbergere9b5d772011-07-15 18:23:44 +000078 << " ByVal=" << getIndirectByVal()
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +000079 << " Realign=" << getIndirectRealign();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000080 break;
81 case Expand:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000082 OS << "Expand";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000083 break;
84 }
Daniel Dunbar28df7a52009-12-03 09:13:49 +000085 OS << ")\n";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000086}
87
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000088TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
89
John McCall49e34be2011-08-30 01:42:09 +000090// If someone can figure out a general rule for this, that would be great.
91// It's probably just doomed to be platform-dependent, though.
92unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
93 // Verified for:
94 // x86-64 FreeBSD, Linux, Darwin
95 // x86-32 FreeBSD, Linux, Darwin
96 // PowerPC Linux, Darwin
97 // ARM Darwin (*not* EABI)
98 return 32;
99}
100
John McCall01f151e2011-09-21 08:08:30 +0000101bool TargetCodeGenInfo::isNoProtoCallVariadic(CallingConv CC) const {
102 // The following conventions are known to require this to be false:
103 // x86_stdcall
104 // MIPS
105 // For everything else, we just prefer false unless we opt out.
106 return false;
107}
108
Daniel Dunbar98303b92009-09-13 08:03:58 +0000109static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000110
111/// isEmptyField - Return true iff a the field is "empty", that is it
112/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +0000113static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
114 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000115 if (FD->isUnnamedBitfield())
116 return true;
117
118 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000119
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000120 // Constant arrays of empty records count as empty, strip them off.
121 // Constant arrays of zero length always count as empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000122 if (AllowArrays)
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000123 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
124 if (AT->getSize() == 0)
125 return true;
Daniel Dunbar98303b92009-09-13 08:03:58 +0000126 FT = AT->getElementType();
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000127 }
Daniel Dunbar98303b92009-09-13 08:03:58 +0000128
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000129 const RecordType *RT = FT->getAs<RecordType>();
130 if (!RT)
131 return false;
132
133 // C++ record fields are never empty, at least in the Itanium ABI.
134 //
135 // FIXME: We should use a predicate for whether this behavior is true in the
136 // current ABI.
137 if (isa<CXXRecordDecl>(RT->getDecl()))
138 return false;
139
Daniel Dunbar98303b92009-09-13 08:03:58 +0000140 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000141}
142
143/// isEmptyRecord - Return true iff a structure contains only empty
144/// fields. Note that a structure with a flexible array member is not
145/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000146static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000147 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000148 if (!RT)
149 return 0;
150 const RecordDecl *RD = RT->getDecl();
151 if (RD->hasFlexibleArrayMember())
152 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000153
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000154 // If this is a C++ record, check the bases first.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000155 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000156 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
157 e = CXXRD->bases_end(); i != e; ++i)
158 if (!isEmptyRecord(Context, i->getType(), true))
159 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000160
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000161 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
162 i != e; ++i)
Daniel Dunbar98303b92009-09-13 08:03:58 +0000163 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000164 return false;
165 return true;
166}
167
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000168/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
169/// a non-trivial destructor or a non-trivial copy constructor.
170static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
171 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
172 if (!RD)
173 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000174
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000175 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
176}
177
178/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
179/// a record type with either a non-trivial destructor or a non-trivial copy
180/// constructor.
181static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
182 const RecordType *RT = T->getAs<RecordType>();
183 if (!RT)
184 return false;
185
186 return hasNonTrivialDestructorOrCopyConstructor(RT);
187}
188
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000189/// isSingleElementStruct - Determine if a structure is a "single
190/// element struct", i.e. it has exactly one non-empty field or
191/// exactly one field which is itself a single element
192/// struct. Structures with flexible array members are never
193/// considered single element structs.
194///
195/// \return The field declaration for the single non-empty field, if
196/// it exists.
197static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
198 const RecordType *RT = T->getAsStructureType();
199 if (!RT)
200 return 0;
201
202 const RecordDecl *RD = RT->getDecl();
203 if (RD->hasFlexibleArrayMember())
204 return 0;
205
206 const Type *Found = 0;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000207
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000208 // If this is a C++ record, check the bases first.
209 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
210 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
211 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000212 // Ignore empty records.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000213 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000214 continue;
215
216 // If we already found an element then this isn't a single-element struct.
217 if (Found)
218 return 0;
219
220 // If this is non-empty and not a single element struct, the composite
221 // cannot be a single element struct.
222 Found = isSingleElementStruct(i->getType(), Context);
223 if (!Found)
224 return 0;
225 }
226 }
227
228 // Check for single element.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000229 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
230 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000231 const FieldDecl *FD = *i;
232 QualType FT = FD->getType();
233
234 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000235 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000236 continue;
237
238 // If we already found an element then this isn't a single-element
239 // struct.
240 if (Found)
241 return 0;
242
243 // Treat single element arrays as the element.
244 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
245 if (AT->getSize().getZExtValue() != 1)
246 break;
247 FT = AT->getElementType();
248 }
249
John McCalld608cdb2010-08-22 10:59:02 +0000250 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000251 Found = FT.getTypePtr();
252 } else {
253 Found = isSingleElementStruct(FT, Context);
254 if (!Found)
255 return 0;
256 }
257 }
258
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000259 // We don't consider a struct a single-element struct if it has
260 // padding beyond the element type.
261 if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
262 return 0;
263
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000264 return Found;
265}
266
267static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbara1842d32010-05-14 03:40:53 +0000268 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000269 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
270 !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000271 return false;
272
273 uint64_t Size = Context.getTypeSize(Ty);
274 return Size == 32 || Size == 64;
275}
276
Daniel Dunbar53012f42009-11-09 01:33:53 +0000277/// canExpandIndirectArgument - Test whether an argument type which is to be
278/// passed indirectly (on the stack) would have the equivalent layout if it was
279/// expanded into separate arguments. If so, we prefer to do the latter to avoid
280/// inhibiting optimizations.
281///
282// FIXME: This predicate is missing many cases, currently it just follows
283// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
284// should probably make this smarter, or better yet make the LLVM backend
285// capable of handling it.
286static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
287 // We can only expand structure types.
288 const RecordType *RT = Ty->getAs<RecordType>();
289 if (!RT)
290 return false;
291
292 // We can only expand (C) structures.
293 //
294 // FIXME: This needs to be generalized to handle classes as well.
295 const RecordDecl *RD = RT->getDecl();
296 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
297 return false;
298
Eli Friedman506d4e32011-11-18 01:32:26 +0000299 uint64_t Size = 0;
300
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000301 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
302 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000303 const FieldDecl *FD = *i;
304
305 if (!is32Or64BitBasicType(FD->getType(), Context))
306 return false;
307
308 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
309 // how to expand them yet, and the predicate for telling if a bitfield still
310 // counts as "basic" is more complicated than what we were doing previously.
311 if (FD->isBitField())
312 return false;
Eli Friedman506d4e32011-11-18 01:32:26 +0000313
314 Size += Context.getTypeSize(FD->getType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000315 }
316
Eli Friedman506d4e32011-11-18 01:32:26 +0000317 // Make sure there are not any holes in the struct.
318 if (Size != Context.getTypeSize(Ty))
319 return false;
320
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000321 return true;
322}
323
324namespace {
325/// DefaultABIInfo - The default implementation for ABI specific
326/// details. This implementation provides information which results in
327/// self-consistent and sensible LLVM IR generation, but does not
328/// conform to any particular ABI.
329class DefaultABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +0000330public:
331 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000332
Chris Lattnera3c109b2010-07-29 02:16:43 +0000333 ABIArgInfo classifyReturnType(QualType RetTy) const;
334 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000335
Chris Lattneree5dcd02010-07-29 02:31:05 +0000336 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000337 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000338 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
339 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000340 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000341 }
342
343 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
344 CodeGenFunction &CGF) const;
345};
346
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000347class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
348public:
Chris Lattnerea044322010-07-29 02:01:43 +0000349 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
350 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000351};
352
353llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
354 CodeGenFunction &CGF) const {
355 return 0;
356}
357
Chris Lattnera3c109b2010-07-29 02:16:43 +0000358ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
Jan Wen Voung90306932011-11-03 00:59:44 +0000359 if (isAggregateTypeForABI(Ty)) {
360 // Records with non trivial destructors/constructors should not be passed
361 // by value.
362 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
363 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
364
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000365 return ABIArgInfo::getIndirect(0);
Jan Wen Voung90306932011-11-03 00:59:44 +0000366 }
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000367
Chris Lattnera14db752010-03-11 18:19:55 +0000368 // Treat an enum type as its underlying type.
369 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
370 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000371
Chris Lattnera14db752010-03-11 18:19:55 +0000372 return (Ty->isPromotableIntegerType() ?
373 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000374}
375
Bob Wilson0024f942011-01-10 23:54:17 +0000376ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
377 if (RetTy->isVoidType())
378 return ABIArgInfo::getIgnore();
379
380 if (isAggregateTypeForABI(RetTy))
381 return ABIArgInfo::getIndirect(0);
382
383 // Treat an enum type as its underlying type.
384 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
385 RetTy = EnumTy->getDecl()->getIntegerType();
386
387 return (RetTy->isPromotableIntegerType() ?
388 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
389}
390
Bill Wendlingbb465d72010-10-18 03:41:31 +0000391/// UseX86_MMXType - Return true if this is an MMX type that should use the special
392/// x86_mmx type.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000393bool UseX86_MMXType(llvm::Type *IRType) {
Bill Wendlingbb465d72010-10-18 03:41:31 +0000394 // If the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>, use the
395 // special x86_mmx type.
396 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
397 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
398 IRType->getScalarSizeInBits() != 64;
399}
400
Jay Foadef6de3d2011-07-11 09:56:20 +0000401static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000402 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000403 llvm::Type* Ty) {
Bill Wendling0507be62011-03-07 22:47:14 +0000404 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy())
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000405 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
406 return Ty;
407}
408
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000409//===----------------------------------------------------------------------===//
410// X86-32 ABI Implementation
411//===----------------------------------------------------------------------===//
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000412
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000413/// X86_32ABIInfo - The X86-32 ABI information.
414class X86_32ABIInfo : public ABIInfo {
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000415 static const unsigned MinABIStackAlignInBytes = 4;
416
David Chisnall1e4249c2009-08-17 23:08:21 +0000417 bool IsDarwinVectorABI;
418 bool IsSmallStructInRegABI;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000419 bool IsMMXDisabled;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000420
421 static bool isRegisterSize(unsigned Size) {
422 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
423 }
424
425 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
426
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000427 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
428 /// such that the argument will be passed in memory.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000429 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000430
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000431 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbare59d8582010-09-16 20:42:06 +0000432 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000433
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000434public:
Chris Lattnerea044322010-07-29 02:01:43 +0000435
Chris Lattnera3c109b2010-07-29 02:16:43 +0000436 ABIArgInfo classifyReturnType(QualType RetTy) const;
437 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000438
Chris Lattneree5dcd02010-07-29 02:31:05 +0000439 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000440 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000441 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
442 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000443 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000444 }
445
446 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
447 CodeGenFunction &CGF) const;
448
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000449 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m)
450 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
451 IsMMXDisabled(m) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000452};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000453
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000454class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
455public:
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000456 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m)
457 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, m)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000458
459 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
460 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000461
462 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
463 // Darwin uses different dwarf register numbers for EH.
464 if (CGM.isTargetDarwin()) return 5;
465
466 return 4;
467 }
468
469 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
470 llvm::Value *Address) const;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000471
Jay Foadef6de3d2011-07-11 09:56:20 +0000472 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000473 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000474 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000475 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
476 }
477
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000478};
479
480}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000481
482/// shouldReturnTypeInRegister - Determine if the given type should be
483/// passed in a register (for the Darwin ABI).
484bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
485 ASTContext &Context) {
486 uint64_t Size = Context.getTypeSize(Ty);
487
488 // Type must be register sized.
489 if (!isRegisterSize(Size))
490 return false;
491
492 if (Ty->isVectorType()) {
493 // 64- and 128- bit vectors inside structures are not returned in
494 // registers.
495 if (Size == 64 || Size == 128)
496 return false;
497
498 return true;
499 }
500
Daniel Dunbar77115232010-05-15 00:00:30 +0000501 // If this is a builtin, pointer, enum, complex type, member pointer, or
502 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000503 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000504 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000505 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000506 return true;
507
508 // Arrays are treated like records.
509 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
510 return shouldReturnTypeInRegister(AT->getElementType(), Context);
511
512 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000513 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000514 if (!RT) return false;
515
Anders Carlssona8874232010-01-27 03:25:19 +0000516 // FIXME: Traverse bases here too.
517
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000518 // Structure types are passed in register if all fields would be
519 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000520 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
521 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000522 const FieldDecl *FD = *i;
523
524 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000525 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000526 continue;
527
528 // Check fields recursively.
529 if (!shouldReturnTypeInRegister(FD->getType(), Context))
530 return false;
531 }
532
533 return true;
534}
535
Chris Lattnera3c109b2010-07-29 02:16:43 +0000536ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const {
537 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000538 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000539
Chris Lattnera3c109b2010-07-29 02:16:43 +0000540 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000541 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000542 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000543 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000544
545 // 128-bit vectors are a special case; they are returned in
546 // registers and we need to make sure to pick a type the LLVM
547 // backend will like.
548 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +0000549 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000550 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000551
552 // Always return in register if it fits in a general purpose
553 // register, or if it is 64 bits and has a single element.
554 if ((Size == 8 || Size == 16 || Size == 32) ||
555 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +0000556 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000557 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000558
559 return ABIArgInfo::getIndirect(0);
560 }
561
562 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000563 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000564
John McCalld608cdb2010-08-22 10:59:02 +0000565 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000566 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000567 // Structures with either a non-trivial destructor or a non-trivial
568 // copy constructor are always indirect.
569 if (hasNonTrivialDestructorOrCopyConstructor(RT))
570 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000571
Anders Carlsson40092972009-10-20 22:07:59 +0000572 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000573 if (RT->getDecl()->hasFlexibleArrayMember())
574 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000575 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000576
David Chisnall1e4249c2009-08-17 23:08:21 +0000577 // If specified, structs and unions are always indirect.
578 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000579 return ABIArgInfo::getIndirect(0);
580
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000581 // Small structures which are register sized are generally returned
582 // in a register.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000583 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) {
584 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000585
586 // As a special-case, if the struct is a "single-element" struct, and
587 // the field is of type "float" or "double", return it in a
588 // floating-point register. We apply a similar transformation for
589 // pointer types to improve the quality of the generated IR.
590 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
591 if (SeltTy->isRealFloatingType() || SeltTy->hasPointerRepresentation())
592 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
593
594 // FIXME: We should be able to narrow this integer in cases with dead
595 // padding.
Chris Lattner800588f2010-07-29 06:26:06 +0000596 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000597 }
598
599 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000600 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000601
Chris Lattnera3c109b2010-07-29 02:16:43 +0000602 // Treat an enum type as its underlying type.
603 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
604 RetTy = EnumTy->getDecl()->getIntegerType();
605
606 return (RetTy->isPromotableIntegerType() ?
607 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000608}
609
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000610static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
611 const RecordType *RT = Ty->getAs<RecordType>();
612 if (!RT)
613 return 0;
614 const RecordDecl *RD = RT->getDecl();
615
616 // If this is a C++ record, check the bases first.
617 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
618 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
619 e = CXXRD->bases_end(); i != e; ++i)
620 if (!isRecordWithSSEVectorType(Context, i->getType()))
621 return false;
622
623 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
624 i != e; ++i) {
625 QualType FT = i->getType();
626
Eli Friedman7b1fb812011-11-18 02:12:09 +0000627 if (FT->getAs<VectorType>() && Context.getTypeSize(FT) == 128)
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000628 return true;
629
630 if (isRecordWithSSEVectorType(Context, FT))
631 return true;
632 }
633
634 return false;
635}
636
Daniel Dunbare59d8582010-09-16 20:42:06 +0000637unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
638 unsigned Align) const {
639 // Otherwise, if the alignment is less than or equal to the minimum ABI
640 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000641 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +0000642 return 0; // Use default alignment.
643
644 // On non-Darwin, the stack type alignment is always 4.
645 if (!IsDarwinVectorABI) {
646 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000647 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +0000648 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000649
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000650 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedman7b1fb812011-11-18 02:12:09 +0000651 if (Align >= 16 && isRecordWithSSEVectorType(getContext(), Ty))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000652 return 16;
653
654 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000655}
656
Chris Lattnera3c109b2010-07-29 02:16:43 +0000657ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000658 if (!ByVal)
659 return ABIArgInfo::getIndirect(0, false);
660
Daniel Dunbare59d8582010-09-16 20:42:06 +0000661 // Compute the byval alignment.
662 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
663 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
664 if (StackAlign == 0)
Chris Lattnerde92d732011-05-22 23:35:00 +0000665 return ABIArgInfo::getIndirect(4);
Daniel Dunbare59d8582010-09-16 20:42:06 +0000666
667 // If the stack alignment is less than the type alignment, realign the
668 // argument.
669 if (StackAlign < TypeAlign)
670 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
671 /*Realign=*/true);
672
673 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000674}
675
Chris Lattnera3c109b2010-07-29 02:16:43 +0000676ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000677 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000678 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000679 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000680 if (const RecordType *RT = Ty->getAs<RecordType>()) {
681 // Structures with either a non-trivial destructor or a non-trivial
682 // copy constructor are always indirect.
683 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Chris Lattnera3c109b2010-07-29 02:16:43 +0000684 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000685
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000686 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattnera3c109b2010-07-29 02:16:43 +0000687 return getIndirectResult(Ty);
Anders Carlssona8874232010-01-27 03:25:19 +0000688 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000689
Eli Friedman5a4d3522011-11-18 00:28:11 +0000690 // Ignore empty structs/unions.
Eli Friedman5a1ac892011-11-18 04:01:36 +0000691 if (isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000692 return ABIArgInfo::getIgnore();
693
Daniel Dunbar53012f42009-11-09 01:33:53 +0000694 // Expand small (<= 128-bit) record types when we know that the stack layout
695 // of those arguments will match the struct. This is important because the
696 // LLVM backend isn't smart enough to remove byval, which inhibits many
697 // optimizations.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000698 if (getContext().getTypeSize(Ty) <= 4*32 &&
699 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar53012f42009-11-09 01:33:53 +0000700 return ABIArgInfo::getExpand();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000701
Chris Lattnera3c109b2010-07-29 02:16:43 +0000702 return getIndirectResult(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000703 }
704
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000705 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +0000706 // On Darwin, some vectors are passed in memory, we handle this by passing
707 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000708 if (IsDarwinVectorABI) {
709 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000710 if ((Size == 8 || Size == 16 || Size == 32) ||
711 (Size == 64 && VT->getNumElements() == 1))
712 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
713 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000714 }
Bill Wendlingbb465d72010-10-18 03:41:31 +0000715
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000716 llvm::Type *IRType = CGT.ConvertType(Ty);
Bill Wendlingbb465d72010-10-18 03:41:31 +0000717 if (UseX86_MMXType(IRType)) {
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000718 if (IsMMXDisabled)
719 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
720 64));
Bill Wendlingbb465d72010-10-18 03:41:31 +0000721 ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
722 AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
723 return AAI;
724 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000725
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000726 return ABIArgInfo::getDirect();
727 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000728
729
Chris Lattnera3c109b2010-07-29 02:16:43 +0000730 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
731 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000732
Chris Lattnera3c109b2010-07-29 02:16:43 +0000733 return (Ty->isPromotableIntegerType() ?
734 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000735}
736
737llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
738 CodeGenFunction &CGF) const {
Chris Lattner2acc6e32011-07-18 04:24:23 +0000739 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
740 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000741
742 CGBuilderTy &Builder = CGF.Builder;
743 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
744 "ap");
745 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Eli Friedman7b1fb812011-11-18 02:12:09 +0000746
747 // Compute if the address needs to be aligned
748 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
749 Align = getTypeStackAlignInBytes(Ty, Align);
750 Align = std::max(Align, 4U);
751 if (Align > 4) {
752 // addr = (addr + align - 1) & -align;
753 llvm::Value *Offset =
754 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
755 Addr = CGF.Builder.CreateGEP(Addr, Offset);
756 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
757 CGF.Int32Ty);
758 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
759 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
760 Addr->getType(),
761 "ap.cur.aligned");
762 }
763
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000764 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000765 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000766 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
767
768 uint64_t Offset =
Eli Friedman7b1fb812011-11-18 02:12:09 +0000769 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000770 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +0000771 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000772 "ap.next");
773 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
774
775 return AddrTyped;
776}
777
Charles Davis74f72932010-02-13 15:54:06 +0000778void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
779 llvm::GlobalValue *GV,
780 CodeGen::CodeGenModule &CGM) const {
781 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
782 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
783 // Get the LLVM function.
784 llvm::Function *Fn = cast<llvm::Function>(GV);
785
786 // Now add the 'alignstack' attribute with a value of 16.
787 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
788 }
789 }
790}
791
John McCall6374c332010-03-06 00:35:14 +0000792bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
793 CodeGen::CodeGenFunction &CGF,
794 llvm::Value *Address) const {
795 CodeGen::CGBuilderTy &Builder = CGF.Builder;
796 llvm::LLVMContext &Context = CGF.getLLVMContext();
797
Chris Lattner2acc6e32011-07-18 04:24:23 +0000798 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCall6374c332010-03-06 00:35:14 +0000799 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000800
John McCall6374c332010-03-06 00:35:14 +0000801 // 0-7 are the eight integer registers; the order is different
802 // on Darwin (for EH), but the range is the same.
803 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +0000804 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +0000805
806 if (CGF.CGM.isTargetDarwin()) {
807 // 12-16 are st(0..4). Not sure why we stop at 4.
808 // These have size 16, which is sizeof(long double) on
809 // platforms with 8-byte alignment for that type.
810 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCallaeeb7012010-05-27 06:19:26 +0000811 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000812
John McCall6374c332010-03-06 00:35:14 +0000813 } else {
814 // 9 is %eflags, which doesn't get a size on Darwin for some
815 // reason.
816 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
817
818 // 11-16 are st(0..5). Not sure why we stop at 5.
819 // These have size 12, which is sizeof(long double) on
820 // platforms with 4-byte alignment for that type.
821 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCallaeeb7012010-05-27 06:19:26 +0000822 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
823 }
John McCall6374c332010-03-06 00:35:14 +0000824
825 return false;
826}
827
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000828//===----------------------------------------------------------------------===//
829// X86-64 ABI Implementation
830//===----------------------------------------------------------------------===//
831
832
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000833namespace {
834/// X86_64ABIInfo - The X86_64 ABI information.
835class X86_64ABIInfo : public ABIInfo {
836 enum Class {
837 Integer = 0,
838 SSE,
839 SSEUp,
840 X87,
841 X87Up,
842 ComplexX87,
843 NoClass,
844 Memory
845 };
846
847 /// merge - Implement the X86_64 ABI merging algorithm.
848 ///
849 /// Merge an accumulating classification \arg Accum with a field
850 /// classification \arg Field.
851 ///
852 /// \param Accum - The accumulating classification. This should
853 /// always be either NoClass or the result of a previous merge
854 /// call. In addition, this should never be Memory (the caller
855 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +0000856 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000857
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +0000858 /// postMerge - Implement the X86_64 ABI post merging algorithm.
859 ///
860 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
861 /// final MEMORY or SSE classes when necessary.
862 ///
863 /// \param AggregateSize - The size of the current aggregate in
864 /// the classification process.
865 ///
866 /// \param Lo - The classification for the parts of the type
867 /// residing in the low word of the containing object.
868 ///
869 /// \param Hi - The classification for the parts of the type
870 /// residing in the higher words of the containing object.
871 ///
872 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
873
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000874 /// classify - Determine the x86_64 register classes in which the
875 /// given type T should be passed.
876 ///
877 /// \param Lo - The classification for the parts of the type
878 /// residing in the low word of the containing object.
879 ///
880 /// \param Hi - The classification for the parts of the type
881 /// residing in the high word of the containing object.
882 ///
883 /// \param OffsetBase - The bit offset of this type in the
884 /// containing object. Some parameters are classified different
885 /// depending on whether they straddle an eightbyte boundary.
886 ///
887 /// If a word is unused its result will be NoClass; if a type should
888 /// be passed in Memory then at least the classification of \arg Lo
889 /// will be Memory.
890 ///
891 /// The \arg Lo class will be NoClass iff the argument is ignored.
892 ///
893 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
894 /// also be ComplexX87.
Chris Lattner9c254f02010-06-29 06:01:59 +0000895 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000896
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +0000897 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000898 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
899 unsigned IROffset, QualType SourceTy,
900 unsigned SourceOffset) const;
901 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
902 unsigned IROffset, QualType SourceTy,
903 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000904
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000905 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000906 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000907 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000908
909 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000910 /// such that the argument will be passed in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000911 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000912
Chris Lattnera3c109b2010-07-29 02:16:43 +0000913 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000914
Bill Wendlingbb465d72010-10-18 03:41:31 +0000915 ABIArgInfo classifyArgumentType(QualType Ty,
916 unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +0000917 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000918
John McCall67a57732011-04-21 01:20:55 +0000919 /// The 0.98 ABI revision clarified a lot of ambiguities,
920 /// unfortunately in ways that were not always consistent with
921 /// certain previous compilers. In particular, platforms which
922 /// required strict binary compatibility with older versions of GCC
923 /// may need to exempt themselves.
924 bool honorsRevision0_98() const {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000925 return !getContext().getTargetInfo().getTriple().isOSDarwin();
John McCall67a57732011-04-21 01:20:55 +0000926 }
927
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000928public:
Chris Lattnerea044322010-07-29 02:01:43 +0000929 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Chris Lattner9c254f02010-06-29 06:01:59 +0000930
Chris Lattneree5dcd02010-07-29 02:31:05 +0000931 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000932
933 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
934 CodeGenFunction &CGF) const;
935};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000936
Chris Lattnerf13721d2010-08-31 16:44:54 +0000937/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +0000938class WinX86_64ABIInfo : public ABIInfo {
939
940 ABIArgInfo classify(QualType Ty) const;
941
Chris Lattnerf13721d2010-08-31 16:44:54 +0000942public:
NAKAMURA Takumia7573222011-01-17 22:56:31 +0000943 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
944
945 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattnerf13721d2010-08-31 16:44:54 +0000946
947 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
948 CodeGenFunction &CGF) const;
949};
950
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000951class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
952public:
Chris Lattnerea044322010-07-29 02:01:43 +0000953 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
954 : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {}
John McCall6374c332010-03-06 00:35:14 +0000955
956 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
957 return 7;
958 }
959
960 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
961 llvm::Value *Address) const {
962 CodeGen::CGBuilderTy &Builder = CGF.Builder;
963 llvm::LLVMContext &Context = CGF.getLLVMContext();
964
Chris Lattner2acc6e32011-07-18 04:24:23 +0000965 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCall6374c332010-03-06 00:35:14 +0000966 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000967
John McCallaeeb7012010-05-27 06:19:26 +0000968 // 0-15 are the 16 integer registers.
969 // 16 is %rip.
970 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +0000971
972 return false;
973 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000974
Jay Foadef6de3d2011-07-11 09:56:20 +0000975 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000976 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000977 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000978 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
979 }
980
John McCall01f151e2011-09-21 08:08:30 +0000981 bool isNoProtoCallVariadic(CallingConv CC) const {
982 // The default CC on x86-64 sets %al to the number of SSA
983 // registers used, and GCC sets this when calling an unprototyped
984 // function, so we override the default behavior.
985 if (CC == CC_Default || CC == CC_C) return true;
986
987 return TargetCodeGenInfo::isNoProtoCallVariadic(CC);
988 }
989
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000990};
991
Chris Lattnerf13721d2010-08-31 16:44:54 +0000992class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
993public:
994 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
995 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
996
997 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
998 return 7;
999 }
1000
1001 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1002 llvm::Value *Address) const {
1003 CodeGen::CGBuilderTy &Builder = CGF.Builder;
1004 llvm::LLVMContext &Context = CGF.getLLVMContext();
1005
Chris Lattner2acc6e32011-07-18 04:24:23 +00001006 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
Chris Lattnerf13721d2010-08-31 16:44:54 +00001007 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001008
Chris Lattnerf13721d2010-08-31 16:44:54 +00001009 // 0-15 are the 16 integer registers.
1010 // 16 is %rip.
1011 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
1012
1013 return false;
1014 }
1015};
1016
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001017}
1018
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001019void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1020 Class &Hi) const {
1021 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1022 //
1023 // (a) If one of the classes is Memory, the whole argument is passed in
1024 // memory.
1025 //
1026 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1027 // memory.
1028 //
1029 // (c) If the size of the aggregate exceeds two eightbytes and the first
1030 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1031 // argument is passed in memory. NOTE: This is necessary to keep the
1032 // ABI working for processors that don't support the __m256 type.
1033 //
1034 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1035 //
1036 // Some of these are enforced by the merging logic. Others can arise
1037 // only with unions; for example:
1038 // union { _Complex double; unsigned; }
1039 //
1040 // Note that clauses (b) and (c) were added in 0.98.
1041 //
1042 if (Hi == Memory)
1043 Lo = Memory;
1044 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1045 Lo = Memory;
1046 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1047 Lo = Memory;
1048 if (Hi == SSEUp && Lo != SSE)
1049 Hi = SSE;
1050}
1051
Chris Lattner1090a9b2010-06-28 21:43:59 +00001052X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001053 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1054 // classified recursively so that always two fields are
1055 // considered. The resulting class is calculated according to
1056 // the classes of the fields in the eightbyte:
1057 //
1058 // (a) If both classes are equal, this is the resulting class.
1059 //
1060 // (b) If one of the classes is NO_CLASS, the resulting class is
1061 // the other class.
1062 //
1063 // (c) If one of the classes is MEMORY, the result is the MEMORY
1064 // class.
1065 //
1066 // (d) If one of the classes is INTEGER, the result is the
1067 // INTEGER.
1068 //
1069 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1070 // MEMORY is used as class.
1071 //
1072 // (f) Otherwise class SSE is used.
1073
1074 // Accum should never be memory (we should have returned) or
1075 // ComplexX87 (because this cannot be passed in a structure).
1076 assert((Accum != Memory && Accum != ComplexX87) &&
1077 "Invalid accumulated classification during merge.");
1078 if (Accum == Field || Field == NoClass)
1079 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001080 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001081 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001082 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001083 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001084 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001085 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001086 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1087 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001088 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001089 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001090}
1091
Chris Lattnerbcaedae2010-06-30 19:14:05 +00001092void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001093 Class &Lo, Class &Hi) const {
1094 // FIXME: This code can be simplified by introducing a simple value class for
1095 // Class pairs with appropriate constructor methods for the various
1096 // situations.
1097
1098 // FIXME: Some of the split computations are wrong; unaligned vectors
1099 // shouldn't be passed in registers for example, so there is no chance they
1100 // can straddle an eightbyte. Verify & simplify.
1101
1102 Lo = Hi = NoClass;
1103
1104 Class &Current = OffsetBase < 64 ? Lo : Hi;
1105 Current = Memory;
1106
John McCall183700f2009-09-21 23:43:11 +00001107 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001108 BuiltinType::Kind k = BT->getKind();
1109
1110 if (k == BuiltinType::Void) {
1111 Current = NoClass;
1112 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1113 Lo = Integer;
1114 Hi = Integer;
1115 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1116 Current = Integer;
1117 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
1118 Current = SSE;
1119 } else if (k == BuiltinType::LongDouble) {
1120 Lo = X87;
1121 Hi = X87Up;
1122 }
1123 // FIXME: _Decimal32 and _Decimal64 are SSE.
1124 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001125 return;
1126 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001127
Chris Lattner1090a9b2010-06-28 21:43:59 +00001128 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001129 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +00001130 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001131 return;
1132 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001133
Chris Lattner1090a9b2010-06-28 21:43:59 +00001134 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001135 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001136 return;
1137 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001138
Chris Lattner1090a9b2010-06-28 21:43:59 +00001139 if (Ty->isMemberPointerType()) {
Daniel Dunbar67d438d2010-05-15 00:00:37 +00001140 if (Ty->isMemberFunctionPointerType())
1141 Lo = Hi = Integer;
1142 else
1143 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001144 return;
1145 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001146
Chris Lattner1090a9b2010-06-28 21:43:59 +00001147 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001148 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001149 if (Size == 32) {
1150 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1151 // float> as integer.
1152 Current = Integer;
1153
1154 // If this type crosses an eightbyte boundary, it should be
1155 // split.
1156 uint64_t EB_Real = (OffsetBase) / 64;
1157 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1158 if (EB_Real != EB_Imag)
1159 Hi = Lo;
1160 } else if (Size == 64) {
1161 // gcc passes <1 x double> in memory. :(
1162 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1163 return;
1164
1165 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001166 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001167 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1168 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1169 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001170 Current = Integer;
1171 else
1172 Current = SSE;
1173
1174 // If this type crosses an eightbyte boundary, it should be
1175 // split.
1176 if (OffsetBase && OffsetBase != 64)
1177 Hi = Lo;
Bruno Cardoso Lopes75d28b52011-07-12 02:47:38 +00001178 } else if (Size == 128 || Size == 256) {
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001179 // Arguments of 256-bits are split into four eightbyte chunks. The
1180 // least significant one belongs to class SSE and all the others to class
1181 // SSEUP. The original Lo and Hi design considers that types can't be
1182 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1183 // This design isn't correct for 256-bits, but since there're no cases
1184 // where the upper parts would need to be inspected, avoid adding
1185 // complexity and just consider Hi to match the 64-256 part.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001186 Lo = SSE;
1187 Hi = SSEUp;
1188 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001189 return;
1190 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001191
Chris Lattner1090a9b2010-06-28 21:43:59 +00001192 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001193 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001194
Chris Lattnerea044322010-07-29 02:01:43 +00001195 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001196 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001197 if (Size <= 64)
1198 Current = Integer;
1199 else if (Size <= 128)
1200 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001201 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001202 Current = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001203 else if (ET == getContext().DoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001204 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001205 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001206 Current = ComplexX87;
1207
1208 // If this complex type crosses an eightbyte boundary then it
1209 // should be split.
1210 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001211 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001212 if (Hi == NoClass && EB_Real != EB_Imag)
1213 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001214
Chris Lattner1090a9b2010-06-28 21:43:59 +00001215 return;
1216 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001217
Chris Lattnerea044322010-07-29 02:01:43 +00001218 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001219 // Arrays are treated like structures.
1220
Chris Lattnerea044322010-07-29 02:01:43 +00001221 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001222
1223 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001224 // than four eightbytes, ..., it has class MEMORY.
1225 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001226 return;
1227
1228 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1229 // fields, it has class MEMORY.
1230 //
1231 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001232 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001233 return;
1234
1235 // Otherwise implement simplified merge. We could be smarter about
1236 // this, but it isn't worth it and would be harder to verify.
1237 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001238 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001239 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes089d8922011-07-12 01:27:38 +00001240
1241 // The only case a 256-bit wide vector could be used is when the array
1242 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1243 // to work for sizes wider than 128, early check and fallback to memory.
1244 if (Size > 128 && EltSize != 256)
1245 return;
1246
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001247 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1248 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001249 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001250 Lo = merge(Lo, FieldLo);
1251 Hi = merge(Hi, FieldHi);
1252 if (Lo == Memory || Hi == Memory)
1253 break;
1254 }
1255
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001256 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001257 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001258 return;
1259 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001260
Chris Lattner1090a9b2010-06-28 21:43:59 +00001261 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001262 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001263
1264 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001265 // than four eightbytes, ..., it has class MEMORY.
1266 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001267 return;
1268
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001269 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1270 // copy constructor or a non-trivial destructor, it is passed by invisible
1271 // reference.
1272 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1273 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001274
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001275 const RecordDecl *RD = RT->getDecl();
1276
1277 // Assume variable sized types are passed in memory.
1278 if (RD->hasFlexibleArrayMember())
1279 return;
1280
Chris Lattnerea044322010-07-29 02:01:43 +00001281 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001282
1283 // Reset Lo class, this will be recomputed.
1284 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001285
1286 // If this is a C++ record, classify the bases first.
1287 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1288 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1289 e = CXXRD->bases_end(); i != e; ++i) {
1290 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1291 "Unexpected base class!");
1292 const CXXRecordDecl *Base =
1293 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1294
1295 // Classify this field.
1296 //
1297 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1298 // single eightbyte, each is classified separately. Each eightbyte gets
1299 // initialized to class NO_CLASS.
1300 Class FieldLo, FieldHi;
Anders Carlssona14f5972010-10-31 23:22:37 +00001301 uint64_t Offset = OffsetBase + Layout.getBaseClassOffsetInBits(Base);
Chris Lattner9c254f02010-06-29 06:01:59 +00001302 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001303 Lo = merge(Lo, FieldLo);
1304 Hi = merge(Hi, FieldHi);
1305 if (Lo == Memory || Hi == Memory)
1306 break;
1307 }
1308 }
1309
1310 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001311 unsigned idx = 0;
Bruno Cardoso Lopes548e4782011-07-12 22:30:58 +00001312 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001313 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001314 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1315 bool BitField = i->isBitField();
1316
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001317 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1318 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001319 //
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001320 // The only case a 256-bit wide vector could be used is when the struct
1321 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1322 // to work for sizes wider than 128, early check and fallback to memory.
1323 //
1324 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1325 Lo = Memory;
1326 return;
1327 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001328 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001329 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001330 Lo = Memory;
1331 return;
1332 }
1333
1334 // Classify this field.
1335 //
1336 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1337 // exceeds a single eightbyte, each is classified
1338 // separately. Each eightbyte gets initialized to class
1339 // NO_CLASS.
1340 Class FieldLo, FieldHi;
1341
1342 // Bit-fields require special handling, they do not force the
1343 // structure to be passed in memory even if unaligned, and
1344 // therefore they can straddle an eightbyte.
1345 if (BitField) {
1346 // Ignore padding bit-fields.
1347 if (i->isUnnamedBitfield())
1348 continue;
1349
1350 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001351 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001352
1353 uint64_t EB_Lo = Offset / 64;
1354 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1355 FieldLo = FieldHi = NoClass;
1356 if (EB_Lo) {
1357 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1358 FieldLo = NoClass;
1359 FieldHi = Integer;
1360 } else {
1361 FieldLo = Integer;
1362 FieldHi = EB_Hi ? Integer : NoClass;
1363 }
1364 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001365 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001366 Lo = merge(Lo, FieldLo);
1367 Hi = merge(Hi, FieldHi);
1368 if (Lo == Memory || Hi == Memory)
1369 break;
1370 }
1371
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001372 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001373 }
1374}
1375
Chris Lattner9c254f02010-06-29 06:01:59 +00001376ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001377 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1378 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001379 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001380 // Treat an enum type as its underlying type.
1381 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1382 Ty = EnumTy->getDecl()->getIntegerType();
1383
1384 return (Ty->isPromotableIntegerType() ?
1385 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1386 }
1387
1388 return ABIArgInfo::getIndirect(0);
1389}
1390
Chris Lattner9c254f02010-06-29 06:01:59 +00001391ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001392 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1393 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001394 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001395 // Treat an enum type as its underlying type.
1396 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1397 Ty = EnumTy->getDecl()->getIntegerType();
1398
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001399 return (Ty->isPromotableIntegerType() ?
1400 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001401 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001402
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001403 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1404 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001405
Chris Lattner855d2272011-05-22 23:21:23 +00001406 // Compute the byval alignment. We specify the alignment of the byval in all
1407 // cases so that the mid-level optimizer knows the alignment of the byval.
1408 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
1409 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001410}
1411
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001412/// GetByteVectorType - The ABI specifies that a value should be passed in an
1413/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner0f408f52010-07-29 04:56:46 +00001414/// vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001415llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001416 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001417
Chris Lattner15842bd2010-07-29 05:02:29 +00001418 // Wrapper structs that just contain vectors are passed just like vectors,
1419 // strip them off if present.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001420 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner15842bd2010-07-29 05:02:29 +00001421 while (STy && STy->getNumElements() == 1) {
1422 IRType = STy->getElementType(0);
1423 STy = dyn_cast<llvm::StructType>(IRType);
1424 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001425
Bruno Cardoso Lopes528a8c72011-07-08 22:57:35 +00001426 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001427 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1428 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001429 unsigned BitWidth = VT->getBitWidth();
Tanya Lattnerce275672011-11-28 23:18:11 +00001430 if ((BitWidth >= 128 && BitWidth <= 256) &&
Chris Lattner0f408f52010-07-29 04:56:46 +00001431 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1432 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1433 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1434 EltTy->isIntegerTy(128)))
1435 return VT;
1436 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001437
Chris Lattner0f408f52010-07-29 04:56:46 +00001438 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1439}
1440
Chris Lattnere2962be2010-07-29 07:30:00 +00001441/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1442/// is known to either be off the end of the specified type or being in
1443/// alignment padding. The user type specified is known to be at most 128 bits
1444/// in size, and have passed through X86_64ABIInfo::classify with a successful
1445/// classification that put one of the two halves in the INTEGER class.
1446///
1447/// It is conservatively correct to return false.
1448static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1449 unsigned EndBit, ASTContext &Context) {
1450 // If the bytes being queried are off the end of the type, there is no user
1451 // data hiding here. This handles analysis of builtins, vectors and other
1452 // types that don't contain interesting padding.
1453 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1454 if (TySize <= StartBit)
1455 return true;
1456
Chris Lattner021c3a32010-07-29 07:43:55 +00001457 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1458 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1459 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1460
1461 // Check each element to see if the element overlaps with the queried range.
1462 for (unsigned i = 0; i != NumElts; ++i) {
1463 // If the element is after the span we care about, then we're done..
1464 unsigned EltOffset = i*EltSize;
1465 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001466
Chris Lattner021c3a32010-07-29 07:43:55 +00001467 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1468 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1469 EndBit-EltOffset, Context))
1470 return false;
1471 }
1472 // If it overlaps no elements, then it is safe to process as padding.
1473 return true;
1474 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001475
Chris Lattnere2962be2010-07-29 07:30:00 +00001476 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1477 const RecordDecl *RD = RT->getDecl();
1478 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001479
Chris Lattnere2962be2010-07-29 07:30:00 +00001480 // If this is a C++ record, check the bases first.
1481 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1482 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1483 e = CXXRD->bases_end(); i != e; ++i) {
1484 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1485 "Unexpected base class!");
1486 const CXXRecordDecl *Base =
1487 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001488
Chris Lattnere2962be2010-07-29 07:30:00 +00001489 // If the base is after the span we care about, ignore it.
Anders Carlssona14f5972010-10-31 23:22:37 +00001490 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffsetInBits(Base);
Chris Lattnere2962be2010-07-29 07:30:00 +00001491 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001492
Chris Lattnere2962be2010-07-29 07:30:00 +00001493 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1494 if (!BitsContainNoUserData(i->getType(), BaseStart,
1495 EndBit-BaseOffset, Context))
1496 return false;
1497 }
1498 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001499
Chris Lattnere2962be2010-07-29 07:30:00 +00001500 // Verify that no field has data that overlaps the region of interest. Yes
1501 // this could be sped up a lot by being smarter about queried fields,
1502 // however we're only looking at structs up to 16 bytes, so we don't care
1503 // much.
1504 unsigned idx = 0;
1505 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1506 i != e; ++i, ++idx) {
1507 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001508
Chris Lattnere2962be2010-07-29 07:30:00 +00001509 // If we found a field after the region we care about, then we're done.
1510 if (FieldOffset >= EndBit) break;
1511
1512 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1513 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1514 Context))
1515 return false;
1516 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001517
Chris Lattnere2962be2010-07-29 07:30:00 +00001518 // If nothing in this record overlapped the area of interest, then we're
1519 // clean.
1520 return true;
1521 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001522
Chris Lattnere2962be2010-07-29 07:30:00 +00001523 return false;
1524}
1525
Chris Lattner0b362002010-07-29 18:39:32 +00001526/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1527/// float member at the specified offset. For example, {int,{float}} has a
1528/// float at offset 4. It is conservatively correct for this routine to return
1529/// false.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001530static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0b362002010-07-29 18:39:32 +00001531 const llvm::TargetData &TD) {
1532 // Base case if we find a float.
1533 if (IROffset == 0 && IRType->isFloatTy())
1534 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001535
Chris Lattner0b362002010-07-29 18:39:32 +00001536 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001537 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner0b362002010-07-29 18:39:32 +00001538 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1539 unsigned Elt = SL->getElementContainingOffset(IROffset);
1540 IROffset -= SL->getElementOffset(Elt);
1541 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1542 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001543
Chris Lattner0b362002010-07-29 18:39:32 +00001544 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001545 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1546 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner0b362002010-07-29 18:39:32 +00001547 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1548 IROffset -= IROffset/EltSize*EltSize;
1549 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1550 }
1551
1552 return false;
1553}
1554
Chris Lattnerf47c9442010-07-29 18:13:09 +00001555
1556/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1557/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001558llvm::Type *X86_64ABIInfo::
1559GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00001560 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001561 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001562 // pass as float if the last 4 bytes is just padding. This happens for
1563 // structs that contain 3 floats.
1564 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1565 SourceOffset*8+64, getContext()))
1566 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001567
Chris Lattner0b362002010-07-29 18:39:32 +00001568 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1569 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1570 // case.
1571 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001572 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1573 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001574
Chris Lattnerf47c9442010-07-29 18:13:09 +00001575 return llvm::Type::getDoubleTy(getVMContext());
1576}
1577
1578
Chris Lattner0d2656d2010-07-29 17:40:35 +00001579/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1580/// an 8-byte GPR. This means that we either have a scalar or we are talking
1581/// about the high or low part of an up-to-16-byte struct. This routine picks
1582/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001583/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1584/// etc).
1585///
1586/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1587/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1588/// the 8-byte value references. PrefType may be null.
1589///
1590/// SourceTy is the source level type for the entire argument. SourceOffset is
1591/// an offset into this that we're processing (which is always either 0 or 8).
1592///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001593llvm::Type *X86_64ABIInfo::
1594GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00001595 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001596 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1597 // returning an 8-byte unit starting with it. See if we can safely use it.
1598 if (IROffset == 0) {
1599 // Pointers and int64's always fill the 8-byte unit.
1600 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1601 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001602
Chris Lattnere2962be2010-07-29 07:30:00 +00001603 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1604 // goodness in the source type is just tail padding. This is allowed to
1605 // kick in for struct {double,int} on the int, but not on
1606 // struct{double,int,int} because we wouldn't return the second int. We
1607 // have to do this analysis on the source type because we can't depend on
1608 // unions being lowered a specific way etc.
1609 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1610 IRType->isIntegerTy(32)) {
1611 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001612
Chris Lattnere2962be2010-07-29 07:30:00 +00001613 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1614 SourceOffset*8+64, getContext()))
1615 return IRType;
1616 }
1617 }
Chris Lattner49382de2010-07-28 22:44:07 +00001618
Chris Lattner2acc6e32011-07-18 04:24:23 +00001619 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001620 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner44f0fd22010-07-29 02:20:19 +00001621 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001622 if (IROffset < SL->getSizeInBytes()) {
1623 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1624 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001625
Chris Lattner0d2656d2010-07-29 17:40:35 +00001626 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1627 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001628 }
Chris Lattner49382de2010-07-28 22:44:07 +00001629 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001630
Chris Lattner2acc6e32011-07-18 04:24:23 +00001631 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001632 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner021c3a32010-07-29 07:43:55 +00001633 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1634 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00001635 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1636 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00001637 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001638
Chris Lattner49382de2010-07-28 22:44:07 +00001639 // Okay, we don't have any better idea of what to pass, so we pass this in an
1640 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001641 unsigned TySizeInBytes =
1642 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00001643
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001644 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001645
Chris Lattner49382de2010-07-28 22:44:07 +00001646 // It is always safe to classify this as an integer type up to i64 that
1647 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001648 return llvm::IntegerType::get(getVMContext(),
1649 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00001650}
1651
Chris Lattner66e7b682010-09-01 00:50:20 +00001652
1653/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1654/// be used as elements of a two register pair to pass or return, return a
1655/// first class aggregate to represent them. For example, if the low part of
1656/// a by-value argument should be passed as i32* and the high part as float,
1657/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001658static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00001659GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Chris Lattner66e7b682010-09-01 00:50:20 +00001660 const llvm::TargetData &TD) {
1661 // In order to correctly satisfy the ABI, we need to the high part to start
1662 // at offset 8. If the high and low parts we inferred are both 4-byte types
1663 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1664 // the second element at offset 8. Check for this:
1665 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1666 unsigned HiAlign = TD.getABITypeAlignment(Hi);
1667 unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
1668 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001669
Chris Lattner66e7b682010-09-01 00:50:20 +00001670 // To handle this, we have to increase the size of the low part so that the
1671 // second element will start at an 8 byte offset. We can't increase the size
1672 // of the second element because it might make us access off the end of the
1673 // struct.
1674 if (HiStart != 8) {
1675 // There are only two sorts of types the ABI generation code can produce for
1676 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1677 // Promote these to a larger type.
1678 if (Lo->isFloatTy())
1679 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1680 else {
1681 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1682 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1683 }
1684 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001685
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001686 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001687
1688
Chris Lattner66e7b682010-09-01 00:50:20 +00001689 // Verify that the second element is at an 8-byte offset.
1690 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1691 "Invalid x86-64 argument pair!");
1692 return Result;
1693}
1694
Chris Lattner519f68c2010-07-28 23:06:14 +00001695ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00001696classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00001697 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1698 // classification algorithm.
1699 X86_64ABIInfo::Class Lo, Hi;
1700 classify(RetTy, 0, Lo, Hi);
1701
1702 // Check some invariants.
1703 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001704 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1705
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001706 llvm::Type *ResType = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001707 switch (Lo) {
1708 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001709 if (Hi == NoClass)
1710 return ABIArgInfo::getIgnore();
1711 // If the low part is just padding, it takes no register, leave ResType
1712 // null.
1713 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1714 "Unknown missing lo part");
1715 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001716
1717 case SSEUp:
1718 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00001719 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001720
1721 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1722 // hidden argument.
1723 case Memory:
1724 return getIndirectReturnResult(RetTy);
1725
1726 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1727 // available register of the sequence %rax, %rdx is used.
1728 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001729 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001730
Chris Lattnereb518b42010-07-29 21:42:50 +00001731 // If we have a sign or zero extended integer, make sure to return Extend
1732 // so that the parameter gets the right LLVM IR attributes.
1733 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1734 // Treat an enum type as its underlying type.
1735 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1736 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001737
Chris Lattnereb518b42010-07-29 21:42:50 +00001738 if (RetTy->isIntegralOrEnumerationType() &&
1739 RetTy->isPromotableIntegerType())
1740 return ABIArgInfo::getExtend();
1741 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001742 break;
1743
1744 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1745 // available SSE register of the sequence %xmm0, %xmm1 is used.
1746 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001747 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00001748 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001749
1750 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1751 // returned on the X87 stack in %st0 as 80-bit x87 number.
1752 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00001753 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00001754 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001755
1756 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1757 // part of the value is returned in %st0 and the imaginary part in
1758 // %st1.
1759 case ComplexX87:
1760 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00001761 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00001762 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00001763 NULL);
1764 break;
1765 }
1766
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001767 llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001768 switch (Hi) {
1769 // Memory was handled previously and X87 should
1770 // never occur as a hi class.
1771 case Memory:
1772 case X87:
David Blaikieb219cfc2011-09-23 05:06:16 +00001773 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001774
1775 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00001776 case NoClass:
1777 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001778
Chris Lattner3db4dde2010-09-01 00:20:33 +00001779 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001780 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001781 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1782 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001783 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00001784 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001785 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001786 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1787 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001788 break;
1789
1790 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001791 // is passed in the next available eightbyte chunk if the last used
1792 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00001793 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001794 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00001795 case SSEUp:
1796 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001797 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00001798 break;
1799
1800 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1801 // returned together with the previous X87 value in %st0.
1802 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001803 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00001804 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001805 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00001806 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00001807 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001808 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001809 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1810 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00001811 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001812 break;
1813 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001814
Chris Lattner3db4dde2010-09-01 00:20:33 +00001815 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00001816 // known to pass in the high eightbyte of the result. We do this by forming a
1817 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00001818 if (HighPart)
1819 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner519f68c2010-07-28 23:06:14 +00001820
Chris Lattnereb518b42010-07-29 21:42:50 +00001821 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00001822}
1823
Chris Lattnera3c109b2010-07-29 02:16:43 +00001824ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +00001825 unsigned &neededSSE) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001826 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001827 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001828
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001829 // Check some invariants.
1830 // FIXME: Enforce these by construction.
1831 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001832 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1833
1834 neededInt = 0;
1835 neededSSE = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001836 llvm::Type *ResType = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001837 switch (Lo) {
1838 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001839 if (Hi == NoClass)
1840 return ABIArgInfo::getIgnore();
1841 // If the low part is just padding, it takes no register, leave ResType
1842 // null.
1843 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1844 "Unknown missing lo part");
1845 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001846
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001847 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1848 // on the stack.
1849 case Memory:
1850
1851 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1852 // COMPLEX_X87, it is passed in memory.
1853 case X87:
1854 case ComplexX87:
Eli Friedmanded137f2011-06-29 07:04:55 +00001855 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1856 ++neededInt;
Chris Lattner9c254f02010-06-29 06:01:59 +00001857 return getIndirectResult(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001858
1859 case SSEUp:
1860 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00001861 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001862
1863 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1864 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1865 // and %r9 is used.
1866 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00001867 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001868
Chris Lattner49382de2010-07-28 22:44:07 +00001869 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001870 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00001871
1872 // If we have a sign or zero extended integer, make sure to return Extend
1873 // so that the parameter gets the right LLVM IR attributes.
1874 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1875 // Treat an enum type as its underlying type.
1876 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1877 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001878
Chris Lattnereb518b42010-07-29 21:42:50 +00001879 if (Ty->isIntegralOrEnumerationType() &&
1880 Ty->isPromotableIntegerType())
1881 return ABIArgInfo::getExtend();
1882 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001883
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001884 break;
1885
1886 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1887 // available SSE register is used, the registers are taken in the
1888 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00001889 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001890 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00001891 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00001892 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001893 break;
1894 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00001895 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001896
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001897 llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001898 switch (Hi) {
1899 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001900 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001901 // which is passed in memory.
1902 case Memory:
1903 case X87:
1904 case ComplexX87:
David Blaikieb219cfc2011-09-23 05:06:16 +00001905 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001906
1907 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001908
Chris Lattner645406a2010-09-01 00:24:35 +00001909 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001910 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00001911 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001912 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001913
Chris Lattner645406a2010-09-01 00:24:35 +00001914 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1915 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001916 break;
1917
1918 // X87Up generally doesn't occur here (long double is passed in
1919 // memory), except in situations involving unions.
1920 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00001921 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001922 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001923
Chris Lattner645406a2010-09-01 00:24:35 +00001924 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1925 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00001926
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001927 ++neededSSE;
1928 break;
1929
1930 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1931 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001932 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001933 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00001934 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001935 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001936 break;
1937 }
1938
Chris Lattner645406a2010-09-01 00:24:35 +00001939 // If a high part was specified, merge it together with the low part. It is
1940 // known to pass in the high eightbyte of the result. We do this by forming a
1941 // first class struct aggregate with the high and low part: {low, high}
1942 if (HighPart)
Chris Lattner66e7b682010-09-01 00:50:20 +00001943 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001944
Chris Lattnereb518b42010-07-29 21:42:50 +00001945 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001946}
1947
Chris Lattneree5dcd02010-07-29 02:31:05 +00001948void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001949
Chris Lattnera3c109b2010-07-29 02:16:43 +00001950 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001951
1952 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00001953 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001954
1955 // If the return value is indirect, then the hidden argument is consuming one
1956 // integer register.
1957 if (FI.getReturnInfo().isIndirect())
1958 --freeIntRegs;
1959
1960 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1961 // get assigned (in left-to-right order) for passing as follows...
1962 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1963 it != ie; ++it) {
Bill Wendling99aaae82010-10-18 23:51:38 +00001964 unsigned neededInt, neededSSE;
1965 it->info = classifyArgumentType(it->type, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001966
1967 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1968 // eightbyte of an argument, the whole argument is passed on the
1969 // stack. If registers have already been assigned for some
1970 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00001971 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001972 freeIntRegs -= neededInt;
1973 freeSSERegs -= neededSSE;
1974 } else {
Chris Lattner9c254f02010-06-29 06:01:59 +00001975 it->info = getIndirectResult(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001976 }
1977 }
1978}
1979
1980static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1981 QualType Ty,
1982 CodeGenFunction &CGF) {
1983 llvm::Value *overflow_arg_area_p =
1984 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1985 llvm::Value *overflow_arg_area =
1986 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1987
1988 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1989 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedman8d2fe422011-11-18 02:44:19 +00001990 // It isn't stated explicitly in the standard, but in practice we use
1991 // alignment greater than 16 where necessary.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001992 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1993 if (Align > 8) {
Eli Friedman8d2fe422011-11-18 02:44:19 +00001994 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
Owen Anderson0032b272009-08-13 21:57:51 +00001995 llvm::Value *Offset =
Eli Friedman8d2fe422011-11-18 02:44:19 +00001996 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001997 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1998 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00001999 CGF.Int64Ty);
Eli Friedman8d2fe422011-11-18 02:44:19 +00002000 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002001 overflow_arg_area =
2002 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2003 overflow_arg_area->getType(),
2004 "overflow_arg_area.align");
2005 }
2006
2007 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002008 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002009 llvm::Value *Res =
2010 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002011 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002012
2013 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2014 // l->overflow_arg_area + sizeof(type).
2015 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2016 // an 8 byte boundary.
2017
2018 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00002019 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00002020 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002021 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2022 "overflow_arg_area.next");
2023 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2024
2025 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2026 return Res;
2027}
2028
2029llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2030 CodeGenFunction &CGF) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00002031 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump1eb44332009-09-09 15:08:12 +00002032
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002033 // Assume that va_list type is correct; should be pointer to LLVM type:
2034 // struct {
2035 // i32 gp_offset;
2036 // i32 fp_offset;
2037 // i8* overflow_arg_area;
2038 // i8* reg_save_area;
2039 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00002040 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002041
Chris Lattnera14db752010-03-11 18:19:55 +00002042 Ty = CGF.getContext().getCanonicalType(Ty);
Bill Wendling99aaae82010-10-18 23:51:38 +00002043 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002044
2045 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2046 // in the registers. If not go to step 7.
2047 if (!neededInt && !neededSSE)
2048 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2049
2050 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2051 // general purpose registers needed to pass type and num_fp to hold
2052 // the number of floating point registers needed.
2053
2054 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2055 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2056 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2057 //
2058 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2059 // register save space).
2060
2061 llvm::Value *InRegs = 0;
2062 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2063 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2064 if (neededInt) {
2065 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2066 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002067 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2068 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002069 }
2070
2071 if (neededSSE) {
2072 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2073 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2074 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00002075 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2076 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002077 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2078 }
2079
2080 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2081 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2082 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2083 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2084
2085 // Emit code to load the value if it was passed in registers.
2086
2087 CGF.EmitBlock(InRegBlock);
2088
2089 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2090 // an offset of l->gp_offset and/or l->fp_offset. This may require
2091 // copying to a temporary location in case the parameter is passed
2092 // in different register classes or requires an alignment greater
2093 // than 8 for general purpose registers and 16 for XMM registers.
2094 //
2095 // FIXME: This really results in shameful code when we end up needing to
2096 // collect arguments from different places; often what should result in a
2097 // simple assembling of a structure from scattered addresses has many more
2098 // loads than necessary. Can we clean this up?
Chris Lattner2acc6e32011-07-18 04:24:23 +00002099 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002100 llvm::Value *RegAddr =
2101 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2102 "reg_save_area");
2103 if (neededInt && neededSSE) {
2104 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002105 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002106 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002107 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2108 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002109 llvm::Type *TyLo = ST->getElementType(0);
2110 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002111 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002112 "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002113 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2114 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002115 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2116 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002117 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2118 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002119 llvm::Value *V =
2120 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2121 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2122 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2123 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2124
Owen Andersona1cf15f2009-07-14 23:10:40 +00002125 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002126 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002127 } else if (neededInt) {
2128 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2129 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002130 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002131 } else if (neededSSE == 1) {
2132 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2133 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2134 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002135 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002136 assert(neededSSE == 2 && "Invalid number of needed registers!");
2137 // SSE registers are spaced 16 bytes apart in the register save
2138 // area, we need to collect the two eightbytes together.
2139 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002140 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Jay Foadef6de3d2011-07-11 09:56:20 +00002141 llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002142 llvm::Type *DblPtrTy =
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002143 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002144 llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002145 DoubleTy, NULL);
2146 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2147 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2148 DblPtrTy));
2149 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2150 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2151 DblPtrTy));
2152 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2153 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2154 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002155 }
2156
2157 // AMD64-ABI 3.5.7p5: Step 5. Set:
2158 // l->gp_offset = l->gp_offset + num_gp * 8
2159 // l->fp_offset = l->fp_offset + num_fp * 16.
2160 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002161 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002162 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2163 gp_offset_p);
2164 }
2165 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002166 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002167 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2168 fp_offset_p);
2169 }
2170 CGF.EmitBranch(ContBlock);
2171
2172 // Emit code to load the value if it was passed in memory.
2173
2174 CGF.EmitBlock(InMemBlock);
2175 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2176
2177 // Return the appropriate result.
2178
2179 CGF.EmitBlock(ContBlock);
Jay Foadbbf3bac2011-03-30 11:28:58 +00002180 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002181 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002182 ResAddr->addIncoming(RegAddr, InRegBlock);
2183 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002184 return ResAddr;
2185}
2186
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002187ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2188
2189 if (Ty->isVoidType())
2190 return ABIArgInfo::getIgnore();
2191
2192 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2193 Ty = EnumTy->getDecl()->getIntegerType();
2194
2195 uint64_t Size = getContext().getTypeSize(Ty);
2196
2197 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002198 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2199 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002200 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2201
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002202 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2203 if (Size == 128 &&
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002204 getContext().getTargetInfo().getTriple().getOS() == llvm::Triple::MinGW32)
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002205 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2206 Size));
2207
2208 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2209 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2210 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002211 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002212 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2213 Size));
2214
2215 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2216 }
2217
2218 if (Ty->isPromotableIntegerType())
2219 return ABIArgInfo::getExtend();
2220
2221 return ABIArgInfo::getDirect();
2222}
2223
2224void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2225
2226 QualType RetTy = FI.getReturnType();
2227 FI.getReturnInfo() = classify(RetTy);
2228
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002229 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2230 it != ie; ++it)
2231 it->info = classify(it->type);
2232}
2233
Chris Lattnerf13721d2010-08-31 16:44:54 +00002234llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2235 CodeGenFunction &CGF) const {
Chris Lattner2acc6e32011-07-18 04:24:23 +00002236 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2237 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002238
Chris Lattnerf13721d2010-08-31 16:44:54 +00002239 CGBuilderTy &Builder = CGF.Builder;
2240 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2241 "ap");
2242 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2243 llvm::Type *PTy =
2244 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2245 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2246
2247 uint64_t Offset =
2248 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2249 llvm::Value *NextAddr =
2250 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2251 "ap.next");
2252 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2253
2254 return AddrTyped;
2255}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002256
John McCallec853ba2010-03-11 00:10:12 +00002257// PowerPC-32
2258
2259namespace {
2260class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2261public:
Chris Lattnerea044322010-07-29 02:01:43 +00002262 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002263
John McCallec853ba2010-03-11 00:10:12 +00002264 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2265 // This is recovered from gcc output.
2266 return 1; // r1 is the dedicated stack pointer
2267 }
2268
2269 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002270 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002271};
2272
2273}
2274
2275bool
2276PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2277 llvm::Value *Address) const {
2278 // This is calculated from the LLVM and GCC tables and verified
2279 // against gcc output. AFAIK all ABIs use the same encoding.
2280
2281 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2282 llvm::LLVMContext &Context = CGF.getLLVMContext();
2283
Chris Lattner2acc6e32011-07-18 04:24:23 +00002284 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCallec853ba2010-03-11 00:10:12 +00002285 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2286 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2287 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2288
2289 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002290 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002291
2292 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002293 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002294
2295 // 64-76 are various 4-byte special-purpose registers:
2296 // 64: mq
2297 // 65: lr
2298 // 66: ctr
2299 // 67: ap
2300 // 68-75 cr0-7
2301 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002302 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002303
2304 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002305 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002306
2307 // 109: vrsave
2308 // 110: vscr
2309 // 111: spe_acc
2310 // 112: spefscr
2311 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002312 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002313
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002314 return false;
John McCallec853ba2010-03-11 00:10:12 +00002315}
2316
2317
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002318//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002319// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002320//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002321
2322namespace {
2323
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002324class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002325public:
2326 enum ABIKind {
2327 APCS = 0,
2328 AAPCS = 1,
2329 AAPCS_VFP
2330 };
2331
2332private:
2333 ABIKind Kind;
2334
2335public:
Chris Lattnerea044322010-07-29 02:01:43 +00002336 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002337
John McCall49e34be2011-08-30 01:42:09 +00002338 bool isEABI() const {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002339 StringRef Env = getContext().getTargetInfo().getTriple().getEnvironmentName();
John McCall49e34be2011-08-30 01:42:09 +00002340 return (Env == "gnueabi" || Env == "eabi");
2341 }
2342
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002343private:
2344 ABIKind getABIKind() const { return Kind; }
2345
Chris Lattnera3c109b2010-07-29 02:16:43 +00002346 ABIArgInfo classifyReturnType(QualType RetTy) const;
2347 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002348
Chris Lattneree5dcd02010-07-29 02:31:05 +00002349 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002350
2351 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2352 CodeGenFunction &CGF) const;
2353};
2354
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002355class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2356public:
Chris Lattnerea044322010-07-29 02:01:43 +00002357 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2358 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00002359
John McCall49e34be2011-08-30 01:42:09 +00002360 const ARMABIInfo &getABIInfo() const {
2361 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
2362 }
2363
John McCall6374c332010-03-06 00:35:14 +00002364 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2365 return 13;
2366 }
Roman Divacky09345d12011-05-18 19:36:54 +00002367
Chris Lattner5f9e2722011-07-23 10:55:15 +00002368 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCallf85e1932011-06-15 23:02:42 +00002369 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2370 }
2371
Roman Divacky09345d12011-05-18 19:36:54 +00002372 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2373 llvm::Value *Address) const {
2374 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2375 llvm::LLVMContext &Context = CGF.getLLVMContext();
2376
Chris Lattner2acc6e32011-07-18 04:24:23 +00002377 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
Roman Divacky09345d12011-05-18 19:36:54 +00002378 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2379
2380 // 0-15 are the 16 integer registers.
2381 AssignToArrayRange(Builder, Address, Four8, 0, 15);
2382
2383 return false;
2384 }
John McCall49e34be2011-08-30 01:42:09 +00002385
2386 unsigned getSizeOfUnwindException() const {
2387 if (getABIInfo().isEABI()) return 88;
2388 return TargetCodeGenInfo::getSizeOfUnwindException();
2389 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002390};
2391
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002392}
2393
Chris Lattneree5dcd02010-07-29 02:31:05 +00002394void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002395 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002396 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattnera3c109b2010-07-29 02:16:43 +00002397 it != ie; ++it)
2398 it->info = classifyArgumentType(it->type);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002399
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002400 // Always honor user-specified calling convention.
2401 if (FI.getCallingConvention() != llvm::CallingConv::C)
2402 return;
2403
2404 // Calling convention as default by an ABI.
Rafael Espindola25117ab2010-06-16 16:13:39 +00002405 llvm::CallingConv::ID DefaultCC;
John McCall49e34be2011-08-30 01:42:09 +00002406 if (isEABI())
Rafael Espindola25117ab2010-06-16 16:13:39 +00002407 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00002408 else
2409 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00002410
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002411 // If user did not ask for specific calling convention explicitly (e.g. via
2412 // pcs attribute), set effective calling convention if it's different than ABI
2413 // default.
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002414 switch (getABIKind()) {
2415 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002416 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2417 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002418 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002419 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002420 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2421 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002422 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002423 case AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002424 if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
2425 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002426 break;
2427 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002428}
2429
Bob Wilson194f06a2011-08-03 05:58:22 +00002430/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
2431/// aggregate. If HAMembers is non-null, the number of base elements
2432/// contained in the type is returned through it; this is used for the
2433/// recursive calls that check aggregate component types.
2434static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
2435 ASTContext &Context,
2436 uint64_t *HAMembers = 0) {
2437 uint64_t Members;
2438 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2439 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
2440 return false;
2441 Members *= AT->getSize().getZExtValue();
2442 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
2443 const RecordDecl *RD = RT->getDecl();
2444 if (RD->isUnion() || RD->hasFlexibleArrayMember())
2445 return false;
2446 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
2447 if (!CXXRD->isAggregate())
2448 return false;
2449 }
2450 Members = 0;
2451 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2452 i != e; ++i) {
2453 const FieldDecl *FD = *i;
2454 uint64_t FldMembers;
2455 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
2456 return false;
2457 Members += FldMembers;
2458 }
2459 } else {
2460 Members = 1;
2461 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2462 Members = 2;
2463 Ty = CT->getElementType();
2464 }
2465
2466 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
2467 // double, or 64-bit or 128-bit vectors.
2468 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
2469 if (BT->getKind() != BuiltinType::Float &&
2470 BT->getKind() != BuiltinType::Double)
2471 return false;
2472 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
2473 unsigned VecSize = Context.getTypeSize(VT);
2474 if (VecSize != 64 && VecSize != 128)
2475 return false;
2476 } else {
2477 return false;
2478 }
2479
2480 // The base type must be the same for all members. Vector types of the
2481 // same total size are treated as being equivalent here.
2482 const Type *TyPtr = Ty.getTypePtr();
2483 if (!Base)
2484 Base = TyPtr;
2485 if (Base != TyPtr &&
2486 (!Base->isVectorType() || !TyPtr->isVectorType() ||
2487 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
2488 return false;
2489 }
2490
2491 // Homogeneous Aggregates can have at most 4 members of the base type.
2492 if (HAMembers)
2493 *HAMembers = Members;
2494 return (Members <= 4);
2495}
2496
Chris Lattnera3c109b2010-07-29 02:16:43 +00002497ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +00002498 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002499 // Treat an enum type as its underlying type.
2500 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2501 Ty = EnumTy->getDecl()->getIntegerType();
2502
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002503 return (Ty->isPromotableIntegerType() ?
2504 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002505 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002506
Daniel Dunbar42025572009-09-14 21:54:03 +00002507 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002508 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00002509 return ABIArgInfo::getIgnore();
2510
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002511 // Structures with either a non-trivial destructor or a non-trivial
2512 // copy constructor are always indirect.
2513 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2514 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2515
Bob Wilson194f06a2011-08-03 05:58:22 +00002516 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
2517 // Homogeneous Aggregates need to be expanded.
2518 const Type *Base = 0;
2519 if (isHomogeneousAggregate(Ty, Base, getContext()))
2520 return ABIArgInfo::getExpand();
2521 }
2522
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00002523 // Otherwise, pass by coercing to a structure of the appropriate size.
2524 //
Bob Wilson53fc1a62011-08-01 23:39:04 +00002525 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
2526 // backend doesn't support byval.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002527 // FIXME: This doesn't handle alignment > 64 bits.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002528 llvm::Type* ElemTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002529 unsigned SizeRegs;
Bob Wilson53fc1a62011-08-01 23:39:04 +00002530 if (getContext().getTypeAlign(Ty) > 32) {
Stuart Hastings67d097e2011-04-27 17:24:02 +00002531 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2532 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Bob Wilson53fc1a62011-08-01 23:39:04 +00002533 } else {
2534 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2535 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Stuart Hastings67d097e2011-04-27 17:24:02 +00002536 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00002537
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002538 llvm::Type *STy =
Chris Lattner7650d952011-06-18 22:49:11 +00002539 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00002540 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002541}
2542
Chris Lattnera3c109b2010-07-29 02:16:43 +00002543static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00002544 llvm::LLVMContext &VMContext) {
2545 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2546 // is called integer-like if its size is less than or equal to one word, and
2547 // the offset of each of its addressable sub-fields is zero.
2548
2549 uint64_t Size = Context.getTypeSize(Ty);
2550
2551 // Check that the type fits in a word.
2552 if (Size > 32)
2553 return false;
2554
2555 // FIXME: Handle vector types!
2556 if (Ty->isVectorType())
2557 return false;
2558
Daniel Dunbarb0d58192009-09-14 02:20:34 +00002559 // Float types are never treated as "integer like".
2560 if (Ty->isRealFloatingType())
2561 return false;
2562
Daniel Dunbar98303b92009-09-13 08:03:58 +00002563 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00002564 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00002565 return true;
2566
Daniel Dunbar45815812010-02-01 23:31:26 +00002567 // Small complex integer types are "integer like".
2568 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2569 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002570
2571 // Single element and zero sized arrays should be allowed, by the definition
2572 // above, but they are not.
2573
2574 // Otherwise, it must be a record type.
2575 const RecordType *RT = Ty->getAs<RecordType>();
2576 if (!RT) return false;
2577
2578 // Ignore records with flexible arrays.
2579 const RecordDecl *RD = RT->getDecl();
2580 if (RD->hasFlexibleArrayMember())
2581 return false;
2582
2583 // Check that all sub-fields are at offset 0, and are themselves "integer
2584 // like".
2585 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2586
2587 bool HadField = false;
2588 unsigned idx = 0;
2589 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2590 i != e; ++i, ++idx) {
2591 const FieldDecl *FD = *i;
2592
Daniel Dunbar679855a2010-01-29 03:22:29 +00002593 // Bit-fields are not addressable, we only need to verify they are "integer
2594 // like". We still have to disallow a subsequent non-bitfield, for example:
2595 // struct { int : 0; int x }
2596 // is non-integer like according to gcc.
2597 if (FD->isBitField()) {
2598 if (!RD->isUnion())
2599 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002600
Daniel Dunbar679855a2010-01-29 03:22:29 +00002601 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2602 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002603
Daniel Dunbar679855a2010-01-29 03:22:29 +00002604 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002605 }
2606
Daniel Dunbar679855a2010-01-29 03:22:29 +00002607 // Check if this field is at offset 0.
2608 if (Layout.getFieldOffset(idx) != 0)
2609 return false;
2610
Daniel Dunbar98303b92009-09-13 08:03:58 +00002611 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2612 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002613
Daniel Dunbar679855a2010-01-29 03:22:29 +00002614 // Only allow at most one field in a structure. This doesn't match the
2615 // wording above, but follows gcc in situations with a field following an
2616 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00002617 if (!RD->isUnion()) {
2618 if (HadField)
2619 return false;
2620
2621 HadField = true;
2622 }
2623 }
2624
2625 return true;
2626}
2627
Chris Lattnera3c109b2010-07-29 02:16:43 +00002628ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002629 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002630 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00002631
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00002632 // Large vector types should be returned via memory.
2633 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
2634 return ABIArgInfo::getIndirect(0);
2635
John McCalld608cdb2010-08-22 10:59:02 +00002636 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002637 // Treat an enum type as its underlying type.
2638 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2639 RetTy = EnumTy->getDecl()->getIntegerType();
2640
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002641 return (RetTy->isPromotableIntegerType() ?
2642 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002643 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002644
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002645 // Structures with either a non-trivial destructor or a non-trivial
2646 // copy constructor are always indirect.
2647 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2648 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2649
Daniel Dunbar98303b92009-09-13 08:03:58 +00002650 // Are we following APCS?
2651 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002652 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00002653 return ABIArgInfo::getIgnore();
2654
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002655 // Complex types are all returned as packed integers.
2656 //
2657 // FIXME: Consider using 2 x vector types if the back end handles them
2658 // correctly.
2659 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00002660 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00002661 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002662
Daniel Dunbar98303b92009-09-13 08:03:58 +00002663 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002664 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002665 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002666 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002667 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002668 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002669 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002670 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2671 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002672 }
2673
2674 // Otherwise return in memory.
2675 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002676 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002677
2678 // Otherwise this is an AAPCS variant.
2679
Chris Lattnera3c109b2010-07-29 02:16:43 +00002680 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00002681 return ABIArgInfo::getIgnore();
2682
Bob Wilson3b694fa2011-11-02 04:51:36 +00002683 // Check for homogeneous aggregates with AAPCS-VFP.
2684 if (getABIKind() == AAPCS_VFP) {
2685 const Type *Base = 0;
2686 if (isHomogeneousAggregate(RetTy, Base, getContext()))
2687 // Homogeneous Aggregates are returned directly.
2688 return ABIArgInfo::getDirect();
2689 }
2690
Daniel Dunbar98303b92009-09-13 08:03:58 +00002691 // Aggregates <= 4 bytes are returned in r0; other aggregates
2692 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002693 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00002694 if (Size <= 32) {
2695 // Return in the smallest viable integer type.
2696 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002697 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002698 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002699 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2700 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002701 }
2702
Daniel Dunbar98303b92009-09-13 08:03:58 +00002703 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002704}
2705
2706llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00002707 CodeGenFunction &CGF) const {
Chris Lattner2acc6e32011-07-18 04:24:23 +00002708 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2709 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002710
2711 CGBuilderTy &Builder = CGF.Builder;
2712 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2713 "ap");
2714 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Rafael Espindolae164c182011-08-02 22:33:37 +00002715 // Handle address alignment for type alignment > 32 bits
2716 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
2717 if (TyAlign > 4) {
2718 assert((TyAlign & (TyAlign - 1)) == 0 &&
2719 "Alignment is not power of 2!");
2720 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
2721 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
2722 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
2723 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2724 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002725 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00002726 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002727 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2728
2729 uint64_t Offset =
2730 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2731 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00002732 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002733 "ap.next");
2734 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2735
2736 return AddrTyped;
2737}
2738
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002739//===----------------------------------------------------------------------===//
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002740// PTX ABI Implementation
2741//===----------------------------------------------------------------------===//
2742
2743namespace {
2744
2745class PTXABIInfo : public ABIInfo {
2746public:
2747 PTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2748
2749 ABIArgInfo classifyReturnType(QualType RetTy) const;
2750 ABIArgInfo classifyArgumentType(QualType Ty) const;
2751
2752 virtual void computeInfo(CGFunctionInfo &FI) const;
2753 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2754 CodeGenFunction &CFG) const;
2755};
2756
2757class PTXTargetCodeGenInfo : public TargetCodeGenInfo {
2758public:
2759 PTXTargetCodeGenInfo(CodeGenTypes &CGT)
2760 : TargetCodeGenInfo(new PTXABIInfo(CGT)) {}
Justin Holewinski818eafb2011-10-05 17:58:44 +00002761
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00002762 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2763 CodeGen::CodeGenModule &M) const;
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002764};
2765
2766ABIArgInfo PTXABIInfo::classifyReturnType(QualType RetTy) const {
2767 if (RetTy->isVoidType())
2768 return ABIArgInfo::getIgnore();
2769 if (isAggregateTypeForABI(RetTy))
2770 return ABIArgInfo::getIndirect(0);
2771 return ABIArgInfo::getDirect();
2772}
2773
2774ABIArgInfo PTXABIInfo::classifyArgumentType(QualType Ty) const {
2775 if (isAggregateTypeForABI(Ty))
2776 return ABIArgInfo::getIndirect(0);
2777
2778 return ABIArgInfo::getDirect();
2779}
2780
2781void PTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
2782 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2783 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2784 it != ie; ++it)
2785 it->info = classifyArgumentType(it->type);
2786
2787 // Always honor user-specified calling convention.
2788 if (FI.getCallingConvention() != llvm::CallingConv::C)
2789 return;
2790
2791 // Calling convention as default by an ABI.
2792 llvm::CallingConv::ID DefaultCC;
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002793 const LangOptions &LangOpts = getContext().getLangOptions();
2794 if (LangOpts.OpenCL || LangOpts.CUDA) {
2795 // If we are in OpenCL or CUDA mode, then default to device functions
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002796 DefaultCC = llvm::CallingConv::PTX_Device;
Justin Holewinski818eafb2011-10-05 17:58:44 +00002797 } else {
2798 // If we are in standard C/C++ mode, use the triple to decide on the default
2799 StringRef Env =
2800 getContext().getTargetInfo().getTriple().getEnvironmentName();
2801 if (Env == "device")
2802 DefaultCC = llvm::CallingConv::PTX_Device;
2803 else
2804 DefaultCC = llvm::CallingConv::PTX_Kernel;
2805 }
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002806 FI.setEffectiveCallingConvention(DefaultCC);
Justin Holewinski818eafb2011-10-05 17:58:44 +00002807
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002808}
2809
2810llvm::Value *PTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2811 CodeGenFunction &CFG) const {
2812 llvm_unreachable("PTX does not support varargs");
2813 return 0;
2814}
2815
Justin Holewinski818eafb2011-10-05 17:58:44 +00002816void PTXTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2817 llvm::GlobalValue *GV,
2818 CodeGen::CodeGenModule &M) const{
2819 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2820 if (!FD) return;
2821
2822 llvm::Function *F = cast<llvm::Function>(GV);
2823
2824 // Perform special handling in OpenCL mode
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002825 if (M.getLangOptions().OpenCL) {
Justin Holewinski818eafb2011-10-05 17:58:44 +00002826 // Use OpenCL function attributes to set proper calling conventions
2827 // By default, all functions are device functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00002828 if (FD->hasAttr<OpenCLKernelAttr>()) {
2829 // OpenCL __kernel functions get a kernel calling convention
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002830 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00002831 // And kernel functions are not subject to inlining
2832 F->addFnAttr(llvm::Attribute::NoInline);
2833 }
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002834 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00002835
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002836 // Perform special handling in CUDA mode.
2837 if (M.getLangOptions().CUDA) {
2838 // CUDA __global__ functions get a kernel calling convention. Since
2839 // __global__ functions cannot be called from the device, we do not
2840 // need to set the noinline attribute.
2841 if (FD->getAttr<CUDAGlobalAttr>())
2842 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00002843 }
2844}
2845
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002846}
2847
2848//===----------------------------------------------------------------------===//
Wesley Peck276fdf42010-12-19 19:57:51 +00002849// MBlaze ABI Implementation
2850//===----------------------------------------------------------------------===//
2851
2852namespace {
2853
2854class MBlazeABIInfo : public ABIInfo {
2855public:
2856 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2857
2858 bool isPromotableIntegerType(QualType Ty) const;
2859
2860 ABIArgInfo classifyReturnType(QualType RetTy) const;
2861 ABIArgInfo classifyArgumentType(QualType RetTy) const;
2862
2863 virtual void computeInfo(CGFunctionInfo &FI) const {
2864 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2865 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2866 it != ie; ++it)
2867 it->info = classifyArgumentType(it->type);
2868 }
2869
2870 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2871 CodeGenFunction &CGF) const;
2872};
2873
2874class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
2875public:
2876 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
2877 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
2878 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2879 CodeGen::CodeGenModule &M) const;
2880};
2881
2882}
2883
2884bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
2885 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
2886 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2887 switch (BT->getKind()) {
2888 case BuiltinType::Bool:
2889 case BuiltinType::Char_S:
2890 case BuiltinType::Char_U:
2891 case BuiltinType::SChar:
2892 case BuiltinType::UChar:
2893 case BuiltinType::Short:
2894 case BuiltinType::UShort:
2895 return true;
2896 default:
2897 return false;
2898 }
2899 return false;
2900}
2901
2902llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2903 CodeGenFunction &CGF) const {
2904 // FIXME: Implement
2905 return 0;
2906}
2907
2908
2909ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
2910 if (RetTy->isVoidType())
2911 return ABIArgInfo::getIgnore();
2912 if (isAggregateTypeForABI(RetTy))
2913 return ABIArgInfo::getIndirect(0);
2914
2915 return (isPromotableIntegerType(RetTy) ?
2916 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2917}
2918
2919ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
2920 if (isAggregateTypeForABI(Ty))
2921 return ABIArgInfo::getIndirect(0);
2922
2923 return (isPromotableIntegerType(Ty) ?
2924 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2925}
2926
2927void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2928 llvm::GlobalValue *GV,
2929 CodeGen::CodeGenModule &M)
2930 const {
2931 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2932 if (!FD) return;
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00002933
Wesley Peck276fdf42010-12-19 19:57:51 +00002934 llvm::CallingConv::ID CC = llvm::CallingConv::C;
2935 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
2936 CC = llvm::CallingConv::MBLAZE_INTR;
2937 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
2938 CC = llvm::CallingConv::MBLAZE_SVOL;
2939
2940 if (CC != llvm::CallingConv::C) {
2941 // Handle 'interrupt_handler' attribute:
2942 llvm::Function *F = cast<llvm::Function>(GV);
2943
2944 // Step 1: Set ISR calling convention.
2945 F->setCallingConv(CC);
2946
2947 // Step 2: Add attributes goodness.
2948 F->addFnAttr(llvm::Attribute::NoInline);
2949 }
2950
2951 // Step 3: Emit _interrupt_handler alias.
2952 if (CC == llvm::CallingConv::MBLAZE_INTR)
2953 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2954 "_interrupt_handler", GV, &M.getModule());
2955}
2956
2957
2958//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002959// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002960//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002961
2962namespace {
2963
2964class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2965public:
Chris Lattnerea044322010-07-29 02:01:43 +00002966 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
2967 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002968 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2969 CodeGen::CodeGenModule &M) const;
2970};
2971
2972}
2973
2974void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2975 llvm::GlobalValue *GV,
2976 CodeGen::CodeGenModule &M) const {
2977 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2978 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2979 // Handle 'interrupt' attribute:
2980 llvm::Function *F = cast<llvm::Function>(GV);
2981
2982 // Step 1: Set ISR calling convention.
2983 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2984
2985 // Step 2: Add attributes goodness.
2986 F->addFnAttr(llvm::Attribute::NoInline);
2987
2988 // Step 3: Emit ISR vector alias.
2989 unsigned Num = attr->getNumber() + 0xffe0;
2990 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002991 "vector_" + Twine::utohexstr(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002992 GV, &M.getModule());
2993 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002994 }
2995}
2996
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002997//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00002998// MIPS ABI Implementation. This works for both little-endian and
2999// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003000//===----------------------------------------------------------------------===//
3001
John McCallaeeb7012010-05-27 06:19:26 +00003002namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00003003class MipsABIInfo : public ABIInfo {
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003004 bool IsO32;
Akira Hatanakab551dd32011-11-03 00:05:50 +00003005 unsigned MinABIStackAlignInBytes;
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003006 llvm::Type* HandleStructTy(QualType Ty) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003007public:
Akira Hatanakab551dd32011-11-03 00:05:50 +00003008 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
3009 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8) {}
Akira Hatanaka619e8872011-06-02 00:09:17 +00003010
3011 ABIArgInfo classifyReturnType(QualType RetTy) const;
3012 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3013 virtual void computeInfo(CGFunctionInfo &FI) const;
3014 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3015 CodeGenFunction &CGF) const;
3016};
3017
John McCallaeeb7012010-05-27 06:19:26 +00003018class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003019 unsigned SizeOfUnwindException;
John McCallaeeb7012010-05-27 06:19:26 +00003020public:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003021 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
3022 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
3023 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCallaeeb7012010-05-27 06:19:26 +00003024
3025 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
3026 return 29;
3027 }
3028
3029 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003030 llvm::Value *Address) const;
John McCall49e34be2011-08-30 01:42:09 +00003031
3032 unsigned getSizeOfUnwindException() const {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003033 return SizeOfUnwindException;
John McCall49e34be2011-08-30 01:42:09 +00003034 }
John McCallaeeb7012010-05-27 06:19:26 +00003035};
3036}
3037
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003038// In N32/64, an aligned double precision floating point field is passed in
3039// a register.
3040llvm::Type* MipsABIInfo::HandleStructTy(QualType Ty) const {
3041 if (IsO32)
3042 return 0;
3043
3044 const RecordType *RT = Ty->getAsStructureType();
3045
3046 if (!RT)
3047 return 0;
3048
3049 const RecordDecl *RD = RT->getDecl();
3050 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3051 uint64_t StructSize = getContext().getTypeSize(Ty);
3052 assert(!(StructSize % 8) && "Size of structure must be multiple of 8.");
3053
3054 SmallVector<llvm::Type*, 8> ArgList;
3055 uint64_t LastOffset = 0;
3056 unsigned idx = 0;
3057 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
3058
3059 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3060 i != e; ++i, ++idx) {
3061 const QualType Ty = (*i)->getType();
3062 const BuiltinType *BT = Ty->getAs<BuiltinType>();
3063
3064 if (!BT || BT->getKind() != BuiltinType::Double)
3065 continue;
3066
3067 uint64_t Offset = Layout.getFieldOffset(idx);
3068 if (Offset % 64) // Ignore doubles that are not aligned.
3069 continue;
3070
3071 // Add ((Offset - LastOffset) / 64) args of type i64.
3072 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
3073 ArgList.push_back(I64);
3074
3075 // Add double type.
3076 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
3077 LastOffset = Offset + 64;
3078 }
3079
3080 // This structure doesn't have an aligned double field.
3081 if (!LastOffset)
3082 return 0;
3083
3084 // Add ((StructSize - LastOffset) / 64) args of type i64.
3085 for (unsigned N = (StructSize - LastOffset) / 64; N; --N)
3086 ArgList.push_back(I64);
3087
Akira Hatanakab49d5a62011-11-03 23:31:00 +00003088 // If the size of the remainder is not zero, add one more integer type to
3089 // ArgList.
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003090 unsigned R = (StructSize - LastOffset) % 64;
Akira Hatanakab49d5a62011-11-03 23:31:00 +00003091 if (R)
3092 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003093
3094 return llvm::StructType::get(getVMContext(), ArgList);
3095}
3096
Akira Hatanaka619e8872011-06-02 00:09:17 +00003097ABIArgInfo MipsABIInfo::classifyArgumentType(QualType Ty) const {
3098 if (isAggregateTypeForABI(Ty)) {
3099 // Ignore empty aggregates.
3100 if (getContext().getTypeSize(Ty) == 0)
3101 return ABIArgInfo::getIgnore();
3102
Akira Hatanaka511949b2011-08-01 18:09:58 +00003103 // Records with non trivial destructors/constructors should not be passed
3104 // by value.
3105 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
3106 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3107
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003108 llvm::Type *ResType;
3109 if ((ResType = HandleStructTy(Ty)))
3110 return ABIArgInfo::getDirect(ResType);
3111
Akira Hatanaka619e8872011-06-02 00:09:17 +00003112 return ABIArgInfo::getIndirect(0);
3113 }
3114
3115 // Treat an enum type as its underlying type.
3116 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3117 Ty = EnumTy->getDecl()->getIntegerType();
3118
3119 return (Ty->isPromotableIntegerType() ?
3120 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3121}
3122
3123ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
3124 if (RetTy->isVoidType())
3125 return ABIArgInfo::getIgnore();
3126
3127 if (isAggregateTypeForABI(RetTy)) {
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003128 if ((IsO32 && RetTy->isAnyComplexType()) ||
3129 (!IsO32 && (getContext().getTypeSize(RetTy) <= 128)))
Akira Hatanaka619e8872011-06-02 00:09:17 +00003130 return ABIArgInfo::getDirect();
3131
3132 return ABIArgInfo::getIndirect(0);
3133 }
3134
3135 // Treat an enum type as its underlying type.
3136 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3137 RetTy = EnumTy->getDecl()->getIntegerType();
3138
3139 return (RetTy->isPromotableIntegerType() ?
3140 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3141}
3142
3143void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
3144 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3145 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3146 it != ie; ++it)
3147 it->info = classifyArgumentType(it->type);
3148}
3149
3150llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3151 CodeGenFunction &CGF) const {
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003152 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
3153 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
3154
3155 CGBuilderTy &Builder = CGF.Builder;
3156 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3157 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
3158 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
3159 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3160 llvm::Value *AddrTyped;
3161
3162 if (TypeAlign > MinABIStackAlignInBytes) {
3163 llvm::Value *AddrAsInt32 = CGF.Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3164 llvm::Value *Inc = llvm::ConstantInt::get(CGF.Int32Ty, TypeAlign - 1);
3165 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -TypeAlign);
3166 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt32, Inc);
3167 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
3168 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
3169 }
3170 else
3171 AddrTyped = Builder.CreateBitCast(Addr, PTy);
3172
3173 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka7b0a0382011-08-12 02:30:14 +00003174 TypeAlign = std::max(TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003175 uint64_t Offset =
3176 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
3177 llvm::Value *NextAddr =
3178 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
3179 "ap.next");
3180 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3181
3182 return AddrTyped;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003183}
3184
John McCallaeeb7012010-05-27 06:19:26 +00003185bool
3186MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3187 llvm::Value *Address) const {
3188 // This information comes from gcc's implementation, which seems to
3189 // as canonical as it gets.
3190
3191 CodeGen::CGBuilderTy &Builder = CGF.Builder;
3192 llvm::LLVMContext &Context = CGF.getLLVMContext();
3193
3194 // Everything on MIPS is 4 bytes. Double-precision FP registers
3195 // are aliased to pairs of single-precision FP registers.
Chris Lattner2acc6e32011-07-18 04:24:23 +00003196 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCallaeeb7012010-05-27 06:19:26 +00003197 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
3198
3199 // 0-31 are the general purpose registers, $0 - $31.
3200 // 32-63 are the floating-point registers, $f0 - $f31.
3201 // 64 and 65 are the multiply/divide registers, $hi and $lo.
3202 // 66 is the (notional, I think) register for signal-handler return.
3203 AssignToArrayRange(Builder, Address, Four8, 0, 65);
3204
3205 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
3206 // They are one bit wide and ignored here.
3207
3208 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
3209 // (coprocessor 1 is the FP unit)
3210 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
3211 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
3212 // 176-181 are the DSP accumulator registers.
3213 AssignToArrayRange(Builder, Address, Four8, 80, 181);
3214
3215 return false;
3216}
3217
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003218//===----------------------------------------------------------------------===//
3219// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
3220// Currently subclassed only to implement custom OpenCL C function attribute
3221// handling.
3222//===----------------------------------------------------------------------===//
3223
3224namespace {
3225
3226class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
3227public:
3228 TCETargetCodeGenInfo(CodeGenTypes &CGT)
3229 : DefaultTargetCodeGenInfo(CGT) {}
3230
3231 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3232 CodeGen::CodeGenModule &M) const;
3233};
3234
3235void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3236 llvm::GlobalValue *GV,
3237 CodeGen::CodeGenModule &M) const {
3238 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3239 if (!FD) return;
3240
3241 llvm::Function *F = cast<llvm::Function>(GV);
3242
3243 if (M.getLangOptions().OpenCL) {
3244 if (FD->hasAttr<OpenCLKernelAttr>()) {
3245 // OpenCL C Kernel functions are not subject to inlining
3246 F->addFnAttr(llvm::Attribute::NoInline);
3247
3248 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
3249
3250 // Convert the reqd_work_group_size() attributes to metadata.
3251 llvm::LLVMContext &Context = F->getContext();
3252 llvm::NamedMDNode *OpenCLMetadata =
3253 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
3254
3255 SmallVector<llvm::Value*, 5> Operands;
3256 Operands.push_back(F);
3257
3258 Operands.push_back(llvm::Constant::getIntegerValue(
3259 llvm::Type::getInt32Ty(Context),
3260 llvm::APInt(
3261 32,
3262 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
3263 Operands.push_back(llvm::Constant::getIntegerValue(
3264 llvm::Type::getInt32Ty(Context),
3265 llvm::APInt(
3266 32,
3267 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
3268 Operands.push_back(llvm::Constant::getIntegerValue(
3269 llvm::Type::getInt32Ty(Context),
3270 llvm::APInt(
3271 32,
3272 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
3273
3274 // Add a boolean constant operand for "required" (true) or "hint" (false)
3275 // for implementing the work_group_size_hint attr later. Currently
3276 // always true as the hint is not yet implemented.
3277 Operands.push_back(llvm::ConstantInt::getTrue(llvm::Type::getInt1Ty(Context)));
3278
3279 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
3280 }
3281 }
3282 }
3283}
3284
3285}
John McCallaeeb7012010-05-27 06:19:26 +00003286
Chris Lattnerea044322010-07-29 02:01:43 +00003287const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003288 if (TheTargetCodeGenInfo)
3289 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003290
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003291 const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00003292 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003293 default:
Chris Lattnerea044322010-07-29 02:01:43 +00003294 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003295
John McCallaeeb7012010-05-27 06:19:26 +00003296 case llvm::Triple::mips:
3297 case llvm::Triple::mipsel:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003298 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCallaeeb7012010-05-27 06:19:26 +00003299
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00003300 case llvm::Triple::mips64:
3301 case llvm::Triple::mips64el:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003302 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00003303
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003304 case llvm::Triple::arm:
3305 case llvm::Triple::thumb:
Sandeep Patel34c1af82011-04-05 00:23:47 +00003306 {
3307 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003308
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003309 if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
Sandeep Patel34c1af82011-04-05 00:23:47 +00003310 Kind = ARMABIInfo::APCS;
3311 else if (CodeGenOpts.FloatABI == "hard")
3312 Kind = ARMABIInfo::AAPCS_VFP;
3313
3314 return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind));
3315 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003316
John McCallec853ba2010-03-11 00:10:12 +00003317 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00003318 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00003319
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003320 case llvm::Triple::ptx32:
3321 case llvm::Triple::ptx64:
3322 return *(TheTargetCodeGenInfo = new PTXTargetCodeGenInfo(Types));
3323
Wesley Peck276fdf42010-12-19 19:57:51 +00003324 case llvm::Triple::mblaze:
3325 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
3326
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003327 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00003328 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003329
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003330 case llvm::Triple::tce:
3331 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
3332
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003333 case llvm::Triple::x86: {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003334 bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003335
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00003336 if (Triple.isOSDarwin())
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003337 return *(TheTargetCodeGenInfo =
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003338 new X86_32TargetCodeGenInfo(Types, true, true, DisableMMX));
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00003339
3340 switch (Triple.getOS()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003341 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003342 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00003343 case llvm::Triple::AuroraUX:
3344 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00003345 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003346 case llvm::Triple::OpenBSD:
Benjamin Kramer8e50a962011-02-02 18:59:27 +00003347 case llvm::Triple::NetBSD:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003348 return *(TheTargetCodeGenInfo =
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003349 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003350
3351 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003352 return *(TheTargetCodeGenInfo =
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003353 new X86_32TargetCodeGenInfo(Types, false, false, DisableMMX));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003354 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003355 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003356
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003357 case llvm::Triple::x86_64:
Chris Lattnerf13721d2010-08-31 16:44:54 +00003358 switch (Triple.getOS()) {
3359 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00003360 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00003361 case llvm::Triple::Cygwin:
3362 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
3363 default:
3364 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
3365 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003366 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003367}