blob: 3ed1778cc90fb9160ab1306a47bd44153cbe0646 [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)
Daniel Dunbar98303b92009-09-13 08:03:58 +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) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000232 const FieldDecl *FD = *i;
233 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) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000304 const FieldDecl *FD = *i;
305
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 {
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000416 static const unsigned MinABIStackAlignInBytes = 4;
417
David Chisnall1e4249c2009-08-17 23:08:21 +0000418 bool IsDarwinVectorABI;
419 bool IsSmallStructInRegABI;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000420 bool IsMMXDisabled;
Eli Friedman55fc7e22012-01-25 22:46:34 +0000421 bool IsWin32FloatStructABI;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000422
423 static bool isRegisterSize(unsigned Size) {
424 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
425 }
426
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000427 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context,
428 unsigned callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000429
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000430 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
431 /// such that the argument will be passed in memory.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000432 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000433
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000434 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbare59d8582010-09-16 20:42:06 +0000435 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000436
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000437public:
Chris Lattnerea044322010-07-29 02:01:43 +0000438
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000439 ABIArgInfo classifyReturnType(QualType RetTy,
440 unsigned callingConvention) const;
Chris Lattnera3c109b2010-07-29 02:16:43 +0000441 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000442
Chris Lattneree5dcd02010-07-29 02:31:05 +0000443 virtual void computeInfo(CGFunctionInfo &FI) const {
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000444 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
445 FI.getCallingConvention());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000446 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
447 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000448 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000449 }
450
451 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
452 CodeGenFunction &CGF) const;
453
Eli Friedman55fc7e22012-01-25 22:46:34 +0000454 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m, bool w)
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000455 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
Eli Friedman55fc7e22012-01-25 22:46:34 +0000456 IsMMXDisabled(m), IsWin32FloatStructABI(w) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000457};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000458
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000459class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
460public:
Eli Friedman55fc7e22012-01-25 22:46:34 +0000461 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
462 bool d, bool p, bool m, bool w)
463 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, m, w)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000464
465 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
466 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000467
468 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
469 // Darwin uses different dwarf register numbers for EH.
470 if (CGM.isTargetDarwin()) return 5;
471
472 return 4;
473 }
474
475 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
476 llvm::Value *Address) const;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000477
Jay Foadef6de3d2011-07-11 09:56:20 +0000478 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000479 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000480 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000481 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
482 }
483
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000484};
485
486}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000487
488/// shouldReturnTypeInRegister - Determine if the given type should be
489/// passed in a register (for the Darwin ABI).
490bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000491 ASTContext &Context,
492 unsigned callingConvention) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000493 uint64_t Size = Context.getTypeSize(Ty);
494
495 // Type must be register sized.
496 if (!isRegisterSize(Size))
497 return false;
498
499 if (Ty->isVectorType()) {
500 // 64- and 128- bit vectors inside structures are not returned in
501 // registers.
502 if (Size == 64 || Size == 128)
503 return false;
504
505 return true;
506 }
507
Daniel Dunbar77115232010-05-15 00:00:30 +0000508 // If this is a builtin, pointer, enum, complex type, member pointer, or
509 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000510 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000511 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000512 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000513 return true;
514
515 // Arrays are treated like records.
516 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000517 return shouldReturnTypeInRegister(AT->getElementType(), Context,
518 callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000519
520 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000521 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000522 if (!RT) return false;
523
Anders Carlssona8874232010-01-27 03:25:19 +0000524 // FIXME: Traverse bases here too.
525
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000526 // For thiscall conventions, structures will never be returned in
527 // a register. This is for compatibility with the MSVC ABI
528 if (callingConvention == llvm::CallingConv::X86_ThisCall &&
529 RT->isStructureType()) {
530 return false;
531 }
532
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000533 // Structure types are passed in register if all fields would be
534 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000535 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
536 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000537 const FieldDecl *FD = *i;
538
539 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000540 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000541 continue;
542
543 // Check fields recursively.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000544 if (!shouldReturnTypeInRegister(FD->getType(), Context,
545 callingConvention))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000546 return false;
547 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000548 return true;
549}
550
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000551ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
552 unsigned callingConvention) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000553 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000554 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000555
Chris Lattnera3c109b2010-07-29 02:16:43 +0000556 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000557 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000558 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000559 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000560
561 // 128-bit vectors are a special case; they are returned in
562 // registers and we need to make sure to pick a type the LLVM
563 // backend will like.
564 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +0000565 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000566 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000567
568 // Always return in register if it fits in a general purpose
569 // register, or if it is 64 bits and has a single element.
570 if ((Size == 8 || Size == 16 || Size == 32) ||
571 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +0000572 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000573 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000574
575 return ABIArgInfo::getIndirect(0);
576 }
577
578 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000579 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000580
John McCalld608cdb2010-08-22 10:59:02 +0000581 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000582 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000583 // Structures with either a non-trivial destructor or a non-trivial
584 // copy constructor are always indirect.
585 if (hasNonTrivialDestructorOrCopyConstructor(RT))
586 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000587
Anders Carlsson40092972009-10-20 22:07:59 +0000588 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000589 if (RT->getDecl()->hasFlexibleArrayMember())
590 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000591 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000592
David Chisnall1e4249c2009-08-17 23:08:21 +0000593 // If specified, structs and unions are always indirect.
594 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000595 return ABIArgInfo::getIndirect(0);
596
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000597 // Small structures which are register sized are generally returned
598 // in a register.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000599 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(),
600 callingConvention)) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000601 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000602
603 // As a special-case, if the struct is a "single-element" struct, and
604 // the field is of type "float" or "double", return it in a
Eli Friedman55fc7e22012-01-25 22:46:34 +0000605 // floating-point register. (MSVC does not apply this special case.)
606 // We apply a similar transformation for pointer types to improve the
607 // quality of the generated IR.
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000608 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
Eli Friedman55fc7e22012-01-25 22:46:34 +0000609 if ((!IsWin32FloatStructABI && SeltTy->isRealFloatingType())
610 || SeltTy->hasPointerRepresentation())
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000611 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
612
613 // FIXME: We should be able to narrow this integer in cases with dead
614 // padding.
Chris Lattner800588f2010-07-29 06:26:06 +0000615 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000616 }
617
618 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000619 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000620
Chris Lattnera3c109b2010-07-29 02:16:43 +0000621 // Treat an enum type as its underlying type.
622 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
623 RetTy = EnumTy->getDecl()->getIntegerType();
624
625 return (RetTy->isPromotableIntegerType() ?
626 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000627}
628
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000629static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
630 const RecordType *RT = Ty->getAs<RecordType>();
631 if (!RT)
632 return 0;
633 const RecordDecl *RD = RT->getDecl();
634
635 // If this is a C++ record, check the bases first.
636 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
637 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
638 e = CXXRD->bases_end(); i != e; ++i)
639 if (!isRecordWithSSEVectorType(Context, i->getType()))
640 return false;
641
642 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
643 i != e; ++i) {
644 QualType FT = i->getType();
645
Eli Friedman7b1fb812011-11-18 02:12:09 +0000646 if (FT->getAs<VectorType>() && Context.getTypeSize(FT) == 128)
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000647 return true;
648
649 if (isRecordWithSSEVectorType(Context, FT))
650 return true;
651 }
652
653 return false;
654}
655
Daniel Dunbare59d8582010-09-16 20:42:06 +0000656unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
657 unsigned Align) const {
658 // Otherwise, if the alignment is less than or equal to the minimum ABI
659 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000660 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +0000661 return 0; // Use default alignment.
662
663 // On non-Darwin, the stack type alignment is always 4.
664 if (!IsDarwinVectorABI) {
665 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000666 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +0000667 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000668
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000669 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedman7b1fb812011-11-18 02:12:09 +0000670 if (Align >= 16 && isRecordWithSSEVectorType(getContext(), Ty))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000671 return 16;
672
673 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000674}
675
Chris Lattnera3c109b2010-07-29 02:16:43 +0000676ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000677 if (!ByVal)
678 return ABIArgInfo::getIndirect(0, false);
679
Daniel Dunbare59d8582010-09-16 20:42:06 +0000680 // Compute the byval alignment.
681 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
682 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
683 if (StackAlign == 0)
Chris Lattnerde92d732011-05-22 23:35:00 +0000684 return ABIArgInfo::getIndirect(4);
Daniel Dunbare59d8582010-09-16 20:42:06 +0000685
686 // If the stack alignment is less than the type alignment, realign the
687 // argument.
688 if (StackAlign < TypeAlign)
689 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
690 /*Realign=*/true);
691
692 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000693}
694
Chris Lattnera3c109b2010-07-29 02:16:43 +0000695ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000696 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000697 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000698 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000699 if (const RecordType *RT = Ty->getAs<RecordType>()) {
700 // Structures with either a non-trivial destructor or a non-trivial
701 // copy constructor are always indirect.
702 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Chris Lattnera3c109b2010-07-29 02:16:43 +0000703 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000704
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000705 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattnera3c109b2010-07-29 02:16:43 +0000706 return getIndirectResult(Ty);
Anders Carlssona8874232010-01-27 03:25:19 +0000707 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000708
Eli Friedman5a4d3522011-11-18 00:28:11 +0000709 // Ignore empty structs/unions.
Eli Friedman5a1ac892011-11-18 04:01:36 +0000710 if (isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000711 return ABIArgInfo::getIgnore();
712
Daniel Dunbar53012f42009-11-09 01:33:53 +0000713 // Expand small (<= 128-bit) record types when we know that the stack layout
714 // of those arguments will match the struct. This is important because the
715 // LLVM backend isn't smart enough to remove byval, which inhibits many
716 // optimizations.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000717 if (getContext().getTypeSize(Ty) <= 4*32 &&
718 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar53012f42009-11-09 01:33:53 +0000719 return ABIArgInfo::getExpand();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000720
Chris Lattnera3c109b2010-07-29 02:16:43 +0000721 return getIndirectResult(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000722 }
723
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000724 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +0000725 // On Darwin, some vectors are passed in memory, we handle this by passing
726 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000727 if (IsDarwinVectorABI) {
728 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000729 if ((Size == 8 || Size == 16 || Size == 32) ||
730 (Size == 64 && VT->getNumElements() == 1))
731 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
732 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000733 }
Bill Wendlingbb465d72010-10-18 03:41:31 +0000734
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000735 llvm::Type *IRType = CGT.ConvertType(Ty);
Bill Wendlingbb465d72010-10-18 03:41:31 +0000736 if (UseX86_MMXType(IRType)) {
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000737 if (IsMMXDisabled)
738 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
739 64));
Bill Wendlingbb465d72010-10-18 03:41:31 +0000740 ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
741 AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
742 return AAI;
743 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000744
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000745 return ABIArgInfo::getDirect();
746 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000747
748
Chris Lattnera3c109b2010-07-29 02:16:43 +0000749 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
750 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000751
Chris Lattnera3c109b2010-07-29 02:16:43 +0000752 return (Ty->isPromotableIntegerType() ?
753 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000754}
755
756llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
757 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +0000758 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000759
760 CGBuilderTy &Builder = CGF.Builder;
761 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
762 "ap");
763 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Eli Friedman7b1fb812011-11-18 02:12:09 +0000764
765 // Compute if the address needs to be aligned
766 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
767 Align = getTypeStackAlignInBytes(Ty, Align);
768 Align = std::max(Align, 4U);
769 if (Align > 4) {
770 // addr = (addr + align - 1) & -align;
771 llvm::Value *Offset =
772 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
773 Addr = CGF.Builder.CreateGEP(Addr, Offset);
774 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
775 CGF.Int32Ty);
776 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
777 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
778 Addr->getType(),
779 "ap.cur.aligned");
780 }
781
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000782 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000783 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000784 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
785
786 uint64_t Offset =
Eli Friedman7b1fb812011-11-18 02:12:09 +0000787 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000788 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +0000789 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000790 "ap.next");
791 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
792
793 return AddrTyped;
794}
795
Charles Davis74f72932010-02-13 15:54:06 +0000796void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
797 llvm::GlobalValue *GV,
798 CodeGen::CodeGenModule &CGM) const {
799 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
800 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
801 // Get the LLVM function.
802 llvm::Function *Fn = cast<llvm::Function>(GV);
803
804 // Now add the 'alignstack' attribute with a value of 16.
805 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
806 }
807 }
808}
809
John McCall6374c332010-03-06 00:35:14 +0000810bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
811 CodeGen::CodeGenFunction &CGF,
812 llvm::Value *Address) const {
813 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCall6374c332010-03-06 00:35:14 +0000814
Chris Lattner8b418682012-02-07 00:39:47 +0000815 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000816
John McCall6374c332010-03-06 00:35:14 +0000817 // 0-7 are the eight integer registers; the order is different
818 // on Darwin (for EH), but the range is the same.
819 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +0000820 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +0000821
822 if (CGF.CGM.isTargetDarwin()) {
823 // 12-16 are st(0..4). Not sure why we stop at 4.
824 // These have size 16, which is sizeof(long double) on
825 // platforms with 8-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +0000826 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCallaeeb7012010-05-27 06:19:26 +0000827 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000828
John McCall6374c332010-03-06 00:35:14 +0000829 } else {
830 // 9 is %eflags, which doesn't get a size on Darwin for some
831 // reason.
832 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
833
834 // 11-16 are st(0..5). Not sure why we stop at 5.
835 // These have size 12, which is sizeof(long double) on
836 // platforms with 4-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +0000837 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCallaeeb7012010-05-27 06:19:26 +0000838 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
839 }
John McCall6374c332010-03-06 00:35:14 +0000840
841 return false;
842}
843
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000844//===----------------------------------------------------------------------===//
845// X86-64 ABI Implementation
846//===----------------------------------------------------------------------===//
847
848
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000849namespace {
850/// X86_64ABIInfo - The X86_64 ABI information.
851class X86_64ABIInfo : public ABIInfo {
852 enum Class {
853 Integer = 0,
854 SSE,
855 SSEUp,
856 X87,
857 X87Up,
858 ComplexX87,
859 NoClass,
860 Memory
861 };
862
863 /// merge - Implement the X86_64 ABI merging algorithm.
864 ///
865 /// Merge an accumulating classification \arg Accum with a field
866 /// classification \arg Field.
867 ///
868 /// \param Accum - The accumulating classification. This should
869 /// always be either NoClass or the result of a previous merge
870 /// call. In addition, this should never be Memory (the caller
871 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +0000872 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000873
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +0000874 /// postMerge - Implement the X86_64 ABI post merging algorithm.
875 ///
876 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
877 /// final MEMORY or SSE classes when necessary.
878 ///
879 /// \param AggregateSize - The size of the current aggregate in
880 /// the classification process.
881 ///
882 /// \param Lo - The classification for the parts of the type
883 /// residing in the low word of the containing object.
884 ///
885 /// \param Hi - The classification for the parts of the type
886 /// residing in the higher words of the containing object.
887 ///
888 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
889
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000890 /// classify - Determine the x86_64 register classes in which the
891 /// given type T should be passed.
892 ///
893 /// \param Lo - The classification for the parts of the type
894 /// residing in the low word of the containing object.
895 ///
896 /// \param Hi - The classification for the parts of the type
897 /// residing in the high word of the containing object.
898 ///
899 /// \param OffsetBase - The bit offset of this type in the
900 /// containing object. Some parameters are classified different
901 /// depending on whether they straddle an eightbyte boundary.
902 ///
903 /// If a word is unused its result will be NoClass; if a type should
904 /// be passed in Memory then at least the classification of \arg Lo
905 /// will be Memory.
906 ///
907 /// The \arg Lo class will be NoClass iff the argument is ignored.
908 ///
909 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
910 /// also be ComplexX87.
Chris Lattner9c254f02010-06-29 06:01:59 +0000911 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000912
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +0000913 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000914 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
915 unsigned IROffset, QualType SourceTy,
916 unsigned SourceOffset) const;
917 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
918 unsigned IROffset, QualType SourceTy,
919 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000920
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000921 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000922 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000923 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000924
925 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000926 /// such that the argument will be passed in memory.
Daniel Dunbaredfac032012-03-10 01:03:58 +0000927 ///
928 /// \param freeIntRegs - The number of free integer registers remaining
929 /// available.
930 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000931
Chris Lattnera3c109b2010-07-29 02:16:43 +0000932 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000933
Bill Wendlingbb465d72010-10-18 03:41:31 +0000934 ABIArgInfo classifyArgumentType(QualType Ty,
Daniel Dunbaredfac032012-03-10 01:03:58 +0000935 unsigned freeIntRegs,
Bill Wendlingbb465d72010-10-18 03:41:31 +0000936 unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +0000937 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000938
Eli Friedmanee1ad992011-12-02 00:11:43 +0000939 bool IsIllegalVectorType(QualType Ty) const;
940
John McCall67a57732011-04-21 01:20:55 +0000941 /// The 0.98 ABI revision clarified a lot of ambiguities,
942 /// unfortunately in ways that were not always consistent with
943 /// certain previous compilers. In particular, platforms which
944 /// required strict binary compatibility with older versions of GCC
945 /// may need to exempt themselves.
946 bool honorsRevision0_98() const {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000947 return !getContext().getTargetInfo().getTriple().isOSDarwin();
John McCall67a57732011-04-21 01:20:55 +0000948 }
949
Eli Friedmanee1ad992011-12-02 00:11:43 +0000950 bool HasAVX;
951
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000952public:
Eli Friedmanee1ad992011-12-02 00:11:43 +0000953 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
954 ABIInfo(CGT), HasAVX(hasavx) {}
Chris Lattner9c254f02010-06-29 06:01:59 +0000955
John McCallde5d3c72012-02-17 03:33:10 +0000956 bool isPassedUsingAVXType(QualType type) const {
957 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +0000958 // The freeIntRegs argument doesn't matter here.
959 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE);
John McCallde5d3c72012-02-17 03:33:10 +0000960 if (info.isDirect()) {
961 llvm::Type *ty = info.getCoerceToType();
962 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
963 return (vectorTy->getBitWidth() > 128);
964 }
965 return false;
966 }
967
Chris Lattneree5dcd02010-07-29 02:31:05 +0000968 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000969
970 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
971 CodeGenFunction &CGF) const;
972};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000973
Chris Lattnerf13721d2010-08-31 16:44:54 +0000974/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +0000975class WinX86_64ABIInfo : public ABIInfo {
976
977 ABIArgInfo classify(QualType Ty) const;
978
Chris Lattnerf13721d2010-08-31 16:44:54 +0000979public:
NAKAMURA Takumia7573222011-01-17 22:56:31 +0000980 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
981
982 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattnerf13721d2010-08-31 16:44:54 +0000983
984 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
985 CodeGenFunction &CGF) const;
986};
987
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000988class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
989public:
Eli Friedmanee1ad992011-12-02 00:11:43 +0000990 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
991 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
John McCall6374c332010-03-06 00:35:14 +0000992
John McCallde5d3c72012-02-17 03:33:10 +0000993 const X86_64ABIInfo &getABIInfo() const {
994 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
995 }
996
John McCall6374c332010-03-06 00:35:14 +0000997 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
998 return 7;
999 }
1000
1001 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1002 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001003 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001004
John McCallaeeb7012010-05-27 06:19:26 +00001005 // 0-15 are the 16 integer registers.
1006 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001007 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +00001008 return false;
1009 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001010
Jay Foadef6de3d2011-07-11 09:56:20 +00001011 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001012 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +00001013 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001014 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1015 }
1016
John McCallde5d3c72012-02-17 03:33:10 +00001017 bool isNoProtoCallVariadic(const CallArgList &args,
1018 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +00001019 // The default CC on x86-64 sets %al to the number of SSA
1020 // registers used, and GCC sets this when calling an unprototyped
Eli Friedman3ed79032011-12-01 04:53:19 +00001021 // function, so we override the default behavior. However, don't do
Eli Friedman68805fe2011-12-06 03:08:26 +00001022 // that when AVX types are involved: the ABI explicitly states it is
1023 // undefined, and it doesn't work in practice because of how the ABI
1024 // defines varargs anyway.
John McCallde5d3c72012-02-17 03:33:10 +00001025 if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) {
Eli Friedman3ed79032011-12-01 04:53:19 +00001026 bool HasAVXType = false;
John McCallde5d3c72012-02-17 03:33:10 +00001027 for (CallArgList::const_iterator
1028 it = args.begin(), ie = args.end(); it != ie; ++it) {
1029 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1030 HasAVXType = true;
1031 break;
Eli Friedman3ed79032011-12-01 04:53:19 +00001032 }
1033 }
John McCallde5d3c72012-02-17 03:33:10 +00001034
Eli Friedman3ed79032011-12-01 04:53:19 +00001035 if (!HasAVXType)
1036 return true;
1037 }
John McCall01f151e2011-09-21 08:08:30 +00001038
John McCallde5d3c72012-02-17 03:33:10 +00001039 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
John McCall01f151e2011-09-21 08:08:30 +00001040 }
1041
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001042};
1043
Chris Lattnerf13721d2010-08-31 16:44:54 +00001044class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1045public:
1046 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1047 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1048
1049 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1050 return 7;
1051 }
1052
1053 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1054 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001055 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001056
Chris Lattnerf13721d2010-08-31 16:44:54 +00001057 // 0-15 are the 16 integer registers.
1058 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001059 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattnerf13721d2010-08-31 16:44:54 +00001060 return false;
1061 }
1062};
1063
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001064}
1065
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001066void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1067 Class &Hi) const {
1068 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1069 //
1070 // (a) If one of the classes is Memory, the whole argument is passed in
1071 // memory.
1072 //
1073 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1074 // memory.
1075 //
1076 // (c) If the size of the aggregate exceeds two eightbytes and the first
1077 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1078 // argument is passed in memory. NOTE: This is necessary to keep the
1079 // ABI working for processors that don't support the __m256 type.
1080 //
1081 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1082 //
1083 // Some of these are enforced by the merging logic. Others can arise
1084 // only with unions; for example:
1085 // union { _Complex double; unsigned; }
1086 //
1087 // Note that clauses (b) and (c) were added in 0.98.
1088 //
1089 if (Hi == Memory)
1090 Lo = Memory;
1091 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1092 Lo = Memory;
1093 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1094 Lo = Memory;
1095 if (Hi == SSEUp && Lo != SSE)
1096 Hi = SSE;
1097}
1098
Chris Lattner1090a9b2010-06-28 21:43:59 +00001099X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001100 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1101 // classified recursively so that always two fields are
1102 // considered. The resulting class is calculated according to
1103 // the classes of the fields in the eightbyte:
1104 //
1105 // (a) If both classes are equal, this is the resulting class.
1106 //
1107 // (b) If one of the classes is NO_CLASS, the resulting class is
1108 // the other class.
1109 //
1110 // (c) If one of the classes is MEMORY, the result is the MEMORY
1111 // class.
1112 //
1113 // (d) If one of the classes is INTEGER, the result is the
1114 // INTEGER.
1115 //
1116 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1117 // MEMORY is used as class.
1118 //
1119 // (f) Otherwise class SSE is used.
1120
1121 // Accum should never be memory (we should have returned) or
1122 // ComplexX87 (because this cannot be passed in a structure).
1123 assert((Accum != Memory && Accum != ComplexX87) &&
1124 "Invalid accumulated classification during merge.");
1125 if (Accum == Field || Field == NoClass)
1126 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001127 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001128 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001129 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001130 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001131 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001132 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001133 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1134 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001135 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001136 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001137}
1138
Chris Lattnerbcaedae2010-06-30 19:14:05 +00001139void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001140 Class &Lo, Class &Hi) const {
1141 // FIXME: This code can be simplified by introducing a simple value class for
1142 // Class pairs with appropriate constructor methods for the various
1143 // situations.
1144
1145 // FIXME: Some of the split computations are wrong; unaligned vectors
1146 // shouldn't be passed in registers for example, so there is no chance they
1147 // can straddle an eightbyte. Verify & simplify.
1148
1149 Lo = Hi = NoClass;
1150
1151 Class &Current = OffsetBase < 64 ? Lo : Hi;
1152 Current = Memory;
1153
John McCall183700f2009-09-21 23:43:11 +00001154 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001155 BuiltinType::Kind k = BT->getKind();
1156
1157 if (k == BuiltinType::Void) {
1158 Current = NoClass;
1159 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1160 Lo = Integer;
1161 Hi = Integer;
1162 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1163 Current = Integer;
1164 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
1165 Current = SSE;
1166 } else if (k == BuiltinType::LongDouble) {
1167 Lo = X87;
1168 Hi = X87Up;
1169 }
1170 // FIXME: _Decimal32 and _Decimal64 are SSE.
1171 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001172 return;
1173 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001174
Chris Lattner1090a9b2010-06-28 21:43:59 +00001175 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001176 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +00001177 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001178 return;
1179 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001180
Chris Lattner1090a9b2010-06-28 21:43:59 +00001181 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001182 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001183 return;
1184 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001185
Chris Lattner1090a9b2010-06-28 21:43:59 +00001186 if (Ty->isMemberPointerType()) {
Daniel Dunbar67d438d2010-05-15 00:00:37 +00001187 if (Ty->isMemberFunctionPointerType())
1188 Lo = Hi = Integer;
1189 else
1190 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001191 return;
1192 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001193
Chris Lattner1090a9b2010-06-28 21:43:59 +00001194 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001195 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001196 if (Size == 32) {
1197 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1198 // float> as integer.
1199 Current = Integer;
1200
1201 // If this type crosses an eightbyte boundary, it should be
1202 // split.
1203 uint64_t EB_Real = (OffsetBase) / 64;
1204 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1205 if (EB_Real != EB_Imag)
1206 Hi = Lo;
1207 } else if (Size == 64) {
1208 // gcc passes <1 x double> in memory. :(
1209 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1210 return;
1211
1212 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001213 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001214 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1215 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1216 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001217 Current = Integer;
1218 else
1219 Current = SSE;
1220
1221 // If this type crosses an eightbyte boundary, it should be
1222 // split.
1223 if (OffsetBase && OffsetBase != 64)
1224 Hi = Lo;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001225 } else if (Size == 128 || (HasAVX && Size == 256)) {
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001226 // Arguments of 256-bits are split into four eightbyte chunks. The
1227 // least significant one belongs to class SSE and all the others to class
1228 // SSEUP. The original Lo and Hi design considers that types can't be
1229 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1230 // This design isn't correct for 256-bits, but since there're no cases
1231 // where the upper parts would need to be inspected, avoid adding
1232 // complexity and just consider Hi to match the 64-256 part.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001233 Lo = SSE;
1234 Hi = SSEUp;
1235 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001236 return;
1237 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001238
Chris Lattner1090a9b2010-06-28 21:43:59 +00001239 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001240 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001241
Chris Lattnerea044322010-07-29 02:01:43 +00001242 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001243 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001244 if (Size <= 64)
1245 Current = Integer;
1246 else if (Size <= 128)
1247 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001248 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001249 Current = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001250 else if (ET == getContext().DoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001251 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001252 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001253 Current = ComplexX87;
1254
1255 // If this complex type crosses an eightbyte boundary then it
1256 // should be split.
1257 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001258 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001259 if (Hi == NoClass && EB_Real != EB_Imag)
1260 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001261
Chris Lattner1090a9b2010-06-28 21:43:59 +00001262 return;
1263 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001264
Chris Lattnerea044322010-07-29 02:01:43 +00001265 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001266 // Arrays are treated like structures.
1267
Chris Lattnerea044322010-07-29 02:01:43 +00001268 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001269
1270 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001271 // than four eightbytes, ..., it has class MEMORY.
1272 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001273 return;
1274
1275 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1276 // fields, it has class MEMORY.
1277 //
1278 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001279 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001280 return;
1281
1282 // Otherwise implement simplified merge. We could be smarter about
1283 // this, but it isn't worth it and would be harder to verify.
1284 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001285 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001286 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes089d8922011-07-12 01:27:38 +00001287
1288 // The only case a 256-bit wide vector could be used is when the array
1289 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1290 // to work for sizes wider than 128, early check and fallback to memory.
1291 if (Size > 128 && EltSize != 256)
1292 return;
1293
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001294 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1295 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001296 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001297 Lo = merge(Lo, FieldLo);
1298 Hi = merge(Hi, FieldHi);
1299 if (Lo == Memory || Hi == Memory)
1300 break;
1301 }
1302
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001303 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001304 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001305 return;
1306 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001307
Chris Lattner1090a9b2010-06-28 21:43:59 +00001308 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001309 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001310
1311 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001312 // than four eightbytes, ..., it has class MEMORY.
1313 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001314 return;
1315
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001316 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1317 // copy constructor or a non-trivial destructor, it is passed by invisible
1318 // reference.
1319 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1320 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001321
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001322 const RecordDecl *RD = RT->getDecl();
1323
1324 // Assume variable sized types are passed in memory.
1325 if (RD->hasFlexibleArrayMember())
1326 return;
1327
Chris Lattnerea044322010-07-29 02:01:43 +00001328 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001329
1330 // Reset Lo class, this will be recomputed.
1331 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001332
1333 // If this is a C++ record, classify the bases first.
1334 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1335 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1336 e = CXXRD->bases_end(); i != e; ++i) {
1337 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1338 "Unexpected base class!");
1339 const CXXRecordDecl *Base =
1340 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1341
1342 // Classify this field.
1343 //
1344 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1345 // single eightbyte, each is classified separately. Each eightbyte gets
1346 // initialized to class NO_CLASS.
1347 Class FieldLo, FieldHi;
Anders Carlssona14f5972010-10-31 23:22:37 +00001348 uint64_t Offset = OffsetBase + Layout.getBaseClassOffsetInBits(Base);
Chris Lattner9c254f02010-06-29 06:01:59 +00001349 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001350 Lo = merge(Lo, FieldLo);
1351 Hi = merge(Hi, FieldHi);
1352 if (Lo == Memory || Hi == Memory)
1353 break;
1354 }
1355 }
1356
1357 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001358 unsigned idx = 0;
Bruno Cardoso Lopes548e4782011-07-12 22:30:58 +00001359 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001360 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001361 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1362 bool BitField = i->isBitField();
1363
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001364 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1365 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001366 //
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001367 // The only case a 256-bit wide vector could be used is when the struct
1368 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1369 // to work for sizes wider than 128, early check and fallback to memory.
1370 //
1371 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1372 Lo = Memory;
1373 return;
1374 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001375 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001376 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001377 Lo = Memory;
1378 return;
1379 }
1380
1381 // Classify this field.
1382 //
1383 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1384 // exceeds a single eightbyte, each is classified
1385 // separately. Each eightbyte gets initialized to class
1386 // NO_CLASS.
1387 Class FieldLo, FieldHi;
1388
1389 // Bit-fields require special handling, they do not force the
1390 // structure to be passed in memory even if unaligned, and
1391 // therefore they can straddle an eightbyte.
1392 if (BitField) {
1393 // Ignore padding bit-fields.
1394 if (i->isUnnamedBitfield())
1395 continue;
1396
1397 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001398 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001399
1400 uint64_t EB_Lo = Offset / 64;
1401 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1402 FieldLo = FieldHi = NoClass;
1403 if (EB_Lo) {
1404 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1405 FieldLo = NoClass;
1406 FieldHi = Integer;
1407 } else {
1408 FieldLo = Integer;
1409 FieldHi = EB_Hi ? Integer : NoClass;
1410 }
1411 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001412 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001413 Lo = merge(Lo, FieldLo);
1414 Hi = merge(Hi, FieldHi);
1415 if (Lo == Memory || Hi == Memory)
1416 break;
1417 }
1418
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001419 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001420 }
1421}
1422
Chris Lattner9c254f02010-06-29 06:01:59 +00001423ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001424 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1425 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001426 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001427 // Treat an enum type as its underlying type.
1428 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1429 Ty = EnumTy->getDecl()->getIntegerType();
1430
1431 return (Ty->isPromotableIntegerType() ?
1432 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1433 }
1434
1435 return ABIArgInfo::getIndirect(0);
1436}
1437
Eli Friedmanee1ad992011-12-02 00:11:43 +00001438bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1439 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1440 uint64_t Size = getContext().getTypeSize(VecTy);
1441 unsigned LargestVector = HasAVX ? 256 : 128;
1442 if (Size <= 64 || Size > LargestVector)
1443 return true;
1444 }
1445
1446 return false;
1447}
1448
Daniel Dunbaredfac032012-03-10 01:03:58 +00001449ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1450 unsigned freeIntRegs) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001451 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1452 // place naturally.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001453 //
1454 // This assumption is optimistic, as there could be free registers available
1455 // when we need to pass this argument in memory, and LLVM could try to pass
1456 // the argument in the free register. This does not seem to happen currently,
1457 // but this code would be much safer if we could mark the argument with
1458 // 'onstack'. See PR12193.
Eli Friedmanee1ad992011-12-02 00:11:43 +00001459 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001460 // Treat an enum type as its underlying type.
1461 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1462 Ty = EnumTy->getDecl()->getIntegerType();
1463
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001464 return (Ty->isPromotableIntegerType() ?
1465 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001466 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001467
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001468 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1469 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001470
Chris Lattner855d2272011-05-22 23:21:23 +00001471 // Compute the byval alignment. We specify the alignment of the byval in all
1472 // cases so that the mid-level optimizer knows the alignment of the byval.
1473 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
Daniel Dunbaredfac032012-03-10 01:03:58 +00001474
1475 // Attempt to avoid passing indirect results using byval when possible. This
1476 // is important for good codegen.
1477 //
1478 // We do this by coercing the value into a scalar type which the backend can
1479 // handle naturally (i.e., without using byval).
1480 //
1481 // For simplicity, we currently only do this when we have exhausted all of the
1482 // free integer registers. Doing this when there are free integer registers
1483 // would require more care, as we would have to ensure that the coerced value
1484 // did not claim the unused register. That would require either reording the
1485 // arguments to the function (so that any subsequent inreg values came first),
1486 // or only doing this optimization when there were no following arguments that
1487 // might be inreg.
1488 //
1489 // We currently expect it to be rare (particularly in well written code) for
1490 // arguments to be passed on the stack when there are still free integer
1491 // registers available (this would typically imply large structs being passed
1492 // by value), so this seems like a fair tradeoff for now.
1493 //
1494 // We can revisit this if the backend grows support for 'onstack' parameter
1495 // attributes. See PR12193.
1496 if (freeIntRegs == 0) {
1497 uint64_t Size = getContext().getTypeSize(Ty);
1498
1499 // If this type fits in an eightbyte, coerce it into the matching integral
1500 // type, which will end up on the stack (with alignment 8).
1501 if (Align == 8 && Size <= 64)
1502 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1503 Size));
1504 }
1505
Chris Lattner855d2272011-05-22 23:21:23 +00001506 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001507}
1508
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001509/// GetByteVectorType - The ABI specifies that a value should be passed in an
1510/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner0f408f52010-07-29 04:56:46 +00001511/// vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001512llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001513 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001514
Chris Lattner15842bd2010-07-29 05:02:29 +00001515 // Wrapper structs that just contain vectors are passed just like vectors,
1516 // strip them off if present.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001517 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner15842bd2010-07-29 05:02:29 +00001518 while (STy && STy->getNumElements() == 1) {
1519 IRType = STy->getElementType(0);
1520 STy = dyn_cast<llvm::StructType>(IRType);
1521 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001522
Bruno Cardoso Lopes528a8c72011-07-08 22:57:35 +00001523 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001524 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1525 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001526 unsigned BitWidth = VT->getBitWidth();
Tanya Lattnerce275672011-11-28 23:18:11 +00001527 if ((BitWidth >= 128 && BitWidth <= 256) &&
Chris Lattner0f408f52010-07-29 04:56:46 +00001528 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1529 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1530 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1531 EltTy->isIntegerTy(128)))
1532 return VT;
1533 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001534
Chris Lattner0f408f52010-07-29 04:56:46 +00001535 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1536}
1537
Chris Lattnere2962be2010-07-29 07:30:00 +00001538/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1539/// is known to either be off the end of the specified type or being in
1540/// alignment padding. The user type specified is known to be at most 128 bits
1541/// in size, and have passed through X86_64ABIInfo::classify with a successful
1542/// classification that put one of the two halves in the INTEGER class.
1543///
1544/// It is conservatively correct to return false.
1545static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1546 unsigned EndBit, ASTContext &Context) {
1547 // If the bytes being queried are off the end of the type, there is no user
1548 // data hiding here. This handles analysis of builtins, vectors and other
1549 // types that don't contain interesting padding.
1550 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1551 if (TySize <= StartBit)
1552 return true;
1553
Chris Lattner021c3a32010-07-29 07:43:55 +00001554 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1555 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1556 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1557
1558 // Check each element to see if the element overlaps with the queried range.
1559 for (unsigned i = 0; i != NumElts; ++i) {
1560 // If the element is after the span we care about, then we're done..
1561 unsigned EltOffset = i*EltSize;
1562 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001563
Chris Lattner021c3a32010-07-29 07:43:55 +00001564 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1565 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1566 EndBit-EltOffset, Context))
1567 return false;
1568 }
1569 // If it overlaps no elements, then it is safe to process as padding.
1570 return true;
1571 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001572
Chris Lattnere2962be2010-07-29 07:30:00 +00001573 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1574 const RecordDecl *RD = RT->getDecl();
1575 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001576
Chris Lattnere2962be2010-07-29 07:30:00 +00001577 // If this is a C++ record, check the bases first.
1578 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1579 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1580 e = CXXRD->bases_end(); i != e; ++i) {
1581 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1582 "Unexpected base class!");
1583 const CXXRecordDecl *Base =
1584 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001585
Chris Lattnere2962be2010-07-29 07:30:00 +00001586 // If the base is after the span we care about, ignore it.
Anders Carlssona14f5972010-10-31 23:22:37 +00001587 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffsetInBits(Base);
Chris Lattnere2962be2010-07-29 07:30:00 +00001588 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001589
Chris Lattnere2962be2010-07-29 07:30:00 +00001590 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1591 if (!BitsContainNoUserData(i->getType(), BaseStart,
1592 EndBit-BaseOffset, Context))
1593 return false;
1594 }
1595 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001596
Chris Lattnere2962be2010-07-29 07:30:00 +00001597 // Verify that no field has data that overlaps the region of interest. Yes
1598 // this could be sped up a lot by being smarter about queried fields,
1599 // however we're only looking at structs up to 16 bytes, so we don't care
1600 // much.
1601 unsigned idx = 0;
1602 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1603 i != e; ++i, ++idx) {
1604 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001605
Chris Lattnere2962be2010-07-29 07:30:00 +00001606 // If we found a field after the region we care about, then we're done.
1607 if (FieldOffset >= EndBit) break;
1608
1609 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1610 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1611 Context))
1612 return false;
1613 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001614
Chris Lattnere2962be2010-07-29 07:30:00 +00001615 // If nothing in this record overlapped the area of interest, then we're
1616 // clean.
1617 return true;
1618 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001619
Chris Lattnere2962be2010-07-29 07:30:00 +00001620 return false;
1621}
1622
Chris Lattner0b362002010-07-29 18:39:32 +00001623/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1624/// float member at the specified offset. For example, {int,{float}} has a
1625/// float at offset 4. It is conservatively correct for this routine to return
1626/// false.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001627static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0b362002010-07-29 18:39:32 +00001628 const llvm::TargetData &TD) {
1629 // Base case if we find a float.
1630 if (IROffset == 0 && IRType->isFloatTy())
1631 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001632
Chris Lattner0b362002010-07-29 18:39:32 +00001633 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001634 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner0b362002010-07-29 18:39:32 +00001635 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1636 unsigned Elt = SL->getElementContainingOffset(IROffset);
1637 IROffset -= SL->getElementOffset(Elt);
1638 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1639 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001640
Chris Lattner0b362002010-07-29 18:39:32 +00001641 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001642 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1643 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner0b362002010-07-29 18:39:32 +00001644 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1645 IROffset -= IROffset/EltSize*EltSize;
1646 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1647 }
1648
1649 return false;
1650}
1651
Chris Lattnerf47c9442010-07-29 18:13:09 +00001652
1653/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1654/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001655llvm::Type *X86_64ABIInfo::
1656GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00001657 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001658 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001659 // pass as float if the last 4 bytes is just padding. This happens for
1660 // structs that contain 3 floats.
1661 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1662 SourceOffset*8+64, getContext()))
1663 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001664
Chris Lattner0b362002010-07-29 18:39:32 +00001665 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1666 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1667 // case.
1668 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001669 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1670 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001671
Chris Lattnerf47c9442010-07-29 18:13:09 +00001672 return llvm::Type::getDoubleTy(getVMContext());
1673}
1674
1675
Chris Lattner0d2656d2010-07-29 17:40:35 +00001676/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1677/// an 8-byte GPR. This means that we either have a scalar or we are talking
1678/// about the high or low part of an up-to-16-byte struct. This routine picks
1679/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001680/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1681/// etc).
1682///
1683/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1684/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1685/// the 8-byte value references. PrefType may be null.
1686///
1687/// SourceTy is the source level type for the entire argument. SourceOffset is
1688/// an offset into this that we're processing (which is always either 0 or 8).
1689///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001690llvm::Type *X86_64ABIInfo::
1691GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00001692 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001693 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1694 // returning an 8-byte unit starting with it. See if we can safely use it.
1695 if (IROffset == 0) {
1696 // Pointers and int64's always fill the 8-byte unit.
1697 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1698 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001699
Chris Lattnere2962be2010-07-29 07:30:00 +00001700 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1701 // goodness in the source type is just tail padding. This is allowed to
1702 // kick in for struct {double,int} on the int, but not on
1703 // struct{double,int,int} because we wouldn't return the second int. We
1704 // have to do this analysis on the source type because we can't depend on
1705 // unions being lowered a specific way etc.
1706 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1707 IRType->isIntegerTy(32)) {
1708 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001709
Chris Lattnere2962be2010-07-29 07:30:00 +00001710 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1711 SourceOffset*8+64, getContext()))
1712 return IRType;
1713 }
1714 }
Chris Lattner49382de2010-07-28 22:44:07 +00001715
Chris Lattner2acc6e32011-07-18 04:24:23 +00001716 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001717 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner44f0fd22010-07-29 02:20:19 +00001718 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001719 if (IROffset < SL->getSizeInBytes()) {
1720 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1721 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001722
Chris Lattner0d2656d2010-07-29 17:40:35 +00001723 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1724 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001725 }
Chris Lattner49382de2010-07-28 22:44:07 +00001726 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001727
Chris Lattner2acc6e32011-07-18 04:24:23 +00001728 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001729 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner021c3a32010-07-29 07:43:55 +00001730 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1731 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00001732 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1733 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00001734 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001735
Chris Lattner49382de2010-07-28 22:44:07 +00001736 // Okay, we don't have any better idea of what to pass, so we pass this in an
1737 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001738 unsigned TySizeInBytes =
1739 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00001740
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001741 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001742
Chris Lattner49382de2010-07-28 22:44:07 +00001743 // It is always safe to classify this as an integer type up to i64 that
1744 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001745 return llvm::IntegerType::get(getVMContext(),
1746 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00001747}
1748
Chris Lattner66e7b682010-09-01 00:50:20 +00001749
1750/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1751/// be used as elements of a two register pair to pass or return, return a
1752/// first class aggregate to represent them. For example, if the low part of
1753/// a by-value argument should be passed as i32* and the high part as float,
1754/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001755static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00001756GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Chris Lattner66e7b682010-09-01 00:50:20 +00001757 const llvm::TargetData &TD) {
1758 // In order to correctly satisfy the ABI, we need to the high part to start
1759 // at offset 8. If the high and low parts we inferred are both 4-byte types
1760 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1761 // the second element at offset 8. Check for this:
1762 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1763 unsigned HiAlign = TD.getABITypeAlignment(Hi);
1764 unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
1765 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001766
Chris Lattner66e7b682010-09-01 00:50:20 +00001767 // To handle this, we have to increase the size of the low part so that the
1768 // second element will start at an 8 byte offset. We can't increase the size
1769 // of the second element because it might make us access off the end of the
1770 // struct.
1771 if (HiStart != 8) {
1772 // There are only two sorts of types the ABI generation code can produce for
1773 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1774 // Promote these to a larger type.
1775 if (Lo->isFloatTy())
1776 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1777 else {
1778 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1779 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1780 }
1781 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001782
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001783 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001784
1785
Chris Lattner66e7b682010-09-01 00:50:20 +00001786 // Verify that the second element is at an 8-byte offset.
1787 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1788 "Invalid x86-64 argument pair!");
1789 return Result;
1790}
1791
Chris Lattner519f68c2010-07-28 23:06:14 +00001792ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00001793classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00001794 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1795 // classification algorithm.
1796 X86_64ABIInfo::Class Lo, Hi;
1797 classify(RetTy, 0, Lo, Hi);
1798
1799 // Check some invariants.
1800 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001801 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1802
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001803 llvm::Type *ResType = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001804 switch (Lo) {
1805 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001806 if (Hi == NoClass)
1807 return ABIArgInfo::getIgnore();
1808 // If the low part is just padding, it takes no register, leave ResType
1809 // null.
1810 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1811 "Unknown missing lo part");
1812 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001813
1814 case SSEUp:
1815 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00001816 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001817
1818 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1819 // hidden argument.
1820 case Memory:
1821 return getIndirectReturnResult(RetTy);
1822
1823 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1824 // available register of the sequence %rax, %rdx is used.
1825 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001826 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001827
Chris Lattnereb518b42010-07-29 21:42:50 +00001828 // If we have a sign or zero extended integer, make sure to return Extend
1829 // so that the parameter gets the right LLVM IR attributes.
1830 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1831 // Treat an enum type as its underlying type.
1832 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1833 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001834
Chris Lattnereb518b42010-07-29 21:42:50 +00001835 if (RetTy->isIntegralOrEnumerationType() &&
1836 RetTy->isPromotableIntegerType())
1837 return ABIArgInfo::getExtend();
1838 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001839 break;
1840
1841 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1842 // available SSE register of the sequence %xmm0, %xmm1 is used.
1843 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001844 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00001845 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001846
1847 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1848 // returned on the X87 stack in %st0 as 80-bit x87 number.
1849 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00001850 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00001851 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001852
1853 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1854 // part of the value is returned in %st0 and the imaginary part in
1855 // %st1.
1856 case ComplexX87:
1857 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00001858 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00001859 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00001860 NULL);
1861 break;
1862 }
1863
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001864 llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001865 switch (Hi) {
1866 // Memory was handled previously and X87 should
1867 // never occur as a hi class.
1868 case Memory:
1869 case X87:
David Blaikieb219cfc2011-09-23 05:06:16 +00001870 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001871
1872 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00001873 case NoClass:
1874 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001875
Chris Lattner3db4dde2010-09-01 00:20:33 +00001876 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001877 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001878 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1879 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001880 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00001881 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001882 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001883 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1884 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001885 break;
1886
1887 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001888 // is passed in the next available eightbyte chunk if the last used
1889 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00001890 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001891 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00001892 case SSEUp:
1893 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001894 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00001895 break;
1896
1897 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1898 // returned together with the previous X87 value in %st0.
1899 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001900 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00001901 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001902 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00001903 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00001904 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001905 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001906 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1907 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00001908 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001909 break;
1910 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001911
Chris Lattner3db4dde2010-09-01 00:20:33 +00001912 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00001913 // known to pass in the high eightbyte of the result. We do this by forming a
1914 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00001915 if (HighPart)
1916 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner519f68c2010-07-28 23:06:14 +00001917
Chris Lattnereb518b42010-07-29 21:42:50 +00001918 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00001919}
1920
Daniel Dunbaredfac032012-03-10 01:03:58 +00001921ABIArgInfo X86_64ABIInfo::classifyArgumentType(
1922 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE)
1923 const
1924{
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001925 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001926 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001927
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001928 // Check some invariants.
1929 // FIXME: Enforce these by construction.
1930 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001931 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1932
1933 neededInt = 0;
1934 neededSSE = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001935 llvm::Type *ResType = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001936 switch (Lo) {
1937 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001938 if (Hi == NoClass)
1939 return ABIArgInfo::getIgnore();
1940 // If the low part is just padding, it takes no register, leave ResType
1941 // null.
1942 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1943 "Unknown missing lo part");
1944 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001945
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001946 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1947 // on the stack.
1948 case Memory:
1949
1950 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1951 // COMPLEX_X87, it is passed in memory.
1952 case X87:
1953 case ComplexX87:
Eli Friedmanded137f2011-06-29 07:04:55 +00001954 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1955 ++neededInt;
Daniel Dunbaredfac032012-03-10 01:03:58 +00001956 return getIndirectResult(Ty, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001957
1958 case SSEUp:
1959 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00001960 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001961
1962 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1963 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1964 // and %r9 is used.
1965 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00001966 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001967
Chris Lattner49382de2010-07-28 22:44:07 +00001968 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001969 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00001970
1971 // If we have a sign or zero extended integer, make sure to return Extend
1972 // so that the parameter gets the right LLVM IR attributes.
1973 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1974 // Treat an enum type as its underlying type.
1975 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1976 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001977
Chris Lattnereb518b42010-07-29 21:42:50 +00001978 if (Ty->isIntegralOrEnumerationType() &&
1979 Ty->isPromotableIntegerType())
1980 return ABIArgInfo::getExtend();
1981 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001982
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001983 break;
1984
1985 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1986 // available SSE register is used, the registers are taken in the
1987 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00001988 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001989 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00001990 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00001991 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001992 break;
1993 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00001994 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001995
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001996 llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001997 switch (Hi) {
1998 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001999 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002000 // which is passed in memory.
2001 case Memory:
2002 case X87:
2003 case ComplexX87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002004 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002005
2006 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002007
Chris Lattner645406a2010-09-01 00:24:35 +00002008 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002009 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00002010 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002011 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002012
Chris Lattner645406a2010-09-01 00:24:35 +00002013 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2014 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002015 break;
2016
2017 // X87Up generally doesn't occur here (long double is passed in
2018 // memory), except in situations involving unions.
2019 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00002020 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002021 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002022
Chris Lattner645406a2010-09-01 00:24:35 +00002023 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2024 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00002025
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002026 ++neededSSE;
2027 break;
2028
2029 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
2030 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002031 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002032 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00002033 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002034 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002035 break;
2036 }
2037
Chris Lattner645406a2010-09-01 00:24:35 +00002038 // If a high part was specified, merge it together with the low part. It is
2039 // known to pass in the high eightbyte of the result. We do this by forming a
2040 // first class struct aggregate with the high and low part: {low, high}
2041 if (HighPart)
Chris Lattner66e7b682010-09-01 00:50:20 +00002042 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002043
Chris Lattnereb518b42010-07-29 21:42:50 +00002044 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002045}
2046
Chris Lattneree5dcd02010-07-29 02:31:05 +00002047void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002048
Chris Lattnera3c109b2010-07-29 02:16:43 +00002049 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002050
2051 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00002052 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002053
2054 // If the return value is indirect, then the hidden argument is consuming one
2055 // integer register.
2056 if (FI.getReturnInfo().isIndirect())
2057 --freeIntRegs;
2058
2059 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2060 // get assigned (in left-to-right order) for passing as follows...
2061 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2062 it != ie; ++it) {
Bill Wendling99aaae82010-10-18 23:51:38 +00002063 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002064 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
2065 neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002066
2067 // AMD64-ABI 3.2.3p3: If there are no registers available for any
2068 // eightbyte of an argument, the whole argument is passed on the
2069 // stack. If registers have already been assigned for some
2070 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00002071 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002072 freeIntRegs -= neededInt;
2073 freeSSERegs -= neededSSE;
2074 } else {
Daniel Dunbaredfac032012-03-10 01:03:58 +00002075 it->info = getIndirectResult(it->type, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002076 }
2077 }
2078}
2079
2080static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2081 QualType Ty,
2082 CodeGenFunction &CGF) {
2083 llvm::Value *overflow_arg_area_p =
2084 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2085 llvm::Value *overflow_arg_area =
2086 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2087
2088 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2089 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedman8d2fe422011-11-18 02:44:19 +00002090 // It isn't stated explicitly in the standard, but in practice we use
2091 // alignment greater than 16 where necessary.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002092 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2093 if (Align > 8) {
Eli Friedman8d2fe422011-11-18 02:44:19 +00002094 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
Owen Anderson0032b272009-08-13 21:57:51 +00002095 llvm::Value *Offset =
Eli Friedman8d2fe422011-11-18 02:44:19 +00002096 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002097 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2098 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00002099 CGF.Int64Ty);
Eli Friedman8d2fe422011-11-18 02:44:19 +00002100 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002101 overflow_arg_area =
2102 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2103 overflow_arg_area->getType(),
2104 "overflow_arg_area.align");
2105 }
2106
2107 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002108 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002109 llvm::Value *Res =
2110 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002111 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002112
2113 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2114 // l->overflow_arg_area + sizeof(type).
2115 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2116 // an 8 byte boundary.
2117
2118 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00002119 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00002120 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002121 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2122 "overflow_arg_area.next");
2123 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2124
2125 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2126 return Res;
2127}
2128
2129llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2130 CodeGenFunction &CGF) const {
2131 // Assume that va_list type is correct; should be pointer to LLVM type:
2132 // struct {
2133 // i32 gp_offset;
2134 // i32 fp_offset;
2135 // i8* overflow_arg_area;
2136 // i8* reg_save_area;
2137 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00002138 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002139
Chris Lattnera14db752010-03-11 18:19:55 +00002140 Ty = CGF.getContext().getCanonicalType(Ty);
Daniel Dunbaredfac032012-03-10 01:03:58 +00002141 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002142
2143 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2144 // in the registers. If not go to step 7.
2145 if (!neededInt && !neededSSE)
2146 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2147
2148 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2149 // general purpose registers needed to pass type and num_fp to hold
2150 // the number of floating point registers needed.
2151
2152 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2153 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2154 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2155 //
2156 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2157 // register save space).
2158
2159 llvm::Value *InRegs = 0;
2160 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2161 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2162 if (neededInt) {
2163 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2164 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002165 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2166 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002167 }
2168
2169 if (neededSSE) {
2170 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2171 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2172 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00002173 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2174 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002175 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2176 }
2177
2178 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2179 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2180 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2181 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2182
2183 // Emit code to load the value if it was passed in registers.
2184
2185 CGF.EmitBlock(InRegBlock);
2186
2187 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2188 // an offset of l->gp_offset and/or l->fp_offset. This may require
2189 // copying to a temporary location in case the parameter is passed
2190 // in different register classes or requires an alignment greater
2191 // than 8 for general purpose registers and 16 for XMM registers.
2192 //
2193 // FIXME: This really results in shameful code when we end up needing to
2194 // collect arguments from different places; often what should result in a
2195 // simple assembling of a structure from scattered addresses has many more
2196 // loads than necessary. Can we clean this up?
Chris Lattner2acc6e32011-07-18 04:24:23 +00002197 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002198 llvm::Value *RegAddr =
2199 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2200 "reg_save_area");
2201 if (neededInt && neededSSE) {
2202 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002203 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002204 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002205 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2206 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002207 llvm::Type *TyLo = ST->getElementType(0);
2208 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002209 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002210 "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002211 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2212 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002213 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2214 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002215 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2216 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002217 llvm::Value *V =
2218 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2219 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2220 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2221 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2222
Owen Andersona1cf15f2009-07-14 23:10:40 +00002223 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002224 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002225 } else if (neededInt) {
2226 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2227 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002228 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002229 } else if (neededSSE == 1) {
2230 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2231 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2232 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002233 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002234 assert(neededSSE == 2 && "Invalid number of needed registers!");
2235 // SSE registers are spaced 16 bytes apart in the register save
2236 // area, we need to collect the two eightbytes together.
2237 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002238 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner8b418682012-02-07 00:39:47 +00002239 llvm::Type *DoubleTy = CGF.DoubleTy;
Chris Lattner2acc6e32011-07-18 04:24:23 +00002240 llvm::Type *DblPtrTy =
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002241 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002242 llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002243 DoubleTy, NULL);
2244 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2245 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2246 DblPtrTy));
2247 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2248 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2249 DblPtrTy));
2250 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2251 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2252 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002253 }
2254
2255 // AMD64-ABI 3.5.7p5: Step 5. Set:
2256 // l->gp_offset = l->gp_offset + num_gp * 8
2257 // l->fp_offset = l->fp_offset + num_fp * 16.
2258 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002259 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002260 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2261 gp_offset_p);
2262 }
2263 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002264 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002265 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2266 fp_offset_p);
2267 }
2268 CGF.EmitBranch(ContBlock);
2269
2270 // Emit code to load the value if it was passed in memory.
2271
2272 CGF.EmitBlock(InMemBlock);
2273 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2274
2275 // Return the appropriate result.
2276
2277 CGF.EmitBlock(ContBlock);
Jay Foadbbf3bac2011-03-30 11:28:58 +00002278 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002279 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002280 ResAddr->addIncoming(RegAddr, InRegBlock);
2281 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002282 return ResAddr;
2283}
2284
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002285ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2286
2287 if (Ty->isVoidType())
2288 return ABIArgInfo::getIgnore();
2289
2290 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2291 Ty = EnumTy->getDecl()->getIntegerType();
2292
2293 uint64_t Size = getContext().getTypeSize(Ty);
2294
2295 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002296 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2297 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002298 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2299
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002300 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2301 if (Size == 128 &&
Eli Friedman55fc7e22012-01-25 22:46:34 +00002302 getContext().getTargetInfo().getTriple().getOS()
2303 == llvm::Triple::MinGW32)
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002304 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2305 Size));
2306
2307 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2308 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2309 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002310 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002311 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2312 Size));
2313
2314 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2315 }
2316
2317 if (Ty->isPromotableIntegerType())
2318 return ABIArgInfo::getExtend();
2319
2320 return ABIArgInfo::getDirect();
2321}
2322
2323void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2324
2325 QualType RetTy = FI.getReturnType();
2326 FI.getReturnInfo() = classify(RetTy);
2327
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002328 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2329 it != ie; ++it)
2330 it->info = classify(it->type);
2331}
2332
Chris Lattnerf13721d2010-08-31 16:44:54 +00002333llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2334 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002335 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002336
Chris Lattnerf13721d2010-08-31 16:44:54 +00002337 CGBuilderTy &Builder = CGF.Builder;
2338 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2339 "ap");
2340 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2341 llvm::Type *PTy =
2342 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2343 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2344
2345 uint64_t Offset =
2346 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2347 llvm::Value *NextAddr =
2348 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2349 "ap.next");
2350 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2351
2352 return AddrTyped;
2353}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002354
John McCallec853ba2010-03-11 00:10:12 +00002355// PowerPC-32
2356
2357namespace {
2358class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2359public:
Chris Lattnerea044322010-07-29 02:01:43 +00002360 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002361
John McCallec853ba2010-03-11 00:10:12 +00002362 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2363 // This is recovered from gcc output.
2364 return 1; // r1 is the dedicated stack pointer
2365 }
2366
2367 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002368 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002369};
2370
2371}
2372
2373bool
2374PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2375 llvm::Value *Address) const {
2376 // This is calculated from the LLVM and GCC tables and verified
2377 // against gcc output. AFAIK all ABIs use the same encoding.
2378
2379 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallec853ba2010-03-11 00:10:12 +00002380
Chris Lattner8b418682012-02-07 00:39:47 +00002381 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallec853ba2010-03-11 00:10:12 +00002382 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2383 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2384 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2385
2386 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002387 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002388
2389 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002390 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002391
2392 // 64-76 are various 4-byte special-purpose registers:
2393 // 64: mq
2394 // 65: lr
2395 // 66: ctr
2396 // 67: ap
2397 // 68-75 cr0-7
2398 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002399 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002400
2401 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002402 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002403
2404 // 109: vrsave
2405 // 110: vscr
2406 // 111: spe_acc
2407 // 112: spefscr
2408 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002409 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002410
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002411 return false;
John McCallec853ba2010-03-11 00:10:12 +00002412}
2413
2414
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002415//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002416// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002417//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002418
2419namespace {
2420
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002421class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002422public:
2423 enum ABIKind {
2424 APCS = 0,
2425 AAPCS = 1,
2426 AAPCS_VFP
2427 };
2428
2429private:
2430 ABIKind Kind;
2431
2432public:
Chris Lattnerea044322010-07-29 02:01:43 +00002433 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002434
John McCall49e34be2011-08-30 01:42:09 +00002435 bool isEABI() const {
Eli Friedman55fc7e22012-01-25 22:46:34 +00002436 StringRef Env =
2437 getContext().getTargetInfo().getTriple().getEnvironmentName();
Chandler Carruthb43550b2012-01-10 19:47:42 +00002438 return (Env == "gnueabi" || Env == "eabi" || Env == "androideabi");
John McCall49e34be2011-08-30 01:42:09 +00002439 }
2440
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002441private:
2442 ABIKind getABIKind() const { return Kind; }
2443
Chris Lattnera3c109b2010-07-29 02:16:43 +00002444 ABIArgInfo classifyReturnType(QualType RetTy) const;
2445 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002446
Chris Lattneree5dcd02010-07-29 02:31:05 +00002447 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002448
2449 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2450 CodeGenFunction &CGF) const;
2451};
2452
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002453class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2454public:
Chris Lattnerea044322010-07-29 02:01:43 +00002455 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2456 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00002457
John McCall49e34be2011-08-30 01:42:09 +00002458 const ARMABIInfo &getABIInfo() const {
2459 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
2460 }
2461
John McCall6374c332010-03-06 00:35:14 +00002462 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2463 return 13;
2464 }
Roman Divacky09345d12011-05-18 19:36:54 +00002465
Chris Lattner5f9e2722011-07-23 10:55:15 +00002466 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCallf85e1932011-06-15 23:02:42 +00002467 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2468 }
2469
Roman Divacky09345d12011-05-18 19:36:54 +00002470 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2471 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002472 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divacky09345d12011-05-18 19:36:54 +00002473
2474 // 0-15 are the 16 integer registers.
Chris Lattner8b418682012-02-07 00:39:47 +00002475 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divacky09345d12011-05-18 19:36:54 +00002476 return false;
2477 }
John McCall49e34be2011-08-30 01:42:09 +00002478
2479 unsigned getSizeOfUnwindException() const {
2480 if (getABIInfo().isEABI()) return 88;
2481 return TargetCodeGenInfo::getSizeOfUnwindException();
2482 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002483};
2484
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002485}
2486
Chris Lattneree5dcd02010-07-29 02:31:05 +00002487void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002488 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002489 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattnera3c109b2010-07-29 02:16:43 +00002490 it != ie; ++it)
2491 it->info = classifyArgumentType(it->type);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002492
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002493 // Always honor user-specified calling convention.
2494 if (FI.getCallingConvention() != llvm::CallingConv::C)
2495 return;
2496
2497 // Calling convention as default by an ABI.
Rafael Espindola25117ab2010-06-16 16:13:39 +00002498 llvm::CallingConv::ID DefaultCC;
John McCall49e34be2011-08-30 01:42:09 +00002499 if (isEABI())
Rafael Espindola25117ab2010-06-16 16:13:39 +00002500 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00002501 else
2502 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00002503
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002504 // If user did not ask for specific calling convention explicitly (e.g. via
2505 // pcs attribute), set effective calling convention if it's different than ABI
2506 // default.
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002507 switch (getABIKind()) {
2508 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002509 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2510 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002511 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002512 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002513 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2514 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002515 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002516 case AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002517 if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
2518 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002519 break;
2520 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002521}
2522
Bob Wilson194f06a2011-08-03 05:58:22 +00002523/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
2524/// aggregate. If HAMembers is non-null, the number of base elements
2525/// contained in the type is returned through it; this is used for the
2526/// recursive calls that check aggregate component types.
2527static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
2528 ASTContext &Context,
2529 uint64_t *HAMembers = 0) {
2530 uint64_t Members;
2531 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2532 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
2533 return false;
2534 Members *= AT->getSize().getZExtValue();
2535 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
2536 const RecordDecl *RD = RT->getDecl();
2537 if (RD->isUnion() || RD->hasFlexibleArrayMember())
2538 return false;
2539 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
2540 if (!CXXRD->isAggregate())
2541 return false;
2542 }
2543 Members = 0;
2544 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2545 i != e; ++i) {
2546 const FieldDecl *FD = *i;
2547 uint64_t FldMembers;
2548 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
2549 return false;
2550 Members += FldMembers;
2551 }
2552 } else {
2553 Members = 1;
2554 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2555 Members = 2;
2556 Ty = CT->getElementType();
2557 }
2558
2559 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
2560 // double, or 64-bit or 128-bit vectors.
2561 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
2562 if (BT->getKind() != BuiltinType::Float &&
2563 BT->getKind() != BuiltinType::Double)
2564 return false;
2565 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
2566 unsigned VecSize = Context.getTypeSize(VT);
2567 if (VecSize != 64 && VecSize != 128)
2568 return false;
2569 } else {
2570 return false;
2571 }
2572
2573 // The base type must be the same for all members. Vector types of the
2574 // same total size are treated as being equivalent here.
2575 const Type *TyPtr = Ty.getTypePtr();
2576 if (!Base)
2577 Base = TyPtr;
2578 if (Base != TyPtr &&
2579 (!Base->isVectorType() || !TyPtr->isVectorType() ||
2580 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
2581 return false;
2582 }
2583
2584 // Homogeneous Aggregates can have at most 4 members of the base type.
2585 if (HAMembers)
2586 *HAMembers = Members;
2587 return (Members <= 4);
2588}
2589
Chris Lattnera3c109b2010-07-29 02:16:43 +00002590ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +00002591 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002592 // Treat an enum type as its underlying type.
2593 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2594 Ty = EnumTy->getDecl()->getIntegerType();
2595
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002596 return (Ty->isPromotableIntegerType() ?
2597 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002598 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002599
Daniel Dunbar42025572009-09-14 21:54:03 +00002600 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002601 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00002602 return ABIArgInfo::getIgnore();
2603
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002604 // Structures with either a non-trivial destructor or a non-trivial
2605 // copy constructor are always indirect.
2606 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2607 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2608
Bob Wilson194f06a2011-08-03 05:58:22 +00002609 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
2610 // Homogeneous Aggregates need to be expanded.
2611 const Type *Base = 0;
2612 if (isHomogeneousAggregate(Ty, Base, getContext()))
2613 return ABIArgInfo::getExpand();
2614 }
2615
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00002616 // Otherwise, pass by coercing to a structure of the appropriate size.
2617 //
Bob Wilson53fc1a62011-08-01 23:39:04 +00002618 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
2619 // backend doesn't support byval.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002620 // FIXME: This doesn't handle alignment > 64 bits.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002621 llvm::Type* ElemTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002622 unsigned SizeRegs;
Bob Wilson53fc1a62011-08-01 23:39:04 +00002623 if (getContext().getTypeAlign(Ty) > 32) {
Stuart Hastings67d097e2011-04-27 17:24:02 +00002624 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2625 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Bob Wilson53fc1a62011-08-01 23:39:04 +00002626 } else {
2627 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2628 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Stuart Hastings67d097e2011-04-27 17:24:02 +00002629 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00002630
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002631 llvm::Type *STy =
Chris Lattner7650d952011-06-18 22:49:11 +00002632 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00002633 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002634}
2635
Chris Lattnera3c109b2010-07-29 02:16:43 +00002636static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00002637 llvm::LLVMContext &VMContext) {
2638 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2639 // is called integer-like if its size is less than or equal to one word, and
2640 // the offset of each of its addressable sub-fields is zero.
2641
2642 uint64_t Size = Context.getTypeSize(Ty);
2643
2644 // Check that the type fits in a word.
2645 if (Size > 32)
2646 return false;
2647
2648 // FIXME: Handle vector types!
2649 if (Ty->isVectorType())
2650 return false;
2651
Daniel Dunbarb0d58192009-09-14 02:20:34 +00002652 // Float types are never treated as "integer like".
2653 if (Ty->isRealFloatingType())
2654 return false;
2655
Daniel Dunbar98303b92009-09-13 08:03:58 +00002656 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00002657 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00002658 return true;
2659
Daniel Dunbar45815812010-02-01 23:31:26 +00002660 // Small complex integer types are "integer like".
2661 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2662 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002663
2664 // Single element and zero sized arrays should be allowed, by the definition
2665 // above, but they are not.
2666
2667 // Otherwise, it must be a record type.
2668 const RecordType *RT = Ty->getAs<RecordType>();
2669 if (!RT) return false;
2670
2671 // Ignore records with flexible arrays.
2672 const RecordDecl *RD = RT->getDecl();
2673 if (RD->hasFlexibleArrayMember())
2674 return false;
2675
2676 // Check that all sub-fields are at offset 0, and are themselves "integer
2677 // like".
2678 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2679
2680 bool HadField = false;
2681 unsigned idx = 0;
2682 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2683 i != e; ++i, ++idx) {
2684 const FieldDecl *FD = *i;
2685
Daniel Dunbar679855a2010-01-29 03:22:29 +00002686 // Bit-fields are not addressable, we only need to verify they are "integer
2687 // like". We still have to disallow a subsequent non-bitfield, for example:
2688 // struct { int : 0; int x }
2689 // is non-integer like according to gcc.
2690 if (FD->isBitField()) {
2691 if (!RD->isUnion())
2692 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002693
Daniel Dunbar679855a2010-01-29 03:22:29 +00002694 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2695 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002696
Daniel Dunbar679855a2010-01-29 03:22:29 +00002697 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002698 }
2699
Daniel Dunbar679855a2010-01-29 03:22:29 +00002700 // Check if this field is at offset 0.
2701 if (Layout.getFieldOffset(idx) != 0)
2702 return false;
2703
Daniel Dunbar98303b92009-09-13 08:03:58 +00002704 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2705 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002706
Daniel Dunbar679855a2010-01-29 03:22:29 +00002707 // Only allow at most one field in a structure. This doesn't match the
2708 // wording above, but follows gcc in situations with a field following an
2709 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00002710 if (!RD->isUnion()) {
2711 if (HadField)
2712 return false;
2713
2714 HadField = true;
2715 }
2716 }
2717
2718 return true;
2719}
2720
Chris Lattnera3c109b2010-07-29 02:16:43 +00002721ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002722 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002723 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00002724
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00002725 // Large vector types should be returned via memory.
2726 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
2727 return ABIArgInfo::getIndirect(0);
2728
John McCalld608cdb2010-08-22 10:59:02 +00002729 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002730 // Treat an enum type as its underlying type.
2731 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2732 RetTy = EnumTy->getDecl()->getIntegerType();
2733
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002734 return (RetTy->isPromotableIntegerType() ?
2735 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002736 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002737
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002738 // Structures with either a non-trivial destructor or a non-trivial
2739 // copy constructor are always indirect.
2740 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2741 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2742
Daniel Dunbar98303b92009-09-13 08:03:58 +00002743 // Are we following APCS?
2744 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002745 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00002746 return ABIArgInfo::getIgnore();
2747
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002748 // Complex types are all returned as packed integers.
2749 //
2750 // FIXME: Consider using 2 x vector types if the back end handles them
2751 // correctly.
2752 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00002753 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00002754 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002755
Daniel Dunbar98303b92009-09-13 08:03:58 +00002756 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002757 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002758 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002759 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002760 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002761 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002762 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002763 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2764 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002765 }
2766
2767 // Otherwise return in memory.
2768 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002769 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002770
2771 // Otherwise this is an AAPCS variant.
2772
Chris Lattnera3c109b2010-07-29 02:16:43 +00002773 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00002774 return ABIArgInfo::getIgnore();
2775
Bob Wilson3b694fa2011-11-02 04:51:36 +00002776 // Check for homogeneous aggregates with AAPCS-VFP.
2777 if (getABIKind() == AAPCS_VFP) {
2778 const Type *Base = 0;
2779 if (isHomogeneousAggregate(RetTy, Base, getContext()))
2780 // Homogeneous Aggregates are returned directly.
2781 return ABIArgInfo::getDirect();
2782 }
2783
Daniel Dunbar98303b92009-09-13 08:03:58 +00002784 // Aggregates <= 4 bytes are returned in r0; other aggregates
2785 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002786 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00002787 if (Size <= 32) {
2788 // Return in the smallest viable integer type.
2789 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002790 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002791 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002792 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2793 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002794 }
2795
Daniel Dunbar98303b92009-09-13 08:03:58 +00002796 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002797}
2798
2799llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00002800 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002801 llvm::Type *BP = CGF.Int8PtrTy;
2802 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002803
2804 CGBuilderTy &Builder = CGF.Builder;
Chris Lattner8b418682012-02-07 00:39:47 +00002805 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002806 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Rafael Espindolae164c182011-08-02 22:33:37 +00002807 // Handle address alignment for type alignment > 32 bits
2808 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
2809 if (TyAlign > 4) {
2810 assert((TyAlign & (TyAlign - 1)) == 0 &&
2811 "Alignment is not power of 2!");
2812 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
2813 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
2814 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
2815 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2816 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002817 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00002818 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002819 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2820
2821 uint64_t Offset =
2822 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2823 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00002824 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002825 "ap.next");
2826 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2827
2828 return AddrTyped;
2829}
2830
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002831//===----------------------------------------------------------------------===//
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002832// PTX ABI Implementation
2833//===----------------------------------------------------------------------===//
2834
2835namespace {
2836
2837class PTXABIInfo : public ABIInfo {
2838public:
2839 PTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2840
2841 ABIArgInfo classifyReturnType(QualType RetTy) const;
2842 ABIArgInfo classifyArgumentType(QualType Ty) const;
2843
2844 virtual void computeInfo(CGFunctionInfo &FI) const;
2845 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2846 CodeGenFunction &CFG) const;
2847};
2848
2849class PTXTargetCodeGenInfo : public TargetCodeGenInfo {
2850public:
2851 PTXTargetCodeGenInfo(CodeGenTypes &CGT)
2852 : TargetCodeGenInfo(new PTXABIInfo(CGT)) {}
Justin Holewinski818eafb2011-10-05 17:58:44 +00002853
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00002854 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2855 CodeGen::CodeGenModule &M) const;
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002856};
2857
2858ABIArgInfo PTXABIInfo::classifyReturnType(QualType RetTy) const {
2859 if (RetTy->isVoidType())
2860 return ABIArgInfo::getIgnore();
2861 if (isAggregateTypeForABI(RetTy))
2862 return ABIArgInfo::getIndirect(0);
2863 return ABIArgInfo::getDirect();
2864}
2865
2866ABIArgInfo PTXABIInfo::classifyArgumentType(QualType Ty) const {
2867 if (isAggregateTypeForABI(Ty))
2868 return ABIArgInfo::getIndirect(0);
2869
2870 return ABIArgInfo::getDirect();
2871}
2872
2873void PTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
2874 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2875 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2876 it != ie; ++it)
2877 it->info = classifyArgumentType(it->type);
2878
2879 // Always honor user-specified calling convention.
2880 if (FI.getCallingConvention() != llvm::CallingConv::C)
2881 return;
2882
2883 // Calling convention as default by an ABI.
2884 llvm::CallingConv::ID DefaultCC;
David Blaikie4e4d0842012-03-11 07:00:24 +00002885 const LangOptions &LangOpts = getContext().getLangOpts();
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002886 if (LangOpts.OpenCL || LangOpts.CUDA) {
2887 // If we are in OpenCL or CUDA mode, then default to device functions
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002888 DefaultCC = llvm::CallingConv::PTX_Device;
Justin Holewinski818eafb2011-10-05 17:58:44 +00002889 } else {
2890 // If we are in standard C/C++ mode, use the triple to decide on the default
2891 StringRef Env =
2892 getContext().getTargetInfo().getTriple().getEnvironmentName();
2893 if (Env == "device")
2894 DefaultCC = llvm::CallingConv::PTX_Device;
2895 else
2896 DefaultCC = llvm::CallingConv::PTX_Kernel;
2897 }
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002898 FI.setEffectiveCallingConvention(DefaultCC);
Justin Holewinski818eafb2011-10-05 17:58:44 +00002899
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002900}
2901
2902llvm::Value *PTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2903 CodeGenFunction &CFG) const {
2904 llvm_unreachable("PTX does not support varargs");
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002905}
2906
Justin Holewinski818eafb2011-10-05 17:58:44 +00002907void PTXTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2908 llvm::GlobalValue *GV,
2909 CodeGen::CodeGenModule &M) const{
2910 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2911 if (!FD) return;
2912
2913 llvm::Function *F = cast<llvm::Function>(GV);
2914
2915 // Perform special handling in OpenCL mode
David Blaikie4e4d0842012-03-11 07:00:24 +00002916 if (M.getLangOpts().OpenCL) {
Justin Holewinski818eafb2011-10-05 17:58:44 +00002917 // Use OpenCL function attributes to set proper calling conventions
2918 // By default, all functions are device functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00002919 if (FD->hasAttr<OpenCLKernelAttr>()) {
2920 // OpenCL __kernel functions get a kernel calling convention
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002921 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00002922 // And kernel functions are not subject to inlining
2923 F->addFnAttr(llvm::Attribute::NoInline);
2924 }
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002925 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00002926
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002927 // Perform special handling in CUDA mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002928 if (M.getLangOpts().CUDA) {
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002929 // CUDA __global__ functions get a kernel calling convention. Since
2930 // __global__ functions cannot be called from the device, we do not
2931 // need to set the noinline attribute.
2932 if (FD->getAttr<CUDAGlobalAttr>())
2933 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00002934 }
2935}
2936
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002937}
2938
2939//===----------------------------------------------------------------------===//
Wesley Peck276fdf42010-12-19 19:57:51 +00002940// MBlaze ABI Implementation
2941//===----------------------------------------------------------------------===//
2942
2943namespace {
2944
2945class MBlazeABIInfo : public ABIInfo {
2946public:
2947 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2948
2949 bool isPromotableIntegerType(QualType Ty) const;
2950
2951 ABIArgInfo classifyReturnType(QualType RetTy) const;
2952 ABIArgInfo classifyArgumentType(QualType RetTy) const;
2953
2954 virtual void computeInfo(CGFunctionInfo &FI) const {
2955 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2956 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2957 it != ie; ++it)
2958 it->info = classifyArgumentType(it->type);
2959 }
2960
2961 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2962 CodeGenFunction &CGF) const;
2963};
2964
2965class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
2966public:
2967 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
2968 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
2969 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2970 CodeGen::CodeGenModule &M) const;
2971};
2972
2973}
2974
2975bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
2976 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
2977 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2978 switch (BT->getKind()) {
2979 case BuiltinType::Bool:
2980 case BuiltinType::Char_S:
2981 case BuiltinType::Char_U:
2982 case BuiltinType::SChar:
2983 case BuiltinType::UChar:
2984 case BuiltinType::Short:
2985 case BuiltinType::UShort:
2986 return true;
2987 default:
2988 return false;
2989 }
2990 return false;
2991}
2992
2993llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2994 CodeGenFunction &CGF) const {
2995 // FIXME: Implement
2996 return 0;
2997}
2998
2999
3000ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
3001 if (RetTy->isVoidType())
3002 return ABIArgInfo::getIgnore();
3003 if (isAggregateTypeForABI(RetTy))
3004 return ABIArgInfo::getIndirect(0);
3005
3006 return (isPromotableIntegerType(RetTy) ?
3007 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3008}
3009
3010ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
3011 if (isAggregateTypeForABI(Ty))
3012 return ABIArgInfo::getIndirect(0);
3013
3014 return (isPromotableIntegerType(Ty) ?
3015 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3016}
3017
3018void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3019 llvm::GlobalValue *GV,
3020 CodeGen::CodeGenModule &M)
3021 const {
3022 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3023 if (!FD) return;
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00003024
Wesley Peck276fdf42010-12-19 19:57:51 +00003025 llvm::CallingConv::ID CC = llvm::CallingConv::C;
3026 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
3027 CC = llvm::CallingConv::MBLAZE_INTR;
3028 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
3029 CC = llvm::CallingConv::MBLAZE_SVOL;
3030
3031 if (CC != llvm::CallingConv::C) {
3032 // Handle 'interrupt_handler' attribute:
3033 llvm::Function *F = cast<llvm::Function>(GV);
3034
3035 // Step 1: Set ISR calling convention.
3036 F->setCallingConv(CC);
3037
3038 // Step 2: Add attributes goodness.
3039 F->addFnAttr(llvm::Attribute::NoInline);
3040 }
3041
3042 // Step 3: Emit _interrupt_handler alias.
3043 if (CC == llvm::CallingConv::MBLAZE_INTR)
3044 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
3045 "_interrupt_handler", GV, &M.getModule());
3046}
3047
3048
3049//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003050// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003051//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003052
3053namespace {
3054
3055class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
3056public:
Chris Lattnerea044322010-07-29 02:01:43 +00003057 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
3058 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003059 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3060 CodeGen::CodeGenModule &M) const;
3061};
3062
3063}
3064
3065void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3066 llvm::GlobalValue *GV,
3067 CodeGen::CodeGenModule &M) const {
3068 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
3069 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
3070 // Handle 'interrupt' attribute:
3071 llvm::Function *F = cast<llvm::Function>(GV);
3072
3073 // Step 1: Set ISR calling convention.
3074 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
3075
3076 // Step 2: Add attributes goodness.
3077 F->addFnAttr(llvm::Attribute::NoInline);
3078
3079 // Step 3: Emit ISR vector alias.
3080 unsigned Num = attr->getNumber() + 0xffe0;
3081 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003082 "vector_" + Twine::utohexstr(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003083 GV, &M.getModule());
3084 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003085 }
3086}
3087
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003088//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00003089// MIPS ABI Implementation. This works for both little-endian and
3090// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003091//===----------------------------------------------------------------------===//
3092
John McCallaeeb7012010-05-27 06:19:26 +00003093namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00003094class MipsABIInfo : public ABIInfo {
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003095 bool IsO32;
Akira Hatanakab551dd32011-11-03 00:05:50 +00003096 unsigned MinABIStackAlignInBytes;
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00003097 llvm::Type* HandleAggregates(QualType Ty) const;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003098 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003099 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003100public:
Akira Hatanakab551dd32011-11-03 00:05:50 +00003101 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
3102 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8) {}
Akira Hatanaka619e8872011-06-02 00:09:17 +00003103
3104 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003105 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003106 virtual void computeInfo(CGFunctionInfo &FI) const;
3107 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3108 CodeGenFunction &CGF) const;
3109};
3110
John McCallaeeb7012010-05-27 06:19:26 +00003111class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003112 unsigned SizeOfUnwindException;
John McCallaeeb7012010-05-27 06:19:26 +00003113public:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003114 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
3115 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
3116 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCallaeeb7012010-05-27 06:19:26 +00003117
3118 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
3119 return 29;
3120 }
3121
3122 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003123 llvm::Value *Address) const;
John McCall49e34be2011-08-30 01:42:09 +00003124
3125 unsigned getSizeOfUnwindException() const {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003126 return SizeOfUnwindException;
John McCall49e34be2011-08-30 01:42:09 +00003127 }
John McCallaeeb7012010-05-27 06:19:26 +00003128};
3129}
3130
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003131// In N32/64, an aligned double precision floating point field is passed in
3132// a register.
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00003133llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty) const {
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003134 if (IsO32)
3135 return 0;
3136
Akira Hatanaka2afd23d2012-01-12 00:52:17 +00003137 if (Ty->isComplexType())
3138 return CGT.ConvertType(Ty);
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00003139
Akira Hatanakaa34e9212012-02-09 19:54:16 +00003140 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003141
Akira Hatanakaa34e9212012-02-09 19:54:16 +00003142 // Unions are passed in integer registers.
3143 if (!RT || !RT->isStructureOrClassType())
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003144 return 0;
3145
3146 const RecordDecl *RD = RT->getDecl();
3147 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3148 uint64_t StructSize = getContext().getTypeSize(Ty);
3149 assert(!(StructSize % 8) && "Size of structure must be multiple of 8.");
3150
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003151 uint64_t LastOffset = 0;
3152 unsigned idx = 0;
3153 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
Akira Hatanaka2afd23d2012-01-12 00:52:17 +00003154 SmallVector<llvm::Type*, 8> ArgList;
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003155
Akira Hatanakaa34e9212012-02-09 19:54:16 +00003156 // Iterate over fields in the struct/class and check if there are any aligned
3157 // double fields.
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003158 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3159 i != e; ++i, ++idx) {
3160 const QualType Ty = (*i)->getType();
3161 const BuiltinType *BT = Ty->getAs<BuiltinType>();
3162
3163 if (!BT || BT->getKind() != BuiltinType::Double)
3164 continue;
3165
3166 uint64_t Offset = Layout.getFieldOffset(idx);
3167 if (Offset % 64) // Ignore doubles that are not aligned.
3168 continue;
3169
3170 // Add ((Offset - LastOffset) / 64) args of type i64.
3171 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
3172 ArgList.push_back(I64);
3173
3174 // Add double type.
3175 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
3176 LastOffset = Offset + 64;
3177 }
3178
Akira Hatanakaa34e9212012-02-09 19:54:16 +00003179 // This struct/class doesn't have an aligned double field.
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003180 if (!LastOffset)
3181 return 0;
3182
3183 // Add ((StructSize - LastOffset) / 64) args of type i64.
3184 for (unsigned N = (StructSize - LastOffset) / 64; N; --N)
3185 ArgList.push_back(I64);
3186
Akira Hatanakab49d5a62011-11-03 23:31:00 +00003187 // If the size of the remainder is not zero, add one more integer type to
3188 // ArgList.
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003189 unsigned R = (StructSize - LastOffset) % 64;
Akira Hatanakab49d5a62011-11-03 23:31:00 +00003190 if (R)
3191 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003192
3193 return llvm::StructType::get(getVMContext(), ArgList);
3194}
3195
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003196llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
3197 // Padding is inserted only for N32/64.
3198 if (IsO32)
3199 return 0;
3200
3201 assert(Align <= 16 && "Alignment larger than 16 not handled.");
3202 return (Align == 16 && Offset & 0xf) ?
3203 llvm::IntegerType::get(getVMContext(), 64) : 0;
3204}
Akira Hatanaka9659d592012-01-10 22:44:52 +00003205
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003206ABIArgInfo
3207MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003208 uint64_t OrigOffset = Offset;
3209 uint64_t TySize =
3210 llvm::RoundUpToAlignment(getContext().getTypeSize(Ty), 64) / 8;
3211 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
3212 Offset = llvm::RoundUpToAlignment(Offset, std::max(Align, (uint64_t)8));
3213 Offset += TySize;
3214
Akira Hatanaka619e8872011-06-02 00:09:17 +00003215 if (isAggregateTypeForABI(Ty)) {
3216 // Ignore empty aggregates.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003217 if (TySize == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00003218 return ABIArgInfo::getIgnore();
3219
Akira Hatanaka511949b2011-08-01 18:09:58 +00003220 // Records with non trivial destructors/constructors should not be passed
3221 // by value.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003222 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) {
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003223 Offset = OrigOffset + 8;
Akira Hatanaka511949b2011-08-01 18:09:58 +00003224 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003225 }
Akira Hatanaka511949b2011-08-01 18:09:58 +00003226
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003227 // If we have reached here, aggregates are passed either indirectly via a
3228 // byval pointer or directly by coercing to another structure type. In the
3229 // latter case, padding is inserted if the offset of the aggregate is
3230 // unaligned.
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00003231 llvm::Type *ResType = HandleAggregates(Ty);
Akira Hatanaka9659d592012-01-10 22:44:52 +00003232
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003233 if (!ResType)
3234 return ABIArgInfo::getIndirect(0);
3235
3236 return ABIArgInfo::getDirect(ResType, 0, getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00003237 }
3238
3239 // Treat an enum type as its underlying type.
3240 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3241 Ty = EnumTy->getDecl()->getIntegerType();
3242
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003243 if (Ty->isPromotableIntegerType())
3244 return ABIArgInfo::getExtend();
3245
3246 return ABIArgInfo::getDirect(0, 0, getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00003247}
3248
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003249llvm::Type*
3250MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakada54ff32012-02-09 18:49:26 +00003251 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003252 SmallVector<llvm::Type*, 2> RTList;
3253
Akira Hatanakada54ff32012-02-09 18:49:26 +00003254 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003255 const RecordDecl *RD = RT->getDecl();
Akira Hatanakada54ff32012-02-09 18:49:26 +00003256 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3257 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003258
Akira Hatanakada54ff32012-02-09 18:49:26 +00003259 // N32/64 returns struct/classes in floating point registers if the
3260 // following conditions are met:
3261 // 1. The size of the struct/class is no larger than 128-bit.
3262 // 2. The struct/class has one or two fields all of which are floating
3263 // point types.
3264 // 3. The offset of the first field is zero (this follows what gcc does).
3265 //
3266 // Any other composite results are returned in integer registers.
3267 //
3268 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
3269 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
3270 for (; b != e; ++b) {
3271 const BuiltinType *BT = (*b)->getType()->getAs<BuiltinType>();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003272
Akira Hatanakada54ff32012-02-09 18:49:26 +00003273 if (!BT || !BT->isFloatingPoint())
3274 break;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003275
Akira Hatanakada54ff32012-02-09 18:49:26 +00003276 RTList.push_back(CGT.ConvertType((*b)->getType()));
3277 }
3278
3279 if (b == e)
3280 return llvm::StructType::get(getVMContext(), RTList,
3281 RD->hasAttr<PackedAttr>());
3282
3283 RTList.clear();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003284 }
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003285 }
3286
3287 RTList.push_back(llvm::IntegerType::get(getVMContext(),
3288 std::min(Size, (uint64_t)64)));
3289 if (Size > 64)
3290 RTList.push_back(llvm::IntegerType::get(getVMContext(), Size - 64));
3291
3292 return llvm::StructType::get(getVMContext(), RTList);
3293}
3294
Akira Hatanaka619e8872011-06-02 00:09:17 +00003295ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanakaa8536c02012-01-23 23:18:57 +00003296 uint64_t Size = getContext().getTypeSize(RetTy);
3297
3298 if (RetTy->isVoidType() || Size == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00003299 return ABIArgInfo::getIgnore();
3300
3301 if (isAggregateTypeForABI(RetTy)) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003302 if (Size <= 128) {
3303 if (RetTy->isAnyComplexType())
3304 return ABIArgInfo::getDirect();
3305
Akira Hatanaka526cdfb2012-02-08 01:31:22 +00003306 if (!IsO32 && !isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003307 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
3308 }
Akira Hatanaka619e8872011-06-02 00:09:17 +00003309
3310 return ABIArgInfo::getIndirect(0);
3311 }
3312
3313 // Treat an enum type as its underlying type.
3314 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3315 RetTy = EnumTy->getDecl()->getIntegerType();
3316
3317 return (RetTy->isPromotableIntegerType() ?
3318 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3319}
3320
3321void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanakacc662542012-01-12 01:10:09 +00003322 ABIArgInfo &RetInfo = FI.getReturnInfo();
3323 RetInfo = classifyReturnType(FI.getReturnType());
3324
3325 // Check if a pointer to an aggregate is passed as a hidden argument.
3326 uint64_t Offset = RetInfo.isIndirect() ? 8 : 0;
3327
Akira Hatanaka619e8872011-06-02 00:09:17 +00003328 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3329 it != ie; ++it)
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003330 it->info = classifyArgumentType(it->type, Offset);
Akira Hatanaka619e8872011-06-02 00:09:17 +00003331}
3332
3333llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3334 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003335 llvm::Type *BP = CGF.Int8PtrTy;
3336 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003337
3338 CGBuilderTy &Builder = CGF.Builder;
3339 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3340 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003341 int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003342 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3343 llvm::Value *AddrTyped;
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003344 unsigned PtrWidth = getContext().getTargetInfo().getPointerWidth(0);
3345 llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003346
3347 if (TypeAlign > MinABIStackAlignInBytes) {
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003348 llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
3349 llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
3350 llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
3351 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003352 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
3353 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
3354 }
3355 else
3356 AddrTyped = Builder.CreateBitCast(Addr, PTy);
3357
3358 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003359 TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003360 uint64_t Offset =
3361 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
3362 llvm::Value *NextAddr =
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003363 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003364 "ap.next");
3365 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3366
3367 return AddrTyped;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003368}
3369
John McCallaeeb7012010-05-27 06:19:26 +00003370bool
3371MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3372 llvm::Value *Address) const {
3373 // This information comes from gcc's implementation, which seems to
3374 // as canonical as it gets.
3375
John McCallaeeb7012010-05-27 06:19:26 +00003376 // Everything on MIPS is 4 bytes. Double-precision FP registers
3377 // are aliased to pairs of single-precision FP registers.
Chris Lattner8b418682012-02-07 00:39:47 +00003378 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCallaeeb7012010-05-27 06:19:26 +00003379
3380 // 0-31 are the general purpose registers, $0 - $31.
3381 // 32-63 are the floating-point registers, $f0 - $f31.
3382 // 64 and 65 are the multiply/divide registers, $hi and $lo.
3383 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattner8b418682012-02-07 00:39:47 +00003384 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCallaeeb7012010-05-27 06:19:26 +00003385
3386 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
3387 // They are one bit wide and ignored here.
3388
3389 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
3390 // (coprocessor 1 is the FP unit)
3391 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
3392 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
3393 // 176-181 are the DSP accumulator registers.
Chris Lattner8b418682012-02-07 00:39:47 +00003394 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCallaeeb7012010-05-27 06:19:26 +00003395 return false;
3396}
3397
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003398//===----------------------------------------------------------------------===//
3399// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
3400// Currently subclassed only to implement custom OpenCL C function attribute
3401// handling.
3402//===----------------------------------------------------------------------===//
3403
3404namespace {
3405
3406class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
3407public:
3408 TCETargetCodeGenInfo(CodeGenTypes &CGT)
3409 : DefaultTargetCodeGenInfo(CGT) {}
3410
3411 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3412 CodeGen::CodeGenModule &M) const;
3413};
3414
3415void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3416 llvm::GlobalValue *GV,
3417 CodeGen::CodeGenModule &M) const {
3418 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3419 if (!FD) return;
3420
3421 llvm::Function *F = cast<llvm::Function>(GV);
3422
David Blaikie4e4d0842012-03-11 07:00:24 +00003423 if (M.getLangOpts().OpenCL) {
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003424 if (FD->hasAttr<OpenCLKernelAttr>()) {
3425 // OpenCL C Kernel functions are not subject to inlining
3426 F->addFnAttr(llvm::Attribute::NoInline);
3427
3428 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
3429
3430 // Convert the reqd_work_group_size() attributes to metadata.
3431 llvm::LLVMContext &Context = F->getContext();
3432 llvm::NamedMDNode *OpenCLMetadata =
3433 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
3434
3435 SmallVector<llvm::Value*, 5> Operands;
3436 Operands.push_back(F);
3437
Chris Lattner8b418682012-02-07 00:39:47 +00003438 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
3439 llvm::APInt(32,
3440 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
3441 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
3442 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003443 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
Chris Lattner8b418682012-02-07 00:39:47 +00003444 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
3445 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003446 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
3447
3448 // Add a boolean constant operand for "required" (true) or "hint" (false)
3449 // for implementing the work_group_size_hint attr later. Currently
3450 // always true as the hint is not yet implemented.
Chris Lattner8b418682012-02-07 00:39:47 +00003451 Operands.push_back(llvm::ConstantInt::getTrue(Context));
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003452 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
3453 }
3454 }
3455 }
3456}
3457
3458}
John McCallaeeb7012010-05-27 06:19:26 +00003459
Tony Linthicum96319392011-12-12 21:14:55 +00003460//===----------------------------------------------------------------------===//
3461// Hexagon ABI Implementation
3462//===----------------------------------------------------------------------===//
3463
3464namespace {
3465
3466class HexagonABIInfo : public ABIInfo {
3467
3468
3469public:
3470 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3471
3472private:
3473
3474 ABIArgInfo classifyReturnType(QualType RetTy) const;
3475 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3476
3477 virtual void computeInfo(CGFunctionInfo &FI) const;
3478
3479 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3480 CodeGenFunction &CGF) const;
3481};
3482
3483class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
3484public:
3485 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
3486 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
3487
3488 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3489 return 29;
3490 }
3491};
3492
3493}
3494
3495void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
3496 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3497 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3498 it != ie; ++it)
3499 it->info = classifyArgumentType(it->type);
3500}
3501
3502ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
3503 if (!isAggregateTypeForABI(Ty)) {
3504 // Treat an enum type as its underlying type.
3505 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3506 Ty = EnumTy->getDecl()->getIntegerType();
3507
3508 return (Ty->isPromotableIntegerType() ?
3509 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3510 }
3511
3512 // Ignore empty records.
3513 if (isEmptyRecord(getContext(), Ty, true))
3514 return ABIArgInfo::getIgnore();
3515
3516 // Structures with either a non-trivial destructor or a non-trivial
3517 // copy constructor are always indirect.
3518 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
3519 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3520
3521 uint64_t Size = getContext().getTypeSize(Ty);
3522 if (Size > 64)
3523 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
3524 // Pass in the smallest viable integer type.
3525 else if (Size > 32)
3526 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
3527 else if (Size > 16)
3528 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
3529 else if (Size > 8)
3530 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3531 else
3532 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
3533}
3534
3535ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
3536 if (RetTy->isVoidType())
3537 return ABIArgInfo::getIgnore();
3538
3539 // Large vector types should be returned via memory.
3540 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
3541 return ABIArgInfo::getIndirect(0);
3542
3543 if (!isAggregateTypeForABI(RetTy)) {
3544 // Treat an enum type as its underlying type.
3545 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3546 RetTy = EnumTy->getDecl()->getIntegerType();
3547
3548 return (RetTy->isPromotableIntegerType() ?
3549 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3550 }
3551
3552 // Structures with either a non-trivial destructor or a non-trivial
3553 // copy constructor are always indirect.
3554 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
3555 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3556
3557 if (isEmptyRecord(getContext(), RetTy, true))
3558 return ABIArgInfo::getIgnore();
3559
3560 // Aggregates <= 8 bytes are returned in r0; other aggregates
3561 // are returned indirectly.
3562 uint64_t Size = getContext().getTypeSize(RetTy);
3563 if (Size <= 64) {
3564 // Return in the smallest viable integer type.
3565 if (Size <= 8)
3566 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
3567 if (Size <= 16)
3568 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3569 if (Size <= 32)
3570 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
3571 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
3572 }
3573
3574 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
3575}
3576
3577llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner8b418682012-02-07 00:39:47 +00003578 CodeGenFunction &CGF) const {
Tony Linthicum96319392011-12-12 21:14:55 +00003579 // FIXME: Need to handle alignment
Chris Lattner8b418682012-02-07 00:39:47 +00003580 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Tony Linthicum96319392011-12-12 21:14:55 +00003581
3582 CGBuilderTy &Builder = CGF.Builder;
3583 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
3584 "ap");
3585 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
3586 llvm::Type *PTy =
3587 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3588 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3589
3590 uint64_t Offset =
3591 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
3592 llvm::Value *NextAddr =
3593 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
3594 "ap.next");
3595 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3596
3597 return AddrTyped;
3598}
3599
3600
Chris Lattnerea044322010-07-29 02:01:43 +00003601const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003602 if (TheTargetCodeGenInfo)
3603 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003604
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003605 const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00003606 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003607 default:
Chris Lattnerea044322010-07-29 02:01:43 +00003608 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003609
John McCallaeeb7012010-05-27 06:19:26 +00003610 case llvm::Triple::mips:
3611 case llvm::Triple::mipsel:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003612 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCallaeeb7012010-05-27 06:19:26 +00003613
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00003614 case llvm::Triple::mips64:
3615 case llvm::Triple::mips64el:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003616 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00003617
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003618 case llvm::Triple::arm:
3619 case llvm::Triple::thumb:
Sandeep Patel34c1af82011-04-05 00:23:47 +00003620 {
3621 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003622
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003623 if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
Sandeep Patel34c1af82011-04-05 00:23:47 +00003624 Kind = ARMABIInfo::APCS;
3625 else if (CodeGenOpts.FloatABI == "hard")
3626 Kind = ARMABIInfo::AAPCS_VFP;
3627
3628 return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind));
3629 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003630
John McCallec853ba2010-03-11 00:10:12 +00003631 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00003632 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00003633
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003634 case llvm::Triple::ptx32:
3635 case llvm::Triple::ptx64:
3636 return *(TheTargetCodeGenInfo = new PTXTargetCodeGenInfo(Types));
3637
Wesley Peck276fdf42010-12-19 19:57:51 +00003638 case llvm::Triple::mblaze:
3639 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
3640
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003641 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00003642 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003643
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003644 case llvm::Triple::tce:
3645 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
3646
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003647 case llvm::Triple::x86: {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003648 bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003649
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00003650 if (Triple.isOSDarwin())
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003651 return *(TheTargetCodeGenInfo =
Eli Friedman55fc7e22012-01-25 22:46:34 +00003652 new X86_32TargetCodeGenInfo(
3653 Types, true, true, DisableMMX, false));
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00003654
3655 switch (Triple.getOS()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003656 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003657 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00003658 case llvm::Triple::AuroraUX:
3659 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00003660 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003661 case llvm::Triple::OpenBSD:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003662 return *(TheTargetCodeGenInfo =
Eli Friedman55fc7e22012-01-25 22:46:34 +00003663 new X86_32TargetCodeGenInfo(
3664 Types, false, true, DisableMMX, false));
3665
3666 case llvm::Triple::Win32:
3667 return *(TheTargetCodeGenInfo =
3668 new X86_32TargetCodeGenInfo(
3669 Types, false, true, DisableMMX, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003670
3671 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003672 return *(TheTargetCodeGenInfo =
Eli Friedman55fc7e22012-01-25 22:46:34 +00003673 new X86_32TargetCodeGenInfo(
3674 Types, false, false, DisableMMX, false));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003675 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003676 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003677
Eli Friedmanee1ad992011-12-02 00:11:43 +00003678 case llvm::Triple::x86_64: {
3679 bool HasAVX = strcmp(getContext().getTargetInfo().getABI(), "avx") == 0;
3680
Chris Lattnerf13721d2010-08-31 16:44:54 +00003681 switch (Triple.getOS()) {
3682 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00003683 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00003684 case llvm::Triple::Cygwin:
3685 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
3686 default:
Eli Friedmanee1ad992011-12-02 00:11:43 +00003687 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
3688 HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00003689 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003690 }
Tony Linthicum96319392011-12-12 21:14:55 +00003691 case llvm::Triple::hexagon:
3692 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
Eli Friedmanee1ad992011-12-02 00:11:43 +00003693 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003694}