blob: 4aa98d2fd8319d01903c2c2fae4a171f5250ae53 [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 McCallde5d3c72012-02-17 03:33:10 +0000101bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
102 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +0000103 // The following conventions are known to require this to be false:
104 // x86_stdcall
105 // MIPS
106 // For everything else, we just prefer false unless we opt out.
107 return false;
108}
109
Daniel Dunbar98303b92009-09-13 08:03:58 +0000110static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000111
112/// isEmptyField - Return true iff a the field is "empty", that is it
113/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +0000114static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
115 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000116 if (FD->isUnnamedBitfield())
117 return true;
118
119 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000120
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000121 // Constant arrays of empty records count as empty, strip them off.
122 // Constant arrays of zero length always count as empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000123 if (AllowArrays)
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000124 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
125 if (AT->getSize() == 0)
126 return true;
Daniel Dunbar98303b92009-09-13 08:03:58 +0000127 FT = AT->getElementType();
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000128 }
Daniel Dunbar98303b92009-09-13 08:03:58 +0000129
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000130 const RecordType *RT = FT->getAs<RecordType>();
131 if (!RT)
132 return false;
133
134 // C++ record fields are never empty, at least in the Itanium ABI.
135 //
136 // FIXME: We should use a predicate for whether this behavior is true in the
137 // current ABI.
138 if (isa<CXXRecordDecl>(RT->getDecl()))
139 return false;
140
Daniel Dunbar98303b92009-09-13 08:03:58 +0000141 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000142}
143
144/// isEmptyRecord - Return true iff a structure contains only empty
145/// fields. Note that a structure with a flexible array member is not
146/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000147static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000148 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000149 if (!RT)
150 return 0;
151 const RecordDecl *RD = RT->getDecl();
152 if (RD->hasFlexibleArrayMember())
153 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000154
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000155 // If this is a C++ record, check the bases first.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000156 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000157 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
158 e = CXXRD->bases_end(); i != e; ++i)
159 if (!isEmptyRecord(Context, i->getType(), true))
160 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000161
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000162 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
163 i != e; ++i)
David Blaikie581deb32012-06-06 20:45:41 +0000164 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000165 return false;
166 return true;
167}
168
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000169/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
170/// a non-trivial destructor or a non-trivial copy constructor.
171static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
172 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
173 if (!RD)
174 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000175
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000176 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
177}
178
179/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
180/// a record type with either a non-trivial destructor or a non-trivial copy
181/// constructor.
182static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
183 const RecordType *RT = T->getAs<RecordType>();
184 if (!RT)
185 return false;
186
187 return hasNonTrivialDestructorOrCopyConstructor(RT);
188}
189
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000190/// isSingleElementStruct - Determine if a structure is a "single
191/// element struct", i.e. it has exactly one non-empty field or
192/// exactly one field which is itself a single element
193/// struct. Structures with flexible array members are never
194/// considered single element structs.
195///
196/// \return The field declaration for the single non-empty field, if
197/// it exists.
198static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
199 const RecordType *RT = T->getAsStructureType();
200 if (!RT)
201 return 0;
202
203 const RecordDecl *RD = RT->getDecl();
204 if (RD->hasFlexibleArrayMember())
205 return 0;
206
207 const Type *Found = 0;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000208
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000209 // If this is a C++ record, check the bases first.
210 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
211 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
212 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000213 // Ignore empty records.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000214 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000215 continue;
216
217 // If we already found an element then this isn't a single-element struct.
218 if (Found)
219 return 0;
220
221 // If this is non-empty and not a single element struct, the composite
222 // cannot be a single element struct.
223 Found = isSingleElementStruct(i->getType(), Context);
224 if (!Found)
225 return 0;
226 }
227 }
228
229 // Check for single element.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000230 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
231 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000232 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000233 QualType FT = FD->getType();
234
235 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000236 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000237 continue;
238
239 // If we already found an element then this isn't a single-element
240 // struct.
241 if (Found)
242 return 0;
243
244 // Treat single element arrays as the element.
245 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
246 if (AT->getSize().getZExtValue() != 1)
247 break;
248 FT = AT->getElementType();
249 }
250
John McCalld608cdb2010-08-22 10:59:02 +0000251 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000252 Found = FT.getTypePtr();
253 } else {
254 Found = isSingleElementStruct(FT, Context);
255 if (!Found)
256 return 0;
257 }
258 }
259
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000260 // We don't consider a struct a single-element struct if it has
261 // padding beyond the element type.
262 if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
263 return 0;
264
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000265 return Found;
266}
267
268static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbara1842d32010-05-14 03:40:53 +0000269 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000270 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
271 !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000272 return false;
273
274 uint64_t Size = Context.getTypeSize(Ty);
275 return Size == 32 || Size == 64;
276}
277
Daniel Dunbar53012f42009-11-09 01:33:53 +0000278/// canExpandIndirectArgument - Test whether an argument type which is to be
279/// passed indirectly (on the stack) would have the equivalent layout if it was
280/// expanded into separate arguments. If so, we prefer to do the latter to avoid
281/// inhibiting optimizations.
282///
283// FIXME: This predicate is missing many cases, currently it just follows
284// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
285// should probably make this smarter, or better yet make the LLVM backend
286// capable of handling it.
287static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
288 // We can only expand structure types.
289 const RecordType *RT = Ty->getAs<RecordType>();
290 if (!RT)
291 return false;
292
293 // We can only expand (C) structures.
294 //
295 // FIXME: This needs to be generalized to handle classes as well.
296 const RecordDecl *RD = RT->getDecl();
297 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
298 return false;
299
Eli Friedman506d4e32011-11-18 01:32:26 +0000300 uint64_t Size = 0;
301
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000302 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
303 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000304 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000305
306 if (!is32Or64BitBasicType(FD->getType(), Context))
307 return false;
308
309 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
310 // how to expand them yet, and the predicate for telling if a bitfield still
311 // counts as "basic" is more complicated than what we were doing previously.
312 if (FD->isBitField())
313 return false;
Eli Friedman506d4e32011-11-18 01:32:26 +0000314
315 Size += Context.getTypeSize(FD->getType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000316 }
317
Eli Friedman506d4e32011-11-18 01:32:26 +0000318 // Make sure there are not any holes in the struct.
319 if (Size != Context.getTypeSize(Ty))
320 return false;
321
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000322 return true;
323}
324
325namespace {
326/// DefaultABIInfo - The default implementation for ABI specific
327/// details. This implementation provides information which results in
328/// self-consistent and sensible LLVM IR generation, but does not
329/// conform to any particular ABI.
330class DefaultABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +0000331public:
332 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000333
Chris Lattnera3c109b2010-07-29 02:16:43 +0000334 ABIArgInfo classifyReturnType(QualType RetTy) const;
335 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000336
Chris Lattneree5dcd02010-07-29 02:31:05 +0000337 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000338 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000339 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
340 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000341 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000342 }
343
344 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
345 CodeGenFunction &CGF) const;
346};
347
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000348class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
349public:
Chris Lattnerea044322010-07-29 02:01:43 +0000350 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
351 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000352};
353
354llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
355 CodeGenFunction &CGF) const {
356 return 0;
357}
358
Chris Lattnera3c109b2010-07-29 02:16:43 +0000359ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
Jan Wen Voung90306932011-11-03 00:59:44 +0000360 if (isAggregateTypeForABI(Ty)) {
361 // Records with non trivial destructors/constructors should not be passed
362 // by value.
363 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
364 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
365
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000366 return ABIArgInfo::getIndirect(0);
Jan Wen Voung90306932011-11-03 00:59:44 +0000367 }
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000368
Chris Lattnera14db752010-03-11 18:19:55 +0000369 // Treat an enum type as its underlying type.
370 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
371 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000372
Chris Lattnera14db752010-03-11 18:19:55 +0000373 return (Ty->isPromotableIntegerType() ?
374 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000375}
376
Bob Wilson0024f942011-01-10 23:54:17 +0000377ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
378 if (RetTy->isVoidType())
379 return ABIArgInfo::getIgnore();
380
381 if (isAggregateTypeForABI(RetTy))
382 return ABIArgInfo::getIndirect(0);
383
384 // Treat an enum type as its underlying type.
385 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
386 RetTy = EnumTy->getDecl()->getIntegerType();
387
388 return (RetTy->isPromotableIntegerType() ?
389 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
390}
391
Eli Friedman55fc7e22012-01-25 22:46:34 +0000392/// UseX86_MMXType - Return true if this is an MMX type that should use the
393/// special x86_mmx type.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000394bool UseX86_MMXType(llvm::Type *IRType) {
Bill Wendlingbb465d72010-10-18 03:41:31 +0000395 // If the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>, use the
396 // special x86_mmx type.
397 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
398 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
399 IRType->getScalarSizeInBits() != 64;
400}
401
Jay Foadef6de3d2011-07-11 09:56:20 +0000402static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000403 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000404 llvm::Type* Ty) {
Bill Wendling0507be62011-03-07 22:47:14 +0000405 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy())
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000406 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
407 return Ty;
408}
409
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000410//===----------------------------------------------------------------------===//
411// X86-32 ABI Implementation
412//===----------------------------------------------------------------------===//
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000413
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000414/// X86_32ABIInfo - The X86-32 ABI information.
415class X86_32ABIInfo : public ABIInfo {
Rafael Espindolab48280b2012-07-31 02:44:24 +0000416 enum Class {
417 Integer,
418 Float
419 };
420
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000421 static const unsigned MinABIStackAlignInBytes = 4;
422
David Chisnall1e4249c2009-08-17 23:08:21 +0000423 bool IsDarwinVectorABI;
424 bool IsSmallStructInRegABI;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000425 bool IsMMXDisabled;
Eli Friedman55fc7e22012-01-25 22:46:34 +0000426 bool IsWin32FloatStructABI;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000427 unsigned DefaultNumRegisterParameters;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000428
429 static bool isRegisterSize(unsigned Size) {
430 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
431 }
432
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000433 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context,
434 unsigned callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000435
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000436 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
437 /// such that the argument will be passed in memory.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000438 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000439
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000440 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbare59d8582010-09-16 20:42:06 +0000441 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000442
Rafael Espindolab48280b2012-07-31 02:44:24 +0000443 Class classify(QualType Ty) const;
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000444 ABIArgInfo classifyReturnType(QualType RetTy,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000445 unsigned callingConvention) const;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000446 ABIArgInfo classifyArgumentTypeWithReg(QualType RetTy,
447 unsigned &FreeRegs) const;
Chris Lattnera3c109b2010-07-29 02:16:43 +0000448 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000449
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000450public:
451
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000452 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000453 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
454 CodeGenFunction &CGF) const;
455
Rafael Espindolab48280b2012-07-31 02:44:24 +0000456 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m, bool w,
457 unsigned r)
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000458 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
Rafael Espindolab48280b2012-07-31 02:44:24 +0000459 IsMMXDisabled(m), IsWin32FloatStructABI(w),
460 DefaultNumRegisterParameters(r) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000461};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000462
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000463class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
464public:
Eli Friedman55fc7e22012-01-25 22:46:34 +0000465 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
Rafael Espindolab48280b2012-07-31 02:44:24 +0000466 bool d, bool p, bool m, bool w, unsigned r)
467 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, m, w, r)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000468
469 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
470 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000471
472 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
473 // Darwin uses different dwarf register numbers for EH.
474 if (CGM.isTargetDarwin()) return 5;
475
476 return 4;
477 }
478
479 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
480 llvm::Value *Address) const;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000481
Jay Foadef6de3d2011-07-11 09:56:20 +0000482 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000483 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000484 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000485 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
486 }
487
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000488};
489
490}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000491
492/// shouldReturnTypeInRegister - Determine if the given type should be
493/// passed in a register (for the Darwin ABI).
494bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000495 ASTContext &Context,
496 unsigned callingConvention) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000497 uint64_t Size = Context.getTypeSize(Ty);
498
499 // Type must be register sized.
500 if (!isRegisterSize(Size))
501 return false;
502
503 if (Ty->isVectorType()) {
504 // 64- and 128- bit vectors inside structures are not returned in
505 // registers.
506 if (Size == 64 || Size == 128)
507 return false;
508
509 return true;
510 }
511
Daniel Dunbar77115232010-05-15 00:00:30 +0000512 // If this is a builtin, pointer, enum, complex type, member pointer, or
513 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000514 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000515 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000516 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000517 return true;
518
519 // Arrays are treated like records.
520 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000521 return shouldReturnTypeInRegister(AT->getElementType(), Context,
522 callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000523
524 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000525 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000526 if (!RT) return false;
527
Anders Carlssona8874232010-01-27 03:25:19 +0000528 // FIXME: Traverse bases here too.
529
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000530 // For thiscall conventions, structures will never be returned in
531 // a register. This is for compatibility with the MSVC ABI
532 if (callingConvention == llvm::CallingConv::X86_ThisCall &&
533 RT->isStructureType()) {
534 return false;
535 }
536
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000537 // Structure types are passed in register if all fields would be
538 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000539 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
540 e = RT->getDecl()->field_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000541 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000542
543 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000544 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000545 continue;
546
547 // Check fields recursively.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000548 if (!shouldReturnTypeInRegister(FD->getType(), Context,
549 callingConvention))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000550 return false;
551 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000552 return true;
553}
554
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000555ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
556 unsigned callingConvention) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000557 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000558 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000559
Chris Lattnera3c109b2010-07-29 02:16:43 +0000560 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000561 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000562 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000563 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000564
565 // 128-bit vectors are a special case; they are returned in
566 // registers and we need to make sure to pick a type the LLVM
567 // backend will like.
568 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +0000569 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000570 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000571
572 // Always return in register if it fits in a general purpose
573 // register, or if it is 64 bits and has a single element.
574 if ((Size == 8 || Size == 16 || Size == 32) ||
575 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +0000576 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000577 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000578
579 return ABIArgInfo::getIndirect(0);
580 }
581
582 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000583 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000584
John McCalld608cdb2010-08-22 10:59:02 +0000585 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000586 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000587 // Structures with either a non-trivial destructor or a non-trivial
588 // copy constructor are always indirect.
589 if (hasNonTrivialDestructorOrCopyConstructor(RT))
590 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000591
Anders Carlsson40092972009-10-20 22:07:59 +0000592 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000593 if (RT->getDecl()->hasFlexibleArrayMember())
594 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000595 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000596
David Chisnall1e4249c2009-08-17 23:08:21 +0000597 // If specified, structs and unions are always indirect.
598 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000599 return ABIArgInfo::getIndirect(0);
600
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000601 // Small structures which are register sized are generally returned
602 // in a register.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000603 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(),
604 callingConvention)) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000605 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000606
607 // As a special-case, if the struct is a "single-element" struct, and
608 // the field is of type "float" or "double", return it in a
Eli Friedman55fc7e22012-01-25 22:46:34 +0000609 // floating-point register. (MSVC does not apply this special case.)
610 // We apply a similar transformation for pointer types to improve the
611 // quality of the generated IR.
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000612 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
Eli Friedman55fc7e22012-01-25 22:46:34 +0000613 if ((!IsWin32FloatStructABI && SeltTy->isRealFloatingType())
614 || SeltTy->hasPointerRepresentation())
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000615 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
616
617 // FIXME: We should be able to narrow this integer in cases with dead
618 // padding.
Chris Lattner800588f2010-07-29 06:26:06 +0000619 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000620 }
621
622 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000623 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000624
Chris Lattnera3c109b2010-07-29 02:16:43 +0000625 // Treat an enum type as its underlying type.
626 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
627 RetTy = EnumTy->getDecl()->getIntegerType();
628
629 return (RetTy->isPromotableIntegerType() ?
630 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000631}
632
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000633static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
634 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
635}
636
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000637static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
638 const RecordType *RT = Ty->getAs<RecordType>();
639 if (!RT)
640 return 0;
641 const RecordDecl *RD = RT->getDecl();
642
643 // If this is a C++ record, check the bases first.
644 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
645 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
646 e = CXXRD->bases_end(); i != e; ++i)
647 if (!isRecordWithSSEVectorType(Context, i->getType()))
648 return false;
649
650 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
651 i != e; ++i) {
652 QualType FT = i->getType();
653
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000654 if (isSSEVectorType(Context, FT))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000655 return true;
656
657 if (isRecordWithSSEVectorType(Context, FT))
658 return true;
659 }
660
661 return false;
662}
663
Daniel Dunbare59d8582010-09-16 20:42:06 +0000664unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
665 unsigned Align) const {
666 // Otherwise, if the alignment is less than or equal to the minimum ABI
667 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000668 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +0000669 return 0; // Use default alignment.
670
671 // On non-Darwin, the stack type alignment is always 4.
672 if (!IsDarwinVectorABI) {
673 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000674 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +0000675 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000676
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000677 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000678 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
679 isRecordWithSSEVectorType(getContext(), Ty)))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000680 return 16;
681
682 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000683}
684
Chris Lattnera3c109b2010-07-29 02:16:43 +0000685ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000686 if (!ByVal)
687 return ABIArgInfo::getIndirect(0, false);
688
Daniel Dunbare59d8582010-09-16 20:42:06 +0000689 // Compute the byval alignment.
690 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
691 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
692 if (StackAlign == 0)
Chris Lattnerde92d732011-05-22 23:35:00 +0000693 return ABIArgInfo::getIndirect(4);
Daniel Dunbare59d8582010-09-16 20:42:06 +0000694
695 // If the stack alignment is less than the type alignment, realign the
696 // argument.
697 if (StackAlign < TypeAlign)
698 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
699 /*Realign=*/true);
700
701 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000702}
703
Rafael Espindolab48280b2012-07-31 02:44:24 +0000704X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
705 const Type *T = isSingleElementStruct(Ty, getContext());
706 if (!T)
707 T = Ty.getTypePtr();
708
709 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
710 BuiltinType::Kind K = BT->getKind();
711 if (K == BuiltinType::Float || K == BuiltinType::Double)
712 return Float;
713 }
714 return Integer;
715}
716
717ABIArgInfo
718X86_32ABIInfo::classifyArgumentTypeWithReg(QualType Ty,
719 unsigned &FreeRegs) const {
720 // Common case first.
721 if (FreeRegs == 0)
722 return classifyArgumentType(Ty);
723
724 Class C = classify(Ty);
725 if (C == Float)
726 return classifyArgumentType(Ty);
727
728 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
729 if (SizeInRegs == 0)
730 return classifyArgumentType(Ty);
731
732 if (SizeInRegs > FreeRegs) {
733 FreeRegs = 0;
734 return classifyArgumentType(Ty);
735 }
736 assert(SizeInRegs >= 1 && SizeInRegs <= 3);
737 FreeRegs -= SizeInRegs;
738
739 // If it is a simple scalar, keep the type so that we produce a cleaner IR.
740 ABIArgInfo Foo = classifyArgumentType(Ty);
741 if (Foo.isDirect() && !Foo.getDirectOffset() && !Foo.getPaddingType())
742 return ABIArgInfo::getDirectInReg(Foo.getCoerceToType());
743 if (Foo.isExtend())
744 return ABIArgInfo::getExtendInReg(Foo.getCoerceToType());
745
746 llvm::LLVMContext &LLVMContext = getVMContext();
747 llvm::Type *Int32 = llvm::Type::getInt32Ty(LLVMContext);
748 SmallVector<llvm::Type*, 3> Elements;
749 for (unsigned I = 0; I < SizeInRegs; ++I)
750 Elements.push_back(Int32);
751 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
752 return ABIArgInfo::getDirectInReg(Result);
753}
754
Chris Lattnera3c109b2010-07-29 02:16:43 +0000755ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000756 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000757 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000758 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000759 if (const RecordType *RT = Ty->getAs<RecordType>()) {
760 // Structures with either a non-trivial destructor or a non-trivial
761 // copy constructor are always indirect.
762 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Chris Lattnera3c109b2010-07-29 02:16:43 +0000763 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000764
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000765 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattnera3c109b2010-07-29 02:16:43 +0000766 return getIndirectResult(Ty);
Anders Carlssona8874232010-01-27 03:25:19 +0000767 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000768
Eli Friedman5a4d3522011-11-18 00:28:11 +0000769 // Ignore empty structs/unions.
Eli Friedman5a1ac892011-11-18 04:01:36 +0000770 if (isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000771 return ABIArgInfo::getIgnore();
772
Daniel Dunbar53012f42009-11-09 01:33:53 +0000773 // Expand small (<= 128-bit) record types when we know that the stack layout
774 // of those arguments will match the struct. This is important because the
775 // LLVM backend isn't smart enough to remove byval, which inhibits many
776 // optimizations.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000777 if (getContext().getTypeSize(Ty) <= 4*32 &&
778 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar53012f42009-11-09 01:33:53 +0000779 return ABIArgInfo::getExpand();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000780
Chris Lattnera3c109b2010-07-29 02:16:43 +0000781 return getIndirectResult(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000782 }
783
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000784 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +0000785 // On Darwin, some vectors are passed in memory, we handle this by passing
786 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000787 if (IsDarwinVectorABI) {
788 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000789 if ((Size == 8 || Size == 16 || Size == 32) ||
790 (Size == 64 && VT->getNumElements() == 1))
791 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
792 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000793 }
Bill Wendlingbb465d72010-10-18 03:41:31 +0000794
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000795 llvm::Type *IRType = CGT.ConvertType(Ty);
Bill Wendlingbb465d72010-10-18 03:41:31 +0000796 if (UseX86_MMXType(IRType)) {
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000797 if (IsMMXDisabled)
798 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
799 64));
Bill Wendlingbb465d72010-10-18 03:41:31 +0000800 ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
801 AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
802 return AAI;
803 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000804
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000805 return ABIArgInfo::getDirect();
806 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000807
808
Chris Lattnera3c109b2010-07-29 02:16:43 +0000809 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
810 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000811
Chris Lattnera3c109b2010-07-29 02:16:43 +0000812 return (Ty->isPromotableIntegerType() ?
813 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000814}
815
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000816void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
817 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
818 FI.getCallingConvention());
Rafael Espindolab48280b2012-07-31 02:44:24 +0000819
820 unsigned FreeRegs = FI.getHasRegParm() ? FI.getRegParm() :
821 DefaultNumRegisterParameters;
822
823 // If the return value is indirect, then the hidden argument is consuming one
824 // integer register.
825 if (FI.getReturnInfo().isIndirect() && FreeRegs) {
826 --FreeRegs;
827 ABIArgInfo &Old = FI.getReturnInfo();
828 Old = ABIArgInfo::getIndirectInReg(Old.getIndirectAlign(),
829 Old.getIndirectByVal(),
830 Old.getIndirectRealign());
831 }
832
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000833 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
834 it != ie; ++it)
Rafael Espindolab48280b2012-07-31 02:44:24 +0000835 it->info = classifyArgumentTypeWithReg(it->type, FreeRegs);
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000836}
837
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000838llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
839 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +0000840 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000841
842 CGBuilderTy &Builder = CGF.Builder;
843 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
844 "ap");
845 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Eli Friedman7b1fb812011-11-18 02:12:09 +0000846
847 // Compute if the address needs to be aligned
848 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
849 Align = getTypeStackAlignInBytes(Ty, Align);
850 Align = std::max(Align, 4U);
851 if (Align > 4) {
852 // addr = (addr + align - 1) & -align;
853 llvm::Value *Offset =
854 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
855 Addr = CGF.Builder.CreateGEP(Addr, Offset);
856 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
857 CGF.Int32Ty);
858 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
859 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
860 Addr->getType(),
861 "ap.cur.aligned");
862 }
863
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000864 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000865 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000866 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
867
868 uint64_t Offset =
Eli Friedman7b1fb812011-11-18 02:12:09 +0000869 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000870 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +0000871 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000872 "ap.next");
873 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
874
875 return AddrTyped;
876}
877
Charles Davis74f72932010-02-13 15:54:06 +0000878void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
879 llvm::GlobalValue *GV,
880 CodeGen::CodeGenModule &CGM) const {
881 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
882 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
883 // Get the LLVM function.
884 llvm::Function *Fn = cast<llvm::Function>(GV);
885
886 // Now add the 'alignstack' attribute with a value of 16.
887 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
888 }
889 }
890}
891
John McCall6374c332010-03-06 00:35:14 +0000892bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
893 CodeGen::CodeGenFunction &CGF,
894 llvm::Value *Address) const {
895 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCall6374c332010-03-06 00:35:14 +0000896
Chris Lattner8b418682012-02-07 00:39:47 +0000897 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000898
John McCall6374c332010-03-06 00:35:14 +0000899 // 0-7 are the eight integer registers; the order is different
900 // on Darwin (for EH), but the range is the same.
901 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +0000902 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +0000903
904 if (CGF.CGM.isTargetDarwin()) {
905 // 12-16 are st(0..4). Not sure why we stop at 4.
906 // These have size 16, which is sizeof(long double) on
907 // platforms with 8-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +0000908 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCallaeeb7012010-05-27 06:19:26 +0000909 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000910
John McCall6374c332010-03-06 00:35:14 +0000911 } else {
912 // 9 is %eflags, which doesn't get a size on Darwin for some
913 // reason.
914 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
915
916 // 11-16 are st(0..5). Not sure why we stop at 5.
917 // These have size 12, which is sizeof(long double) on
918 // platforms with 4-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +0000919 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCallaeeb7012010-05-27 06:19:26 +0000920 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
921 }
John McCall6374c332010-03-06 00:35:14 +0000922
923 return false;
924}
925
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000926//===----------------------------------------------------------------------===//
927// X86-64 ABI Implementation
928//===----------------------------------------------------------------------===//
929
930
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000931namespace {
932/// X86_64ABIInfo - The X86_64 ABI information.
933class X86_64ABIInfo : public ABIInfo {
934 enum Class {
935 Integer = 0,
936 SSE,
937 SSEUp,
938 X87,
939 X87Up,
940 ComplexX87,
941 NoClass,
942 Memory
943 };
944
945 /// merge - Implement the X86_64 ABI merging algorithm.
946 ///
947 /// Merge an accumulating classification \arg Accum with a field
948 /// classification \arg Field.
949 ///
950 /// \param Accum - The accumulating classification. This should
951 /// always be either NoClass or the result of a previous merge
952 /// call. In addition, this should never be Memory (the caller
953 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +0000954 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000955
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +0000956 /// postMerge - Implement the X86_64 ABI post merging algorithm.
957 ///
958 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
959 /// final MEMORY or SSE classes when necessary.
960 ///
961 /// \param AggregateSize - The size of the current aggregate in
962 /// the classification process.
963 ///
964 /// \param Lo - The classification for the parts of the type
965 /// residing in the low word of the containing object.
966 ///
967 /// \param Hi - The classification for the parts of the type
968 /// residing in the higher words of the containing object.
969 ///
970 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
971
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000972 /// classify - Determine the x86_64 register classes in which the
973 /// given type T should be passed.
974 ///
975 /// \param Lo - The classification for the parts of the type
976 /// residing in the low word of the containing object.
977 ///
978 /// \param Hi - The classification for the parts of the type
979 /// residing in the high word of the containing object.
980 ///
981 /// \param OffsetBase - The bit offset of this type in the
982 /// containing object. Some parameters are classified different
983 /// depending on whether they straddle an eightbyte boundary.
984 ///
985 /// If a word is unused its result will be NoClass; if a type should
986 /// be passed in Memory then at least the classification of \arg Lo
987 /// will be Memory.
988 ///
989 /// The \arg Lo class will be NoClass iff the argument is ignored.
990 ///
991 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
992 /// also be ComplexX87.
Chris Lattner9c254f02010-06-29 06:01:59 +0000993 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000994
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +0000995 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000996 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
997 unsigned IROffset, QualType SourceTy,
998 unsigned SourceOffset) const;
999 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
1000 unsigned IROffset, QualType SourceTy,
1001 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001002
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001003 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001004 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +00001005 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001006
1007 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001008 /// such that the argument will be passed in memory.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001009 ///
1010 /// \param freeIntRegs - The number of free integer registers remaining
1011 /// available.
1012 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001013
Chris Lattnera3c109b2010-07-29 02:16:43 +00001014 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001015
Bill Wendlingbb465d72010-10-18 03:41:31 +00001016 ABIArgInfo classifyArgumentType(QualType Ty,
Daniel Dunbaredfac032012-03-10 01:03:58 +00001017 unsigned freeIntRegs,
Bill Wendlingbb465d72010-10-18 03:41:31 +00001018 unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +00001019 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001020
Eli Friedmanee1ad992011-12-02 00:11:43 +00001021 bool IsIllegalVectorType(QualType Ty) const;
1022
John McCall67a57732011-04-21 01:20:55 +00001023 /// The 0.98 ABI revision clarified a lot of ambiguities,
1024 /// unfortunately in ways that were not always consistent with
1025 /// certain previous compilers. In particular, platforms which
1026 /// required strict binary compatibility with older versions of GCC
1027 /// may need to exempt themselves.
1028 bool honorsRevision0_98() const {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001029 return !getContext().getTargetInfo().getTriple().isOSDarwin();
John McCall67a57732011-04-21 01:20:55 +00001030 }
1031
Eli Friedmanee1ad992011-12-02 00:11:43 +00001032 bool HasAVX;
1033
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001034public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001035 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
1036 ABIInfo(CGT), HasAVX(hasavx) {}
Chris Lattner9c254f02010-06-29 06:01:59 +00001037
John McCallde5d3c72012-02-17 03:33:10 +00001038 bool isPassedUsingAVXType(QualType type) const {
1039 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00001040 // The freeIntRegs argument doesn't matter here.
1041 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE);
John McCallde5d3c72012-02-17 03:33:10 +00001042 if (info.isDirect()) {
1043 llvm::Type *ty = info.getCoerceToType();
1044 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
1045 return (vectorTy->getBitWidth() > 128);
1046 }
1047 return false;
1048 }
1049
Chris Lattneree5dcd02010-07-29 02:31:05 +00001050 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001051
1052 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1053 CodeGenFunction &CGF) const;
1054};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001055
Chris Lattnerf13721d2010-08-31 16:44:54 +00001056/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001057class WinX86_64ABIInfo : public ABIInfo {
1058
1059 ABIArgInfo classify(QualType Ty) const;
1060
Chris Lattnerf13721d2010-08-31 16:44:54 +00001061public:
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001062 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
1063
1064 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattnerf13721d2010-08-31 16:44:54 +00001065
1066 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1067 CodeGenFunction &CGF) const;
1068};
1069
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001070class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1071public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001072 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
1073 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
John McCall6374c332010-03-06 00:35:14 +00001074
John McCallde5d3c72012-02-17 03:33:10 +00001075 const X86_64ABIInfo &getABIInfo() const {
1076 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
1077 }
1078
John McCall6374c332010-03-06 00:35:14 +00001079 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1080 return 7;
1081 }
1082
1083 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1084 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001085 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001086
John McCallaeeb7012010-05-27 06:19:26 +00001087 // 0-15 are the 16 integer registers.
1088 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001089 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +00001090 return false;
1091 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001092
Jay Foadef6de3d2011-07-11 09:56:20 +00001093 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001094 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +00001095 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001096 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1097 }
1098
John McCallde5d3c72012-02-17 03:33:10 +00001099 bool isNoProtoCallVariadic(const CallArgList &args,
1100 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +00001101 // The default CC on x86-64 sets %al to the number of SSA
1102 // registers used, and GCC sets this when calling an unprototyped
Eli Friedman3ed79032011-12-01 04:53:19 +00001103 // function, so we override the default behavior. However, don't do
Eli Friedman68805fe2011-12-06 03:08:26 +00001104 // that when AVX types are involved: the ABI explicitly states it is
1105 // undefined, and it doesn't work in practice because of how the ABI
1106 // defines varargs anyway.
John McCallde5d3c72012-02-17 03:33:10 +00001107 if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) {
Eli Friedman3ed79032011-12-01 04:53:19 +00001108 bool HasAVXType = false;
John McCallde5d3c72012-02-17 03:33:10 +00001109 for (CallArgList::const_iterator
1110 it = args.begin(), ie = args.end(); it != ie; ++it) {
1111 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1112 HasAVXType = true;
1113 break;
Eli Friedman3ed79032011-12-01 04:53:19 +00001114 }
1115 }
John McCallde5d3c72012-02-17 03:33:10 +00001116
Eli Friedman3ed79032011-12-01 04:53:19 +00001117 if (!HasAVXType)
1118 return true;
1119 }
John McCall01f151e2011-09-21 08:08:30 +00001120
John McCallde5d3c72012-02-17 03:33:10 +00001121 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
John McCall01f151e2011-09-21 08:08:30 +00001122 }
1123
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001124};
1125
Chris Lattnerf13721d2010-08-31 16:44:54 +00001126class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1127public:
1128 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1129 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1130
1131 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1132 return 7;
1133 }
1134
1135 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1136 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001137 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001138
Chris Lattnerf13721d2010-08-31 16:44:54 +00001139 // 0-15 are the 16 integer registers.
1140 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001141 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattnerf13721d2010-08-31 16:44:54 +00001142 return false;
1143 }
1144};
1145
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001146}
1147
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001148void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1149 Class &Hi) const {
1150 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1151 //
1152 // (a) If one of the classes is Memory, the whole argument is passed in
1153 // memory.
1154 //
1155 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1156 // memory.
1157 //
1158 // (c) If the size of the aggregate exceeds two eightbytes and the first
1159 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1160 // argument is passed in memory. NOTE: This is necessary to keep the
1161 // ABI working for processors that don't support the __m256 type.
1162 //
1163 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1164 //
1165 // Some of these are enforced by the merging logic. Others can arise
1166 // only with unions; for example:
1167 // union { _Complex double; unsigned; }
1168 //
1169 // Note that clauses (b) and (c) were added in 0.98.
1170 //
1171 if (Hi == Memory)
1172 Lo = Memory;
1173 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1174 Lo = Memory;
1175 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1176 Lo = Memory;
1177 if (Hi == SSEUp && Lo != SSE)
1178 Hi = SSE;
1179}
1180
Chris Lattner1090a9b2010-06-28 21:43:59 +00001181X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001182 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1183 // classified recursively so that always two fields are
1184 // considered. The resulting class is calculated according to
1185 // the classes of the fields in the eightbyte:
1186 //
1187 // (a) If both classes are equal, this is the resulting class.
1188 //
1189 // (b) If one of the classes is NO_CLASS, the resulting class is
1190 // the other class.
1191 //
1192 // (c) If one of the classes is MEMORY, the result is the MEMORY
1193 // class.
1194 //
1195 // (d) If one of the classes is INTEGER, the result is the
1196 // INTEGER.
1197 //
1198 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1199 // MEMORY is used as class.
1200 //
1201 // (f) Otherwise class SSE is used.
1202
1203 // Accum should never be memory (we should have returned) or
1204 // ComplexX87 (because this cannot be passed in a structure).
1205 assert((Accum != Memory && Accum != ComplexX87) &&
1206 "Invalid accumulated classification during merge.");
1207 if (Accum == Field || Field == NoClass)
1208 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001209 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001210 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001211 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001212 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001213 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001214 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001215 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1216 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001217 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001218 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001219}
1220
Chris Lattnerbcaedae2010-06-30 19:14:05 +00001221void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001222 Class &Lo, Class &Hi) const {
1223 // FIXME: This code can be simplified by introducing a simple value class for
1224 // Class pairs with appropriate constructor methods for the various
1225 // situations.
1226
1227 // FIXME: Some of the split computations are wrong; unaligned vectors
1228 // shouldn't be passed in registers for example, so there is no chance they
1229 // can straddle an eightbyte. Verify & simplify.
1230
1231 Lo = Hi = NoClass;
1232
1233 Class &Current = OffsetBase < 64 ? Lo : Hi;
1234 Current = Memory;
1235
John McCall183700f2009-09-21 23:43:11 +00001236 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001237 BuiltinType::Kind k = BT->getKind();
1238
1239 if (k == BuiltinType::Void) {
1240 Current = NoClass;
1241 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1242 Lo = Integer;
1243 Hi = Integer;
1244 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1245 Current = Integer;
1246 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
1247 Current = SSE;
1248 } else if (k == BuiltinType::LongDouble) {
1249 Lo = X87;
1250 Hi = X87Up;
1251 }
1252 // FIXME: _Decimal32 and _Decimal64 are SSE.
1253 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001254 return;
1255 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001256
Chris Lattner1090a9b2010-06-28 21:43:59 +00001257 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001258 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +00001259 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001260 return;
1261 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001262
Chris Lattner1090a9b2010-06-28 21:43:59 +00001263 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001264 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001265 return;
1266 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001267
Chris Lattner1090a9b2010-06-28 21:43:59 +00001268 if (Ty->isMemberPointerType()) {
Daniel Dunbar67d438d2010-05-15 00:00:37 +00001269 if (Ty->isMemberFunctionPointerType())
1270 Lo = Hi = Integer;
1271 else
1272 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001273 return;
1274 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001275
Chris Lattner1090a9b2010-06-28 21:43:59 +00001276 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001277 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001278 if (Size == 32) {
1279 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1280 // float> as integer.
1281 Current = Integer;
1282
1283 // If this type crosses an eightbyte boundary, it should be
1284 // split.
1285 uint64_t EB_Real = (OffsetBase) / 64;
1286 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1287 if (EB_Real != EB_Imag)
1288 Hi = Lo;
1289 } else if (Size == 64) {
1290 // gcc passes <1 x double> in memory. :(
1291 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1292 return;
1293
1294 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001295 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001296 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1297 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1298 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001299 Current = Integer;
1300 else
1301 Current = SSE;
1302
1303 // If this type crosses an eightbyte boundary, it should be
1304 // split.
1305 if (OffsetBase && OffsetBase != 64)
1306 Hi = Lo;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001307 } else if (Size == 128 || (HasAVX && Size == 256)) {
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001308 // Arguments of 256-bits are split into four eightbyte chunks. The
1309 // least significant one belongs to class SSE and all the others to class
1310 // SSEUP. The original Lo and Hi design considers that types can't be
1311 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1312 // This design isn't correct for 256-bits, but since there're no cases
1313 // where the upper parts would need to be inspected, avoid adding
1314 // complexity and just consider Hi to match the 64-256 part.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001315 Lo = SSE;
1316 Hi = SSEUp;
1317 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001318 return;
1319 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001320
Chris Lattner1090a9b2010-06-28 21:43:59 +00001321 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001322 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001323
Chris Lattnerea044322010-07-29 02:01:43 +00001324 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001325 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001326 if (Size <= 64)
1327 Current = Integer;
1328 else if (Size <= 128)
1329 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001330 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001331 Current = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001332 else if (ET == getContext().DoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001333 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001334 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001335 Current = ComplexX87;
1336
1337 // If this complex type crosses an eightbyte boundary then it
1338 // should be split.
1339 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001340 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001341 if (Hi == NoClass && EB_Real != EB_Imag)
1342 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001343
Chris Lattner1090a9b2010-06-28 21:43:59 +00001344 return;
1345 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001346
Chris Lattnerea044322010-07-29 02:01:43 +00001347 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001348 // Arrays are treated like structures.
1349
Chris Lattnerea044322010-07-29 02:01:43 +00001350 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001351
1352 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001353 // than four eightbytes, ..., it has class MEMORY.
1354 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001355 return;
1356
1357 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1358 // fields, it has class MEMORY.
1359 //
1360 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001361 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001362 return;
1363
1364 // Otherwise implement simplified merge. We could be smarter about
1365 // this, but it isn't worth it and would be harder to verify.
1366 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001367 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001368 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes089d8922011-07-12 01:27:38 +00001369
1370 // The only case a 256-bit wide vector could be used is when the array
1371 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1372 // to work for sizes wider than 128, early check and fallback to memory.
1373 if (Size > 128 && EltSize != 256)
1374 return;
1375
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001376 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1377 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001378 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001379 Lo = merge(Lo, FieldLo);
1380 Hi = merge(Hi, FieldHi);
1381 if (Lo == Memory || Hi == Memory)
1382 break;
1383 }
1384
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001385 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001386 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001387 return;
1388 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001389
Chris Lattner1090a9b2010-06-28 21:43:59 +00001390 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001391 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001392
1393 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001394 // than four eightbytes, ..., it has class MEMORY.
1395 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001396 return;
1397
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001398 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1399 // copy constructor or a non-trivial destructor, it is passed by invisible
1400 // reference.
1401 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1402 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001403
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001404 const RecordDecl *RD = RT->getDecl();
1405
1406 // Assume variable sized types are passed in memory.
1407 if (RD->hasFlexibleArrayMember())
1408 return;
1409
Chris Lattnerea044322010-07-29 02:01:43 +00001410 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001411
1412 // Reset Lo class, this will be recomputed.
1413 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001414
1415 // If this is a C++ record, classify the bases first.
1416 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1417 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1418 e = CXXRD->bases_end(); i != e; ++i) {
1419 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1420 "Unexpected base class!");
1421 const CXXRecordDecl *Base =
1422 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1423
1424 // Classify this field.
1425 //
1426 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1427 // single eightbyte, each is classified separately. Each eightbyte gets
1428 // initialized to class NO_CLASS.
1429 Class FieldLo, FieldHi;
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001430 uint64_t Offset =
1431 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
Chris Lattner9c254f02010-06-29 06:01:59 +00001432 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001433 Lo = merge(Lo, FieldLo);
1434 Hi = merge(Hi, FieldHi);
1435 if (Lo == Memory || Hi == Memory)
1436 break;
1437 }
1438 }
1439
1440 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001441 unsigned idx = 0;
Bruno Cardoso Lopes548e4782011-07-12 22:30:58 +00001442 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001443 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001444 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1445 bool BitField = i->isBitField();
1446
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001447 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1448 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001449 //
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001450 // The only case a 256-bit wide vector could be used is when the struct
1451 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1452 // to work for sizes wider than 128, early check and fallback to memory.
1453 //
1454 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1455 Lo = Memory;
1456 return;
1457 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001458 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001459 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001460 Lo = Memory;
1461 return;
1462 }
1463
1464 // Classify this field.
1465 //
1466 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1467 // exceeds a single eightbyte, each is classified
1468 // separately. Each eightbyte gets initialized to class
1469 // NO_CLASS.
1470 Class FieldLo, FieldHi;
1471
1472 // Bit-fields require special handling, they do not force the
1473 // structure to be passed in memory even if unaligned, and
1474 // therefore they can straddle an eightbyte.
1475 if (BitField) {
1476 // Ignore padding bit-fields.
1477 if (i->isUnnamedBitfield())
1478 continue;
1479
1480 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001481 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001482
1483 uint64_t EB_Lo = Offset / 64;
1484 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1485 FieldLo = FieldHi = NoClass;
1486 if (EB_Lo) {
1487 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1488 FieldLo = NoClass;
1489 FieldHi = Integer;
1490 } else {
1491 FieldLo = Integer;
1492 FieldHi = EB_Hi ? Integer : NoClass;
1493 }
1494 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001495 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001496 Lo = merge(Lo, FieldLo);
1497 Hi = merge(Hi, FieldHi);
1498 if (Lo == Memory || Hi == Memory)
1499 break;
1500 }
1501
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001502 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001503 }
1504}
1505
Chris Lattner9c254f02010-06-29 06:01:59 +00001506ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001507 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1508 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001509 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001510 // Treat an enum type as its underlying type.
1511 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1512 Ty = EnumTy->getDecl()->getIntegerType();
1513
1514 return (Ty->isPromotableIntegerType() ?
1515 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1516 }
1517
1518 return ABIArgInfo::getIndirect(0);
1519}
1520
Eli Friedmanee1ad992011-12-02 00:11:43 +00001521bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1522 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1523 uint64_t Size = getContext().getTypeSize(VecTy);
1524 unsigned LargestVector = HasAVX ? 256 : 128;
1525 if (Size <= 64 || Size > LargestVector)
1526 return true;
1527 }
1528
1529 return false;
1530}
1531
Daniel Dunbaredfac032012-03-10 01:03:58 +00001532ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1533 unsigned freeIntRegs) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001534 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1535 // place naturally.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001536 //
1537 // This assumption is optimistic, as there could be free registers available
1538 // when we need to pass this argument in memory, and LLVM could try to pass
1539 // the argument in the free register. This does not seem to happen currently,
1540 // but this code would be much safer if we could mark the argument with
1541 // 'onstack'. See PR12193.
Eli Friedmanee1ad992011-12-02 00:11:43 +00001542 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001543 // Treat an enum type as its underlying type.
1544 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1545 Ty = EnumTy->getDecl()->getIntegerType();
1546
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001547 return (Ty->isPromotableIntegerType() ?
1548 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001549 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001550
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001551 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1552 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001553
Chris Lattner855d2272011-05-22 23:21:23 +00001554 // Compute the byval alignment. We specify the alignment of the byval in all
1555 // cases so that the mid-level optimizer knows the alignment of the byval.
1556 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
Daniel Dunbaredfac032012-03-10 01:03:58 +00001557
1558 // Attempt to avoid passing indirect results using byval when possible. This
1559 // is important for good codegen.
1560 //
1561 // We do this by coercing the value into a scalar type which the backend can
1562 // handle naturally (i.e., without using byval).
1563 //
1564 // For simplicity, we currently only do this when we have exhausted all of the
1565 // free integer registers. Doing this when there are free integer registers
1566 // would require more care, as we would have to ensure that the coerced value
1567 // did not claim the unused register. That would require either reording the
1568 // arguments to the function (so that any subsequent inreg values came first),
1569 // or only doing this optimization when there were no following arguments that
1570 // might be inreg.
1571 //
1572 // We currently expect it to be rare (particularly in well written code) for
1573 // arguments to be passed on the stack when there are still free integer
1574 // registers available (this would typically imply large structs being passed
1575 // by value), so this seems like a fair tradeoff for now.
1576 //
1577 // We can revisit this if the backend grows support for 'onstack' parameter
1578 // attributes. See PR12193.
1579 if (freeIntRegs == 0) {
1580 uint64_t Size = getContext().getTypeSize(Ty);
1581
1582 // If this type fits in an eightbyte, coerce it into the matching integral
1583 // type, which will end up on the stack (with alignment 8).
1584 if (Align == 8 && Size <= 64)
1585 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1586 Size));
1587 }
1588
Chris Lattner855d2272011-05-22 23:21:23 +00001589 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001590}
1591
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001592/// GetByteVectorType - The ABI specifies that a value should be passed in an
1593/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner0f408f52010-07-29 04:56:46 +00001594/// vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001595llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001596 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001597
Chris Lattner15842bd2010-07-29 05:02:29 +00001598 // Wrapper structs that just contain vectors are passed just like vectors,
1599 // strip them off if present.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001600 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner15842bd2010-07-29 05:02:29 +00001601 while (STy && STy->getNumElements() == 1) {
1602 IRType = STy->getElementType(0);
1603 STy = dyn_cast<llvm::StructType>(IRType);
1604 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001605
Bruno Cardoso Lopes528a8c72011-07-08 22:57:35 +00001606 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001607 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1608 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001609 unsigned BitWidth = VT->getBitWidth();
Tanya Lattnerce275672011-11-28 23:18:11 +00001610 if ((BitWidth >= 128 && BitWidth <= 256) &&
Chris Lattner0f408f52010-07-29 04:56:46 +00001611 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1612 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1613 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1614 EltTy->isIntegerTy(128)))
1615 return VT;
1616 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001617
Chris Lattner0f408f52010-07-29 04:56:46 +00001618 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1619}
1620
Chris Lattnere2962be2010-07-29 07:30:00 +00001621/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1622/// is known to either be off the end of the specified type or being in
1623/// alignment padding. The user type specified is known to be at most 128 bits
1624/// in size, and have passed through X86_64ABIInfo::classify with a successful
1625/// classification that put one of the two halves in the INTEGER class.
1626///
1627/// It is conservatively correct to return false.
1628static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1629 unsigned EndBit, ASTContext &Context) {
1630 // If the bytes being queried are off the end of the type, there is no user
1631 // data hiding here. This handles analysis of builtins, vectors and other
1632 // types that don't contain interesting padding.
1633 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1634 if (TySize <= StartBit)
1635 return true;
1636
Chris Lattner021c3a32010-07-29 07:43:55 +00001637 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1638 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1639 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1640
1641 // Check each element to see if the element overlaps with the queried range.
1642 for (unsigned i = 0; i != NumElts; ++i) {
1643 // If the element is after the span we care about, then we're done..
1644 unsigned EltOffset = i*EltSize;
1645 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001646
Chris Lattner021c3a32010-07-29 07:43:55 +00001647 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1648 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1649 EndBit-EltOffset, Context))
1650 return false;
1651 }
1652 // If it overlaps no elements, then it is safe to process as padding.
1653 return true;
1654 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001655
Chris Lattnere2962be2010-07-29 07:30:00 +00001656 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1657 const RecordDecl *RD = RT->getDecl();
1658 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001659
Chris Lattnere2962be2010-07-29 07:30:00 +00001660 // If this is a C++ record, check the bases first.
1661 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1662 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1663 e = CXXRD->bases_end(); i != e; ++i) {
1664 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1665 "Unexpected base class!");
1666 const CXXRecordDecl *Base =
1667 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001668
Chris Lattnere2962be2010-07-29 07:30:00 +00001669 // If the base is after the span we care about, ignore it.
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001670 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
Chris Lattnere2962be2010-07-29 07:30:00 +00001671 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001672
Chris Lattnere2962be2010-07-29 07:30:00 +00001673 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1674 if (!BitsContainNoUserData(i->getType(), BaseStart,
1675 EndBit-BaseOffset, Context))
1676 return false;
1677 }
1678 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001679
Chris Lattnere2962be2010-07-29 07:30:00 +00001680 // Verify that no field has data that overlaps the region of interest. Yes
1681 // this could be sped up a lot by being smarter about queried fields,
1682 // however we're only looking at structs up to 16 bytes, so we don't care
1683 // much.
1684 unsigned idx = 0;
1685 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1686 i != e; ++i, ++idx) {
1687 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001688
Chris Lattnere2962be2010-07-29 07:30:00 +00001689 // If we found a field after the region we care about, then we're done.
1690 if (FieldOffset >= EndBit) break;
1691
1692 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1693 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1694 Context))
1695 return false;
1696 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001697
Chris Lattnere2962be2010-07-29 07:30:00 +00001698 // If nothing in this record overlapped the area of interest, then we're
1699 // clean.
1700 return true;
1701 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001702
Chris Lattnere2962be2010-07-29 07:30:00 +00001703 return false;
1704}
1705
Chris Lattner0b362002010-07-29 18:39:32 +00001706/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1707/// float member at the specified offset. For example, {int,{float}} has a
1708/// float at offset 4. It is conservatively correct for this routine to return
1709/// false.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001710static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0b362002010-07-29 18:39:32 +00001711 const llvm::TargetData &TD) {
1712 // Base case if we find a float.
1713 if (IROffset == 0 && IRType->isFloatTy())
1714 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001715
Chris Lattner0b362002010-07-29 18:39:32 +00001716 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001717 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner0b362002010-07-29 18:39:32 +00001718 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1719 unsigned Elt = SL->getElementContainingOffset(IROffset);
1720 IROffset -= SL->getElementOffset(Elt);
1721 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1722 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001723
Chris Lattner0b362002010-07-29 18:39:32 +00001724 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001725 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1726 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner0b362002010-07-29 18:39:32 +00001727 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1728 IROffset -= IROffset/EltSize*EltSize;
1729 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1730 }
1731
1732 return false;
1733}
1734
Chris Lattnerf47c9442010-07-29 18:13:09 +00001735
1736/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1737/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001738llvm::Type *X86_64ABIInfo::
1739GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00001740 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001741 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001742 // pass as float if the last 4 bytes is just padding. This happens for
1743 // structs that contain 3 floats.
1744 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1745 SourceOffset*8+64, getContext()))
1746 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001747
Chris Lattner0b362002010-07-29 18:39:32 +00001748 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1749 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1750 // case.
1751 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001752 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1753 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001754
Chris Lattnerf47c9442010-07-29 18:13:09 +00001755 return llvm::Type::getDoubleTy(getVMContext());
1756}
1757
1758
Chris Lattner0d2656d2010-07-29 17:40:35 +00001759/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1760/// an 8-byte GPR. This means that we either have a scalar or we are talking
1761/// about the high or low part of an up-to-16-byte struct. This routine picks
1762/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001763/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1764/// etc).
1765///
1766/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1767/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1768/// the 8-byte value references. PrefType may be null.
1769///
1770/// SourceTy is the source level type for the entire argument. SourceOffset is
1771/// an offset into this that we're processing (which is always either 0 or 8).
1772///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001773llvm::Type *X86_64ABIInfo::
1774GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00001775 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001776 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1777 // returning an 8-byte unit starting with it. See if we can safely use it.
1778 if (IROffset == 0) {
1779 // Pointers and int64's always fill the 8-byte unit.
1780 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1781 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001782
Chris Lattnere2962be2010-07-29 07:30:00 +00001783 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1784 // goodness in the source type is just tail padding. This is allowed to
1785 // kick in for struct {double,int} on the int, but not on
1786 // struct{double,int,int} because we wouldn't return the second int. We
1787 // have to do this analysis on the source type because we can't depend on
1788 // unions being lowered a specific way etc.
1789 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1790 IRType->isIntegerTy(32)) {
1791 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001792
Chris Lattnere2962be2010-07-29 07:30:00 +00001793 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1794 SourceOffset*8+64, getContext()))
1795 return IRType;
1796 }
1797 }
Chris Lattner49382de2010-07-28 22:44:07 +00001798
Chris Lattner2acc6e32011-07-18 04:24:23 +00001799 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001800 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner44f0fd22010-07-29 02:20:19 +00001801 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001802 if (IROffset < SL->getSizeInBytes()) {
1803 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1804 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001805
Chris Lattner0d2656d2010-07-29 17:40:35 +00001806 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1807 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001808 }
Chris Lattner49382de2010-07-28 22:44:07 +00001809 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001810
Chris Lattner2acc6e32011-07-18 04:24:23 +00001811 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001812 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner021c3a32010-07-29 07:43:55 +00001813 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1814 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00001815 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1816 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00001817 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001818
Chris Lattner49382de2010-07-28 22:44:07 +00001819 // Okay, we don't have any better idea of what to pass, so we pass this in an
1820 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001821 unsigned TySizeInBytes =
1822 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00001823
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001824 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001825
Chris Lattner49382de2010-07-28 22:44:07 +00001826 // It is always safe to classify this as an integer type up to i64 that
1827 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001828 return llvm::IntegerType::get(getVMContext(),
1829 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00001830}
1831
Chris Lattner66e7b682010-09-01 00:50:20 +00001832
1833/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1834/// be used as elements of a two register pair to pass or return, return a
1835/// first class aggregate to represent them. For example, if the low part of
1836/// a by-value argument should be passed as i32* and the high part as float,
1837/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001838static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00001839GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Chris Lattner66e7b682010-09-01 00:50:20 +00001840 const llvm::TargetData &TD) {
1841 // In order to correctly satisfy the ABI, we need to the high part to start
1842 // at offset 8. If the high and low parts we inferred are both 4-byte types
1843 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1844 // the second element at offset 8. Check for this:
1845 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1846 unsigned HiAlign = TD.getABITypeAlignment(Hi);
1847 unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
1848 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001849
Chris Lattner66e7b682010-09-01 00:50:20 +00001850 // To handle this, we have to increase the size of the low part so that the
1851 // second element will start at an 8 byte offset. We can't increase the size
1852 // of the second element because it might make us access off the end of the
1853 // struct.
1854 if (HiStart != 8) {
1855 // There are only two sorts of types the ABI generation code can produce for
1856 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1857 // Promote these to a larger type.
1858 if (Lo->isFloatTy())
1859 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1860 else {
1861 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1862 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1863 }
1864 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001865
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001866 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001867
1868
Chris Lattner66e7b682010-09-01 00:50:20 +00001869 // Verify that the second element is at an 8-byte offset.
1870 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1871 "Invalid x86-64 argument pair!");
1872 return Result;
1873}
1874
Chris Lattner519f68c2010-07-28 23:06:14 +00001875ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00001876classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00001877 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1878 // classification algorithm.
1879 X86_64ABIInfo::Class Lo, Hi;
1880 classify(RetTy, 0, Lo, Hi);
1881
1882 // Check some invariants.
1883 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001884 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1885
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001886 llvm::Type *ResType = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001887 switch (Lo) {
1888 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001889 if (Hi == NoClass)
1890 return ABIArgInfo::getIgnore();
1891 // If the low part is just padding, it takes no register, leave ResType
1892 // null.
1893 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1894 "Unknown missing lo part");
1895 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001896
1897 case SSEUp:
1898 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00001899 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001900
1901 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1902 // hidden argument.
1903 case Memory:
1904 return getIndirectReturnResult(RetTy);
1905
1906 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1907 // available register of the sequence %rax, %rdx is used.
1908 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001909 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001910
Chris Lattnereb518b42010-07-29 21:42:50 +00001911 // If we have a sign or zero extended integer, make sure to return Extend
1912 // so that the parameter gets the right LLVM IR attributes.
1913 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1914 // Treat an enum type as its underlying type.
1915 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1916 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001917
Chris Lattnereb518b42010-07-29 21:42:50 +00001918 if (RetTy->isIntegralOrEnumerationType() &&
1919 RetTy->isPromotableIntegerType())
1920 return ABIArgInfo::getExtend();
1921 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001922 break;
1923
1924 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1925 // available SSE register of the sequence %xmm0, %xmm1 is used.
1926 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001927 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00001928 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001929
1930 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1931 // returned on the X87 stack in %st0 as 80-bit x87 number.
1932 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00001933 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00001934 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001935
1936 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1937 // part of the value is returned in %st0 and the imaginary part in
1938 // %st1.
1939 case ComplexX87:
1940 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00001941 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00001942 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00001943 NULL);
1944 break;
1945 }
1946
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001947 llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001948 switch (Hi) {
1949 // Memory was handled previously and X87 should
1950 // never occur as a hi class.
1951 case Memory:
1952 case X87:
David Blaikieb219cfc2011-09-23 05:06:16 +00001953 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001954
1955 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00001956 case NoClass:
1957 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001958
Chris Lattner3db4dde2010-09-01 00:20:33 +00001959 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001960 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001961 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1962 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001963 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00001964 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001965 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001966 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1967 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001968 break;
1969
1970 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001971 // is passed in the next available eightbyte chunk if the last used
1972 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00001973 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001974 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00001975 case SSEUp:
1976 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001977 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00001978 break;
1979
1980 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1981 // returned together with the previous X87 value in %st0.
1982 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001983 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00001984 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001985 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00001986 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00001987 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001988 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001989 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1990 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00001991 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001992 break;
1993 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001994
Chris Lattner3db4dde2010-09-01 00:20:33 +00001995 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00001996 // known to pass in the high eightbyte of the result. We do this by forming a
1997 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00001998 if (HighPart)
1999 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner519f68c2010-07-28 23:06:14 +00002000
Chris Lattnereb518b42010-07-29 21:42:50 +00002001 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00002002}
2003
Daniel Dunbaredfac032012-03-10 01:03:58 +00002004ABIArgInfo X86_64ABIInfo::classifyArgumentType(
2005 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE)
2006 const
2007{
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002008 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00002009 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002010
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002011 // Check some invariants.
2012 // FIXME: Enforce these by construction.
2013 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002014 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2015
2016 neededInt = 0;
2017 neededSSE = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002018 llvm::Type *ResType = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002019 switch (Lo) {
2020 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002021 if (Hi == NoClass)
2022 return ABIArgInfo::getIgnore();
2023 // If the low part is just padding, it takes no register, leave ResType
2024 // null.
2025 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2026 "Unknown missing lo part");
2027 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002028
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002029 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
2030 // on the stack.
2031 case Memory:
2032
2033 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
2034 // COMPLEX_X87, it is passed in memory.
2035 case X87:
2036 case ComplexX87:
Eli Friedmanded137f2011-06-29 07:04:55 +00002037 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2038 ++neededInt;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002039 return getIndirectResult(Ty, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002040
2041 case SSEUp:
2042 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002043 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002044
2045 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
2046 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
2047 // and %r9 is used.
2048 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00002049 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002050
Chris Lattner49382de2010-07-28 22:44:07 +00002051 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002052 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00002053
2054 // If we have a sign or zero extended integer, make sure to return Extend
2055 // so that the parameter gets the right LLVM IR attributes.
2056 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2057 // Treat an enum type as its underlying type.
2058 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2059 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002060
Chris Lattnereb518b42010-07-29 21:42:50 +00002061 if (Ty->isIntegralOrEnumerationType() &&
2062 Ty->isPromotableIntegerType())
2063 return ABIArgInfo::getExtend();
2064 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002065
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002066 break;
2067
2068 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
2069 // available SSE register is used, the registers are taken in the
2070 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00002071 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002072 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00002073 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00002074 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002075 break;
2076 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00002077 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002078
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002079 llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002080 switch (Hi) {
2081 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002082 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002083 // which is passed in memory.
2084 case Memory:
2085 case X87:
2086 case ComplexX87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002087 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002088
2089 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002090
Chris Lattner645406a2010-09-01 00:24:35 +00002091 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002092 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00002093 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002094 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002095
Chris Lattner645406a2010-09-01 00:24:35 +00002096 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2097 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002098 break;
2099
2100 // X87Up generally doesn't occur here (long double is passed in
2101 // memory), except in situations involving unions.
2102 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00002103 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002104 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002105
Chris Lattner645406a2010-09-01 00:24:35 +00002106 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2107 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00002108
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002109 ++neededSSE;
2110 break;
2111
2112 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
2113 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002114 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002115 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00002116 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002117 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002118 break;
2119 }
2120
Chris Lattner645406a2010-09-01 00:24:35 +00002121 // If a high part was specified, merge it together with the low part. It is
2122 // known to pass in the high eightbyte of the result. We do this by forming a
2123 // first class struct aggregate with the high and low part: {low, high}
2124 if (HighPart)
Chris Lattner66e7b682010-09-01 00:50:20 +00002125 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002126
Chris Lattnereb518b42010-07-29 21:42:50 +00002127 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002128}
2129
Chris Lattneree5dcd02010-07-29 02:31:05 +00002130void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002131
Chris Lattnera3c109b2010-07-29 02:16:43 +00002132 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002133
2134 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00002135 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002136
2137 // If the return value is indirect, then the hidden argument is consuming one
2138 // integer register.
2139 if (FI.getReturnInfo().isIndirect())
2140 --freeIntRegs;
2141
2142 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2143 // get assigned (in left-to-right order) for passing as follows...
2144 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2145 it != ie; ++it) {
Bill Wendling99aaae82010-10-18 23:51:38 +00002146 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002147 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
2148 neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002149
2150 // AMD64-ABI 3.2.3p3: If there are no registers available for any
2151 // eightbyte of an argument, the whole argument is passed on the
2152 // stack. If registers have already been assigned for some
2153 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00002154 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002155 freeIntRegs -= neededInt;
2156 freeSSERegs -= neededSSE;
2157 } else {
Daniel Dunbaredfac032012-03-10 01:03:58 +00002158 it->info = getIndirectResult(it->type, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002159 }
2160 }
2161}
2162
2163static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2164 QualType Ty,
2165 CodeGenFunction &CGF) {
2166 llvm::Value *overflow_arg_area_p =
2167 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2168 llvm::Value *overflow_arg_area =
2169 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2170
2171 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2172 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedman8d2fe422011-11-18 02:44:19 +00002173 // It isn't stated explicitly in the standard, but in practice we use
2174 // alignment greater than 16 where necessary.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002175 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2176 if (Align > 8) {
Eli Friedman8d2fe422011-11-18 02:44:19 +00002177 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
Owen Anderson0032b272009-08-13 21:57:51 +00002178 llvm::Value *Offset =
Eli Friedman8d2fe422011-11-18 02:44:19 +00002179 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002180 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2181 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00002182 CGF.Int64Ty);
Eli Friedman8d2fe422011-11-18 02:44:19 +00002183 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002184 overflow_arg_area =
2185 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2186 overflow_arg_area->getType(),
2187 "overflow_arg_area.align");
2188 }
2189
2190 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002191 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002192 llvm::Value *Res =
2193 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002194 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002195
2196 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2197 // l->overflow_arg_area + sizeof(type).
2198 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2199 // an 8 byte boundary.
2200
2201 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00002202 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00002203 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002204 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2205 "overflow_arg_area.next");
2206 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2207
2208 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2209 return Res;
2210}
2211
2212llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2213 CodeGenFunction &CGF) const {
2214 // Assume that va_list type is correct; should be pointer to LLVM type:
2215 // struct {
2216 // i32 gp_offset;
2217 // i32 fp_offset;
2218 // i8* overflow_arg_area;
2219 // i8* reg_save_area;
2220 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00002221 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002222
Chris Lattnera14db752010-03-11 18:19:55 +00002223 Ty = CGF.getContext().getCanonicalType(Ty);
Daniel Dunbaredfac032012-03-10 01:03:58 +00002224 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002225
2226 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2227 // in the registers. If not go to step 7.
2228 if (!neededInt && !neededSSE)
2229 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2230
2231 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2232 // general purpose registers needed to pass type and num_fp to hold
2233 // the number of floating point registers needed.
2234
2235 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2236 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2237 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2238 //
2239 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2240 // register save space).
2241
2242 llvm::Value *InRegs = 0;
2243 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2244 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2245 if (neededInt) {
2246 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2247 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002248 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2249 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002250 }
2251
2252 if (neededSSE) {
2253 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2254 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2255 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00002256 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2257 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002258 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2259 }
2260
2261 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2262 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2263 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2264 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2265
2266 // Emit code to load the value if it was passed in registers.
2267
2268 CGF.EmitBlock(InRegBlock);
2269
2270 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2271 // an offset of l->gp_offset and/or l->fp_offset. This may require
2272 // copying to a temporary location in case the parameter is passed
2273 // in different register classes or requires an alignment greater
2274 // than 8 for general purpose registers and 16 for XMM registers.
2275 //
2276 // FIXME: This really results in shameful code when we end up needing to
2277 // collect arguments from different places; often what should result in a
2278 // simple assembling of a structure from scattered addresses has many more
2279 // loads than necessary. Can we clean this up?
Chris Lattner2acc6e32011-07-18 04:24:23 +00002280 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002281 llvm::Value *RegAddr =
2282 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2283 "reg_save_area");
2284 if (neededInt && neededSSE) {
2285 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002286 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002287 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002288 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2289 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002290 llvm::Type *TyLo = ST->getElementType(0);
2291 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002292 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002293 "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002294 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2295 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002296 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2297 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002298 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2299 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002300 llvm::Value *V =
2301 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2302 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2303 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2304 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2305
Owen Andersona1cf15f2009-07-14 23:10:40 +00002306 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002307 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002308 } else if (neededInt) {
2309 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2310 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002311 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002312 } else if (neededSSE == 1) {
2313 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2314 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2315 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002316 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002317 assert(neededSSE == 2 && "Invalid number of needed registers!");
2318 // SSE registers are spaced 16 bytes apart in the register save
2319 // area, we need to collect the two eightbytes together.
2320 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002321 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner8b418682012-02-07 00:39:47 +00002322 llvm::Type *DoubleTy = CGF.DoubleTy;
Chris Lattner2acc6e32011-07-18 04:24:23 +00002323 llvm::Type *DblPtrTy =
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002324 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002325 llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002326 DoubleTy, NULL);
2327 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2328 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2329 DblPtrTy));
2330 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2331 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2332 DblPtrTy));
2333 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2334 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2335 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002336 }
2337
2338 // AMD64-ABI 3.5.7p5: Step 5. Set:
2339 // l->gp_offset = l->gp_offset + num_gp * 8
2340 // l->fp_offset = l->fp_offset + num_fp * 16.
2341 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002342 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002343 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2344 gp_offset_p);
2345 }
2346 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002347 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002348 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2349 fp_offset_p);
2350 }
2351 CGF.EmitBranch(ContBlock);
2352
2353 // Emit code to load the value if it was passed in memory.
2354
2355 CGF.EmitBlock(InMemBlock);
2356 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2357
2358 // Return the appropriate result.
2359
2360 CGF.EmitBlock(ContBlock);
Jay Foadbbf3bac2011-03-30 11:28:58 +00002361 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002362 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002363 ResAddr->addIncoming(RegAddr, InRegBlock);
2364 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002365 return ResAddr;
2366}
2367
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002368ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2369
2370 if (Ty->isVoidType())
2371 return ABIArgInfo::getIgnore();
2372
2373 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2374 Ty = EnumTy->getDecl()->getIntegerType();
2375
2376 uint64_t Size = getContext().getTypeSize(Ty);
2377
2378 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002379 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2380 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002381 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2382
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002383 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2384 if (Size == 128 &&
Eli Friedman55fc7e22012-01-25 22:46:34 +00002385 getContext().getTargetInfo().getTriple().getOS()
2386 == llvm::Triple::MinGW32)
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002387 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2388 Size));
2389
2390 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2391 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2392 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002393 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002394 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2395 Size));
2396
2397 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2398 }
2399
2400 if (Ty->isPromotableIntegerType())
2401 return ABIArgInfo::getExtend();
2402
2403 return ABIArgInfo::getDirect();
2404}
2405
2406void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2407
2408 QualType RetTy = FI.getReturnType();
2409 FI.getReturnInfo() = classify(RetTy);
2410
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002411 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2412 it != ie; ++it)
2413 it->info = classify(it->type);
2414}
2415
Chris Lattnerf13721d2010-08-31 16:44:54 +00002416llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2417 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002418 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002419
Chris Lattnerf13721d2010-08-31 16:44:54 +00002420 CGBuilderTy &Builder = CGF.Builder;
2421 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2422 "ap");
2423 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2424 llvm::Type *PTy =
2425 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2426 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2427
2428 uint64_t Offset =
2429 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2430 llvm::Value *NextAddr =
2431 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2432 "ap.next");
2433 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2434
2435 return AddrTyped;
2436}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002437
John McCallec853ba2010-03-11 00:10:12 +00002438// PowerPC-32
2439
2440namespace {
2441class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2442public:
Chris Lattnerea044322010-07-29 02:01:43 +00002443 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002444
John McCallec853ba2010-03-11 00:10:12 +00002445 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2446 // This is recovered from gcc output.
2447 return 1; // r1 is the dedicated stack pointer
2448 }
2449
2450 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002451 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002452};
2453
2454}
2455
2456bool
2457PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2458 llvm::Value *Address) const {
2459 // This is calculated from the LLVM and GCC tables and verified
2460 // against gcc output. AFAIK all ABIs use the same encoding.
2461
2462 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallec853ba2010-03-11 00:10:12 +00002463
Chris Lattner8b418682012-02-07 00:39:47 +00002464 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallec853ba2010-03-11 00:10:12 +00002465 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2466 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2467 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2468
2469 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002470 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002471
2472 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002473 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002474
2475 // 64-76 are various 4-byte special-purpose registers:
2476 // 64: mq
2477 // 65: lr
2478 // 66: ctr
2479 // 67: ap
2480 // 68-75 cr0-7
2481 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002482 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002483
2484 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002485 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002486
2487 // 109: vrsave
2488 // 110: vscr
2489 // 111: spe_acc
2490 // 112: spefscr
2491 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002492 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002493
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002494 return false;
John McCallec853ba2010-03-11 00:10:12 +00002495}
2496
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002497// PowerPC-64
2498
2499namespace {
2500class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2501public:
2502 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
2503
2504 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2505 // This is recovered from gcc output.
2506 return 1; // r1 is the dedicated stack pointer
2507 }
2508
2509 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2510 llvm::Value *Address) const;
2511};
2512
2513}
2514
2515bool
2516PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2517 llvm::Value *Address) const {
2518 // This is calculated from the LLVM and GCC tables and verified
2519 // against gcc output. AFAIK all ABIs use the same encoding.
2520
2521 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2522
2523 llvm::IntegerType *i8 = CGF.Int8Ty;
2524 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2525 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2526 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2527
2528 // 0-31: r0-31, the 8-byte general-purpose registers
2529 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
2530
2531 // 32-63: fp0-31, the 8-byte floating-point registers
2532 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
2533
2534 // 64-76 are various 4-byte special-purpose registers:
2535 // 64: mq
2536 // 65: lr
2537 // 66: ctr
2538 // 67: ap
2539 // 68-75 cr0-7
2540 // 76: xer
2541 AssignToArrayRange(Builder, Address, Four8, 64, 76);
2542
2543 // 77-108: v0-31, the 16-byte vector registers
2544 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
2545
2546 // 109: vrsave
2547 // 110: vscr
2548 // 111: spe_acc
2549 // 112: spefscr
2550 // 113: sfp
2551 AssignToArrayRange(Builder, Address, Four8, 109, 113);
2552
2553 return false;
2554}
John McCallec853ba2010-03-11 00:10:12 +00002555
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002556//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002557// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002558//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002559
2560namespace {
2561
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002562class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002563public:
2564 enum ABIKind {
2565 APCS = 0,
2566 AAPCS = 1,
2567 AAPCS_VFP
2568 };
2569
2570private:
2571 ABIKind Kind;
2572
2573public:
Chris Lattnerea044322010-07-29 02:01:43 +00002574 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002575
John McCall49e34be2011-08-30 01:42:09 +00002576 bool isEABI() const {
Eli Friedman55fc7e22012-01-25 22:46:34 +00002577 StringRef Env =
2578 getContext().getTargetInfo().getTriple().getEnvironmentName();
Chandler Carruthb43550b2012-01-10 19:47:42 +00002579 return (Env == "gnueabi" || Env == "eabi" || Env == "androideabi");
John McCall49e34be2011-08-30 01:42:09 +00002580 }
2581
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002582private:
2583 ABIKind getABIKind() const { return Kind; }
2584
Chris Lattnera3c109b2010-07-29 02:16:43 +00002585 ABIArgInfo classifyReturnType(QualType RetTy) const;
2586 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002587
Chris Lattneree5dcd02010-07-29 02:31:05 +00002588 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002589
2590 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2591 CodeGenFunction &CGF) const;
2592};
2593
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002594class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2595public:
Chris Lattnerea044322010-07-29 02:01:43 +00002596 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2597 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00002598
John McCall49e34be2011-08-30 01:42:09 +00002599 const ARMABIInfo &getABIInfo() const {
2600 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
2601 }
2602
John McCall6374c332010-03-06 00:35:14 +00002603 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2604 return 13;
2605 }
Roman Divacky09345d12011-05-18 19:36:54 +00002606
Chris Lattner5f9e2722011-07-23 10:55:15 +00002607 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCallf85e1932011-06-15 23:02:42 +00002608 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2609 }
2610
Roman Divacky09345d12011-05-18 19:36:54 +00002611 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2612 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002613 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divacky09345d12011-05-18 19:36:54 +00002614
2615 // 0-15 are the 16 integer registers.
Chris Lattner8b418682012-02-07 00:39:47 +00002616 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divacky09345d12011-05-18 19:36:54 +00002617 return false;
2618 }
John McCall49e34be2011-08-30 01:42:09 +00002619
2620 unsigned getSizeOfUnwindException() const {
2621 if (getABIInfo().isEABI()) return 88;
2622 return TargetCodeGenInfo::getSizeOfUnwindException();
2623 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002624};
2625
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002626}
2627
Chris Lattneree5dcd02010-07-29 02:31:05 +00002628void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002629 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002630 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattnera3c109b2010-07-29 02:16:43 +00002631 it != ie; ++it)
2632 it->info = classifyArgumentType(it->type);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002633
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002634 // Always honor user-specified calling convention.
2635 if (FI.getCallingConvention() != llvm::CallingConv::C)
2636 return;
2637
2638 // Calling convention as default by an ABI.
Rafael Espindola25117ab2010-06-16 16:13:39 +00002639 llvm::CallingConv::ID DefaultCC;
John McCall49e34be2011-08-30 01:42:09 +00002640 if (isEABI())
Rafael Espindola25117ab2010-06-16 16:13:39 +00002641 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00002642 else
2643 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00002644
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002645 // If user did not ask for specific calling convention explicitly (e.g. via
2646 // pcs attribute), set effective calling convention if it's different than ABI
2647 // default.
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002648 switch (getABIKind()) {
2649 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002650 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2651 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002652 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002653 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002654 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2655 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002656 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002657 case AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002658 if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
2659 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002660 break;
2661 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002662}
2663
Bob Wilson194f06a2011-08-03 05:58:22 +00002664/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
2665/// aggregate. If HAMembers is non-null, the number of base elements
2666/// contained in the type is returned through it; this is used for the
2667/// recursive calls that check aggregate component types.
2668static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
2669 ASTContext &Context,
2670 uint64_t *HAMembers = 0) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002671 uint64_t Members = 0;
Bob Wilson194f06a2011-08-03 05:58:22 +00002672 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2673 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
2674 return false;
2675 Members *= AT->getSize().getZExtValue();
2676 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
2677 const RecordDecl *RD = RT->getDecl();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002678 if (RD->hasFlexibleArrayMember())
Bob Wilson194f06a2011-08-03 05:58:22 +00002679 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002680
Bob Wilson194f06a2011-08-03 05:58:22 +00002681 Members = 0;
2682 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2683 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00002684 const FieldDecl *FD = *i;
Bob Wilson194f06a2011-08-03 05:58:22 +00002685 uint64_t FldMembers;
2686 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
2687 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002688
2689 Members = (RD->isUnion() ?
2690 std::max(Members, FldMembers) : Members + FldMembers);
Bob Wilson194f06a2011-08-03 05:58:22 +00002691 }
2692 } else {
2693 Members = 1;
2694 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2695 Members = 2;
2696 Ty = CT->getElementType();
2697 }
2698
2699 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
2700 // double, or 64-bit or 128-bit vectors.
2701 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
2702 if (BT->getKind() != BuiltinType::Float &&
Tim Northoveradfa45f2012-07-20 22:29:29 +00002703 BT->getKind() != BuiltinType::Double &&
2704 BT->getKind() != BuiltinType::LongDouble)
Bob Wilson194f06a2011-08-03 05:58:22 +00002705 return false;
2706 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
2707 unsigned VecSize = Context.getTypeSize(VT);
2708 if (VecSize != 64 && VecSize != 128)
2709 return false;
2710 } else {
2711 return false;
2712 }
2713
2714 // The base type must be the same for all members. Vector types of the
2715 // same total size are treated as being equivalent here.
2716 const Type *TyPtr = Ty.getTypePtr();
2717 if (!Base)
2718 Base = TyPtr;
2719 if (Base != TyPtr &&
2720 (!Base->isVectorType() || !TyPtr->isVectorType() ||
2721 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
2722 return false;
2723 }
2724
2725 // Homogeneous Aggregates can have at most 4 members of the base type.
2726 if (HAMembers)
2727 *HAMembers = Members;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002728
2729 return (Members > 0 && Members <= 4);
Bob Wilson194f06a2011-08-03 05:58:22 +00002730}
2731
Chris Lattnera3c109b2010-07-29 02:16:43 +00002732ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +00002733 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002734 // Treat an enum type as its underlying type.
2735 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2736 Ty = EnumTy->getDecl()->getIntegerType();
2737
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002738 return (Ty->isPromotableIntegerType() ?
2739 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002740 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002741
Daniel Dunbar42025572009-09-14 21:54:03 +00002742 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002743 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00002744 return ABIArgInfo::getIgnore();
2745
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002746 // Structures with either a non-trivial destructor or a non-trivial
2747 // copy constructor are always indirect.
2748 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2749 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2750
Bob Wilson194f06a2011-08-03 05:58:22 +00002751 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
2752 // Homogeneous Aggregates need to be expanded.
2753 const Type *Base = 0;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002754 if (isHomogeneousAggregate(Ty, Base, getContext())) {
2755 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson194f06a2011-08-03 05:58:22 +00002756 return ABIArgInfo::getExpand();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002757 }
Bob Wilson194f06a2011-08-03 05:58:22 +00002758 }
2759
Eli Friedman79f30982012-08-09 00:31:40 +00002760 // FIXME: byval for AAPCS is not yet supported; we need it for performance
2761 // and to support large alignment.
2762 if (getABIKind() == ARMABIInfo::APCS) {
2763 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64) ||
2764 getContext().getTypeAlign(Ty) > 64) {
2765 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
2766 }
2767 }
2768
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00002769 // Otherwise, pass by coercing to a structure of the appropriate size.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002770 llvm::Type* ElemTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002771 unsigned SizeRegs;
Eli Friedman79f30982012-08-09 00:31:40 +00002772 // FIXME: Try to match the types of the arguments more accurately where
2773 // we can.
2774 if (getContext().getTypeAlign(Ty) <= 32) {
Bob Wilson53fc1a62011-08-01 23:39:04 +00002775 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2776 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Manman Ren78eb76e2012-06-25 22:04:00 +00002777 } else {
Manman Ren78eb76e2012-06-25 22:04:00 +00002778 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2779 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastings67d097e2011-04-27 17:24:02 +00002780 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00002781
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002782 llvm::Type *STy =
Chris Lattner7650d952011-06-18 22:49:11 +00002783 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00002784 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002785}
2786
Chris Lattnera3c109b2010-07-29 02:16:43 +00002787static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00002788 llvm::LLVMContext &VMContext) {
2789 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2790 // is called integer-like if its size is less than or equal to one word, and
2791 // the offset of each of its addressable sub-fields is zero.
2792
2793 uint64_t Size = Context.getTypeSize(Ty);
2794
2795 // Check that the type fits in a word.
2796 if (Size > 32)
2797 return false;
2798
2799 // FIXME: Handle vector types!
2800 if (Ty->isVectorType())
2801 return false;
2802
Daniel Dunbarb0d58192009-09-14 02:20:34 +00002803 // Float types are never treated as "integer like".
2804 if (Ty->isRealFloatingType())
2805 return false;
2806
Daniel Dunbar98303b92009-09-13 08:03:58 +00002807 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00002808 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00002809 return true;
2810
Daniel Dunbar45815812010-02-01 23:31:26 +00002811 // Small complex integer types are "integer like".
2812 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2813 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002814
2815 // Single element and zero sized arrays should be allowed, by the definition
2816 // above, but they are not.
2817
2818 // Otherwise, it must be a record type.
2819 const RecordType *RT = Ty->getAs<RecordType>();
2820 if (!RT) return false;
2821
2822 // Ignore records with flexible arrays.
2823 const RecordDecl *RD = RT->getDecl();
2824 if (RD->hasFlexibleArrayMember())
2825 return false;
2826
2827 // Check that all sub-fields are at offset 0, and are themselves "integer
2828 // like".
2829 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2830
2831 bool HadField = false;
2832 unsigned idx = 0;
2833 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2834 i != e; ++i, ++idx) {
David Blaikie581deb32012-06-06 20:45:41 +00002835 const FieldDecl *FD = *i;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002836
Daniel Dunbar679855a2010-01-29 03:22:29 +00002837 // Bit-fields are not addressable, we only need to verify they are "integer
2838 // like". We still have to disallow a subsequent non-bitfield, for example:
2839 // struct { int : 0; int x }
2840 // is non-integer like according to gcc.
2841 if (FD->isBitField()) {
2842 if (!RD->isUnion())
2843 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002844
Daniel Dunbar679855a2010-01-29 03:22:29 +00002845 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2846 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002847
Daniel Dunbar679855a2010-01-29 03:22:29 +00002848 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002849 }
2850
Daniel Dunbar679855a2010-01-29 03:22:29 +00002851 // Check if this field is at offset 0.
2852 if (Layout.getFieldOffset(idx) != 0)
2853 return false;
2854
Daniel Dunbar98303b92009-09-13 08:03:58 +00002855 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2856 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002857
Daniel Dunbar679855a2010-01-29 03:22:29 +00002858 // Only allow at most one field in a structure. This doesn't match the
2859 // wording above, but follows gcc in situations with a field following an
2860 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00002861 if (!RD->isUnion()) {
2862 if (HadField)
2863 return false;
2864
2865 HadField = true;
2866 }
2867 }
2868
2869 return true;
2870}
2871
Chris Lattnera3c109b2010-07-29 02:16:43 +00002872ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002873 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002874 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00002875
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00002876 // Large vector types should be returned via memory.
2877 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
2878 return ABIArgInfo::getIndirect(0);
2879
John McCalld608cdb2010-08-22 10:59:02 +00002880 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002881 // Treat an enum type as its underlying type.
2882 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2883 RetTy = EnumTy->getDecl()->getIntegerType();
2884
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002885 return (RetTy->isPromotableIntegerType() ?
2886 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002887 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002888
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002889 // Structures with either a non-trivial destructor or a non-trivial
2890 // copy constructor are always indirect.
2891 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2892 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2893
Daniel Dunbar98303b92009-09-13 08:03:58 +00002894 // Are we following APCS?
2895 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002896 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00002897 return ABIArgInfo::getIgnore();
2898
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002899 // Complex types are all returned as packed integers.
2900 //
2901 // FIXME: Consider using 2 x vector types if the back end handles them
2902 // correctly.
2903 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00002904 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00002905 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002906
Daniel Dunbar98303b92009-09-13 08:03:58 +00002907 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002908 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002909 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002910 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002911 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002912 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002913 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002914 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2915 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002916 }
2917
2918 // Otherwise return in memory.
2919 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002920 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002921
2922 // Otherwise this is an AAPCS variant.
2923
Chris Lattnera3c109b2010-07-29 02:16:43 +00002924 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00002925 return ABIArgInfo::getIgnore();
2926
Bob Wilson3b694fa2011-11-02 04:51:36 +00002927 // Check for homogeneous aggregates with AAPCS-VFP.
2928 if (getABIKind() == AAPCS_VFP) {
2929 const Type *Base = 0;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002930 if (isHomogeneousAggregate(RetTy, Base, getContext())) {
2931 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson3b694fa2011-11-02 04:51:36 +00002932 // Homogeneous Aggregates are returned directly.
2933 return ABIArgInfo::getDirect();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002934 }
Bob Wilson3b694fa2011-11-02 04:51:36 +00002935 }
2936
Daniel Dunbar98303b92009-09-13 08:03:58 +00002937 // Aggregates <= 4 bytes are returned in r0; other aggregates
2938 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002939 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00002940 if (Size <= 32) {
2941 // Return in the smallest viable integer type.
2942 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002943 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002944 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002945 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2946 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002947 }
2948
Daniel Dunbar98303b92009-09-13 08:03:58 +00002949 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002950}
2951
2952llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00002953 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002954 llvm::Type *BP = CGF.Int8PtrTy;
2955 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002956
2957 CGBuilderTy &Builder = CGF.Builder;
Chris Lattner8b418682012-02-07 00:39:47 +00002958 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002959 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Rafael Espindolae164c182011-08-02 22:33:37 +00002960 // Handle address alignment for type alignment > 32 bits
2961 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
2962 if (TyAlign > 4) {
2963 assert((TyAlign & (TyAlign - 1)) == 0 &&
2964 "Alignment is not power of 2!");
2965 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
2966 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
2967 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
2968 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2969 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002970 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00002971 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002972 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2973
2974 uint64_t Offset =
2975 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2976 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00002977 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002978 "ap.next");
2979 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2980
2981 return AddrTyped;
2982}
2983
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002984//===----------------------------------------------------------------------===//
Justin Holewinski2c585b92012-05-24 17:43:12 +00002985// NVPTX ABI Implementation
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002986//===----------------------------------------------------------------------===//
2987
2988namespace {
2989
Justin Holewinski2c585b92012-05-24 17:43:12 +00002990class NVPTXABIInfo : public ABIInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002991public:
Justin Holewinski2c585b92012-05-24 17:43:12 +00002992 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002993
2994 ABIArgInfo classifyReturnType(QualType RetTy) const;
2995 ABIArgInfo classifyArgumentType(QualType Ty) const;
2996
2997 virtual void computeInfo(CGFunctionInfo &FI) const;
2998 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2999 CodeGenFunction &CFG) const;
3000};
3001
Justin Holewinski2c585b92012-05-24 17:43:12 +00003002class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003003public:
Justin Holewinski2c585b92012-05-24 17:43:12 +00003004 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
3005 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
Justin Holewinski818eafb2011-10-05 17:58:44 +00003006
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003007 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3008 CodeGen::CodeGenModule &M) const;
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003009};
3010
Justin Holewinski2c585b92012-05-24 17:43:12 +00003011ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003012 if (RetTy->isVoidType())
3013 return ABIArgInfo::getIgnore();
3014 if (isAggregateTypeForABI(RetTy))
3015 return ABIArgInfo::getIndirect(0);
3016 return ABIArgInfo::getDirect();
3017}
3018
Justin Holewinski2c585b92012-05-24 17:43:12 +00003019ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003020 if (isAggregateTypeForABI(Ty))
3021 return ABIArgInfo::getIndirect(0);
3022
3023 return ABIArgInfo::getDirect();
3024}
3025
Justin Holewinski2c585b92012-05-24 17:43:12 +00003026void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003027 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3028 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3029 it != ie; ++it)
3030 it->info = classifyArgumentType(it->type);
3031
3032 // Always honor user-specified calling convention.
3033 if (FI.getCallingConvention() != llvm::CallingConv::C)
3034 return;
3035
3036 // Calling convention as default by an ABI.
Justin Holewinski2c585b92012-05-24 17:43:12 +00003037 // We're still using the PTX_Kernel/PTX_Device calling conventions here,
3038 // but we should switch to NVVM metadata later on.
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003039 llvm::CallingConv::ID DefaultCC;
David Blaikie4e4d0842012-03-11 07:00:24 +00003040 const LangOptions &LangOpts = getContext().getLangOpts();
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003041 if (LangOpts.OpenCL || LangOpts.CUDA) {
3042 // If we are in OpenCL or CUDA mode, then default to device functions
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003043 DefaultCC = llvm::CallingConv::PTX_Device;
Justin Holewinski818eafb2011-10-05 17:58:44 +00003044 } else {
3045 // If we are in standard C/C++ mode, use the triple to decide on the default
3046 StringRef Env =
3047 getContext().getTargetInfo().getTriple().getEnvironmentName();
3048 if (Env == "device")
3049 DefaultCC = llvm::CallingConv::PTX_Device;
3050 else
3051 DefaultCC = llvm::CallingConv::PTX_Kernel;
3052 }
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003053 FI.setEffectiveCallingConvention(DefaultCC);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003054
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003055}
3056
Justin Holewinski2c585b92012-05-24 17:43:12 +00003057llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3058 CodeGenFunction &CFG) const {
3059 llvm_unreachable("NVPTX does not support varargs");
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003060}
3061
Justin Holewinski2c585b92012-05-24 17:43:12 +00003062void NVPTXTargetCodeGenInfo::
3063SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3064 CodeGen::CodeGenModule &M) const{
Justin Holewinski818eafb2011-10-05 17:58:44 +00003065 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3066 if (!FD) return;
3067
3068 llvm::Function *F = cast<llvm::Function>(GV);
3069
3070 // Perform special handling in OpenCL mode
David Blaikie4e4d0842012-03-11 07:00:24 +00003071 if (M.getLangOpts().OpenCL) {
Justin Holewinski818eafb2011-10-05 17:58:44 +00003072 // Use OpenCL function attributes to set proper calling conventions
3073 // By default, all functions are device functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00003074 if (FD->hasAttr<OpenCLKernelAttr>()) {
3075 // OpenCL __kernel functions get a kernel calling convention
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003076 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003077 // And kernel functions are not subject to inlining
3078 F->addFnAttr(llvm::Attribute::NoInline);
3079 }
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003080 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00003081
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003082 // Perform special handling in CUDA mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00003083 if (M.getLangOpts().CUDA) {
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003084 // CUDA __global__ functions get a kernel calling convention. Since
3085 // __global__ functions cannot be called from the device, we do not
3086 // need to set the noinline attribute.
3087 if (FD->getAttr<CUDAGlobalAttr>())
3088 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003089 }
3090}
3091
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003092}
3093
3094//===----------------------------------------------------------------------===//
Wesley Peck276fdf42010-12-19 19:57:51 +00003095// MBlaze ABI Implementation
3096//===----------------------------------------------------------------------===//
3097
3098namespace {
3099
3100class MBlazeABIInfo : public ABIInfo {
3101public:
3102 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3103
3104 bool isPromotableIntegerType(QualType Ty) const;
3105
3106 ABIArgInfo classifyReturnType(QualType RetTy) const;
3107 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3108
3109 virtual void computeInfo(CGFunctionInfo &FI) const {
3110 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3111 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3112 it != ie; ++it)
3113 it->info = classifyArgumentType(it->type);
3114 }
3115
3116 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3117 CodeGenFunction &CGF) const;
3118};
3119
3120class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
3121public:
3122 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
3123 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
3124 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3125 CodeGen::CodeGenModule &M) const;
3126};
3127
3128}
3129
3130bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
3131 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
3132 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
3133 switch (BT->getKind()) {
3134 case BuiltinType::Bool:
3135 case BuiltinType::Char_S:
3136 case BuiltinType::Char_U:
3137 case BuiltinType::SChar:
3138 case BuiltinType::UChar:
3139 case BuiltinType::Short:
3140 case BuiltinType::UShort:
3141 return true;
3142 default:
3143 return false;
3144 }
3145 return false;
3146}
3147
3148llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3149 CodeGenFunction &CGF) const {
3150 // FIXME: Implement
3151 return 0;
3152}
3153
3154
3155ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
3156 if (RetTy->isVoidType())
3157 return ABIArgInfo::getIgnore();
3158 if (isAggregateTypeForABI(RetTy))
3159 return ABIArgInfo::getIndirect(0);
3160
3161 return (isPromotableIntegerType(RetTy) ?
3162 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3163}
3164
3165ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
3166 if (isAggregateTypeForABI(Ty))
3167 return ABIArgInfo::getIndirect(0);
3168
3169 return (isPromotableIntegerType(Ty) ?
3170 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3171}
3172
3173void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3174 llvm::GlobalValue *GV,
3175 CodeGen::CodeGenModule &M)
3176 const {
3177 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3178 if (!FD) return;
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00003179
Wesley Peck276fdf42010-12-19 19:57:51 +00003180 llvm::CallingConv::ID CC = llvm::CallingConv::C;
3181 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
3182 CC = llvm::CallingConv::MBLAZE_INTR;
3183 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
3184 CC = llvm::CallingConv::MBLAZE_SVOL;
3185
3186 if (CC != llvm::CallingConv::C) {
3187 // Handle 'interrupt_handler' attribute:
3188 llvm::Function *F = cast<llvm::Function>(GV);
3189
3190 // Step 1: Set ISR calling convention.
3191 F->setCallingConv(CC);
3192
3193 // Step 2: Add attributes goodness.
3194 F->addFnAttr(llvm::Attribute::NoInline);
3195 }
3196
3197 // Step 3: Emit _interrupt_handler alias.
3198 if (CC == llvm::CallingConv::MBLAZE_INTR)
3199 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
3200 "_interrupt_handler", GV, &M.getModule());
3201}
3202
3203
3204//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003205// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003206//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003207
3208namespace {
3209
3210class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
3211public:
Chris Lattnerea044322010-07-29 02:01:43 +00003212 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
3213 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003214 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3215 CodeGen::CodeGenModule &M) const;
3216};
3217
3218}
3219
3220void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3221 llvm::GlobalValue *GV,
3222 CodeGen::CodeGenModule &M) const {
3223 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
3224 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
3225 // Handle 'interrupt' attribute:
3226 llvm::Function *F = cast<llvm::Function>(GV);
3227
3228 // Step 1: Set ISR calling convention.
3229 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
3230
3231 // Step 2: Add attributes goodness.
3232 F->addFnAttr(llvm::Attribute::NoInline);
3233
3234 // Step 3: Emit ISR vector alias.
3235 unsigned Num = attr->getNumber() + 0xffe0;
3236 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003237 "vector_" + Twine::utohexstr(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003238 GV, &M.getModule());
3239 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003240 }
3241}
3242
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003243//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00003244// MIPS ABI Implementation. This works for both little-endian and
3245// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003246//===----------------------------------------------------------------------===//
3247
John McCallaeeb7012010-05-27 06:19:26 +00003248namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00003249class MipsABIInfo : public ABIInfo {
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003250 bool IsO32;
Akira Hatanakac359f202012-07-03 19:24:06 +00003251 unsigned MinABIStackAlignInBytes, StackAlignInBytes;
3252 void CoerceToIntArgs(uint64_t TySize,
3253 SmallVector<llvm::Type*, 8> &ArgList) const;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003254 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003255 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003256 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003257public:
Akira Hatanakab551dd32011-11-03 00:05:50 +00003258 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
Akira Hatanakac359f202012-07-03 19:24:06 +00003259 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
3260 StackAlignInBytes(IsO32 ? 8 : 16) {}
Akira Hatanaka619e8872011-06-02 00:09:17 +00003261
3262 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003263 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003264 virtual void computeInfo(CGFunctionInfo &FI) const;
3265 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3266 CodeGenFunction &CGF) const;
3267};
3268
John McCallaeeb7012010-05-27 06:19:26 +00003269class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003270 unsigned SizeOfUnwindException;
John McCallaeeb7012010-05-27 06:19:26 +00003271public:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003272 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
3273 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
3274 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCallaeeb7012010-05-27 06:19:26 +00003275
3276 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
3277 return 29;
3278 }
3279
3280 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003281 llvm::Value *Address) const;
John McCall49e34be2011-08-30 01:42:09 +00003282
3283 unsigned getSizeOfUnwindException() const {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003284 return SizeOfUnwindException;
John McCall49e34be2011-08-30 01:42:09 +00003285 }
John McCallaeeb7012010-05-27 06:19:26 +00003286};
3287}
3288
Akira Hatanakac359f202012-07-03 19:24:06 +00003289void MipsABIInfo::CoerceToIntArgs(uint64_t TySize,
3290 SmallVector<llvm::Type*, 8> &ArgList) const {
3291 llvm::IntegerType *IntTy =
3292 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003293
3294 // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
3295 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
3296 ArgList.push_back(IntTy);
3297
3298 // If necessary, add one more integer type to ArgList.
3299 unsigned R = TySize % (MinABIStackAlignInBytes * 8);
3300
3301 if (R)
3302 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003303}
3304
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003305// In N32/64, an aligned double precision floating point field is passed in
3306// a register.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003307llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
Akira Hatanakac359f202012-07-03 19:24:06 +00003308 SmallVector<llvm::Type*, 8> ArgList, IntArgList;
3309
3310 if (IsO32) {
3311 CoerceToIntArgs(TySize, ArgList);
3312 return llvm::StructType::get(getVMContext(), ArgList);
3313 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003314
Akira Hatanaka2afd23d2012-01-12 00:52:17 +00003315 if (Ty->isComplexType())
3316 return CGT.ConvertType(Ty);
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00003317
Akira Hatanakaa34e9212012-02-09 19:54:16 +00003318 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003319
Akira Hatanakac359f202012-07-03 19:24:06 +00003320 // Unions/vectors are passed in integer registers.
3321 if (!RT || !RT->isStructureOrClassType()) {
3322 CoerceToIntArgs(TySize, ArgList);
3323 return llvm::StructType::get(getVMContext(), ArgList);
3324 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003325
3326 const RecordDecl *RD = RT->getDecl();
3327 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003328 assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003329
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003330 uint64_t LastOffset = 0;
3331 unsigned idx = 0;
3332 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
3333
Akira Hatanakaa34e9212012-02-09 19:54:16 +00003334 // Iterate over fields in the struct/class and check if there are any aligned
3335 // double fields.
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003336 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3337 i != e; ++i, ++idx) {
David Blaikie262bc182012-04-30 02:36:29 +00003338 const QualType Ty = i->getType();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003339 const BuiltinType *BT = Ty->getAs<BuiltinType>();
3340
3341 if (!BT || BT->getKind() != BuiltinType::Double)
3342 continue;
3343
3344 uint64_t Offset = Layout.getFieldOffset(idx);
3345 if (Offset % 64) // Ignore doubles that are not aligned.
3346 continue;
3347
3348 // Add ((Offset - LastOffset) / 64) args of type i64.
3349 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
3350 ArgList.push_back(I64);
3351
3352 // Add double type.
3353 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
3354 LastOffset = Offset + 64;
3355 }
3356
Akira Hatanakac359f202012-07-03 19:24:06 +00003357 CoerceToIntArgs(TySize - LastOffset, IntArgList);
3358 ArgList.append(IntArgList.begin(), IntArgList.end());
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003359
3360 return llvm::StructType::get(getVMContext(), ArgList);
3361}
3362
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003363llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003364 assert((Offset % MinABIStackAlignInBytes) == 0);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003365
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003366 if ((Align - 1) & Offset)
3367 return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
3368
3369 return 0;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003370}
Akira Hatanaka9659d592012-01-10 22:44:52 +00003371
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003372ABIArgInfo
3373MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003374 uint64_t OrigOffset = Offset;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003375 uint64_t TySize = getContext().getTypeSize(Ty);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003376 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003377
Akira Hatanakac359f202012-07-03 19:24:06 +00003378 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
3379 (uint64_t)StackAlignInBytes);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003380 Offset = llvm::RoundUpToAlignment(Offset, Align);
3381 Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003382
Akira Hatanakac359f202012-07-03 19:24:06 +00003383 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
Akira Hatanaka619e8872011-06-02 00:09:17 +00003384 // Ignore empty aggregates.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003385 if (TySize == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00003386 return ABIArgInfo::getIgnore();
3387
Akira Hatanaka511949b2011-08-01 18:09:58 +00003388 // Records with non trivial destructors/constructors should not be passed
3389 // by value.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003390 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003391 Offset = OrigOffset + MinABIStackAlignInBytes;
Akira Hatanaka511949b2011-08-01 18:09:58 +00003392 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003393 }
Akira Hatanaka511949b2011-08-01 18:09:58 +00003394
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003395 // If we have reached here, aggregates are passed directly by coercing to
3396 // another structure type. Padding is inserted if the offset of the
3397 // aggregate is unaligned.
3398 return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
3399 getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00003400 }
3401
3402 // Treat an enum type as its underlying type.
3403 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3404 Ty = EnumTy->getDecl()->getIntegerType();
3405
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003406 if (Ty->isPromotableIntegerType())
3407 return ABIArgInfo::getExtend();
3408
3409 return ABIArgInfo::getDirect(0, 0, getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00003410}
3411
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003412llvm::Type*
3413MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakada54ff32012-02-09 18:49:26 +00003414 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakac359f202012-07-03 19:24:06 +00003415 SmallVector<llvm::Type*, 8> RTList;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003416
Akira Hatanakada54ff32012-02-09 18:49:26 +00003417 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003418 const RecordDecl *RD = RT->getDecl();
Akira Hatanakada54ff32012-02-09 18:49:26 +00003419 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3420 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003421
Akira Hatanakada54ff32012-02-09 18:49:26 +00003422 // N32/64 returns struct/classes in floating point registers if the
3423 // following conditions are met:
3424 // 1. The size of the struct/class is no larger than 128-bit.
3425 // 2. The struct/class has one or two fields all of which are floating
3426 // point types.
3427 // 3. The offset of the first field is zero (this follows what gcc does).
3428 //
3429 // Any other composite results are returned in integer registers.
3430 //
3431 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
3432 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
3433 for (; b != e; ++b) {
David Blaikie262bc182012-04-30 02:36:29 +00003434 const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003435
Akira Hatanakada54ff32012-02-09 18:49:26 +00003436 if (!BT || !BT->isFloatingPoint())
3437 break;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003438
David Blaikie262bc182012-04-30 02:36:29 +00003439 RTList.push_back(CGT.ConvertType(b->getType()));
Akira Hatanakada54ff32012-02-09 18:49:26 +00003440 }
3441
3442 if (b == e)
3443 return llvm::StructType::get(getVMContext(), RTList,
3444 RD->hasAttr<PackedAttr>());
3445
3446 RTList.clear();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003447 }
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003448 }
3449
Akira Hatanakac359f202012-07-03 19:24:06 +00003450 CoerceToIntArgs(Size, RTList);
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003451 return llvm::StructType::get(getVMContext(), RTList);
3452}
3453
Akira Hatanaka619e8872011-06-02 00:09:17 +00003454ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanakaa8536c02012-01-23 23:18:57 +00003455 uint64_t Size = getContext().getTypeSize(RetTy);
3456
3457 if (RetTy->isVoidType() || Size == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00003458 return ABIArgInfo::getIgnore();
3459
Akira Hatanaka8aeb1472012-05-11 21:01:17 +00003460 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003461 if (Size <= 128) {
3462 if (RetTy->isAnyComplexType())
3463 return ABIArgInfo::getDirect();
3464
Akira Hatanakac359f202012-07-03 19:24:06 +00003465 // O32 returns integer vectors in registers.
3466 if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())
3467 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
3468
Akira Hatanaka526cdfb2012-02-08 01:31:22 +00003469 if (!IsO32 && !isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003470 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
3471 }
Akira Hatanaka619e8872011-06-02 00:09:17 +00003472
3473 return ABIArgInfo::getIndirect(0);
3474 }
3475
3476 // Treat an enum type as its underlying type.
3477 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3478 RetTy = EnumTy->getDecl()->getIntegerType();
3479
3480 return (RetTy->isPromotableIntegerType() ?
3481 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3482}
3483
3484void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanakacc662542012-01-12 01:10:09 +00003485 ABIArgInfo &RetInfo = FI.getReturnInfo();
3486 RetInfo = classifyReturnType(FI.getReturnType());
3487
3488 // Check if a pointer to an aggregate is passed as a hidden argument.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003489 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
Akira Hatanakacc662542012-01-12 01:10:09 +00003490
Akira Hatanaka619e8872011-06-02 00:09:17 +00003491 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3492 it != ie; ++it)
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003493 it->info = classifyArgumentType(it->type, Offset);
Akira Hatanaka619e8872011-06-02 00:09:17 +00003494}
3495
3496llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3497 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003498 llvm::Type *BP = CGF.Int8PtrTy;
3499 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003500
3501 CGBuilderTy &Builder = CGF.Builder;
3502 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3503 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003504 int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003505 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3506 llvm::Value *AddrTyped;
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003507 unsigned PtrWidth = getContext().getTargetInfo().getPointerWidth(0);
3508 llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003509
3510 if (TypeAlign > MinABIStackAlignInBytes) {
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003511 llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
3512 llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
3513 llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
3514 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003515 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
3516 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
3517 }
3518 else
3519 AddrTyped = Builder.CreateBitCast(Addr, PTy);
3520
3521 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003522 TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003523 uint64_t Offset =
3524 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
3525 llvm::Value *NextAddr =
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003526 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003527 "ap.next");
3528 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3529
3530 return AddrTyped;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003531}
3532
John McCallaeeb7012010-05-27 06:19:26 +00003533bool
3534MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3535 llvm::Value *Address) const {
3536 // This information comes from gcc's implementation, which seems to
3537 // as canonical as it gets.
3538
John McCallaeeb7012010-05-27 06:19:26 +00003539 // Everything on MIPS is 4 bytes. Double-precision FP registers
3540 // are aliased to pairs of single-precision FP registers.
Chris Lattner8b418682012-02-07 00:39:47 +00003541 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCallaeeb7012010-05-27 06:19:26 +00003542
3543 // 0-31 are the general purpose registers, $0 - $31.
3544 // 32-63 are the floating-point registers, $f0 - $f31.
3545 // 64 and 65 are the multiply/divide registers, $hi and $lo.
3546 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattner8b418682012-02-07 00:39:47 +00003547 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCallaeeb7012010-05-27 06:19:26 +00003548
3549 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
3550 // They are one bit wide and ignored here.
3551
3552 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
3553 // (coprocessor 1 is the FP unit)
3554 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
3555 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
3556 // 176-181 are the DSP accumulator registers.
Chris Lattner8b418682012-02-07 00:39:47 +00003557 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCallaeeb7012010-05-27 06:19:26 +00003558 return false;
3559}
3560
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003561//===----------------------------------------------------------------------===//
3562// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
3563// Currently subclassed only to implement custom OpenCL C function attribute
3564// handling.
3565//===----------------------------------------------------------------------===//
3566
3567namespace {
3568
3569class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
3570public:
3571 TCETargetCodeGenInfo(CodeGenTypes &CGT)
3572 : DefaultTargetCodeGenInfo(CGT) {}
3573
3574 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3575 CodeGen::CodeGenModule &M) const;
3576};
3577
3578void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3579 llvm::GlobalValue *GV,
3580 CodeGen::CodeGenModule &M) const {
3581 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3582 if (!FD) return;
3583
3584 llvm::Function *F = cast<llvm::Function>(GV);
3585
David Blaikie4e4d0842012-03-11 07:00:24 +00003586 if (M.getLangOpts().OpenCL) {
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003587 if (FD->hasAttr<OpenCLKernelAttr>()) {
3588 // OpenCL C Kernel functions are not subject to inlining
3589 F->addFnAttr(llvm::Attribute::NoInline);
3590
3591 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
3592
3593 // Convert the reqd_work_group_size() attributes to metadata.
3594 llvm::LLVMContext &Context = F->getContext();
3595 llvm::NamedMDNode *OpenCLMetadata =
3596 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
3597
3598 SmallVector<llvm::Value*, 5> Operands;
3599 Operands.push_back(F);
3600
Chris Lattner8b418682012-02-07 00:39:47 +00003601 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
3602 llvm::APInt(32,
3603 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
3604 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
3605 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003606 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
Chris Lattner8b418682012-02-07 00:39:47 +00003607 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
3608 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003609 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
3610
3611 // Add a boolean constant operand for "required" (true) or "hint" (false)
3612 // for implementing the work_group_size_hint attr later. Currently
3613 // always true as the hint is not yet implemented.
Chris Lattner8b418682012-02-07 00:39:47 +00003614 Operands.push_back(llvm::ConstantInt::getTrue(Context));
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003615 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
3616 }
3617 }
3618 }
3619}
3620
3621}
John McCallaeeb7012010-05-27 06:19:26 +00003622
Tony Linthicum96319392011-12-12 21:14:55 +00003623//===----------------------------------------------------------------------===//
3624// Hexagon ABI Implementation
3625//===----------------------------------------------------------------------===//
3626
3627namespace {
3628
3629class HexagonABIInfo : public ABIInfo {
3630
3631
3632public:
3633 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3634
3635private:
3636
3637 ABIArgInfo classifyReturnType(QualType RetTy) const;
3638 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3639
3640 virtual void computeInfo(CGFunctionInfo &FI) const;
3641
3642 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3643 CodeGenFunction &CGF) const;
3644};
3645
3646class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
3647public:
3648 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
3649 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
3650
3651 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3652 return 29;
3653 }
3654};
3655
3656}
3657
3658void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
3659 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3660 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3661 it != ie; ++it)
3662 it->info = classifyArgumentType(it->type);
3663}
3664
3665ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
3666 if (!isAggregateTypeForABI(Ty)) {
3667 // Treat an enum type as its underlying type.
3668 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3669 Ty = EnumTy->getDecl()->getIntegerType();
3670
3671 return (Ty->isPromotableIntegerType() ?
3672 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3673 }
3674
3675 // Ignore empty records.
3676 if (isEmptyRecord(getContext(), Ty, true))
3677 return ABIArgInfo::getIgnore();
3678
3679 // Structures with either a non-trivial destructor or a non-trivial
3680 // copy constructor are always indirect.
3681 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
3682 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3683
3684 uint64_t Size = getContext().getTypeSize(Ty);
3685 if (Size > 64)
3686 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
3687 // Pass in the smallest viable integer type.
3688 else if (Size > 32)
3689 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
3690 else if (Size > 16)
3691 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
3692 else if (Size > 8)
3693 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3694 else
3695 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
3696}
3697
3698ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
3699 if (RetTy->isVoidType())
3700 return ABIArgInfo::getIgnore();
3701
3702 // Large vector types should be returned via memory.
3703 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
3704 return ABIArgInfo::getIndirect(0);
3705
3706 if (!isAggregateTypeForABI(RetTy)) {
3707 // Treat an enum type as its underlying type.
3708 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3709 RetTy = EnumTy->getDecl()->getIntegerType();
3710
3711 return (RetTy->isPromotableIntegerType() ?
3712 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3713 }
3714
3715 // Structures with either a non-trivial destructor or a non-trivial
3716 // copy constructor are always indirect.
3717 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
3718 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3719
3720 if (isEmptyRecord(getContext(), RetTy, true))
3721 return ABIArgInfo::getIgnore();
3722
3723 // Aggregates <= 8 bytes are returned in r0; other aggregates
3724 // are returned indirectly.
3725 uint64_t Size = getContext().getTypeSize(RetTy);
3726 if (Size <= 64) {
3727 // Return in the smallest viable integer type.
3728 if (Size <= 8)
3729 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
3730 if (Size <= 16)
3731 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3732 if (Size <= 32)
3733 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
3734 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
3735 }
3736
3737 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
3738}
3739
3740llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner8b418682012-02-07 00:39:47 +00003741 CodeGenFunction &CGF) const {
Tony Linthicum96319392011-12-12 21:14:55 +00003742 // FIXME: Need to handle alignment
Chris Lattner8b418682012-02-07 00:39:47 +00003743 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Tony Linthicum96319392011-12-12 21:14:55 +00003744
3745 CGBuilderTy &Builder = CGF.Builder;
3746 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
3747 "ap");
3748 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
3749 llvm::Type *PTy =
3750 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3751 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3752
3753 uint64_t Offset =
3754 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
3755 llvm::Value *NextAddr =
3756 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
3757 "ap.next");
3758 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3759
3760 return AddrTyped;
3761}
3762
3763
Chris Lattnerea044322010-07-29 02:01:43 +00003764const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003765 if (TheTargetCodeGenInfo)
3766 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003767
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003768 const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00003769 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003770 default:
Chris Lattnerea044322010-07-29 02:01:43 +00003771 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003772
John McCallaeeb7012010-05-27 06:19:26 +00003773 case llvm::Triple::mips:
3774 case llvm::Triple::mipsel:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003775 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCallaeeb7012010-05-27 06:19:26 +00003776
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00003777 case llvm::Triple::mips64:
3778 case llvm::Triple::mips64el:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003779 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00003780
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003781 case llvm::Triple::arm:
3782 case llvm::Triple::thumb:
Sandeep Patel34c1af82011-04-05 00:23:47 +00003783 {
3784 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003785
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003786 if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
Sandeep Patel34c1af82011-04-05 00:23:47 +00003787 Kind = ARMABIInfo::APCS;
3788 else if (CodeGenOpts.FloatABI == "hard")
3789 Kind = ARMABIInfo::AAPCS_VFP;
3790
3791 return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind));
3792 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003793
John McCallec853ba2010-03-11 00:10:12 +00003794 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00003795 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
Roman Divacky0fbc4b92012-05-09 18:22:46 +00003796 case llvm::Triple::ppc64:
3797 return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00003798
Peter Collingbourneedb66f32012-05-20 23:28:41 +00003799 case llvm::Triple::nvptx:
3800 case llvm::Triple::nvptx64:
Justin Holewinski2c585b92012-05-24 17:43:12 +00003801 return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types));
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003802
Wesley Peck276fdf42010-12-19 19:57:51 +00003803 case llvm::Triple::mblaze:
3804 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
3805
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003806 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00003807 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003808
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003809 case llvm::Triple::tce:
3810 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
3811
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003812 case llvm::Triple::x86: {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003813 bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003814
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00003815 if (Triple.isOSDarwin())
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003816 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00003817 new X86_32TargetCodeGenInfo(Types, true, true, DisableMMX, false,
3818 CodeGenOpts.NumRegisterParameters));
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00003819
3820 switch (Triple.getOS()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003821 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003822 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00003823 case llvm::Triple::AuroraUX:
3824 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00003825 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003826 case llvm::Triple::OpenBSD:
Eli Friedman42f74f22012-08-08 23:57:20 +00003827 case llvm::Triple::Bitrig:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003828 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00003829 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX,
3830 false,
3831 CodeGenOpts.NumRegisterParameters));
Eli Friedman55fc7e22012-01-25 22:46:34 +00003832
3833 case llvm::Triple::Win32:
3834 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00003835 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX, true,
3836 CodeGenOpts.NumRegisterParameters));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003837
3838 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003839 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00003840 new X86_32TargetCodeGenInfo(Types, false, false, DisableMMX,
3841 false,
3842 CodeGenOpts.NumRegisterParameters));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003843 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003844 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003845
Eli Friedmanee1ad992011-12-02 00:11:43 +00003846 case llvm::Triple::x86_64: {
3847 bool HasAVX = strcmp(getContext().getTargetInfo().getABI(), "avx") == 0;
3848
Chris Lattnerf13721d2010-08-31 16:44:54 +00003849 switch (Triple.getOS()) {
3850 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00003851 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00003852 case llvm::Triple::Cygwin:
3853 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
3854 default:
Eli Friedmanee1ad992011-12-02 00:11:43 +00003855 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
3856 HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00003857 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003858 }
Tony Linthicum96319392011-12-12 21:14:55 +00003859 case llvm::Triple::hexagon:
3860 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
Eli Friedmanee1ad992011-12-02 00:11:43 +00003861 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003862}