blob: 2c93249e3b6ac7905e8f2b9a9cd1d3425b74d2fd [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
Daniel Dunbar98303b92009-09-13 08:03:58 +0000120 // Constant arrays of empty records count as empty, strip them off.
121 if (AllowArrays)
122 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
123 FT = AT->getElementType();
124
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000125 const RecordType *RT = FT->getAs<RecordType>();
126 if (!RT)
127 return false;
128
129 // C++ record fields are never empty, at least in the Itanium ABI.
130 //
131 // FIXME: We should use a predicate for whether this behavior is true in the
132 // current ABI.
133 if (isa<CXXRecordDecl>(RT->getDecl()))
134 return false;
135
Daniel Dunbar98303b92009-09-13 08:03:58 +0000136 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000137}
138
139/// isEmptyRecord - Return true iff a structure contains only empty
140/// fields. Note that a structure with a flexible array member is not
141/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000142static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000143 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000144 if (!RT)
145 return 0;
146 const RecordDecl *RD = RT->getDecl();
147 if (RD->hasFlexibleArrayMember())
148 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000149
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000150 // If this is a C++ record, check the bases first.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000151 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000152 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
153 e = CXXRD->bases_end(); i != e; ++i)
154 if (!isEmptyRecord(Context, i->getType(), true))
155 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000156
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000157 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
158 i != e; ++i)
Daniel Dunbar98303b92009-09-13 08:03:58 +0000159 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000160 return false;
161 return true;
162}
163
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000164/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
165/// a non-trivial destructor or a non-trivial copy constructor.
166static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
167 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
168 if (!RD)
169 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000170
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000171 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
172}
173
174/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
175/// a record type with either a non-trivial destructor or a non-trivial copy
176/// constructor.
177static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
178 const RecordType *RT = T->getAs<RecordType>();
179 if (!RT)
180 return false;
181
182 return hasNonTrivialDestructorOrCopyConstructor(RT);
183}
184
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000185/// isSingleElementStruct - Determine if a structure is a "single
186/// element struct", i.e. it has exactly one non-empty field or
187/// exactly one field which is itself a single element
188/// struct. Structures with flexible array members are never
189/// considered single element structs.
190///
191/// \return The field declaration for the single non-empty field, if
192/// it exists.
193static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
194 const RecordType *RT = T->getAsStructureType();
195 if (!RT)
196 return 0;
197
198 const RecordDecl *RD = RT->getDecl();
199 if (RD->hasFlexibleArrayMember())
200 return 0;
201
202 const Type *Found = 0;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000203
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000204 // If this is a C++ record, check the bases first.
205 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
206 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
207 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000208 // Ignore empty records.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000209 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000210 continue;
211
212 // If we already found an element then this isn't a single-element struct.
213 if (Found)
214 return 0;
215
216 // If this is non-empty and not a single element struct, the composite
217 // cannot be a single element struct.
218 Found = isSingleElementStruct(i->getType(), Context);
219 if (!Found)
220 return 0;
221 }
222 }
223
224 // Check for single element.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000225 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
226 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000227 const FieldDecl *FD = *i;
228 QualType FT = FD->getType();
229
230 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000231 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000232 continue;
233
234 // If we already found an element then this isn't a single-element
235 // struct.
236 if (Found)
237 return 0;
238
239 // Treat single element arrays as the element.
240 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
241 if (AT->getSize().getZExtValue() != 1)
242 break;
243 FT = AT->getElementType();
244 }
245
John McCalld608cdb2010-08-22 10:59:02 +0000246 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000247 Found = FT.getTypePtr();
248 } else {
249 Found = isSingleElementStruct(FT, Context);
250 if (!Found)
251 return 0;
252 }
253 }
254
255 return Found;
256}
257
258static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbara1842d32010-05-14 03:40:53 +0000259 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000260 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
261 !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000262 return false;
263
264 uint64_t Size = Context.getTypeSize(Ty);
265 return Size == 32 || Size == 64;
266}
267
Daniel Dunbar53012f42009-11-09 01:33:53 +0000268/// canExpandIndirectArgument - Test whether an argument type which is to be
269/// passed indirectly (on the stack) would have the equivalent layout if it was
270/// expanded into separate arguments. If so, we prefer to do the latter to avoid
271/// inhibiting optimizations.
272///
273// FIXME: This predicate is missing many cases, currently it just follows
274// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
275// should probably make this smarter, or better yet make the LLVM backend
276// capable of handling it.
277static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
278 // We can only expand structure types.
279 const RecordType *RT = Ty->getAs<RecordType>();
280 if (!RT)
281 return false;
282
283 // We can only expand (C) structures.
284 //
285 // FIXME: This needs to be generalized to handle classes as well.
286 const RecordDecl *RD = RT->getDecl();
287 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
288 return false;
289
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000290 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
291 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000292 const FieldDecl *FD = *i;
293
294 if (!is32Or64BitBasicType(FD->getType(), Context))
295 return false;
296
297 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
298 // how to expand them yet, and the predicate for telling if a bitfield still
299 // counts as "basic" is more complicated than what we were doing previously.
300 if (FD->isBitField())
301 return false;
302 }
303
304 return true;
305}
306
307namespace {
308/// DefaultABIInfo - The default implementation for ABI specific
309/// details. This implementation provides information which results in
310/// self-consistent and sensible LLVM IR generation, but does not
311/// conform to any particular ABI.
312class DefaultABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +0000313public:
314 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000315
Chris Lattnera3c109b2010-07-29 02:16:43 +0000316 ABIArgInfo classifyReturnType(QualType RetTy) const;
317 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000318
Chris Lattneree5dcd02010-07-29 02:31:05 +0000319 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000320 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000321 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
322 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000323 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000324 }
325
326 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
327 CodeGenFunction &CGF) const;
328};
329
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000330class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
331public:
Chris Lattnerea044322010-07-29 02:01:43 +0000332 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
333 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000334};
335
336llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
337 CodeGenFunction &CGF) const {
338 return 0;
339}
340
Chris Lattnera3c109b2010-07-29 02:16:43 +0000341ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
Jan Wen Voung90306932011-11-03 00:59:44 +0000342 if (isAggregateTypeForABI(Ty)) {
343 // Records with non trivial destructors/constructors should not be passed
344 // by value.
345 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
346 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
347
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000348 return ABIArgInfo::getIndirect(0);
Jan Wen Voung90306932011-11-03 00:59:44 +0000349 }
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000350
Chris Lattnera14db752010-03-11 18:19:55 +0000351 // Treat an enum type as its underlying type.
352 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
353 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000354
Chris Lattnera14db752010-03-11 18:19:55 +0000355 return (Ty->isPromotableIntegerType() ?
356 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000357}
358
Bob Wilson0024f942011-01-10 23:54:17 +0000359ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
360 if (RetTy->isVoidType())
361 return ABIArgInfo::getIgnore();
362
363 if (isAggregateTypeForABI(RetTy))
364 return ABIArgInfo::getIndirect(0);
365
366 // Treat an enum type as its underlying type.
367 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
368 RetTy = EnumTy->getDecl()->getIntegerType();
369
370 return (RetTy->isPromotableIntegerType() ?
371 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
372}
373
Bill Wendlingbb465d72010-10-18 03:41:31 +0000374/// UseX86_MMXType - Return true if this is an MMX type that should use the special
375/// x86_mmx type.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000376bool UseX86_MMXType(llvm::Type *IRType) {
Bill Wendlingbb465d72010-10-18 03:41:31 +0000377 // If the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>, use the
378 // special x86_mmx type.
379 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
380 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
381 IRType->getScalarSizeInBits() != 64;
382}
383
Jay Foadef6de3d2011-07-11 09:56:20 +0000384static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000385 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000386 llvm::Type* Ty) {
Bill Wendling0507be62011-03-07 22:47:14 +0000387 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy())
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000388 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
389 return Ty;
390}
391
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000392//===----------------------------------------------------------------------===//
393// X86-32 ABI Implementation
394//===----------------------------------------------------------------------===//
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000395
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000396/// X86_32ABIInfo - The X86-32 ABI information.
397class X86_32ABIInfo : public ABIInfo {
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000398 static const unsigned MinABIStackAlignInBytes = 4;
399
David Chisnall1e4249c2009-08-17 23:08:21 +0000400 bool IsDarwinVectorABI;
401 bool IsSmallStructInRegABI;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000402 bool IsMMXDisabled;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000403
404 static bool isRegisterSize(unsigned Size) {
405 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
406 }
407
408 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
409
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000410 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
411 /// such that the argument will be passed in memory.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000412 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000413
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000414 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbare59d8582010-09-16 20:42:06 +0000415 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000416
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000417public:
Chris Lattnerea044322010-07-29 02:01:43 +0000418
Chris Lattnera3c109b2010-07-29 02:16:43 +0000419 ABIArgInfo classifyReturnType(QualType RetTy) const;
420 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000421
Chris Lattneree5dcd02010-07-29 02:31:05 +0000422 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000423 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000424 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
425 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000426 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000427 }
428
429 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
430 CodeGenFunction &CGF) const;
431
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000432 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m)
433 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
434 IsMMXDisabled(m) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000435};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000436
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000437class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
438public:
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000439 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m)
440 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, m)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000441
442 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
443 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000444
445 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
446 // Darwin uses different dwarf register numbers for EH.
447 if (CGM.isTargetDarwin()) return 5;
448
449 return 4;
450 }
451
452 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
453 llvm::Value *Address) const;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000454
Jay Foadef6de3d2011-07-11 09:56:20 +0000455 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000456 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000457 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000458 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
459 }
460
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000461};
462
463}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000464
465/// shouldReturnTypeInRegister - Determine if the given type should be
466/// passed in a register (for the Darwin ABI).
467bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
468 ASTContext &Context) {
469 uint64_t Size = Context.getTypeSize(Ty);
470
471 // Type must be register sized.
472 if (!isRegisterSize(Size))
473 return false;
474
475 if (Ty->isVectorType()) {
476 // 64- and 128- bit vectors inside structures are not returned in
477 // registers.
478 if (Size == 64 || Size == 128)
479 return false;
480
481 return true;
482 }
483
Daniel Dunbar77115232010-05-15 00:00:30 +0000484 // If this is a builtin, pointer, enum, complex type, member pointer, or
485 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000486 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000487 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000488 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000489 return true;
490
491 // Arrays are treated like records.
492 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
493 return shouldReturnTypeInRegister(AT->getElementType(), Context);
494
495 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000496 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000497 if (!RT) return false;
498
Anders Carlssona8874232010-01-27 03:25:19 +0000499 // FIXME: Traverse bases here too.
500
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000501 // Structure types are passed in register if all fields would be
502 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000503 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
504 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000505 const FieldDecl *FD = *i;
506
507 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000508 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000509 continue;
510
511 // Check fields recursively.
512 if (!shouldReturnTypeInRegister(FD->getType(), Context))
513 return false;
514 }
515
516 return true;
517}
518
Chris Lattnera3c109b2010-07-29 02:16:43 +0000519ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const {
520 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000521 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000522
Chris Lattnera3c109b2010-07-29 02:16:43 +0000523 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000524 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000525 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000526 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000527
528 // 128-bit vectors are a special case; they are returned in
529 // registers and we need to make sure to pick a type the LLVM
530 // backend will like.
531 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +0000532 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000533 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000534
535 // Always return in register if it fits in a general purpose
536 // register, or if it is 64 bits and has a single element.
537 if ((Size == 8 || Size == 16 || Size == 32) ||
538 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +0000539 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000540 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000541
542 return ABIArgInfo::getIndirect(0);
543 }
544
545 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000546 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000547
John McCalld608cdb2010-08-22 10:59:02 +0000548 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000549 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000550 // Structures with either a non-trivial destructor or a non-trivial
551 // copy constructor are always indirect.
552 if (hasNonTrivialDestructorOrCopyConstructor(RT))
553 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000554
Anders Carlsson40092972009-10-20 22:07:59 +0000555 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000556 if (RT->getDecl()->hasFlexibleArrayMember())
557 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000558 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000559
David Chisnall1e4249c2009-08-17 23:08:21 +0000560 // If specified, structs and unions are always indirect.
561 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000562 return ABIArgInfo::getIndirect(0);
563
564 // Classify "single element" structs as their element type.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000565 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) {
John McCall183700f2009-09-21 23:43:11 +0000566 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000567 if (BT->isIntegerType()) {
568 // We need to use the size of the structure, padding
569 // bit-fields can adjust that to be larger than the single
570 // element type.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000571 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattner800588f2010-07-29 06:26:06 +0000572 return ABIArgInfo::getDirect(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000573 llvm::IntegerType::get(getVMContext(), (unsigned)Size));
574 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000575
Chris Lattnera3c109b2010-07-29 02:16:43 +0000576 if (BT->getKind() == BuiltinType::Float) {
577 assert(getContext().getTypeSize(RetTy) ==
578 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000579 "Unexpect single element structure size!");
Chris Lattner800588f2010-07-29 06:26:06 +0000580 return ABIArgInfo::getDirect(llvm::Type::getFloatTy(getVMContext()));
Chris Lattnera3c109b2010-07-29 02:16:43 +0000581 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000582
Chris Lattnera3c109b2010-07-29 02:16:43 +0000583 if (BT->getKind() == BuiltinType::Double) {
584 assert(getContext().getTypeSize(RetTy) ==
585 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000586 "Unexpect single element structure size!");
Chris Lattner800588f2010-07-29 06:26:06 +0000587 return ABIArgInfo::getDirect(llvm::Type::getDoubleTy(getVMContext()));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000588 }
589 } else if (SeltTy->isPointerType()) {
590 // FIXME: It would be really nice if this could come out as the proper
591 // pointer type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000592 llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(getVMContext());
Chris Lattner800588f2010-07-29 06:26:06 +0000593 return ABIArgInfo::getDirect(PtrTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000594 } else if (SeltTy->isVectorType()) {
595 // 64- and 128-bit vectors are never returned in a
596 // register when inside a structure.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000597 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000598 if (Size == 64 || Size == 128)
599 return ABIArgInfo::getIndirect(0);
600
Chris Lattnera3c109b2010-07-29 02:16:43 +0000601 return classifyReturnType(QualType(SeltTy, 0));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000602 }
603 }
604
605 // Small structures which are register sized are generally returned
606 // in a register.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000607 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) {
608 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattner800588f2010-07-29 06:26:06 +0000609 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000610 }
611
612 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000613 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000614
Chris Lattnera3c109b2010-07-29 02:16:43 +0000615 // Treat an enum type as its underlying type.
616 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
617 RetTy = EnumTy->getDecl()->getIntegerType();
618
619 return (RetTy->isPromotableIntegerType() ?
620 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000621}
622
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000623static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
624 const RecordType *RT = Ty->getAs<RecordType>();
625 if (!RT)
626 return 0;
627 const RecordDecl *RD = RT->getDecl();
628
629 // If this is a C++ record, check the bases first.
630 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
631 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
632 e = CXXRD->bases_end(); i != e; ++i)
633 if (!isRecordWithSSEVectorType(Context, i->getType()))
634 return false;
635
636 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
637 i != e; ++i) {
638 QualType FT = i->getType();
639
640 if (FT->getAs<VectorType>() && Context.getTypeSize(Ty) == 128)
641 return true;
642
643 if (isRecordWithSSEVectorType(Context, FT))
644 return true;
645 }
646
647 return false;
648}
649
Daniel Dunbare59d8582010-09-16 20:42:06 +0000650unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
651 unsigned Align) const {
652 // Otherwise, if the alignment is less than or equal to the minimum ABI
653 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000654 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +0000655 return 0; // Use default alignment.
656
657 // On non-Darwin, the stack type alignment is always 4.
658 if (!IsDarwinVectorABI) {
659 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000660 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +0000661 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000662
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000663 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
664 if (isRecordWithSSEVectorType(getContext(), Ty))
665 return 16;
666
667 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000668}
669
Chris Lattnera3c109b2010-07-29 02:16:43 +0000670ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000671 if (!ByVal)
672 return ABIArgInfo::getIndirect(0, false);
673
Daniel Dunbare59d8582010-09-16 20:42:06 +0000674 // Compute the byval alignment.
675 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
676 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
677 if (StackAlign == 0)
Chris Lattnerde92d732011-05-22 23:35:00 +0000678 return ABIArgInfo::getIndirect(4);
Daniel Dunbare59d8582010-09-16 20:42:06 +0000679
680 // If the stack alignment is less than the type alignment, realign the
681 // argument.
682 if (StackAlign < TypeAlign)
683 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
684 /*Realign=*/true);
685
686 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000687}
688
Chris Lattnera3c109b2010-07-29 02:16:43 +0000689ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000690 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000691 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000692 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000693 if (const RecordType *RT = Ty->getAs<RecordType>()) {
694 // Structures with either a non-trivial destructor or a non-trivial
695 // copy constructor are always indirect.
696 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Chris Lattnera3c109b2010-07-29 02:16:43 +0000697 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000698
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000699 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattnera3c109b2010-07-29 02:16:43 +0000700 return getIndirectResult(Ty);
Anders Carlssona8874232010-01-27 03:25:19 +0000701 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000702
703 // Ignore empty structs.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000704 if (Ty->isStructureType() && getContext().getTypeSize(Ty) == 0)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000705 return ABIArgInfo::getIgnore();
706
Daniel Dunbar53012f42009-11-09 01:33:53 +0000707 // Expand small (<= 128-bit) record types when we know that the stack layout
708 // of those arguments will match the struct. This is important because the
709 // LLVM backend isn't smart enough to remove byval, which inhibits many
710 // optimizations.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000711 if (getContext().getTypeSize(Ty) <= 4*32 &&
712 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar53012f42009-11-09 01:33:53 +0000713 return ABIArgInfo::getExpand();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000714
Chris Lattnera3c109b2010-07-29 02:16:43 +0000715 return getIndirectResult(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000716 }
717
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000718 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +0000719 // On Darwin, some vectors are passed in memory, we handle this by passing
720 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000721 if (IsDarwinVectorABI) {
722 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000723 if ((Size == 8 || Size == 16 || Size == 32) ||
724 (Size == 64 && VT->getNumElements() == 1))
725 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
726 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000727 }
Bill Wendlingbb465d72010-10-18 03:41:31 +0000728
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000729 llvm::Type *IRType = CGT.ConvertType(Ty);
Bill Wendlingbb465d72010-10-18 03:41:31 +0000730 if (UseX86_MMXType(IRType)) {
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000731 if (IsMMXDisabled)
732 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
733 64));
Bill Wendlingbb465d72010-10-18 03:41:31 +0000734 ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
735 AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
736 return AAI;
737 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000738
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000739 return ABIArgInfo::getDirect();
740 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000741
742
Chris Lattnera3c109b2010-07-29 02:16:43 +0000743 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
744 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000745
Chris Lattnera3c109b2010-07-29 02:16:43 +0000746 return (Ty->isPromotableIntegerType() ?
747 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000748}
749
750llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
751 CodeGenFunction &CGF) const {
Chris Lattner2acc6e32011-07-18 04:24:23 +0000752 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
753 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000754
755 CGBuilderTy &Builder = CGF.Builder;
756 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
757 "ap");
758 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
759 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000760 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000761 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
762
763 uint64_t Offset =
764 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
765 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +0000766 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000767 "ap.next");
768 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
769
770 return AddrTyped;
771}
772
Charles Davis74f72932010-02-13 15:54:06 +0000773void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
774 llvm::GlobalValue *GV,
775 CodeGen::CodeGenModule &CGM) const {
776 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
777 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
778 // Get the LLVM function.
779 llvm::Function *Fn = cast<llvm::Function>(GV);
780
781 // Now add the 'alignstack' attribute with a value of 16.
782 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
783 }
784 }
785}
786
John McCall6374c332010-03-06 00:35:14 +0000787bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
788 CodeGen::CodeGenFunction &CGF,
789 llvm::Value *Address) const {
790 CodeGen::CGBuilderTy &Builder = CGF.Builder;
791 llvm::LLVMContext &Context = CGF.getLLVMContext();
792
Chris Lattner2acc6e32011-07-18 04:24:23 +0000793 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCall6374c332010-03-06 00:35:14 +0000794 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000795
John McCall6374c332010-03-06 00:35:14 +0000796 // 0-7 are the eight integer registers; the order is different
797 // on Darwin (for EH), but the range is the same.
798 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +0000799 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +0000800
801 if (CGF.CGM.isTargetDarwin()) {
802 // 12-16 are st(0..4). Not sure why we stop at 4.
803 // These have size 16, which is sizeof(long double) on
804 // platforms with 8-byte alignment for that type.
805 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCallaeeb7012010-05-27 06:19:26 +0000806 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000807
John McCall6374c332010-03-06 00:35:14 +0000808 } else {
809 // 9 is %eflags, which doesn't get a size on Darwin for some
810 // reason.
811 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
812
813 // 11-16 are st(0..5). Not sure why we stop at 5.
814 // These have size 12, which is sizeof(long double) on
815 // platforms with 4-byte alignment for that type.
816 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCallaeeb7012010-05-27 06:19:26 +0000817 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
818 }
John McCall6374c332010-03-06 00:35:14 +0000819
820 return false;
821}
822
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000823//===----------------------------------------------------------------------===//
824// X86-64 ABI Implementation
825//===----------------------------------------------------------------------===//
826
827
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000828namespace {
829/// X86_64ABIInfo - The X86_64 ABI information.
830class X86_64ABIInfo : public ABIInfo {
831 enum Class {
832 Integer = 0,
833 SSE,
834 SSEUp,
835 X87,
836 X87Up,
837 ComplexX87,
838 NoClass,
839 Memory
840 };
841
842 /// merge - Implement the X86_64 ABI merging algorithm.
843 ///
844 /// Merge an accumulating classification \arg Accum with a field
845 /// classification \arg Field.
846 ///
847 /// \param Accum - The accumulating classification. This should
848 /// always be either NoClass or the result of a previous merge
849 /// call. In addition, this should never be Memory (the caller
850 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +0000851 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000852
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +0000853 /// postMerge - Implement the X86_64 ABI post merging algorithm.
854 ///
855 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
856 /// final MEMORY or SSE classes when necessary.
857 ///
858 /// \param AggregateSize - The size of the current aggregate in
859 /// the classification process.
860 ///
861 /// \param Lo - The classification for the parts of the type
862 /// residing in the low word of the containing object.
863 ///
864 /// \param Hi - The classification for the parts of the type
865 /// residing in the higher words of the containing object.
866 ///
867 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
868
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000869 /// classify - Determine the x86_64 register classes in which the
870 /// given type T should be passed.
871 ///
872 /// \param Lo - The classification for the parts of the type
873 /// residing in the low word of the containing object.
874 ///
875 /// \param Hi - The classification for the parts of the type
876 /// residing in the high word of the containing object.
877 ///
878 /// \param OffsetBase - The bit offset of this type in the
879 /// containing object. Some parameters are classified different
880 /// depending on whether they straddle an eightbyte boundary.
881 ///
882 /// If a word is unused its result will be NoClass; if a type should
883 /// be passed in Memory then at least the classification of \arg Lo
884 /// will be Memory.
885 ///
886 /// The \arg Lo class will be NoClass iff the argument is ignored.
887 ///
888 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
889 /// also be ComplexX87.
Chris Lattner9c254f02010-06-29 06:01:59 +0000890 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000891
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +0000892 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000893 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
894 unsigned IROffset, QualType SourceTy,
895 unsigned SourceOffset) const;
896 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
897 unsigned IROffset, QualType SourceTy,
898 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000899
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000900 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000901 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000902 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000903
904 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000905 /// such that the argument will be passed in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000906 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000907
Chris Lattnera3c109b2010-07-29 02:16:43 +0000908 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000909
Bill Wendlingbb465d72010-10-18 03:41:31 +0000910 ABIArgInfo classifyArgumentType(QualType Ty,
911 unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +0000912 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000913
John McCall67a57732011-04-21 01:20:55 +0000914 /// The 0.98 ABI revision clarified a lot of ambiguities,
915 /// unfortunately in ways that were not always consistent with
916 /// certain previous compilers. In particular, platforms which
917 /// required strict binary compatibility with older versions of GCC
918 /// may need to exempt themselves.
919 bool honorsRevision0_98() const {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000920 return !getContext().getTargetInfo().getTriple().isOSDarwin();
John McCall67a57732011-04-21 01:20:55 +0000921 }
922
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000923public:
Chris Lattnerea044322010-07-29 02:01:43 +0000924 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Chris Lattner9c254f02010-06-29 06:01:59 +0000925
Chris Lattneree5dcd02010-07-29 02:31:05 +0000926 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000927
928 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
929 CodeGenFunction &CGF) const;
930};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000931
Chris Lattnerf13721d2010-08-31 16:44:54 +0000932/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +0000933class WinX86_64ABIInfo : public ABIInfo {
934
935 ABIArgInfo classify(QualType Ty) const;
936
Chris Lattnerf13721d2010-08-31 16:44:54 +0000937public:
NAKAMURA Takumia7573222011-01-17 22:56:31 +0000938 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
939
940 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattnerf13721d2010-08-31 16:44:54 +0000941
942 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
943 CodeGenFunction &CGF) const;
944};
945
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000946class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
947public:
Chris Lattnerea044322010-07-29 02:01:43 +0000948 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
949 : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {}
John McCall6374c332010-03-06 00:35:14 +0000950
951 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
952 return 7;
953 }
954
955 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
956 llvm::Value *Address) const {
957 CodeGen::CGBuilderTy &Builder = CGF.Builder;
958 llvm::LLVMContext &Context = CGF.getLLVMContext();
959
Chris Lattner2acc6e32011-07-18 04:24:23 +0000960 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCall6374c332010-03-06 00:35:14 +0000961 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000962
John McCallaeeb7012010-05-27 06:19:26 +0000963 // 0-15 are the 16 integer registers.
964 // 16 is %rip.
965 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +0000966
967 return false;
968 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000969
Jay Foadef6de3d2011-07-11 09:56:20 +0000970 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000971 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000972 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000973 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
974 }
975
John McCall01f151e2011-09-21 08:08:30 +0000976 bool isNoProtoCallVariadic(CallingConv CC) const {
977 // The default CC on x86-64 sets %al to the number of SSA
978 // registers used, and GCC sets this when calling an unprototyped
979 // function, so we override the default behavior.
980 if (CC == CC_Default || CC == CC_C) return true;
981
982 return TargetCodeGenInfo::isNoProtoCallVariadic(CC);
983 }
984
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000985};
986
Chris Lattnerf13721d2010-08-31 16:44:54 +0000987class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
988public:
989 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
990 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
991
992 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
993 return 7;
994 }
995
996 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
997 llvm::Value *Address) const {
998 CodeGen::CGBuilderTy &Builder = CGF.Builder;
999 llvm::LLVMContext &Context = CGF.getLLVMContext();
1000
Chris Lattner2acc6e32011-07-18 04:24:23 +00001001 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
Chris Lattnerf13721d2010-08-31 16:44:54 +00001002 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001003
Chris Lattnerf13721d2010-08-31 16:44:54 +00001004 // 0-15 are the 16 integer registers.
1005 // 16 is %rip.
1006 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
1007
1008 return false;
1009 }
1010};
1011
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001012}
1013
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001014void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1015 Class &Hi) const {
1016 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1017 //
1018 // (a) If one of the classes is Memory, the whole argument is passed in
1019 // memory.
1020 //
1021 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1022 // memory.
1023 //
1024 // (c) If the size of the aggregate exceeds two eightbytes and the first
1025 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1026 // argument is passed in memory. NOTE: This is necessary to keep the
1027 // ABI working for processors that don't support the __m256 type.
1028 //
1029 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1030 //
1031 // Some of these are enforced by the merging logic. Others can arise
1032 // only with unions; for example:
1033 // union { _Complex double; unsigned; }
1034 //
1035 // Note that clauses (b) and (c) were added in 0.98.
1036 //
1037 if (Hi == Memory)
1038 Lo = Memory;
1039 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1040 Lo = Memory;
1041 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1042 Lo = Memory;
1043 if (Hi == SSEUp && Lo != SSE)
1044 Hi = SSE;
1045}
1046
Chris Lattner1090a9b2010-06-28 21:43:59 +00001047X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001048 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1049 // classified recursively so that always two fields are
1050 // considered. The resulting class is calculated according to
1051 // the classes of the fields in the eightbyte:
1052 //
1053 // (a) If both classes are equal, this is the resulting class.
1054 //
1055 // (b) If one of the classes is NO_CLASS, the resulting class is
1056 // the other class.
1057 //
1058 // (c) If one of the classes is MEMORY, the result is the MEMORY
1059 // class.
1060 //
1061 // (d) If one of the classes is INTEGER, the result is the
1062 // INTEGER.
1063 //
1064 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1065 // MEMORY is used as class.
1066 //
1067 // (f) Otherwise class SSE is used.
1068
1069 // Accum should never be memory (we should have returned) or
1070 // ComplexX87 (because this cannot be passed in a structure).
1071 assert((Accum != Memory && Accum != ComplexX87) &&
1072 "Invalid accumulated classification during merge.");
1073 if (Accum == Field || Field == NoClass)
1074 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001075 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001076 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001077 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001078 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001079 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001080 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001081 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1082 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001083 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001084 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001085}
1086
Chris Lattnerbcaedae2010-06-30 19:14:05 +00001087void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001088 Class &Lo, Class &Hi) const {
1089 // FIXME: This code can be simplified by introducing a simple value class for
1090 // Class pairs with appropriate constructor methods for the various
1091 // situations.
1092
1093 // FIXME: Some of the split computations are wrong; unaligned vectors
1094 // shouldn't be passed in registers for example, so there is no chance they
1095 // can straddle an eightbyte. Verify & simplify.
1096
1097 Lo = Hi = NoClass;
1098
1099 Class &Current = OffsetBase < 64 ? Lo : Hi;
1100 Current = Memory;
1101
John McCall183700f2009-09-21 23:43:11 +00001102 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001103 BuiltinType::Kind k = BT->getKind();
1104
1105 if (k == BuiltinType::Void) {
1106 Current = NoClass;
1107 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1108 Lo = Integer;
1109 Hi = Integer;
1110 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1111 Current = Integer;
1112 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
1113 Current = SSE;
1114 } else if (k == BuiltinType::LongDouble) {
1115 Lo = X87;
1116 Hi = X87Up;
1117 }
1118 // FIXME: _Decimal32 and _Decimal64 are SSE.
1119 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001120 return;
1121 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001122
Chris Lattner1090a9b2010-06-28 21:43:59 +00001123 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001124 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +00001125 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001126 return;
1127 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001128
Chris Lattner1090a9b2010-06-28 21:43:59 +00001129 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001130 Current = Integer;
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->isMemberPointerType()) {
Daniel Dunbar67d438d2010-05-15 00:00:37 +00001135 if (Ty->isMemberFunctionPointerType())
1136 Lo = Hi = Integer;
1137 else
1138 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001139 return;
1140 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001141
Chris Lattner1090a9b2010-06-28 21:43:59 +00001142 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001143 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001144 if (Size == 32) {
1145 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1146 // float> as integer.
1147 Current = Integer;
1148
1149 // If this type crosses an eightbyte boundary, it should be
1150 // split.
1151 uint64_t EB_Real = (OffsetBase) / 64;
1152 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1153 if (EB_Real != EB_Imag)
1154 Hi = Lo;
1155 } else if (Size == 64) {
1156 // gcc passes <1 x double> in memory. :(
1157 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1158 return;
1159
1160 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001161 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001162 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1163 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1164 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001165 Current = Integer;
1166 else
1167 Current = SSE;
1168
1169 // If this type crosses an eightbyte boundary, it should be
1170 // split.
1171 if (OffsetBase && OffsetBase != 64)
1172 Hi = Lo;
Bruno Cardoso Lopes75d28b52011-07-12 02:47:38 +00001173 } else if (Size == 128 || Size == 256) {
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001174 // Arguments of 256-bits are split into four eightbyte chunks. The
1175 // least significant one belongs to class SSE and all the others to class
1176 // SSEUP. The original Lo and Hi design considers that types can't be
1177 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1178 // This design isn't correct for 256-bits, but since there're no cases
1179 // where the upper parts would need to be inspected, avoid adding
1180 // complexity and just consider Hi to match the 64-256 part.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001181 Lo = SSE;
1182 Hi = SSEUp;
1183 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001184 return;
1185 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001186
Chris Lattner1090a9b2010-06-28 21:43:59 +00001187 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001188 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001189
Chris Lattnerea044322010-07-29 02:01:43 +00001190 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001191 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001192 if (Size <= 64)
1193 Current = Integer;
1194 else if (Size <= 128)
1195 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001196 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001197 Current = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001198 else if (ET == getContext().DoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001199 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001200 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001201 Current = ComplexX87;
1202
1203 // If this complex type crosses an eightbyte boundary then it
1204 // should be split.
1205 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001206 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001207 if (Hi == NoClass && EB_Real != EB_Imag)
1208 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001209
Chris Lattner1090a9b2010-06-28 21:43:59 +00001210 return;
1211 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001212
Chris Lattnerea044322010-07-29 02:01:43 +00001213 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001214 // Arrays are treated like structures.
1215
Chris Lattnerea044322010-07-29 02:01:43 +00001216 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001217
1218 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001219 // than four eightbytes, ..., it has class MEMORY.
1220 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001221 return;
1222
1223 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1224 // fields, it has class MEMORY.
1225 //
1226 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001227 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001228 return;
1229
1230 // Otherwise implement simplified merge. We could be smarter about
1231 // this, but it isn't worth it and would be harder to verify.
1232 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001233 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001234 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes089d8922011-07-12 01:27:38 +00001235
1236 // The only case a 256-bit wide vector could be used is when the array
1237 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1238 // to work for sizes wider than 128, early check and fallback to memory.
1239 if (Size > 128 && EltSize != 256)
1240 return;
1241
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001242 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1243 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001244 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001245 Lo = merge(Lo, FieldLo);
1246 Hi = merge(Hi, FieldHi);
1247 if (Lo == Memory || Hi == Memory)
1248 break;
1249 }
1250
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001251 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001252 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001253 return;
1254 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001255
Chris Lattner1090a9b2010-06-28 21:43:59 +00001256 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001257 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001258
1259 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001260 // than four eightbytes, ..., it has class MEMORY.
1261 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001262 return;
1263
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001264 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1265 // copy constructor or a non-trivial destructor, it is passed by invisible
1266 // reference.
1267 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1268 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001269
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001270 const RecordDecl *RD = RT->getDecl();
1271
1272 // Assume variable sized types are passed in memory.
1273 if (RD->hasFlexibleArrayMember())
1274 return;
1275
Chris Lattnerea044322010-07-29 02:01:43 +00001276 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001277
1278 // Reset Lo class, this will be recomputed.
1279 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001280
1281 // If this is a C++ record, classify the bases first.
1282 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1283 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1284 e = CXXRD->bases_end(); i != e; ++i) {
1285 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1286 "Unexpected base class!");
1287 const CXXRecordDecl *Base =
1288 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1289
1290 // Classify this field.
1291 //
1292 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1293 // single eightbyte, each is classified separately. Each eightbyte gets
1294 // initialized to class NO_CLASS.
1295 Class FieldLo, FieldHi;
Anders Carlssona14f5972010-10-31 23:22:37 +00001296 uint64_t Offset = OffsetBase + Layout.getBaseClassOffsetInBits(Base);
Chris Lattner9c254f02010-06-29 06:01:59 +00001297 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001298 Lo = merge(Lo, FieldLo);
1299 Hi = merge(Hi, FieldHi);
1300 if (Lo == Memory || Hi == Memory)
1301 break;
1302 }
1303 }
1304
1305 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001306 unsigned idx = 0;
Bruno Cardoso Lopes548e4782011-07-12 22:30:58 +00001307 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001308 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001309 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1310 bool BitField = i->isBitField();
1311
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001312 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1313 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001314 //
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001315 // The only case a 256-bit wide vector could be used is when the struct
1316 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1317 // to work for sizes wider than 128, early check and fallback to memory.
1318 //
1319 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1320 Lo = Memory;
1321 return;
1322 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001323 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001324 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001325 Lo = Memory;
1326 return;
1327 }
1328
1329 // Classify this field.
1330 //
1331 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1332 // exceeds a single eightbyte, each is classified
1333 // separately. Each eightbyte gets initialized to class
1334 // NO_CLASS.
1335 Class FieldLo, FieldHi;
1336
1337 // Bit-fields require special handling, they do not force the
1338 // structure to be passed in memory even if unaligned, and
1339 // therefore they can straddle an eightbyte.
1340 if (BitField) {
1341 // Ignore padding bit-fields.
1342 if (i->isUnnamedBitfield())
1343 continue;
1344
1345 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001346 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001347
1348 uint64_t EB_Lo = Offset / 64;
1349 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1350 FieldLo = FieldHi = NoClass;
1351 if (EB_Lo) {
1352 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1353 FieldLo = NoClass;
1354 FieldHi = Integer;
1355 } else {
1356 FieldLo = Integer;
1357 FieldHi = EB_Hi ? Integer : NoClass;
1358 }
1359 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001360 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001361 Lo = merge(Lo, FieldLo);
1362 Hi = merge(Hi, FieldHi);
1363 if (Lo == Memory || Hi == Memory)
1364 break;
1365 }
1366
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001367 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001368 }
1369}
1370
Chris Lattner9c254f02010-06-29 06:01:59 +00001371ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001372 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1373 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001374 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001375 // Treat an enum type as its underlying type.
1376 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1377 Ty = EnumTy->getDecl()->getIntegerType();
1378
1379 return (Ty->isPromotableIntegerType() ?
1380 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1381 }
1382
1383 return ABIArgInfo::getIndirect(0);
1384}
1385
Chris Lattner9c254f02010-06-29 06:01:59 +00001386ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001387 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1388 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001389 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001390 // Treat an enum type as its underlying type.
1391 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1392 Ty = EnumTy->getDecl()->getIntegerType();
1393
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001394 return (Ty->isPromotableIntegerType() ?
1395 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001396 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001397
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001398 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1399 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001400
Chris Lattner855d2272011-05-22 23:21:23 +00001401 // Compute the byval alignment. We specify the alignment of the byval in all
1402 // cases so that the mid-level optimizer knows the alignment of the byval.
1403 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
1404 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001405}
1406
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001407/// GetByteVectorType - The ABI specifies that a value should be passed in an
1408/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner0f408f52010-07-29 04:56:46 +00001409/// vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001410llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001411 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001412
Chris Lattner15842bd2010-07-29 05:02:29 +00001413 // Wrapper structs that just contain vectors are passed just like vectors,
1414 // strip them off if present.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001415 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner15842bd2010-07-29 05:02:29 +00001416 while (STy && STy->getNumElements() == 1) {
1417 IRType = STy->getElementType(0);
1418 STy = dyn_cast<llvm::StructType>(IRType);
1419 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001420
Bruno Cardoso Lopes528a8c72011-07-08 22:57:35 +00001421 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001422 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1423 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001424 unsigned BitWidth = VT->getBitWidth();
1425 if ((BitWidth == 128 || BitWidth == 256) &&
Chris Lattner0f408f52010-07-29 04:56:46 +00001426 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1427 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1428 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1429 EltTy->isIntegerTy(128)))
1430 return VT;
1431 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001432
Chris Lattner0f408f52010-07-29 04:56:46 +00001433 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1434}
1435
Chris Lattnere2962be2010-07-29 07:30:00 +00001436/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1437/// is known to either be off the end of the specified type or being in
1438/// alignment padding. The user type specified is known to be at most 128 bits
1439/// in size, and have passed through X86_64ABIInfo::classify with a successful
1440/// classification that put one of the two halves in the INTEGER class.
1441///
1442/// It is conservatively correct to return false.
1443static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1444 unsigned EndBit, ASTContext &Context) {
1445 // If the bytes being queried are off the end of the type, there is no user
1446 // data hiding here. This handles analysis of builtins, vectors and other
1447 // types that don't contain interesting padding.
1448 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1449 if (TySize <= StartBit)
1450 return true;
1451
Chris Lattner021c3a32010-07-29 07:43:55 +00001452 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1453 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1454 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1455
1456 // Check each element to see if the element overlaps with the queried range.
1457 for (unsigned i = 0; i != NumElts; ++i) {
1458 // If the element is after the span we care about, then we're done..
1459 unsigned EltOffset = i*EltSize;
1460 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001461
Chris Lattner021c3a32010-07-29 07:43:55 +00001462 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1463 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1464 EndBit-EltOffset, Context))
1465 return false;
1466 }
1467 // If it overlaps no elements, then it is safe to process as padding.
1468 return true;
1469 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001470
Chris Lattnere2962be2010-07-29 07:30:00 +00001471 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1472 const RecordDecl *RD = RT->getDecl();
1473 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001474
Chris Lattnere2962be2010-07-29 07:30:00 +00001475 // If this is a C++ record, check the bases first.
1476 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1477 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1478 e = CXXRD->bases_end(); i != e; ++i) {
1479 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1480 "Unexpected base class!");
1481 const CXXRecordDecl *Base =
1482 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001483
Chris Lattnere2962be2010-07-29 07:30:00 +00001484 // If the base is after the span we care about, ignore it.
Anders Carlssona14f5972010-10-31 23:22:37 +00001485 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffsetInBits(Base);
Chris Lattnere2962be2010-07-29 07:30:00 +00001486 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001487
Chris Lattnere2962be2010-07-29 07:30:00 +00001488 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1489 if (!BitsContainNoUserData(i->getType(), BaseStart,
1490 EndBit-BaseOffset, Context))
1491 return false;
1492 }
1493 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001494
Chris Lattnere2962be2010-07-29 07:30:00 +00001495 // Verify that no field has data that overlaps the region of interest. Yes
1496 // this could be sped up a lot by being smarter about queried fields,
1497 // however we're only looking at structs up to 16 bytes, so we don't care
1498 // much.
1499 unsigned idx = 0;
1500 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1501 i != e; ++i, ++idx) {
1502 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001503
Chris Lattnere2962be2010-07-29 07:30:00 +00001504 // If we found a field after the region we care about, then we're done.
1505 if (FieldOffset >= EndBit) break;
1506
1507 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1508 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1509 Context))
1510 return false;
1511 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001512
Chris Lattnere2962be2010-07-29 07:30:00 +00001513 // If nothing in this record overlapped the area of interest, then we're
1514 // clean.
1515 return true;
1516 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001517
Chris Lattnere2962be2010-07-29 07:30:00 +00001518 return false;
1519}
1520
Chris Lattner0b362002010-07-29 18:39:32 +00001521/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1522/// float member at the specified offset. For example, {int,{float}} has a
1523/// float at offset 4. It is conservatively correct for this routine to return
1524/// false.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001525static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0b362002010-07-29 18:39:32 +00001526 const llvm::TargetData &TD) {
1527 // Base case if we find a float.
1528 if (IROffset == 0 && IRType->isFloatTy())
1529 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001530
Chris Lattner0b362002010-07-29 18:39:32 +00001531 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001532 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner0b362002010-07-29 18:39:32 +00001533 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1534 unsigned Elt = SL->getElementContainingOffset(IROffset);
1535 IROffset -= SL->getElementOffset(Elt);
1536 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1537 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001538
Chris Lattner0b362002010-07-29 18:39:32 +00001539 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001540 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1541 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner0b362002010-07-29 18:39:32 +00001542 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1543 IROffset -= IROffset/EltSize*EltSize;
1544 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1545 }
1546
1547 return false;
1548}
1549
Chris Lattnerf47c9442010-07-29 18:13:09 +00001550
1551/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1552/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001553llvm::Type *X86_64ABIInfo::
1554GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00001555 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001556 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001557 // pass as float if the last 4 bytes is just padding. This happens for
1558 // structs that contain 3 floats.
1559 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1560 SourceOffset*8+64, getContext()))
1561 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001562
Chris Lattner0b362002010-07-29 18:39:32 +00001563 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1564 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1565 // case.
1566 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001567 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1568 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001569
Chris Lattnerf47c9442010-07-29 18:13:09 +00001570 return llvm::Type::getDoubleTy(getVMContext());
1571}
1572
1573
Chris Lattner0d2656d2010-07-29 17:40:35 +00001574/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1575/// an 8-byte GPR. This means that we either have a scalar or we are talking
1576/// about the high or low part of an up-to-16-byte struct. This routine picks
1577/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001578/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1579/// etc).
1580///
1581/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1582/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1583/// the 8-byte value references. PrefType may be null.
1584///
1585/// SourceTy is the source level type for the entire argument. SourceOffset is
1586/// an offset into this that we're processing (which is always either 0 or 8).
1587///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001588llvm::Type *X86_64ABIInfo::
1589GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00001590 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001591 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1592 // returning an 8-byte unit starting with it. See if we can safely use it.
1593 if (IROffset == 0) {
1594 // Pointers and int64's always fill the 8-byte unit.
1595 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1596 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001597
Chris Lattnere2962be2010-07-29 07:30:00 +00001598 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1599 // goodness in the source type is just tail padding. This is allowed to
1600 // kick in for struct {double,int} on the int, but not on
1601 // struct{double,int,int} because we wouldn't return the second int. We
1602 // have to do this analysis on the source type because we can't depend on
1603 // unions being lowered a specific way etc.
1604 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1605 IRType->isIntegerTy(32)) {
1606 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001607
Chris Lattnere2962be2010-07-29 07:30:00 +00001608 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1609 SourceOffset*8+64, getContext()))
1610 return IRType;
1611 }
1612 }
Chris Lattner49382de2010-07-28 22:44:07 +00001613
Chris Lattner2acc6e32011-07-18 04:24:23 +00001614 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001615 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner44f0fd22010-07-29 02:20:19 +00001616 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001617 if (IROffset < SL->getSizeInBytes()) {
1618 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1619 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001620
Chris Lattner0d2656d2010-07-29 17:40:35 +00001621 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1622 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001623 }
Chris Lattner49382de2010-07-28 22:44:07 +00001624 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001625
Chris Lattner2acc6e32011-07-18 04:24:23 +00001626 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001627 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner021c3a32010-07-29 07:43:55 +00001628 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1629 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00001630 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1631 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00001632 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001633
Chris Lattner49382de2010-07-28 22:44:07 +00001634 // Okay, we don't have any better idea of what to pass, so we pass this in an
1635 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001636 unsigned TySizeInBytes =
1637 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00001638
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001639 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001640
Chris Lattner49382de2010-07-28 22:44:07 +00001641 // It is always safe to classify this as an integer type up to i64 that
1642 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001643 return llvm::IntegerType::get(getVMContext(),
1644 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00001645}
1646
Chris Lattner66e7b682010-09-01 00:50:20 +00001647
1648/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1649/// be used as elements of a two register pair to pass or return, return a
1650/// first class aggregate to represent them. For example, if the low part of
1651/// a by-value argument should be passed as i32* and the high part as float,
1652/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001653static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00001654GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Chris Lattner66e7b682010-09-01 00:50:20 +00001655 const llvm::TargetData &TD) {
1656 // In order to correctly satisfy the ABI, we need to the high part to start
1657 // at offset 8. If the high and low parts we inferred are both 4-byte types
1658 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1659 // the second element at offset 8. Check for this:
1660 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1661 unsigned HiAlign = TD.getABITypeAlignment(Hi);
1662 unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
1663 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001664
Chris Lattner66e7b682010-09-01 00:50:20 +00001665 // To handle this, we have to increase the size of the low part so that the
1666 // second element will start at an 8 byte offset. We can't increase the size
1667 // of the second element because it might make us access off the end of the
1668 // struct.
1669 if (HiStart != 8) {
1670 // There are only two sorts of types the ABI generation code can produce for
1671 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1672 // Promote these to a larger type.
1673 if (Lo->isFloatTy())
1674 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1675 else {
1676 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1677 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1678 }
1679 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001680
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001681 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001682
1683
Chris Lattner66e7b682010-09-01 00:50:20 +00001684 // Verify that the second element is at an 8-byte offset.
1685 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1686 "Invalid x86-64 argument pair!");
1687 return Result;
1688}
1689
Chris Lattner519f68c2010-07-28 23:06:14 +00001690ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00001691classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00001692 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1693 // classification algorithm.
1694 X86_64ABIInfo::Class Lo, Hi;
1695 classify(RetTy, 0, Lo, Hi);
1696
1697 // Check some invariants.
1698 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001699 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1700
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001701 llvm::Type *ResType = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001702 switch (Lo) {
1703 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001704 if (Hi == NoClass)
1705 return ABIArgInfo::getIgnore();
1706 // If the low part is just padding, it takes no register, leave ResType
1707 // null.
1708 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1709 "Unknown missing lo part");
1710 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001711
1712 case SSEUp:
1713 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00001714 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001715
1716 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1717 // hidden argument.
1718 case Memory:
1719 return getIndirectReturnResult(RetTy);
1720
1721 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1722 // available register of the sequence %rax, %rdx is used.
1723 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001724 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001725
Chris Lattnereb518b42010-07-29 21:42:50 +00001726 // If we have a sign or zero extended integer, make sure to return Extend
1727 // so that the parameter gets the right LLVM IR attributes.
1728 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1729 // Treat an enum type as its underlying type.
1730 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1731 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001732
Chris Lattnereb518b42010-07-29 21:42:50 +00001733 if (RetTy->isIntegralOrEnumerationType() &&
1734 RetTy->isPromotableIntegerType())
1735 return ABIArgInfo::getExtend();
1736 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001737 break;
1738
1739 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1740 // available SSE register of the sequence %xmm0, %xmm1 is used.
1741 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001742 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00001743 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001744
1745 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1746 // returned on the X87 stack in %st0 as 80-bit x87 number.
1747 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00001748 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00001749 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001750
1751 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1752 // part of the value is returned in %st0 and the imaginary part in
1753 // %st1.
1754 case ComplexX87:
1755 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00001756 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00001757 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00001758 NULL);
1759 break;
1760 }
1761
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001762 llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001763 switch (Hi) {
1764 // Memory was handled previously and X87 should
1765 // never occur as a hi class.
1766 case Memory:
1767 case X87:
David Blaikieb219cfc2011-09-23 05:06:16 +00001768 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001769
1770 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00001771 case NoClass:
1772 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001773
Chris Lattner3db4dde2010-09-01 00:20:33 +00001774 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001775 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001776 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1777 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001778 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00001779 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001780 HighPart = GetSSETypeAtOffset(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;
1784
1785 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001786 // is passed in the next available eightbyte chunk if the last used
1787 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00001788 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001789 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00001790 case SSEUp:
1791 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001792 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00001793 break;
1794
1795 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1796 // returned together with the previous X87 value in %st0.
1797 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001798 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00001799 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001800 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00001801 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00001802 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001803 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001804 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1805 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00001806 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001807 break;
1808 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001809
Chris Lattner3db4dde2010-09-01 00:20:33 +00001810 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00001811 // known to pass in the high eightbyte of the result. We do this by forming a
1812 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00001813 if (HighPart)
1814 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner519f68c2010-07-28 23:06:14 +00001815
Chris Lattnereb518b42010-07-29 21:42:50 +00001816 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00001817}
1818
Chris Lattnera3c109b2010-07-29 02:16:43 +00001819ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +00001820 unsigned &neededSSE) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001821 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001822 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001823
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001824 // Check some invariants.
1825 // FIXME: Enforce these by construction.
1826 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001827 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1828
1829 neededInt = 0;
1830 neededSSE = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001831 llvm::Type *ResType = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001832 switch (Lo) {
1833 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001834 if (Hi == NoClass)
1835 return ABIArgInfo::getIgnore();
1836 // If the low part is just padding, it takes no register, leave ResType
1837 // null.
1838 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1839 "Unknown missing lo part");
1840 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001841
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001842 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1843 // on the stack.
1844 case Memory:
1845
1846 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1847 // COMPLEX_X87, it is passed in memory.
1848 case X87:
1849 case ComplexX87:
Eli Friedmanded137f2011-06-29 07:04:55 +00001850 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1851 ++neededInt;
Chris Lattner9c254f02010-06-29 06:01:59 +00001852 return getIndirectResult(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001853
1854 case SSEUp:
1855 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00001856 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001857
1858 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1859 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1860 // and %r9 is used.
1861 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00001862 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001863
Chris Lattner49382de2010-07-28 22:44:07 +00001864 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001865 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00001866
1867 // If we have a sign or zero extended integer, make sure to return Extend
1868 // so that the parameter gets the right LLVM IR attributes.
1869 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1870 // Treat an enum type as its underlying type.
1871 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1872 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001873
Chris Lattnereb518b42010-07-29 21:42:50 +00001874 if (Ty->isIntegralOrEnumerationType() &&
1875 Ty->isPromotableIntegerType())
1876 return ABIArgInfo::getExtend();
1877 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001878
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001879 break;
1880
1881 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1882 // available SSE register is used, the registers are taken in the
1883 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00001884 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001885 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00001886 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00001887 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001888 break;
1889 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00001890 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001891
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001892 llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001893 switch (Hi) {
1894 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001895 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001896 // which is passed in memory.
1897 case Memory:
1898 case X87:
1899 case ComplexX87:
David Blaikieb219cfc2011-09-23 05:06:16 +00001900 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001901
1902 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001903
Chris Lattner645406a2010-09-01 00:24:35 +00001904 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001905 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00001906 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001907 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001908
Chris Lattner645406a2010-09-01 00:24:35 +00001909 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1910 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001911 break;
1912
1913 // X87Up generally doesn't occur here (long double is passed in
1914 // memory), except in situations involving unions.
1915 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00001916 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001917 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001918
Chris Lattner645406a2010-09-01 00:24:35 +00001919 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1920 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00001921
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001922 ++neededSSE;
1923 break;
1924
1925 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1926 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001927 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001928 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00001929 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001930 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001931 break;
1932 }
1933
Chris Lattner645406a2010-09-01 00:24:35 +00001934 // If a high part was specified, merge it together with the low part. It is
1935 // known to pass in the high eightbyte of the result. We do this by forming a
1936 // first class struct aggregate with the high and low part: {low, high}
1937 if (HighPart)
Chris Lattner66e7b682010-09-01 00:50:20 +00001938 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001939
Chris Lattnereb518b42010-07-29 21:42:50 +00001940 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001941}
1942
Chris Lattneree5dcd02010-07-29 02:31:05 +00001943void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001944
Chris Lattnera3c109b2010-07-29 02:16:43 +00001945 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001946
1947 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00001948 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001949
1950 // If the return value is indirect, then the hidden argument is consuming one
1951 // integer register.
1952 if (FI.getReturnInfo().isIndirect())
1953 --freeIntRegs;
1954
1955 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1956 // get assigned (in left-to-right order) for passing as follows...
1957 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1958 it != ie; ++it) {
Bill Wendling99aaae82010-10-18 23:51:38 +00001959 unsigned neededInt, neededSSE;
1960 it->info = classifyArgumentType(it->type, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001961
1962 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1963 // eightbyte of an argument, the whole argument is passed on the
1964 // stack. If registers have already been assigned for some
1965 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00001966 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001967 freeIntRegs -= neededInt;
1968 freeSSERegs -= neededSSE;
1969 } else {
Chris Lattner9c254f02010-06-29 06:01:59 +00001970 it->info = getIndirectResult(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001971 }
1972 }
1973}
1974
1975static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1976 QualType Ty,
1977 CodeGenFunction &CGF) {
1978 llvm::Value *overflow_arg_area_p =
1979 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1980 llvm::Value *overflow_arg_area =
1981 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1982
1983 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1984 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1985 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1986 if (Align > 8) {
1987 // Note that we follow the ABI & gcc here, even though the type
1988 // could in theory have an alignment greater than 16. This case
1989 // shouldn't ever matter in practice.
1990
1991 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson0032b272009-08-13 21:57:51 +00001992 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001993 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001994 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1995 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00001996 CGF.Int64Ty);
1997 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001998 overflow_arg_area =
1999 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2000 overflow_arg_area->getType(),
2001 "overflow_arg_area.align");
2002 }
2003
2004 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002005 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002006 llvm::Value *Res =
2007 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002008 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002009
2010 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2011 // l->overflow_arg_area + sizeof(type).
2012 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2013 // an 8 byte boundary.
2014
2015 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00002016 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00002017 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002018 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2019 "overflow_arg_area.next");
2020 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2021
2022 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2023 return Res;
2024}
2025
2026llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2027 CodeGenFunction &CGF) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00002028 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump1eb44332009-09-09 15:08:12 +00002029
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002030 // Assume that va_list type is correct; should be pointer to LLVM type:
2031 // struct {
2032 // i32 gp_offset;
2033 // i32 fp_offset;
2034 // i8* overflow_arg_area;
2035 // i8* reg_save_area;
2036 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00002037 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002038
Chris Lattnera14db752010-03-11 18:19:55 +00002039 Ty = CGF.getContext().getCanonicalType(Ty);
Bill Wendling99aaae82010-10-18 23:51:38 +00002040 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002041
2042 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2043 // in the registers. If not go to step 7.
2044 if (!neededInt && !neededSSE)
2045 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2046
2047 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2048 // general purpose registers needed to pass type and num_fp to hold
2049 // the number of floating point registers needed.
2050
2051 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2052 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2053 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2054 //
2055 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2056 // register save space).
2057
2058 llvm::Value *InRegs = 0;
2059 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2060 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2061 if (neededInt) {
2062 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2063 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002064 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2065 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002066 }
2067
2068 if (neededSSE) {
2069 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2070 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2071 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00002072 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2073 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002074 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2075 }
2076
2077 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2078 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2079 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2080 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2081
2082 // Emit code to load the value if it was passed in registers.
2083
2084 CGF.EmitBlock(InRegBlock);
2085
2086 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2087 // an offset of l->gp_offset and/or l->fp_offset. This may require
2088 // copying to a temporary location in case the parameter is passed
2089 // in different register classes or requires an alignment greater
2090 // than 8 for general purpose registers and 16 for XMM registers.
2091 //
2092 // FIXME: This really results in shameful code when we end up needing to
2093 // collect arguments from different places; often what should result in a
2094 // simple assembling of a structure from scattered addresses has many more
2095 // loads than necessary. Can we clean this up?
Chris Lattner2acc6e32011-07-18 04:24:23 +00002096 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002097 llvm::Value *RegAddr =
2098 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2099 "reg_save_area");
2100 if (neededInt && neededSSE) {
2101 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002102 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002103 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002104 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2105 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002106 llvm::Type *TyLo = ST->getElementType(0);
2107 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002108 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002109 "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002110 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2111 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002112 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2113 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002114 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2115 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002116 llvm::Value *V =
2117 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2118 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2119 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2120 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2121
Owen Andersona1cf15f2009-07-14 23:10:40 +00002122 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002123 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002124 } else if (neededInt) {
2125 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2126 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002127 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002128 } else if (neededSSE == 1) {
2129 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2130 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2131 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002132 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002133 assert(neededSSE == 2 && "Invalid number of needed registers!");
2134 // SSE registers are spaced 16 bytes apart in the register save
2135 // area, we need to collect the two eightbytes together.
2136 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002137 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Jay Foadef6de3d2011-07-11 09:56:20 +00002138 llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002139 llvm::Type *DblPtrTy =
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002140 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002141 llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002142 DoubleTy, NULL);
2143 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2144 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2145 DblPtrTy));
2146 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2147 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2148 DblPtrTy));
2149 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2150 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2151 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002152 }
2153
2154 // AMD64-ABI 3.5.7p5: Step 5. Set:
2155 // l->gp_offset = l->gp_offset + num_gp * 8
2156 // l->fp_offset = l->fp_offset + num_fp * 16.
2157 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002158 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002159 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2160 gp_offset_p);
2161 }
2162 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002163 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002164 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2165 fp_offset_p);
2166 }
2167 CGF.EmitBranch(ContBlock);
2168
2169 // Emit code to load the value if it was passed in memory.
2170
2171 CGF.EmitBlock(InMemBlock);
2172 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2173
2174 // Return the appropriate result.
2175
2176 CGF.EmitBlock(ContBlock);
Jay Foadbbf3bac2011-03-30 11:28:58 +00002177 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002178 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002179 ResAddr->addIncoming(RegAddr, InRegBlock);
2180 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002181 return ResAddr;
2182}
2183
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002184ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2185
2186 if (Ty->isVoidType())
2187 return ABIArgInfo::getIgnore();
2188
2189 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2190 Ty = EnumTy->getDecl()->getIntegerType();
2191
2192 uint64_t Size = getContext().getTypeSize(Ty);
2193
2194 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002195 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2196 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002197 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2198
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002199 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2200 if (Size == 128 &&
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002201 getContext().getTargetInfo().getTriple().getOS() == llvm::Triple::MinGW32)
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002202 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2203 Size));
2204
2205 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2206 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2207 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002208 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002209 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2210 Size));
2211
2212 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2213 }
2214
2215 if (Ty->isPromotableIntegerType())
2216 return ABIArgInfo::getExtend();
2217
2218 return ABIArgInfo::getDirect();
2219}
2220
2221void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2222
2223 QualType RetTy = FI.getReturnType();
2224 FI.getReturnInfo() = classify(RetTy);
2225
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002226 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2227 it != ie; ++it)
2228 it->info = classify(it->type);
2229}
2230
Chris Lattnerf13721d2010-08-31 16:44:54 +00002231llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2232 CodeGenFunction &CGF) const {
Chris Lattner2acc6e32011-07-18 04:24:23 +00002233 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2234 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002235
Chris Lattnerf13721d2010-08-31 16:44:54 +00002236 CGBuilderTy &Builder = CGF.Builder;
2237 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2238 "ap");
2239 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2240 llvm::Type *PTy =
2241 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2242 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2243
2244 uint64_t Offset =
2245 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2246 llvm::Value *NextAddr =
2247 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2248 "ap.next");
2249 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2250
2251 return AddrTyped;
2252}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002253
John McCallec853ba2010-03-11 00:10:12 +00002254// PowerPC-32
2255
2256namespace {
2257class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2258public:
Chris Lattnerea044322010-07-29 02:01:43 +00002259 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002260
John McCallec853ba2010-03-11 00:10:12 +00002261 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2262 // This is recovered from gcc output.
2263 return 1; // r1 is the dedicated stack pointer
2264 }
2265
2266 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002267 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002268};
2269
2270}
2271
2272bool
2273PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2274 llvm::Value *Address) const {
2275 // This is calculated from the LLVM and GCC tables and verified
2276 // against gcc output. AFAIK all ABIs use the same encoding.
2277
2278 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2279 llvm::LLVMContext &Context = CGF.getLLVMContext();
2280
Chris Lattner2acc6e32011-07-18 04:24:23 +00002281 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCallec853ba2010-03-11 00:10:12 +00002282 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2283 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2284 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2285
2286 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002287 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002288
2289 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002290 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002291
2292 // 64-76 are various 4-byte special-purpose registers:
2293 // 64: mq
2294 // 65: lr
2295 // 66: ctr
2296 // 67: ap
2297 // 68-75 cr0-7
2298 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002299 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002300
2301 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002302 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002303
2304 // 109: vrsave
2305 // 110: vscr
2306 // 111: spe_acc
2307 // 112: spefscr
2308 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002309 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002310
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002311 return false;
John McCallec853ba2010-03-11 00:10:12 +00002312}
2313
2314
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002315//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002316// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002317//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002318
2319namespace {
2320
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002321class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002322public:
2323 enum ABIKind {
2324 APCS = 0,
2325 AAPCS = 1,
2326 AAPCS_VFP
2327 };
2328
2329private:
2330 ABIKind Kind;
2331
2332public:
Chris Lattnerea044322010-07-29 02:01:43 +00002333 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002334
John McCall49e34be2011-08-30 01:42:09 +00002335 bool isEABI() const {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002336 StringRef Env = getContext().getTargetInfo().getTriple().getEnvironmentName();
John McCall49e34be2011-08-30 01:42:09 +00002337 return (Env == "gnueabi" || Env == "eabi");
2338 }
2339
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002340private:
2341 ABIKind getABIKind() const { return Kind; }
2342
Chris Lattnera3c109b2010-07-29 02:16:43 +00002343 ABIArgInfo classifyReturnType(QualType RetTy) const;
2344 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002345
Chris Lattneree5dcd02010-07-29 02:31:05 +00002346 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002347
2348 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2349 CodeGenFunction &CGF) const;
2350};
2351
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002352class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2353public:
Chris Lattnerea044322010-07-29 02:01:43 +00002354 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2355 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00002356
John McCall49e34be2011-08-30 01:42:09 +00002357 const ARMABIInfo &getABIInfo() const {
2358 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
2359 }
2360
John McCall6374c332010-03-06 00:35:14 +00002361 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2362 return 13;
2363 }
Roman Divacky09345d12011-05-18 19:36:54 +00002364
Chris Lattner5f9e2722011-07-23 10:55:15 +00002365 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCallf85e1932011-06-15 23:02:42 +00002366 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2367 }
2368
Roman Divacky09345d12011-05-18 19:36:54 +00002369 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2370 llvm::Value *Address) const {
2371 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2372 llvm::LLVMContext &Context = CGF.getLLVMContext();
2373
Chris Lattner2acc6e32011-07-18 04:24:23 +00002374 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
Roman Divacky09345d12011-05-18 19:36:54 +00002375 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2376
2377 // 0-15 are the 16 integer registers.
2378 AssignToArrayRange(Builder, Address, Four8, 0, 15);
2379
2380 return false;
2381 }
John McCall49e34be2011-08-30 01:42:09 +00002382
2383 unsigned getSizeOfUnwindException() const {
2384 if (getABIInfo().isEABI()) return 88;
2385 return TargetCodeGenInfo::getSizeOfUnwindException();
2386 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002387};
2388
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002389}
2390
Chris Lattneree5dcd02010-07-29 02:31:05 +00002391void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002392 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002393 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattnera3c109b2010-07-29 02:16:43 +00002394 it != ie; ++it)
2395 it->info = classifyArgumentType(it->type);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002396
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002397 // Always honor user-specified calling convention.
2398 if (FI.getCallingConvention() != llvm::CallingConv::C)
2399 return;
2400
2401 // Calling convention as default by an ABI.
Rafael Espindola25117ab2010-06-16 16:13:39 +00002402 llvm::CallingConv::ID DefaultCC;
John McCall49e34be2011-08-30 01:42:09 +00002403 if (isEABI())
Rafael Espindola25117ab2010-06-16 16:13:39 +00002404 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00002405 else
2406 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00002407
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002408 // If user did not ask for specific calling convention explicitly (e.g. via
2409 // pcs attribute), set effective calling convention if it's different than ABI
2410 // default.
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002411 switch (getABIKind()) {
2412 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002413 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2414 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002415 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002416 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002417 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2418 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002419 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002420 case AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002421 if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
2422 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002423 break;
2424 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002425}
2426
Bob Wilson194f06a2011-08-03 05:58:22 +00002427/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
2428/// aggregate. If HAMembers is non-null, the number of base elements
2429/// contained in the type is returned through it; this is used for the
2430/// recursive calls that check aggregate component types.
2431static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
2432 ASTContext &Context,
2433 uint64_t *HAMembers = 0) {
2434 uint64_t Members;
2435 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2436 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
2437 return false;
2438 Members *= AT->getSize().getZExtValue();
2439 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
2440 const RecordDecl *RD = RT->getDecl();
2441 if (RD->isUnion() || RD->hasFlexibleArrayMember())
2442 return false;
2443 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
2444 if (!CXXRD->isAggregate())
2445 return false;
2446 }
2447 Members = 0;
2448 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2449 i != e; ++i) {
2450 const FieldDecl *FD = *i;
2451 uint64_t FldMembers;
2452 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
2453 return false;
2454 Members += FldMembers;
2455 }
2456 } else {
2457 Members = 1;
2458 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2459 Members = 2;
2460 Ty = CT->getElementType();
2461 }
2462
2463 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
2464 // double, or 64-bit or 128-bit vectors.
2465 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
2466 if (BT->getKind() != BuiltinType::Float &&
2467 BT->getKind() != BuiltinType::Double)
2468 return false;
2469 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
2470 unsigned VecSize = Context.getTypeSize(VT);
2471 if (VecSize != 64 && VecSize != 128)
2472 return false;
2473 } else {
2474 return false;
2475 }
2476
2477 // The base type must be the same for all members. Vector types of the
2478 // same total size are treated as being equivalent here.
2479 const Type *TyPtr = Ty.getTypePtr();
2480 if (!Base)
2481 Base = TyPtr;
2482 if (Base != TyPtr &&
2483 (!Base->isVectorType() || !TyPtr->isVectorType() ||
2484 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
2485 return false;
2486 }
2487
2488 // Homogeneous Aggregates can have at most 4 members of the base type.
2489 if (HAMembers)
2490 *HAMembers = Members;
2491 return (Members <= 4);
2492}
2493
Chris Lattnera3c109b2010-07-29 02:16:43 +00002494ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +00002495 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002496 // Treat an enum type as its underlying type.
2497 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2498 Ty = EnumTy->getDecl()->getIntegerType();
2499
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002500 return (Ty->isPromotableIntegerType() ?
2501 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002502 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002503
Daniel Dunbar42025572009-09-14 21:54:03 +00002504 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002505 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00002506 return ABIArgInfo::getIgnore();
2507
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002508 // Structures with either a non-trivial destructor or a non-trivial
2509 // copy constructor are always indirect.
2510 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2511 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2512
Bob Wilson194f06a2011-08-03 05:58:22 +00002513 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
2514 // Homogeneous Aggregates need to be expanded.
2515 const Type *Base = 0;
2516 if (isHomogeneousAggregate(Ty, Base, getContext()))
2517 return ABIArgInfo::getExpand();
2518 }
2519
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00002520 // Otherwise, pass by coercing to a structure of the appropriate size.
2521 //
Bob Wilson53fc1a62011-08-01 23:39:04 +00002522 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
2523 // backend doesn't support byval.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002524 // FIXME: This doesn't handle alignment > 64 bits.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002525 llvm::Type* ElemTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002526 unsigned SizeRegs;
Bob Wilson53fc1a62011-08-01 23:39:04 +00002527 if (getContext().getTypeAlign(Ty) > 32) {
Stuart Hastings67d097e2011-04-27 17:24:02 +00002528 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2529 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Bob Wilson53fc1a62011-08-01 23:39:04 +00002530 } else {
2531 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2532 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Stuart Hastings67d097e2011-04-27 17:24:02 +00002533 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00002534
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002535 llvm::Type *STy =
Chris Lattner7650d952011-06-18 22:49:11 +00002536 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00002537 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002538}
2539
Chris Lattnera3c109b2010-07-29 02:16:43 +00002540static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00002541 llvm::LLVMContext &VMContext) {
2542 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2543 // is called integer-like if its size is less than or equal to one word, and
2544 // the offset of each of its addressable sub-fields is zero.
2545
2546 uint64_t Size = Context.getTypeSize(Ty);
2547
2548 // Check that the type fits in a word.
2549 if (Size > 32)
2550 return false;
2551
2552 // FIXME: Handle vector types!
2553 if (Ty->isVectorType())
2554 return false;
2555
Daniel Dunbarb0d58192009-09-14 02:20:34 +00002556 // Float types are never treated as "integer like".
2557 if (Ty->isRealFloatingType())
2558 return false;
2559
Daniel Dunbar98303b92009-09-13 08:03:58 +00002560 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00002561 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00002562 return true;
2563
Daniel Dunbar45815812010-02-01 23:31:26 +00002564 // Small complex integer types are "integer like".
2565 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2566 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002567
2568 // Single element and zero sized arrays should be allowed, by the definition
2569 // above, but they are not.
2570
2571 // Otherwise, it must be a record type.
2572 const RecordType *RT = Ty->getAs<RecordType>();
2573 if (!RT) return false;
2574
2575 // Ignore records with flexible arrays.
2576 const RecordDecl *RD = RT->getDecl();
2577 if (RD->hasFlexibleArrayMember())
2578 return false;
2579
2580 // Check that all sub-fields are at offset 0, and are themselves "integer
2581 // like".
2582 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2583
2584 bool HadField = false;
2585 unsigned idx = 0;
2586 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2587 i != e; ++i, ++idx) {
2588 const FieldDecl *FD = *i;
2589
Daniel Dunbar679855a2010-01-29 03:22:29 +00002590 // Bit-fields are not addressable, we only need to verify they are "integer
2591 // like". We still have to disallow a subsequent non-bitfield, for example:
2592 // struct { int : 0; int x }
2593 // is non-integer like according to gcc.
2594 if (FD->isBitField()) {
2595 if (!RD->isUnion())
2596 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002597
Daniel Dunbar679855a2010-01-29 03:22:29 +00002598 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2599 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002600
Daniel Dunbar679855a2010-01-29 03:22:29 +00002601 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002602 }
2603
Daniel Dunbar679855a2010-01-29 03:22:29 +00002604 // Check if this field is at offset 0.
2605 if (Layout.getFieldOffset(idx) != 0)
2606 return false;
2607
Daniel Dunbar98303b92009-09-13 08:03:58 +00002608 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2609 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002610
Daniel Dunbar679855a2010-01-29 03:22:29 +00002611 // Only allow at most one field in a structure. This doesn't match the
2612 // wording above, but follows gcc in situations with a field following an
2613 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00002614 if (!RD->isUnion()) {
2615 if (HadField)
2616 return false;
2617
2618 HadField = true;
2619 }
2620 }
2621
2622 return true;
2623}
2624
Chris Lattnera3c109b2010-07-29 02:16:43 +00002625ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002626 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002627 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00002628
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00002629 // Large vector types should be returned via memory.
2630 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
2631 return ABIArgInfo::getIndirect(0);
2632
John McCalld608cdb2010-08-22 10:59:02 +00002633 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002634 // Treat an enum type as its underlying type.
2635 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2636 RetTy = EnumTy->getDecl()->getIntegerType();
2637
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002638 return (RetTy->isPromotableIntegerType() ?
2639 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002640 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002641
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002642 // Structures with either a non-trivial destructor or a non-trivial
2643 // copy constructor are always indirect.
2644 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2645 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2646
Daniel Dunbar98303b92009-09-13 08:03:58 +00002647 // Are we following APCS?
2648 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002649 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00002650 return ABIArgInfo::getIgnore();
2651
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002652 // Complex types are all returned as packed integers.
2653 //
2654 // FIXME: Consider using 2 x vector types if the back end handles them
2655 // correctly.
2656 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00002657 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00002658 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002659
Daniel Dunbar98303b92009-09-13 08:03:58 +00002660 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002661 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002662 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002663 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002664 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002665 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002666 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002667 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2668 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002669 }
2670
2671 // Otherwise return in memory.
2672 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002673 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002674
2675 // Otherwise this is an AAPCS variant.
2676
Chris Lattnera3c109b2010-07-29 02:16:43 +00002677 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00002678 return ABIArgInfo::getIgnore();
2679
Bob Wilson3b694fa2011-11-02 04:51:36 +00002680 // Check for homogeneous aggregates with AAPCS-VFP.
2681 if (getABIKind() == AAPCS_VFP) {
2682 const Type *Base = 0;
2683 if (isHomogeneousAggregate(RetTy, Base, getContext()))
2684 // Homogeneous Aggregates are returned directly.
2685 return ABIArgInfo::getDirect();
2686 }
2687
Daniel Dunbar98303b92009-09-13 08:03:58 +00002688 // Aggregates <= 4 bytes are returned in r0; other aggregates
2689 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002690 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00002691 if (Size <= 32) {
2692 // Return in the smallest viable integer type.
2693 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002694 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002695 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002696 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2697 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002698 }
2699
Daniel Dunbar98303b92009-09-13 08:03:58 +00002700 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002701}
2702
2703llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00002704 CodeGenFunction &CGF) const {
Chris Lattner2acc6e32011-07-18 04:24:23 +00002705 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2706 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002707
2708 CGBuilderTy &Builder = CGF.Builder;
2709 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2710 "ap");
2711 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Rafael Espindolae164c182011-08-02 22:33:37 +00002712 // Handle address alignment for type alignment > 32 bits
2713 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
2714 if (TyAlign > 4) {
2715 assert((TyAlign & (TyAlign - 1)) == 0 &&
2716 "Alignment is not power of 2!");
2717 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
2718 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
2719 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
2720 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2721 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002722 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00002723 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002724 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2725
2726 uint64_t Offset =
2727 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2728 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00002729 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002730 "ap.next");
2731 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2732
2733 return AddrTyped;
2734}
2735
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002736//===----------------------------------------------------------------------===//
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002737// PTX ABI Implementation
2738//===----------------------------------------------------------------------===//
2739
2740namespace {
2741
2742class PTXABIInfo : public ABIInfo {
2743public:
2744 PTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2745
2746 ABIArgInfo classifyReturnType(QualType RetTy) const;
2747 ABIArgInfo classifyArgumentType(QualType Ty) const;
2748
2749 virtual void computeInfo(CGFunctionInfo &FI) const;
2750 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2751 CodeGenFunction &CFG) const;
2752};
2753
2754class PTXTargetCodeGenInfo : public TargetCodeGenInfo {
2755public:
2756 PTXTargetCodeGenInfo(CodeGenTypes &CGT)
2757 : TargetCodeGenInfo(new PTXABIInfo(CGT)) {}
Justin Holewinski818eafb2011-10-05 17:58:44 +00002758
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00002759 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2760 CodeGen::CodeGenModule &M) const;
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002761};
2762
2763ABIArgInfo PTXABIInfo::classifyReturnType(QualType RetTy) const {
2764 if (RetTy->isVoidType())
2765 return ABIArgInfo::getIgnore();
2766 if (isAggregateTypeForABI(RetTy))
2767 return ABIArgInfo::getIndirect(0);
2768 return ABIArgInfo::getDirect();
2769}
2770
2771ABIArgInfo PTXABIInfo::classifyArgumentType(QualType Ty) const {
2772 if (isAggregateTypeForABI(Ty))
2773 return ABIArgInfo::getIndirect(0);
2774
2775 return ABIArgInfo::getDirect();
2776}
2777
2778void PTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
2779 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2780 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2781 it != ie; ++it)
2782 it->info = classifyArgumentType(it->type);
2783
2784 // Always honor user-specified calling convention.
2785 if (FI.getCallingConvention() != llvm::CallingConv::C)
2786 return;
2787
2788 // Calling convention as default by an ABI.
2789 llvm::CallingConv::ID DefaultCC;
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002790 const LangOptions &LangOpts = getContext().getLangOptions();
2791 if (LangOpts.OpenCL || LangOpts.CUDA) {
2792 // If we are in OpenCL or CUDA mode, then default to device functions
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002793 DefaultCC = llvm::CallingConv::PTX_Device;
Justin Holewinski818eafb2011-10-05 17:58:44 +00002794 } else {
2795 // If we are in standard C/C++ mode, use the triple to decide on the default
2796 StringRef Env =
2797 getContext().getTargetInfo().getTriple().getEnvironmentName();
2798 if (Env == "device")
2799 DefaultCC = llvm::CallingConv::PTX_Device;
2800 else
2801 DefaultCC = llvm::CallingConv::PTX_Kernel;
2802 }
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002803 FI.setEffectiveCallingConvention(DefaultCC);
Justin Holewinski818eafb2011-10-05 17:58:44 +00002804
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002805}
2806
2807llvm::Value *PTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2808 CodeGenFunction &CFG) const {
2809 llvm_unreachable("PTX does not support varargs");
2810 return 0;
2811}
2812
Justin Holewinski818eafb2011-10-05 17:58:44 +00002813void PTXTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2814 llvm::GlobalValue *GV,
2815 CodeGen::CodeGenModule &M) const{
2816 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2817 if (!FD) return;
2818
2819 llvm::Function *F = cast<llvm::Function>(GV);
2820
2821 // Perform special handling in OpenCL mode
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002822 if (M.getLangOptions().OpenCL) {
Justin Holewinski818eafb2011-10-05 17:58:44 +00002823 // Use OpenCL function attributes to set proper calling conventions
2824 // By default, all functions are device functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00002825 if (FD->hasAttr<OpenCLKernelAttr>()) {
2826 // OpenCL __kernel functions get a kernel calling convention
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002827 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00002828 // And kernel functions are not subject to inlining
2829 F->addFnAttr(llvm::Attribute::NoInline);
2830 }
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002831 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00002832
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002833 // Perform special handling in CUDA mode.
2834 if (M.getLangOptions().CUDA) {
2835 // CUDA __global__ functions get a kernel calling convention. Since
2836 // __global__ functions cannot be called from the device, we do not
2837 // need to set the noinline attribute.
2838 if (FD->getAttr<CUDAGlobalAttr>())
2839 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00002840 }
2841}
2842
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002843}
2844
2845//===----------------------------------------------------------------------===//
Wesley Peck276fdf42010-12-19 19:57:51 +00002846// MBlaze ABI Implementation
2847//===----------------------------------------------------------------------===//
2848
2849namespace {
2850
2851class MBlazeABIInfo : public ABIInfo {
2852public:
2853 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2854
2855 bool isPromotableIntegerType(QualType Ty) const;
2856
2857 ABIArgInfo classifyReturnType(QualType RetTy) const;
2858 ABIArgInfo classifyArgumentType(QualType RetTy) const;
2859
2860 virtual void computeInfo(CGFunctionInfo &FI) const {
2861 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2862 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2863 it != ie; ++it)
2864 it->info = classifyArgumentType(it->type);
2865 }
2866
2867 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2868 CodeGenFunction &CGF) const;
2869};
2870
2871class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
2872public:
2873 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
2874 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
2875 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2876 CodeGen::CodeGenModule &M) const;
2877};
2878
2879}
2880
2881bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
2882 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
2883 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2884 switch (BT->getKind()) {
2885 case BuiltinType::Bool:
2886 case BuiltinType::Char_S:
2887 case BuiltinType::Char_U:
2888 case BuiltinType::SChar:
2889 case BuiltinType::UChar:
2890 case BuiltinType::Short:
2891 case BuiltinType::UShort:
2892 return true;
2893 default:
2894 return false;
2895 }
2896 return false;
2897}
2898
2899llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2900 CodeGenFunction &CGF) const {
2901 // FIXME: Implement
2902 return 0;
2903}
2904
2905
2906ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
2907 if (RetTy->isVoidType())
2908 return ABIArgInfo::getIgnore();
2909 if (isAggregateTypeForABI(RetTy))
2910 return ABIArgInfo::getIndirect(0);
2911
2912 return (isPromotableIntegerType(RetTy) ?
2913 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2914}
2915
2916ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
2917 if (isAggregateTypeForABI(Ty))
2918 return ABIArgInfo::getIndirect(0);
2919
2920 return (isPromotableIntegerType(Ty) ?
2921 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2922}
2923
2924void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2925 llvm::GlobalValue *GV,
2926 CodeGen::CodeGenModule &M)
2927 const {
2928 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2929 if (!FD) return;
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00002930
Wesley Peck276fdf42010-12-19 19:57:51 +00002931 llvm::CallingConv::ID CC = llvm::CallingConv::C;
2932 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
2933 CC = llvm::CallingConv::MBLAZE_INTR;
2934 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
2935 CC = llvm::CallingConv::MBLAZE_SVOL;
2936
2937 if (CC != llvm::CallingConv::C) {
2938 // Handle 'interrupt_handler' attribute:
2939 llvm::Function *F = cast<llvm::Function>(GV);
2940
2941 // Step 1: Set ISR calling convention.
2942 F->setCallingConv(CC);
2943
2944 // Step 2: Add attributes goodness.
2945 F->addFnAttr(llvm::Attribute::NoInline);
2946 }
2947
2948 // Step 3: Emit _interrupt_handler alias.
2949 if (CC == llvm::CallingConv::MBLAZE_INTR)
2950 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2951 "_interrupt_handler", GV, &M.getModule());
2952}
2953
2954
2955//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002956// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002957//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002958
2959namespace {
2960
2961class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2962public:
Chris Lattnerea044322010-07-29 02:01:43 +00002963 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
2964 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002965 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2966 CodeGen::CodeGenModule &M) const;
2967};
2968
2969}
2970
2971void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2972 llvm::GlobalValue *GV,
2973 CodeGen::CodeGenModule &M) const {
2974 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2975 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2976 // Handle 'interrupt' attribute:
2977 llvm::Function *F = cast<llvm::Function>(GV);
2978
2979 // Step 1: Set ISR calling convention.
2980 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2981
2982 // Step 2: Add attributes goodness.
2983 F->addFnAttr(llvm::Attribute::NoInline);
2984
2985 // Step 3: Emit ISR vector alias.
2986 unsigned Num = attr->getNumber() + 0xffe0;
2987 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002988 "vector_" + Twine::utohexstr(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002989 GV, &M.getModule());
2990 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002991 }
2992}
2993
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002994//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00002995// MIPS ABI Implementation. This works for both little-endian and
2996// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002997//===----------------------------------------------------------------------===//
2998
John McCallaeeb7012010-05-27 06:19:26 +00002999namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00003000class MipsABIInfo : public ABIInfo {
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003001 bool IsO32;
Akira Hatanakab551dd32011-11-03 00:05:50 +00003002 unsigned MinABIStackAlignInBytes;
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003003 llvm::Type* HandleStructTy(QualType Ty) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003004public:
Akira Hatanakab551dd32011-11-03 00:05:50 +00003005 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
3006 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8) {}
Akira Hatanaka619e8872011-06-02 00:09:17 +00003007
3008 ABIArgInfo classifyReturnType(QualType RetTy) const;
3009 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3010 virtual void computeInfo(CGFunctionInfo &FI) const;
3011 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3012 CodeGenFunction &CGF) const;
3013};
3014
John McCallaeeb7012010-05-27 06:19:26 +00003015class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003016 unsigned SizeOfUnwindException;
John McCallaeeb7012010-05-27 06:19:26 +00003017public:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003018 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
3019 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
3020 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCallaeeb7012010-05-27 06:19:26 +00003021
3022 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
3023 return 29;
3024 }
3025
3026 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003027 llvm::Value *Address) const;
John McCall49e34be2011-08-30 01:42:09 +00003028
3029 unsigned getSizeOfUnwindException() const {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003030 return SizeOfUnwindException;
John McCall49e34be2011-08-30 01:42:09 +00003031 }
John McCallaeeb7012010-05-27 06:19:26 +00003032};
3033}
3034
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003035// In N32/64, an aligned double precision floating point field is passed in
3036// a register.
3037llvm::Type* MipsABIInfo::HandleStructTy(QualType Ty) const {
3038 if (IsO32)
3039 return 0;
3040
3041 const RecordType *RT = Ty->getAsStructureType();
3042
3043 if (!RT)
3044 return 0;
3045
3046 const RecordDecl *RD = RT->getDecl();
3047 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3048 uint64_t StructSize = getContext().getTypeSize(Ty);
3049 assert(!(StructSize % 8) && "Size of structure must be multiple of 8.");
3050
3051 SmallVector<llvm::Type*, 8> ArgList;
3052 uint64_t LastOffset = 0;
3053 unsigned idx = 0;
3054 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
3055
3056 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3057 i != e; ++i, ++idx) {
3058 const QualType Ty = (*i)->getType();
3059 const BuiltinType *BT = Ty->getAs<BuiltinType>();
3060
3061 if (!BT || BT->getKind() != BuiltinType::Double)
3062 continue;
3063
3064 uint64_t Offset = Layout.getFieldOffset(idx);
3065 if (Offset % 64) // Ignore doubles that are not aligned.
3066 continue;
3067
3068 // Add ((Offset - LastOffset) / 64) args of type i64.
3069 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
3070 ArgList.push_back(I64);
3071
3072 // Add double type.
3073 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
3074 LastOffset = Offset + 64;
3075 }
3076
3077 // This structure doesn't have an aligned double field.
3078 if (!LastOffset)
3079 return 0;
3080
3081 // Add ((StructSize - LastOffset) / 64) args of type i64.
3082 for (unsigned N = (StructSize - LastOffset) / 64; N; --N)
3083 ArgList.push_back(I64);
3084
3085 // Whatever is left over goes into a structure consisting of sub-doubleword
3086 // types. For example, if the size of the remainder is 40-bytes,
3087 // struct {i32, i8} is added to ArgList.
3088 unsigned R = (StructSize - LastOffset) % 64;
3089 SmallVector<llvm::Type*, 3> ArgList2;
3090
3091 for (; R; R &= (R - 1))
3092 ArgList2.insert(ArgList2.begin(),
3093 llvm::IntegerType::get(getVMContext(), (R & (R - 1)) ^ R));
3094
3095 if (!ArgList2.empty())
3096 ArgList.push_back(llvm::StructType::get(getVMContext(), ArgList2));
3097
3098 return llvm::StructType::get(getVMContext(), ArgList);
3099}
3100
Akira Hatanaka619e8872011-06-02 00:09:17 +00003101ABIArgInfo MipsABIInfo::classifyArgumentType(QualType Ty) const {
3102 if (isAggregateTypeForABI(Ty)) {
3103 // Ignore empty aggregates.
3104 if (getContext().getTypeSize(Ty) == 0)
3105 return ABIArgInfo::getIgnore();
3106
Akira Hatanaka511949b2011-08-01 18:09:58 +00003107 // Records with non trivial destructors/constructors should not be passed
3108 // by value.
3109 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
3110 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3111
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003112 llvm::Type *ResType;
3113 if ((ResType = HandleStructTy(Ty)))
3114 return ABIArgInfo::getDirect(ResType);
3115
Akira Hatanaka619e8872011-06-02 00:09:17 +00003116 return ABIArgInfo::getIndirect(0);
3117 }
3118
3119 // Treat an enum type as its underlying type.
3120 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3121 Ty = EnumTy->getDecl()->getIntegerType();
3122
3123 return (Ty->isPromotableIntegerType() ?
3124 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3125}
3126
3127ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
3128 if (RetTy->isVoidType())
3129 return ABIArgInfo::getIgnore();
3130
3131 if (isAggregateTypeForABI(RetTy)) {
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003132 if ((IsO32 && RetTy->isAnyComplexType()) ||
3133 (!IsO32 && (getContext().getTypeSize(RetTy) <= 128)))
Akira Hatanaka619e8872011-06-02 00:09:17 +00003134 return ABIArgInfo::getDirect();
3135
3136 return ABIArgInfo::getIndirect(0);
3137 }
3138
3139 // Treat an enum type as its underlying type.
3140 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3141 RetTy = EnumTy->getDecl()->getIntegerType();
3142
3143 return (RetTy->isPromotableIntegerType() ?
3144 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3145}
3146
3147void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
3148 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3149 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3150 it != ie; ++it)
3151 it->info = classifyArgumentType(it->type);
3152}
3153
3154llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3155 CodeGenFunction &CGF) const {
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003156 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
3157 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
3158
3159 CGBuilderTy &Builder = CGF.Builder;
3160 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3161 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
3162 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
3163 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3164 llvm::Value *AddrTyped;
3165
3166 if (TypeAlign > MinABIStackAlignInBytes) {
3167 llvm::Value *AddrAsInt32 = CGF.Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3168 llvm::Value *Inc = llvm::ConstantInt::get(CGF.Int32Ty, TypeAlign - 1);
3169 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -TypeAlign);
3170 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt32, Inc);
3171 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
3172 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
3173 }
3174 else
3175 AddrTyped = Builder.CreateBitCast(Addr, PTy);
3176
3177 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka7b0a0382011-08-12 02:30:14 +00003178 TypeAlign = std::max(TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003179 uint64_t Offset =
3180 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
3181 llvm::Value *NextAddr =
3182 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
3183 "ap.next");
3184 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3185
3186 return AddrTyped;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003187}
3188
John McCallaeeb7012010-05-27 06:19:26 +00003189bool
3190MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3191 llvm::Value *Address) const {
3192 // This information comes from gcc's implementation, which seems to
3193 // as canonical as it gets.
3194
3195 CodeGen::CGBuilderTy &Builder = CGF.Builder;
3196 llvm::LLVMContext &Context = CGF.getLLVMContext();
3197
3198 // Everything on MIPS is 4 bytes. Double-precision FP registers
3199 // are aliased to pairs of single-precision FP registers.
Chris Lattner2acc6e32011-07-18 04:24:23 +00003200 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCallaeeb7012010-05-27 06:19:26 +00003201 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
3202
3203 // 0-31 are the general purpose registers, $0 - $31.
3204 // 32-63 are the floating-point registers, $f0 - $f31.
3205 // 64 and 65 are the multiply/divide registers, $hi and $lo.
3206 // 66 is the (notional, I think) register for signal-handler return.
3207 AssignToArrayRange(Builder, Address, Four8, 0, 65);
3208
3209 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
3210 // They are one bit wide and ignored here.
3211
3212 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
3213 // (coprocessor 1 is the FP unit)
3214 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
3215 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
3216 // 176-181 are the DSP accumulator registers.
3217 AssignToArrayRange(Builder, Address, Four8, 80, 181);
3218
3219 return false;
3220}
3221
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003222//===----------------------------------------------------------------------===//
3223// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
3224// Currently subclassed only to implement custom OpenCL C function attribute
3225// handling.
3226//===----------------------------------------------------------------------===//
3227
3228namespace {
3229
3230class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
3231public:
3232 TCETargetCodeGenInfo(CodeGenTypes &CGT)
3233 : DefaultTargetCodeGenInfo(CGT) {}
3234
3235 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3236 CodeGen::CodeGenModule &M) const;
3237};
3238
3239void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3240 llvm::GlobalValue *GV,
3241 CodeGen::CodeGenModule &M) const {
3242 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3243 if (!FD) return;
3244
3245 llvm::Function *F = cast<llvm::Function>(GV);
3246
3247 if (M.getLangOptions().OpenCL) {
3248 if (FD->hasAttr<OpenCLKernelAttr>()) {
3249 // OpenCL C Kernel functions are not subject to inlining
3250 F->addFnAttr(llvm::Attribute::NoInline);
3251
3252 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
3253
3254 // Convert the reqd_work_group_size() attributes to metadata.
3255 llvm::LLVMContext &Context = F->getContext();
3256 llvm::NamedMDNode *OpenCLMetadata =
3257 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
3258
3259 SmallVector<llvm::Value*, 5> Operands;
3260 Operands.push_back(F);
3261
3262 Operands.push_back(llvm::Constant::getIntegerValue(
3263 llvm::Type::getInt32Ty(Context),
3264 llvm::APInt(
3265 32,
3266 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
3267 Operands.push_back(llvm::Constant::getIntegerValue(
3268 llvm::Type::getInt32Ty(Context),
3269 llvm::APInt(
3270 32,
3271 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
3272 Operands.push_back(llvm::Constant::getIntegerValue(
3273 llvm::Type::getInt32Ty(Context),
3274 llvm::APInt(
3275 32,
3276 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
3277
3278 // Add a boolean constant operand for "required" (true) or "hint" (false)
3279 // for implementing the work_group_size_hint attr later. Currently
3280 // always true as the hint is not yet implemented.
3281 Operands.push_back(llvm::ConstantInt::getTrue(llvm::Type::getInt1Ty(Context)));
3282
3283 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
3284 }
3285 }
3286 }
3287}
3288
3289}
John McCallaeeb7012010-05-27 06:19:26 +00003290
Chris Lattnerea044322010-07-29 02:01:43 +00003291const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003292 if (TheTargetCodeGenInfo)
3293 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003294
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003295 const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00003296 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003297 default:
Chris Lattnerea044322010-07-29 02:01:43 +00003298 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003299
John McCallaeeb7012010-05-27 06:19:26 +00003300 case llvm::Triple::mips:
3301 case llvm::Triple::mipsel:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003302 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCallaeeb7012010-05-27 06:19:26 +00003303
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00003304 case llvm::Triple::mips64:
3305 case llvm::Triple::mips64el:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003306 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00003307
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003308 case llvm::Triple::arm:
3309 case llvm::Triple::thumb:
Sandeep Patel34c1af82011-04-05 00:23:47 +00003310 {
3311 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003312
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003313 if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
Sandeep Patel34c1af82011-04-05 00:23:47 +00003314 Kind = ARMABIInfo::APCS;
3315 else if (CodeGenOpts.FloatABI == "hard")
3316 Kind = ARMABIInfo::AAPCS_VFP;
3317
3318 return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind));
3319 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003320
John McCallec853ba2010-03-11 00:10:12 +00003321 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00003322 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00003323
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003324 case llvm::Triple::ptx32:
3325 case llvm::Triple::ptx64:
3326 return *(TheTargetCodeGenInfo = new PTXTargetCodeGenInfo(Types));
3327
Wesley Peck276fdf42010-12-19 19:57:51 +00003328 case llvm::Triple::mblaze:
3329 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
3330
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003331 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00003332 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003333
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003334 case llvm::Triple::tce:
3335 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
3336
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003337 case llvm::Triple::x86: {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003338 bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003339
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00003340 if (Triple.isOSDarwin())
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003341 return *(TheTargetCodeGenInfo =
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003342 new X86_32TargetCodeGenInfo(Types, true, true, DisableMMX));
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00003343
3344 switch (Triple.getOS()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003345 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003346 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00003347 case llvm::Triple::AuroraUX:
3348 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00003349 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003350 case llvm::Triple::OpenBSD:
Benjamin Kramer8e50a962011-02-02 18:59:27 +00003351 case llvm::Triple::NetBSD:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003352 return *(TheTargetCodeGenInfo =
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003353 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003354
3355 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003356 return *(TheTargetCodeGenInfo =
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003357 new X86_32TargetCodeGenInfo(Types, false, false, DisableMMX));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003358 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003359 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003360
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003361 case llvm::Triple::x86_64:
Chris Lattnerf13721d2010-08-31 16:44:54 +00003362 switch (Triple.getOS()) {
3363 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00003364 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00003365 case llvm::Triple::Cygwin:
3366 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
3367 default:
3368 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
3369 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003370 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003371}