blob: ed89e2400810a843de9a85c64e015bb543b5deb9 [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"
Daniel Dunbar2c0843f2009-08-24 08:52:16 +000020#include "llvm/ADT/Triple.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000021#include "llvm/IR/DataLayout.h"
22#include "llvm/IR/Type.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) {
John McCall9d232c82013-03-07 21:37:08 +000040 return !CodeGenFunction::hasScalarEvaluationKind(T) ||
John McCalld608cdb2010-08-22 10:59:02 +000041 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
Micah Villmow25a6a842012-10-08 16:25:52 +000054const llvm::DataLayout &ABIInfo::getDataLayout() const {
55 return CGT.getDataLayout();
Chris Lattnerea044322010-07-29 02:01:43 +000056}
57
John McCall64aa4b32013-04-16 22:48:15 +000058const TargetInfo &ABIInfo::getTarget() const {
59 return CGT.getTarget();
60}
Chris Lattnerea044322010-07-29 02:01:43 +000061
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000062void ABIArgInfo::dump() const {
Chris Lattner5f9e2722011-07-23 10:55:15 +000063 raw_ostream &OS = llvm::errs();
Daniel Dunbar28df7a52009-12-03 09:13:49 +000064 OS << "(ABIArgInfo Kind=";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000065 switch (TheKind) {
66 case Direct:
Chris Lattner800588f2010-07-29 06:26:06 +000067 OS << "Direct Type=";
Chris Lattner2acc6e32011-07-18 04:24:23 +000068 if (llvm::Type *Ty = getCoerceToType())
Chris Lattner800588f2010-07-29 06:26:06 +000069 Ty->print(OS);
70 else
71 OS << "null";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000072 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000073 case Extend:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000074 OS << "Extend";
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000075 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000076 case Ignore:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000077 OS << "Ignore";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000078 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000079 case Indirect:
Daniel Dunbardc6d5742010-04-21 19:10:51 +000080 OS << "Indirect Align=" << getIndirectAlign()
Joerg Sonnenbergere9b5d772011-07-15 18:23:44 +000081 << " ByVal=" << getIndirectByVal()
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +000082 << " Realign=" << getIndirectRealign();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000083 break;
84 case Expand:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000085 OS << "Expand";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000086 break;
87 }
Daniel Dunbar28df7a52009-12-03 09:13:49 +000088 OS << ")\n";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000089}
90
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000091TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
92
John McCall49e34be2011-08-30 01:42:09 +000093// If someone can figure out a general rule for this, that would be great.
94// It's probably just doomed to be platform-dependent, though.
95unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
96 // Verified for:
97 // x86-64 FreeBSD, Linux, Darwin
98 // x86-32 FreeBSD, Linux, Darwin
99 // PowerPC Linux, Darwin
100 // ARM Darwin (*not* EABI)
Tim Northoverc264e162013-01-31 12:13:10 +0000101 // AArch64 Linux
John McCall49e34be2011-08-30 01:42:09 +0000102 return 32;
103}
104
John McCallde5d3c72012-02-17 03:33:10 +0000105bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
106 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +0000107 // The following conventions are known to require this to be false:
108 // x86_stdcall
109 // MIPS
110 // For everything else, we just prefer false unless we opt out.
111 return false;
112}
113
Daniel Dunbar98303b92009-09-13 08:03:58 +0000114static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000115
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000116/// isEmptyField - Return true iff a the field is "empty", that is it
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000117/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +0000118static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
119 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000120 if (FD->isUnnamedBitfield())
121 return true;
122
123 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000124
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000125 // Constant arrays of empty records count as empty, strip them off.
126 // Constant arrays of zero length always count as empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000127 if (AllowArrays)
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000128 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
129 if (AT->getSize() == 0)
130 return true;
Daniel Dunbar98303b92009-09-13 08:03:58 +0000131 FT = AT->getElementType();
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000132 }
Daniel Dunbar98303b92009-09-13 08:03:58 +0000133
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000134 const RecordType *RT = FT->getAs<RecordType>();
135 if (!RT)
136 return false;
137
138 // C++ record fields are never empty, at least in the Itanium ABI.
139 //
140 // FIXME: We should use a predicate for whether this behavior is true in the
141 // current ABI.
142 if (isa<CXXRecordDecl>(RT->getDecl()))
143 return false;
144
Daniel Dunbar98303b92009-09-13 08:03:58 +0000145 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000146}
147
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000148/// isEmptyRecord - Return true iff a structure contains only empty
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000149/// fields. Note that a structure with a flexible array member is not
150/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000151static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000152 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000153 if (!RT)
154 return 0;
155 const RecordDecl *RD = RT->getDecl();
156 if (RD->hasFlexibleArrayMember())
157 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000158
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000159 // If this is a C++ record, check the bases first.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000160 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000161 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
162 e = CXXRD->bases_end(); i != e; ++i)
163 if (!isEmptyRecord(Context, i->getType(), true))
164 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000165
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000166 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
167 i != e; ++i)
David Blaikie581deb32012-06-06 20:45:41 +0000168 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000169 return false;
170 return true;
171}
172
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000173/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
174/// a non-trivial destructor or a non-trivial copy constructor.
175static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
176 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
177 if (!RD)
178 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000179
Richard Smith426391c2012-11-16 00:53:38 +0000180 return !RD->hasTrivialDestructor() || RD->hasNonTrivialCopyConstructor();
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000181}
182
183/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
184/// a record type with either a non-trivial destructor or a non-trivial copy
185/// constructor.
186static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
187 const RecordType *RT = T->getAs<RecordType>();
188 if (!RT)
189 return false;
190
191 return hasNonTrivialDestructorOrCopyConstructor(RT);
192}
193
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000194/// isSingleElementStruct - Determine if a structure is a "single
195/// element struct", i.e. it has exactly one non-empty field or
196/// exactly one field which is itself a single element
197/// struct. Structures with flexible array members are never
198/// considered single element structs.
199///
200/// \return The field declaration for the single non-empty field, if
201/// it exists.
202static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
203 const RecordType *RT = T->getAsStructureType();
204 if (!RT)
205 return 0;
206
207 const RecordDecl *RD = RT->getDecl();
208 if (RD->hasFlexibleArrayMember())
209 return 0;
210
211 const Type *Found = 0;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000212
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000213 // If this is a C++ record, check the bases first.
214 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
215 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
216 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000217 // Ignore empty records.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000218 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000219 continue;
220
221 // If we already found an element then this isn't a single-element struct.
222 if (Found)
223 return 0;
224
225 // If this is non-empty and not a single element struct, the composite
226 // cannot be a single element struct.
227 Found = isSingleElementStruct(i->getType(), Context);
228 if (!Found)
229 return 0;
230 }
231 }
232
233 // Check for single element.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000234 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
235 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000236 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000237 QualType FT = FD->getType();
238
239 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000240 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000241 continue;
242
243 // If we already found an element then this isn't a single-element
244 // struct.
245 if (Found)
246 return 0;
247
248 // Treat single element arrays as the element.
249 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
250 if (AT->getSize().getZExtValue() != 1)
251 break;
252 FT = AT->getElementType();
253 }
254
John McCalld608cdb2010-08-22 10:59:02 +0000255 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000256 Found = FT.getTypePtr();
257 } else {
258 Found = isSingleElementStruct(FT, Context);
259 if (!Found)
260 return 0;
261 }
262 }
263
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000264 // We don't consider a struct a single-element struct if it has
265 // padding beyond the element type.
266 if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
267 return 0;
268
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000269 return Found;
270}
271
272static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Eli Friedmandb748a32012-11-29 23:21:04 +0000273 // Treat complex types as the element type.
274 if (const ComplexType *CTy = Ty->getAs<ComplexType>())
275 Ty = CTy->getElementType();
276
277 // Check for a type which we know has a simple scalar argument-passing
278 // convention without any padding. (We're specifically looking for 32
279 // and 64-bit integer and integer-equivalents, float, and double.)
Daniel Dunbara1842d32010-05-14 03:40:53 +0000280 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Eli Friedmandb748a32012-11-29 23:21:04 +0000281 !Ty->isEnumeralType() && !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000282 return false;
283
284 uint64_t Size = Context.getTypeSize(Ty);
285 return Size == 32 || Size == 64;
286}
287
Daniel Dunbar53012f42009-11-09 01:33:53 +0000288/// canExpandIndirectArgument - Test whether an argument type which is to be
289/// passed indirectly (on the stack) would have the equivalent layout if it was
290/// expanded into separate arguments. If so, we prefer to do the latter to avoid
291/// inhibiting optimizations.
292///
293// FIXME: This predicate is missing many cases, currently it just follows
294// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
295// should probably make this smarter, or better yet make the LLVM backend
296// capable of handling it.
297static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
298 // We can only expand structure types.
299 const RecordType *RT = Ty->getAs<RecordType>();
300 if (!RT)
301 return false;
302
303 // We can only expand (C) structures.
304 //
305 // FIXME: This needs to be generalized to handle classes as well.
306 const RecordDecl *RD = RT->getDecl();
307 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
308 return false;
309
Eli Friedman506d4e32011-11-18 01:32:26 +0000310 uint64_t Size = 0;
311
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000312 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
313 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000314 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000315
316 if (!is32Or64BitBasicType(FD->getType(), Context))
317 return false;
318
319 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
320 // how to expand them yet, and the predicate for telling if a bitfield still
321 // counts as "basic" is more complicated than what we were doing previously.
322 if (FD->isBitField())
323 return false;
Eli Friedman506d4e32011-11-18 01:32:26 +0000324
325 Size += Context.getTypeSize(FD->getType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000326 }
327
Eli Friedman506d4e32011-11-18 01:32:26 +0000328 // Make sure there are not any holes in the struct.
329 if (Size != Context.getTypeSize(Ty))
330 return false;
331
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000332 return true;
333}
334
335namespace {
336/// DefaultABIInfo - The default implementation for ABI specific
337/// details. This implementation provides information which results in
338/// self-consistent and sensible LLVM IR generation, but does not
339/// conform to any particular ABI.
340class DefaultABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +0000341public:
342 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000343
Chris Lattnera3c109b2010-07-29 02:16:43 +0000344 ABIArgInfo classifyReturnType(QualType RetTy) const;
345 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000346
Chris Lattneree5dcd02010-07-29 02:31:05 +0000347 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000348 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000349 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
350 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000351 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000352 }
353
354 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
355 CodeGenFunction &CGF) const;
356};
357
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000358class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
359public:
Chris Lattnerea044322010-07-29 02:01:43 +0000360 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
361 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000362};
363
364llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
365 CodeGenFunction &CGF) const {
366 return 0;
367}
368
Chris Lattnera3c109b2010-07-29 02:16:43 +0000369ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
Jan Wen Voung90306932011-11-03 00:59:44 +0000370 if (isAggregateTypeForABI(Ty)) {
371 // Records with non trivial destructors/constructors should not be passed
372 // by value.
373 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
374 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
375
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000376 return ABIArgInfo::getIndirect(0);
Jan Wen Voung90306932011-11-03 00:59:44 +0000377 }
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000378
Chris Lattnera14db752010-03-11 18:19:55 +0000379 // Treat an enum type as its underlying type.
380 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
381 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000382
Chris Lattnera14db752010-03-11 18:19:55 +0000383 return (Ty->isPromotableIntegerType() ?
384 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000385}
386
Bob Wilson0024f942011-01-10 23:54:17 +0000387ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
388 if (RetTy->isVoidType())
389 return ABIArgInfo::getIgnore();
390
391 if (isAggregateTypeForABI(RetTy))
392 return ABIArgInfo::getIndirect(0);
393
394 // Treat an enum type as its underlying type.
395 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
396 RetTy = EnumTy->getDecl()->getIntegerType();
397
398 return (RetTy->isPromotableIntegerType() ?
399 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
400}
401
Derek Schuff9ed63f82012-09-06 17:37:28 +0000402//===----------------------------------------------------------------------===//
403// le32/PNaCl bitcode ABI Implementation
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000404//
405// This is a simplified version of the x86_32 ABI. Arguments and return values
406// are always passed on the stack.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000407//===----------------------------------------------------------------------===//
408
409class PNaClABIInfo : public ABIInfo {
410 public:
411 PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
412
413 ABIArgInfo classifyReturnType(QualType RetTy) const;
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000414 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Derek Schuff9ed63f82012-09-06 17:37:28 +0000415
416 virtual void computeInfo(CGFunctionInfo &FI) const;
417 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
418 CodeGenFunction &CGF) const;
419};
420
421class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
422 public:
423 PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
424 : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {}
425};
426
427void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
428 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
429
Derek Schuff9ed63f82012-09-06 17:37:28 +0000430 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
431 it != ie; ++it)
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000432 it->info = classifyArgumentType(it->type);
Derek Schuff9ed63f82012-09-06 17:37:28 +0000433 }
434
435llvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
436 CodeGenFunction &CGF) const {
437 return 0;
438}
439
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000440/// \brief Classify argument of given type \p Ty.
441ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const {
Derek Schuff9ed63f82012-09-06 17:37:28 +0000442 if (isAggregateTypeForABI(Ty)) {
Eli Benderskye45dfd12013-04-04 22:49:35 +0000443 // In the PNaCl ABI we always pass records/structures on the stack. The
444 // byval attribute can be used if the record doesn't have non-trivial
445 // constructors/destructors.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000446 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
447 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Derek Schuff9ed63f82012-09-06 17:37:28 +0000448 return ABIArgInfo::getIndirect(0);
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000449 } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
450 // Treat an enum type as its underlying type.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000451 Ty = EnumTy->getDecl()->getIntegerType();
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000452 } else if (Ty->isFloatingType()) {
453 // Floating-point types don't go inreg.
454 return ABIArgInfo::getDirect();
Derek Schuff9ed63f82012-09-06 17:37:28 +0000455 }
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000456
457 return (Ty->isPromotableIntegerType() ?
458 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Derek Schuff9ed63f82012-09-06 17:37:28 +0000459}
460
461ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
462 if (RetTy->isVoidType())
463 return ABIArgInfo::getIgnore();
464
Eli Benderskye45dfd12013-04-04 22:49:35 +0000465 // In the PNaCl ABI we always return records/structures on the stack.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000466 if (isAggregateTypeForABI(RetTy))
467 return ABIArgInfo::getIndirect(0);
468
469 // Treat an enum type as its underlying type.
470 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
471 RetTy = EnumTy->getDecl()->getIntegerType();
472
473 return (RetTy->isPromotableIntegerType() ?
474 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
475}
476
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000477/// IsX86_MMXType - Return true if this is an MMX type.
478bool IsX86_MMXType(llvm::Type *IRType) {
479 // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>.
Bill Wendlingbb465d72010-10-18 03:41:31 +0000480 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
481 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
482 IRType->getScalarSizeInBits() != 64;
483}
484
Jay Foadef6de3d2011-07-11 09:56:20 +0000485static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000486 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000487 llvm::Type* Ty) {
Bill Wendling0507be62011-03-07 22:47:14 +0000488 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy())
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000489 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
490 return Ty;
491}
492
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000493//===----------------------------------------------------------------------===//
494// X86-32 ABI Implementation
495//===----------------------------------------------------------------------===//
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000496
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000497/// X86_32ABIInfo - The X86-32 ABI information.
498class X86_32ABIInfo : public ABIInfo {
Rafael Espindolab48280b2012-07-31 02:44:24 +0000499 enum Class {
500 Integer,
501 Float
502 };
503
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000504 static const unsigned MinABIStackAlignInBytes = 4;
505
David Chisnall1e4249c2009-08-17 23:08:21 +0000506 bool IsDarwinVectorABI;
507 bool IsSmallStructInRegABI;
Eli Friedman55fc7e22012-01-25 22:46:34 +0000508 bool IsWin32FloatStructABI;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000509 unsigned DefaultNumRegisterParameters;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000510
511 static bool isRegisterSize(unsigned Size) {
512 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
513 }
514
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000515 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context,
516 unsigned callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000517
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000518 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
519 /// such that the argument will be passed in memory.
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000520 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal,
521 unsigned &FreeRegs) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000522
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000523 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbare59d8582010-09-16 20:42:06 +0000524 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000525
Rafael Espindolab48280b2012-07-31 02:44:24 +0000526 Class classify(QualType Ty) const;
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000527 ABIArgInfo classifyReturnType(QualType RetTy,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000528 unsigned callingConvention) const;
Rafael Espindolab6932692012-10-24 01:58:58 +0000529 ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &FreeRegs,
530 bool IsFastCall) const;
531 bool shouldUseInReg(QualType Ty, unsigned &FreeRegs,
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000532 bool IsFastCall, bool &NeedsPadding) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000533
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000534public:
535
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000536 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000537 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
538 CodeGenFunction &CGF) const;
539
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000540 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool w,
Rafael Espindolab48280b2012-07-31 02:44:24 +0000541 unsigned r)
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000542 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000543 IsWin32FloatStructABI(w), DefaultNumRegisterParameters(r) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000544};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000545
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000546class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
547public:
Eli Friedman55fc7e22012-01-25 22:46:34 +0000548 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000549 bool d, bool p, bool w, unsigned r)
550 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, w, r)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000551
552 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
553 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000554
555 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
556 // Darwin uses different dwarf register numbers for EH.
John McCall64aa4b32013-04-16 22:48:15 +0000557 if (CGM.getTarget().getTriple().isOSDarwin()) return 5;
John McCall6374c332010-03-06 00:35:14 +0000558 return 4;
559 }
560
561 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
562 llvm::Value *Address) const;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000563
Jay Foadef6de3d2011-07-11 09:56:20 +0000564 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000565 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000566 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000567 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
568 }
569
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000570};
571
572}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000573
574/// shouldReturnTypeInRegister - Determine if the given type should be
575/// passed in a register (for the Darwin ABI).
576bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000577 ASTContext &Context,
578 unsigned callingConvention) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000579 uint64_t Size = Context.getTypeSize(Ty);
580
581 // Type must be register sized.
582 if (!isRegisterSize(Size))
583 return false;
584
585 if (Ty->isVectorType()) {
586 // 64- and 128- bit vectors inside structures are not returned in
587 // registers.
588 if (Size == 64 || Size == 128)
589 return false;
590
591 return true;
592 }
593
Daniel Dunbar77115232010-05-15 00:00:30 +0000594 // If this is a builtin, pointer, enum, complex type, member pointer, or
595 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000596 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000597 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000598 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000599 return true;
600
601 // Arrays are treated like records.
602 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000603 return shouldReturnTypeInRegister(AT->getElementType(), Context,
604 callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000605
606 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000607 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000608 if (!RT) return false;
609
Anders Carlssona8874232010-01-27 03:25:19 +0000610 // FIXME: Traverse bases here too.
611
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000612 // For thiscall conventions, structures will never be returned in
613 // a register. This is for compatibility with the MSVC ABI
614 if (callingConvention == llvm::CallingConv::X86_ThisCall &&
615 RT->isStructureType()) {
616 return false;
617 }
618
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000619 // Structure types are passed in register if all fields would be
620 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000621 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
622 e = RT->getDecl()->field_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000623 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000624
625 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000626 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000627 continue;
628
629 // Check fields recursively.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000630 if (!shouldReturnTypeInRegister(FD->getType(), Context,
631 callingConvention))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000632 return false;
633 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000634 return true;
635}
636
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000637ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
638 unsigned callingConvention) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000639 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000640 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000641
Chris Lattnera3c109b2010-07-29 02:16:43 +0000642 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000643 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000644 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000645 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000646
647 // 128-bit vectors are a special case; they are returned in
648 // registers and we need to make sure to pick a type the LLVM
649 // backend will like.
650 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +0000651 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000652 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000653
654 // Always return in register if it fits in a general purpose
655 // register, or if it is 64 bits and has a single element.
656 if ((Size == 8 || Size == 16 || Size == 32) ||
657 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +0000658 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000659 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000660
661 return ABIArgInfo::getIndirect(0);
662 }
663
664 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000665 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000666
John McCalld608cdb2010-08-22 10:59:02 +0000667 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000668 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000669 // Structures with either a non-trivial destructor or a non-trivial
670 // copy constructor are always indirect.
671 if (hasNonTrivialDestructorOrCopyConstructor(RT))
672 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000673
Anders Carlsson40092972009-10-20 22:07:59 +0000674 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000675 if (RT->getDecl()->hasFlexibleArrayMember())
676 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000677 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000678
David Chisnall1e4249c2009-08-17 23:08:21 +0000679 // If specified, structs and unions are always indirect.
680 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000681 return ABIArgInfo::getIndirect(0);
682
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000683 // Small structures which are register sized are generally returned
684 // in a register.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000685 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(),
686 callingConvention)) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000687 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000688
689 // As a special-case, if the struct is a "single-element" struct, and
690 // the field is of type "float" or "double", return it in a
Eli Friedman55fc7e22012-01-25 22:46:34 +0000691 // floating-point register. (MSVC does not apply this special case.)
692 // We apply a similar transformation for pointer types to improve the
693 // quality of the generated IR.
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000694 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
Eli Friedman55fc7e22012-01-25 22:46:34 +0000695 if ((!IsWin32FloatStructABI && SeltTy->isRealFloatingType())
696 || SeltTy->hasPointerRepresentation())
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000697 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
698
699 // FIXME: We should be able to narrow this integer in cases with dead
700 // padding.
Chris Lattner800588f2010-07-29 06:26:06 +0000701 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000702 }
703
704 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000705 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000706
Chris Lattnera3c109b2010-07-29 02:16:43 +0000707 // Treat an enum type as its underlying type.
708 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
709 RetTy = EnumTy->getDecl()->getIntegerType();
710
711 return (RetTy->isPromotableIntegerType() ?
712 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000713}
714
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000715static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
716 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
717}
718
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000719static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
720 const RecordType *RT = Ty->getAs<RecordType>();
721 if (!RT)
722 return 0;
723 const RecordDecl *RD = RT->getDecl();
724
725 // If this is a C++ record, check the bases first.
726 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
727 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
728 e = CXXRD->bases_end(); i != e; ++i)
729 if (!isRecordWithSSEVectorType(Context, i->getType()))
730 return false;
731
732 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
733 i != e; ++i) {
734 QualType FT = i->getType();
735
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000736 if (isSSEVectorType(Context, FT))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000737 return true;
738
739 if (isRecordWithSSEVectorType(Context, FT))
740 return true;
741 }
742
743 return false;
744}
745
Daniel Dunbare59d8582010-09-16 20:42:06 +0000746unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
747 unsigned Align) const {
748 // Otherwise, if the alignment is less than or equal to the minimum ABI
749 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000750 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +0000751 return 0; // Use default alignment.
752
753 // On non-Darwin, the stack type alignment is always 4.
754 if (!IsDarwinVectorABI) {
755 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000756 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +0000757 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000758
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000759 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000760 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
761 isRecordWithSSEVectorType(getContext(), Ty)))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000762 return 16;
763
764 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000765}
766
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000767ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
768 unsigned &FreeRegs) const {
769 if (!ByVal) {
770 if (FreeRegs) {
771 --FreeRegs; // Non byval indirects just use one pointer.
772 return ABIArgInfo::getIndirectInReg(0, false);
773 }
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000774 return ABIArgInfo::getIndirect(0, false);
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000775 }
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000776
Daniel Dunbare59d8582010-09-16 20:42:06 +0000777 // Compute the byval alignment.
778 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
779 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
780 if (StackAlign == 0)
Chris Lattnerde92d732011-05-22 23:35:00 +0000781 return ABIArgInfo::getIndirect(4);
Daniel Dunbare59d8582010-09-16 20:42:06 +0000782
783 // If the stack alignment is less than the type alignment, realign the
784 // argument.
785 if (StackAlign < TypeAlign)
786 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
787 /*Realign=*/true);
788
789 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000790}
791
Rafael Espindolab48280b2012-07-31 02:44:24 +0000792X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
793 const Type *T = isSingleElementStruct(Ty, getContext());
794 if (!T)
795 T = Ty.getTypePtr();
796
797 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
798 BuiltinType::Kind K = BT->getKind();
799 if (K == BuiltinType::Float || K == BuiltinType::Double)
800 return Float;
801 }
802 return Integer;
803}
804
Rafael Espindolab6932692012-10-24 01:58:58 +0000805bool X86_32ABIInfo::shouldUseInReg(QualType Ty, unsigned &FreeRegs,
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000806 bool IsFastCall, bool &NeedsPadding) const {
807 NeedsPadding = false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000808 Class C = classify(Ty);
809 if (C == Float)
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000810 return false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000811
Rafael Espindolab6932692012-10-24 01:58:58 +0000812 unsigned Size = getContext().getTypeSize(Ty);
813 unsigned SizeInRegs = (Size + 31) / 32;
Rafael Espindola5f14fcb2012-10-23 02:04:01 +0000814
815 if (SizeInRegs == 0)
816 return false;
817
Rafael Espindolab48280b2012-07-31 02:44:24 +0000818 if (SizeInRegs > FreeRegs) {
819 FreeRegs = 0;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000820 return false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000821 }
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000822
Rafael Espindolab48280b2012-07-31 02:44:24 +0000823 FreeRegs -= SizeInRegs;
Rafael Espindolab6932692012-10-24 01:58:58 +0000824
825 if (IsFastCall) {
826 if (Size > 32)
827 return false;
828
829 if (Ty->isIntegralOrEnumerationType())
830 return true;
831
832 if (Ty->isPointerType())
833 return true;
834
835 if (Ty->isReferenceType())
836 return true;
837
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000838 if (FreeRegs)
839 NeedsPadding = true;
840
Rafael Espindolab6932692012-10-24 01:58:58 +0000841 return false;
842 }
843
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000844 return true;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000845}
846
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000847ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Rafael Espindolab6932692012-10-24 01:58:58 +0000848 unsigned &FreeRegs,
849 bool IsFastCall) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000850 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000851 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000852 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000853 if (const RecordType *RT = Ty->getAs<RecordType>()) {
854 // Structures with either a non-trivial destructor or a non-trivial
855 // copy constructor are always indirect.
856 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000857 return getIndirectResult(Ty, false, FreeRegs);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000858
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000859 if (RT->getDecl()->hasFlexibleArrayMember())
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000860 return getIndirectResult(Ty, true, FreeRegs);
Anders Carlssona8874232010-01-27 03:25:19 +0000861 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000862
Eli Friedman5a4d3522011-11-18 00:28:11 +0000863 // Ignore empty structs/unions.
Eli Friedman5a1ac892011-11-18 04:01:36 +0000864 if (isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000865 return ABIArgInfo::getIgnore();
866
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000867 llvm::LLVMContext &LLVMContext = getVMContext();
868 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
869 bool NeedsPadding;
870 if (shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding)) {
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000871 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000872 SmallVector<llvm::Type*, 3> Elements;
873 for (unsigned I = 0; I < SizeInRegs; ++I)
874 Elements.push_back(Int32);
875 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
876 return ABIArgInfo::getDirectInReg(Result);
877 }
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000878 llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : 0;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000879
Daniel Dunbar53012f42009-11-09 01:33:53 +0000880 // Expand small (<= 128-bit) record types when we know that the stack layout
881 // of those arguments will match the struct. This is important because the
882 // LLVM backend isn't smart enough to remove byval, which inhibits many
883 // optimizations.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000884 if (getContext().getTypeSize(Ty) <= 4*32 &&
885 canExpandIndirectArgument(Ty, getContext()))
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000886 return ABIArgInfo::getExpandWithPadding(IsFastCall, PaddingType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000887
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000888 return getIndirectResult(Ty, true, FreeRegs);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000889 }
890
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000891 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +0000892 // On Darwin, some vectors are passed in memory, we handle this by passing
893 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000894 if (IsDarwinVectorABI) {
895 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000896 if ((Size == 8 || Size == 16 || Size == 32) ||
897 (Size == 64 && VT->getNumElements() == 1))
898 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
899 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000900 }
Bill Wendlingbb465d72010-10-18 03:41:31 +0000901
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000902 if (IsX86_MMXType(CGT.ConvertType(Ty)))
903 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64));
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000904
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000905 return ABIArgInfo::getDirect();
906 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000907
908
Chris Lattnera3c109b2010-07-29 02:16:43 +0000909 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
910 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000911
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000912 bool NeedsPadding;
913 bool InReg = shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding);
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000914
915 if (Ty->isPromotableIntegerType()) {
916 if (InReg)
917 return ABIArgInfo::getExtendInReg();
918 return ABIArgInfo::getExtend();
919 }
920 if (InReg)
921 return ABIArgInfo::getDirectInReg();
922 return ABIArgInfo::getDirect();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000923}
924
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000925void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
926 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
927 FI.getCallingConvention());
Rafael Espindolab48280b2012-07-31 02:44:24 +0000928
Rafael Espindolab6932692012-10-24 01:58:58 +0000929 unsigned CC = FI.getCallingConvention();
930 bool IsFastCall = CC == llvm::CallingConv::X86_FastCall;
931 unsigned FreeRegs;
932 if (IsFastCall)
933 FreeRegs = 2;
934 else if (FI.getHasRegParm())
935 FreeRegs = FI.getRegParm();
936 else
937 FreeRegs = DefaultNumRegisterParameters;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000938
939 // If the return value is indirect, then the hidden argument is consuming one
940 // integer register.
941 if (FI.getReturnInfo().isIndirect() && FreeRegs) {
942 --FreeRegs;
943 ABIArgInfo &Old = FI.getReturnInfo();
944 Old = ABIArgInfo::getIndirectInReg(Old.getIndirectAlign(),
945 Old.getIndirectByVal(),
946 Old.getIndirectRealign());
947 }
948
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000949 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
950 it != ie; ++it)
Rafael Espindolab6932692012-10-24 01:58:58 +0000951 it->info = classifyArgumentType(it->type, FreeRegs, IsFastCall);
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000952}
953
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000954llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
955 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +0000956 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000957
958 CGBuilderTy &Builder = CGF.Builder;
959 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
960 "ap");
961 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Eli Friedman7b1fb812011-11-18 02:12:09 +0000962
963 // Compute if the address needs to be aligned
964 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
965 Align = getTypeStackAlignInBytes(Ty, Align);
966 Align = std::max(Align, 4U);
967 if (Align > 4) {
968 // addr = (addr + align - 1) & -align;
969 llvm::Value *Offset =
970 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
971 Addr = CGF.Builder.CreateGEP(Addr, Offset);
972 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
973 CGF.Int32Ty);
974 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
975 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
976 Addr->getType(),
977 "ap.cur.aligned");
978 }
979
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000980 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000981 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000982 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
983
984 uint64_t Offset =
Eli Friedman7b1fb812011-11-18 02:12:09 +0000985 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000986 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +0000987 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000988 "ap.next");
989 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
990
991 return AddrTyped;
992}
993
Charles Davis74f72932010-02-13 15:54:06 +0000994void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
995 llvm::GlobalValue *GV,
996 CodeGen::CodeGenModule &CGM) const {
997 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
998 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
999 // Get the LLVM function.
1000 llvm::Function *Fn = cast<llvm::Function>(GV);
1001
1002 // Now add the 'alignstack' attribute with a value of 16.
Bill Wendling0d583392012-10-15 20:36:26 +00001003 llvm::AttrBuilder B;
Bill Wendlinge91e9ec2012-10-14 03:28:14 +00001004 B.addStackAlignmentAttr(16);
Bill Wendling909b6de2013-01-23 00:21:06 +00001005 Fn->addAttributes(llvm::AttributeSet::FunctionIndex,
1006 llvm::AttributeSet::get(CGM.getLLVMContext(),
1007 llvm::AttributeSet::FunctionIndex,
1008 B));
Charles Davis74f72932010-02-13 15:54:06 +00001009 }
1010 }
1011}
1012
John McCall6374c332010-03-06 00:35:14 +00001013bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
1014 CodeGen::CodeGenFunction &CGF,
1015 llvm::Value *Address) const {
1016 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCall6374c332010-03-06 00:35:14 +00001017
Chris Lattner8b418682012-02-07 00:39:47 +00001018 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001019
John McCall6374c332010-03-06 00:35:14 +00001020 // 0-7 are the eight integer registers; the order is different
1021 // on Darwin (for EH), but the range is the same.
1022 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +00001023 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +00001024
John McCall64aa4b32013-04-16 22:48:15 +00001025 if (CGF.CGM.getTarget().getTriple().isOSDarwin()) {
John McCall6374c332010-03-06 00:35:14 +00001026 // 12-16 are st(0..4). Not sure why we stop at 4.
1027 // These have size 16, which is sizeof(long double) on
1028 // platforms with 8-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001029 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCallaeeb7012010-05-27 06:19:26 +00001030 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001031
John McCall6374c332010-03-06 00:35:14 +00001032 } else {
1033 // 9 is %eflags, which doesn't get a size on Darwin for some
1034 // reason.
1035 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
1036
1037 // 11-16 are st(0..5). Not sure why we stop at 5.
1038 // These have size 12, which is sizeof(long double) on
1039 // platforms with 4-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001040 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCallaeeb7012010-05-27 06:19:26 +00001041 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1042 }
John McCall6374c332010-03-06 00:35:14 +00001043
1044 return false;
1045}
1046
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001047//===----------------------------------------------------------------------===//
1048// X86-64 ABI Implementation
1049//===----------------------------------------------------------------------===//
1050
1051
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001052namespace {
1053/// X86_64ABIInfo - The X86_64 ABI information.
1054class X86_64ABIInfo : public ABIInfo {
1055 enum Class {
1056 Integer = 0,
1057 SSE,
1058 SSEUp,
1059 X87,
1060 X87Up,
1061 ComplexX87,
1062 NoClass,
1063 Memory
1064 };
1065
1066 /// merge - Implement the X86_64 ABI merging algorithm.
1067 ///
1068 /// Merge an accumulating classification \arg Accum with a field
1069 /// classification \arg Field.
1070 ///
1071 /// \param Accum - The accumulating classification. This should
1072 /// always be either NoClass or the result of a previous merge
1073 /// call. In addition, this should never be Memory (the caller
1074 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001075 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001076
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001077 /// postMerge - Implement the X86_64 ABI post merging algorithm.
1078 ///
1079 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
1080 /// final MEMORY or SSE classes when necessary.
1081 ///
1082 /// \param AggregateSize - The size of the current aggregate in
1083 /// the classification process.
1084 ///
1085 /// \param Lo - The classification for the parts of the type
1086 /// residing in the low word of the containing object.
1087 ///
1088 /// \param Hi - The classification for the parts of the type
1089 /// residing in the higher words of the containing object.
1090 ///
1091 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
1092
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001093 /// classify - Determine the x86_64 register classes in which the
1094 /// given type T should be passed.
1095 ///
1096 /// \param Lo - The classification for the parts of the type
1097 /// residing in the low word of the containing object.
1098 ///
1099 /// \param Hi - The classification for the parts of the type
1100 /// residing in the high word of the containing object.
1101 ///
1102 /// \param OffsetBase - The bit offset of this type in the
1103 /// containing object. Some parameters are classified different
1104 /// depending on whether they straddle an eightbyte boundary.
1105 ///
1106 /// If a word is unused its result will be NoClass; if a type should
1107 /// be passed in Memory then at least the classification of \arg Lo
1108 /// will be Memory.
1109 ///
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001110 /// The \arg Lo class will be NoClass iff the argument is ignored.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001111 ///
1112 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
1113 /// also be ComplexX87.
Chris Lattner9c254f02010-06-29 06:01:59 +00001114 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001115
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001116 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001117 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
1118 unsigned IROffset, QualType SourceTy,
1119 unsigned SourceOffset) const;
1120 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
1121 unsigned IROffset, QualType SourceTy,
1122 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001123
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001124 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001125 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +00001126 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001127
1128 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001129 /// such that the argument will be passed in memory.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001130 ///
1131 /// \param freeIntRegs - The number of free integer registers remaining
1132 /// available.
1133 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001134
Chris Lattnera3c109b2010-07-29 02:16:43 +00001135 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001136
Bill Wendlingbb465d72010-10-18 03:41:31 +00001137 ABIArgInfo classifyArgumentType(QualType Ty,
Daniel Dunbaredfac032012-03-10 01:03:58 +00001138 unsigned freeIntRegs,
Bill Wendlingbb465d72010-10-18 03:41:31 +00001139 unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +00001140 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001141
Eli Friedmanee1ad992011-12-02 00:11:43 +00001142 bool IsIllegalVectorType(QualType Ty) const;
1143
John McCall67a57732011-04-21 01:20:55 +00001144 /// The 0.98 ABI revision clarified a lot of ambiguities,
1145 /// unfortunately in ways that were not always consistent with
1146 /// certain previous compilers. In particular, platforms which
1147 /// required strict binary compatibility with older versions of GCC
1148 /// may need to exempt themselves.
1149 bool honorsRevision0_98() const {
John McCall64aa4b32013-04-16 22:48:15 +00001150 return !getTarget().getTriple().isOSDarwin();
John McCall67a57732011-04-21 01:20:55 +00001151 }
1152
Eli Friedmanee1ad992011-12-02 00:11:43 +00001153 bool HasAVX;
Derek Schuffbabaf312012-10-11 15:52:22 +00001154 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
1155 // 64-bit hardware.
1156 bool Has64BitPointers;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001157
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001158public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001159 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
Derek Schuffbabaf312012-10-11 15:52:22 +00001160 ABIInfo(CGT), HasAVX(hasavx),
Derek Schuff90da80c2012-10-11 18:21:13 +00001161 Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001162 }
Chris Lattner9c254f02010-06-29 06:01:59 +00001163
John McCallde5d3c72012-02-17 03:33:10 +00001164 bool isPassedUsingAVXType(QualType type) const {
1165 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00001166 // The freeIntRegs argument doesn't matter here.
1167 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE);
John McCallde5d3c72012-02-17 03:33:10 +00001168 if (info.isDirect()) {
1169 llvm::Type *ty = info.getCoerceToType();
1170 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
1171 return (vectorTy->getBitWidth() > 128);
1172 }
1173 return false;
1174 }
1175
Chris Lattneree5dcd02010-07-29 02:31:05 +00001176 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001177
1178 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1179 CodeGenFunction &CGF) const;
1180};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001181
Chris Lattnerf13721d2010-08-31 16:44:54 +00001182/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001183class WinX86_64ABIInfo : public ABIInfo {
1184
1185 ABIArgInfo classify(QualType Ty) const;
1186
Chris Lattnerf13721d2010-08-31 16:44:54 +00001187public:
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001188 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
1189
1190 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattnerf13721d2010-08-31 16:44:54 +00001191
1192 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1193 CodeGenFunction &CGF) const;
1194};
1195
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001196class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1197public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001198 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
Derek Schuffbabaf312012-10-11 15:52:22 +00001199 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
John McCall6374c332010-03-06 00:35:14 +00001200
John McCallde5d3c72012-02-17 03:33:10 +00001201 const X86_64ABIInfo &getABIInfo() const {
1202 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
1203 }
1204
John McCall6374c332010-03-06 00:35:14 +00001205 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1206 return 7;
1207 }
1208
1209 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1210 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001211 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001212
John McCallaeeb7012010-05-27 06:19:26 +00001213 // 0-15 are the 16 integer registers.
1214 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001215 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +00001216 return false;
1217 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001218
Jay Foadef6de3d2011-07-11 09:56:20 +00001219 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001220 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +00001221 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001222 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1223 }
1224
John McCallde5d3c72012-02-17 03:33:10 +00001225 bool isNoProtoCallVariadic(const CallArgList &args,
1226 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +00001227 // The default CC on x86-64 sets %al to the number of SSA
1228 // registers used, and GCC sets this when calling an unprototyped
Eli Friedman3ed79032011-12-01 04:53:19 +00001229 // function, so we override the default behavior. However, don't do
Eli Friedman68805fe2011-12-06 03:08:26 +00001230 // that when AVX types are involved: the ABI explicitly states it is
1231 // undefined, and it doesn't work in practice because of how the ABI
1232 // defines varargs anyway.
John McCallde5d3c72012-02-17 03:33:10 +00001233 if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) {
Eli Friedman3ed79032011-12-01 04:53:19 +00001234 bool HasAVXType = false;
John McCallde5d3c72012-02-17 03:33:10 +00001235 for (CallArgList::const_iterator
1236 it = args.begin(), ie = args.end(); it != ie; ++it) {
1237 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1238 HasAVXType = true;
1239 break;
Eli Friedman3ed79032011-12-01 04:53:19 +00001240 }
1241 }
John McCallde5d3c72012-02-17 03:33:10 +00001242
Eli Friedman3ed79032011-12-01 04:53:19 +00001243 if (!HasAVXType)
1244 return true;
1245 }
John McCall01f151e2011-09-21 08:08:30 +00001246
John McCallde5d3c72012-02-17 03:33:10 +00001247 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
John McCall01f151e2011-09-21 08:08:30 +00001248 }
1249
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001250};
1251
Chris Lattnerf13721d2010-08-31 16:44:54 +00001252class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1253public:
1254 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1255 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1256
1257 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1258 return 7;
1259 }
1260
1261 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1262 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001263 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001264
Chris Lattnerf13721d2010-08-31 16:44:54 +00001265 // 0-15 are the 16 integer registers.
1266 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001267 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattnerf13721d2010-08-31 16:44:54 +00001268 return false;
1269 }
1270};
1271
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001272}
1273
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001274void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1275 Class &Hi) const {
1276 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1277 //
1278 // (a) If one of the classes is Memory, the whole argument is passed in
1279 // memory.
1280 //
1281 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1282 // memory.
1283 //
1284 // (c) If the size of the aggregate exceeds two eightbytes and the first
1285 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1286 // argument is passed in memory. NOTE: This is necessary to keep the
1287 // ABI working for processors that don't support the __m256 type.
1288 //
1289 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1290 //
1291 // Some of these are enforced by the merging logic. Others can arise
1292 // only with unions; for example:
1293 // union { _Complex double; unsigned; }
1294 //
1295 // Note that clauses (b) and (c) were added in 0.98.
1296 //
1297 if (Hi == Memory)
1298 Lo = Memory;
1299 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1300 Lo = Memory;
1301 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1302 Lo = Memory;
1303 if (Hi == SSEUp && Lo != SSE)
1304 Hi = SSE;
1305}
1306
Chris Lattner1090a9b2010-06-28 21:43:59 +00001307X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001308 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1309 // classified recursively so that always two fields are
1310 // considered. The resulting class is calculated according to
1311 // the classes of the fields in the eightbyte:
1312 //
1313 // (a) If both classes are equal, this is the resulting class.
1314 //
1315 // (b) If one of the classes is NO_CLASS, the resulting class is
1316 // the other class.
1317 //
1318 // (c) If one of the classes is MEMORY, the result is the MEMORY
1319 // class.
1320 //
1321 // (d) If one of the classes is INTEGER, the result is the
1322 // INTEGER.
1323 //
1324 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1325 // MEMORY is used as class.
1326 //
1327 // (f) Otherwise class SSE is used.
1328
1329 // Accum should never be memory (we should have returned) or
1330 // ComplexX87 (because this cannot be passed in a structure).
1331 assert((Accum != Memory && Accum != ComplexX87) &&
1332 "Invalid accumulated classification during merge.");
1333 if (Accum == Field || Field == NoClass)
1334 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001335 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001336 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001337 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001338 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001339 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001340 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001341 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1342 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001343 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001344 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001345}
1346
Chris Lattnerbcaedae2010-06-30 19:14:05 +00001347void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001348 Class &Lo, Class &Hi) const {
1349 // FIXME: This code can be simplified by introducing a simple value class for
1350 // Class pairs with appropriate constructor methods for the various
1351 // situations.
1352
1353 // FIXME: Some of the split computations are wrong; unaligned vectors
1354 // shouldn't be passed in registers for example, so there is no chance they
1355 // can straddle an eightbyte. Verify & simplify.
1356
1357 Lo = Hi = NoClass;
1358
1359 Class &Current = OffsetBase < 64 ? Lo : Hi;
1360 Current = Memory;
1361
John McCall183700f2009-09-21 23:43:11 +00001362 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001363 BuiltinType::Kind k = BT->getKind();
1364
1365 if (k == BuiltinType::Void) {
1366 Current = NoClass;
1367 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1368 Lo = Integer;
1369 Hi = Integer;
1370 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1371 Current = Integer;
Derek Schuff7da46f92012-10-11 16:55:58 +00001372 } else if ((k == BuiltinType::Float || k == BuiltinType::Double) ||
1373 (k == BuiltinType::LongDouble &&
John McCall64aa4b32013-04-16 22:48:15 +00001374 getTarget().getTriple().getOS() == llvm::Triple::NaCl)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001375 Current = SSE;
1376 } else if (k == BuiltinType::LongDouble) {
1377 Lo = X87;
1378 Hi = X87Up;
1379 }
1380 // FIXME: _Decimal32 and _Decimal64 are SSE.
1381 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001382 return;
1383 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001384
Chris Lattner1090a9b2010-06-28 21:43:59 +00001385 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001386 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +00001387 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001388 return;
1389 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001390
Chris Lattner1090a9b2010-06-28 21:43:59 +00001391 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001392 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001393 return;
1394 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001395
Chris Lattner1090a9b2010-06-28 21:43:59 +00001396 if (Ty->isMemberPointerType()) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001397 if (Ty->isMemberFunctionPointerType() && Has64BitPointers)
Daniel Dunbar67d438d2010-05-15 00:00:37 +00001398 Lo = Hi = Integer;
1399 else
1400 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001401 return;
1402 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001403
Chris Lattner1090a9b2010-06-28 21:43:59 +00001404 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001405 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001406 if (Size == 32) {
1407 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1408 // float> as integer.
1409 Current = Integer;
1410
1411 // If this type crosses an eightbyte boundary, it should be
1412 // split.
1413 uint64_t EB_Real = (OffsetBase) / 64;
1414 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1415 if (EB_Real != EB_Imag)
1416 Hi = Lo;
1417 } else if (Size == 64) {
1418 // gcc passes <1 x double> in memory. :(
1419 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1420 return;
1421
1422 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001423 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001424 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1425 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1426 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001427 Current = Integer;
1428 else
1429 Current = SSE;
1430
1431 // If this type crosses an eightbyte boundary, it should be
1432 // split.
1433 if (OffsetBase && OffsetBase != 64)
1434 Hi = Lo;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001435 } else if (Size == 128 || (HasAVX && Size == 256)) {
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001436 // Arguments of 256-bits are split into four eightbyte chunks. The
1437 // least significant one belongs to class SSE and all the others to class
1438 // SSEUP. The original Lo and Hi design considers that types can't be
1439 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1440 // This design isn't correct for 256-bits, but since there're no cases
1441 // where the upper parts would need to be inspected, avoid adding
1442 // complexity and just consider Hi to match the 64-256 part.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001443 Lo = SSE;
1444 Hi = SSEUp;
1445 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001446 return;
1447 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001448
Chris Lattner1090a9b2010-06-28 21:43:59 +00001449 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001450 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001451
Chris Lattnerea044322010-07-29 02:01:43 +00001452 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001453 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001454 if (Size <= 64)
1455 Current = Integer;
1456 else if (Size <= 128)
1457 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001458 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001459 Current = SSE;
Derek Schuff7da46f92012-10-11 16:55:58 +00001460 else if (ET == getContext().DoubleTy ||
1461 (ET == getContext().LongDoubleTy &&
John McCall64aa4b32013-04-16 22:48:15 +00001462 getTarget().getTriple().getOS() == llvm::Triple::NaCl))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001463 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001464 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001465 Current = ComplexX87;
1466
1467 // If this complex type crosses an eightbyte boundary then it
1468 // should be split.
1469 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001470 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001471 if (Hi == NoClass && EB_Real != EB_Imag)
1472 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001473
Chris Lattner1090a9b2010-06-28 21:43:59 +00001474 return;
1475 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001476
Chris Lattnerea044322010-07-29 02:01:43 +00001477 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001478 // Arrays are treated like structures.
1479
Chris Lattnerea044322010-07-29 02:01:43 +00001480 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001481
1482 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001483 // than four eightbytes, ..., it has class MEMORY.
1484 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001485 return;
1486
1487 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1488 // fields, it has class MEMORY.
1489 //
1490 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001491 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001492 return;
1493
1494 // Otherwise implement simplified merge. We could be smarter about
1495 // this, but it isn't worth it and would be harder to verify.
1496 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001497 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001498 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes089d8922011-07-12 01:27:38 +00001499
1500 // The only case a 256-bit wide vector could be used is when the array
1501 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1502 // to work for sizes wider than 128, early check and fallback to memory.
1503 if (Size > 128 && EltSize != 256)
1504 return;
1505
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001506 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1507 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001508 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001509 Lo = merge(Lo, FieldLo);
1510 Hi = merge(Hi, FieldHi);
1511 if (Lo == Memory || Hi == Memory)
1512 break;
1513 }
1514
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001515 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001516 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001517 return;
1518 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001519
Chris Lattner1090a9b2010-06-28 21:43:59 +00001520 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001521 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001522
1523 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001524 // than four eightbytes, ..., it has class MEMORY.
1525 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001526 return;
1527
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001528 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1529 // copy constructor or a non-trivial destructor, it is passed by invisible
1530 // reference.
1531 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1532 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001533
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001534 const RecordDecl *RD = RT->getDecl();
1535
1536 // Assume variable sized types are passed in memory.
1537 if (RD->hasFlexibleArrayMember())
1538 return;
1539
Chris Lattnerea044322010-07-29 02:01:43 +00001540 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001541
1542 // Reset Lo class, this will be recomputed.
1543 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001544
1545 // If this is a C++ record, classify the bases first.
1546 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1547 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1548 e = CXXRD->bases_end(); i != e; ++i) {
1549 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1550 "Unexpected base class!");
1551 const CXXRecordDecl *Base =
1552 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1553
1554 // Classify this field.
1555 //
1556 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1557 // single eightbyte, each is classified separately. Each eightbyte gets
1558 // initialized to class NO_CLASS.
1559 Class FieldLo, FieldHi;
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001560 uint64_t Offset =
1561 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
Chris Lattner9c254f02010-06-29 06:01:59 +00001562 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001563 Lo = merge(Lo, FieldLo);
1564 Hi = merge(Hi, FieldHi);
1565 if (Lo == Memory || Hi == Memory)
1566 break;
1567 }
1568 }
1569
1570 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001571 unsigned idx = 0;
Bruno Cardoso Lopes548e4782011-07-12 22:30:58 +00001572 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001573 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001574 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1575 bool BitField = i->isBitField();
1576
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001577 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1578 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001579 //
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001580 // The only case a 256-bit wide vector could be used is when the struct
1581 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1582 // to work for sizes wider than 128, early check and fallback to memory.
1583 //
1584 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1585 Lo = Memory;
1586 return;
1587 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001588 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001589 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001590 Lo = Memory;
1591 return;
1592 }
1593
1594 // Classify this field.
1595 //
1596 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1597 // exceeds a single eightbyte, each is classified
1598 // separately. Each eightbyte gets initialized to class
1599 // NO_CLASS.
1600 Class FieldLo, FieldHi;
1601
1602 // Bit-fields require special handling, they do not force the
1603 // structure to be passed in memory even if unaligned, and
1604 // therefore they can straddle an eightbyte.
1605 if (BitField) {
1606 // Ignore padding bit-fields.
1607 if (i->isUnnamedBitfield())
1608 continue;
1609
1610 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001611 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001612
1613 uint64_t EB_Lo = Offset / 64;
1614 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1615 FieldLo = FieldHi = NoClass;
1616 if (EB_Lo) {
1617 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1618 FieldLo = NoClass;
1619 FieldHi = Integer;
1620 } else {
1621 FieldLo = Integer;
1622 FieldHi = EB_Hi ? Integer : NoClass;
1623 }
1624 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001625 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001626 Lo = merge(Lo, FieldLo);
1627 Hi = merge(Hi, FieldHi);
1628 if (Lo == Memory || Hi == Memory)
1629 break;
1630 }
1631
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001632 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001633 }
1634}
1635
Chris Lattner9c254f02010-06-29 06:01:59 +00001636ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001637 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1638 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001639 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001640 // Treat an enum type as its underlying type.
1641 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1642 Ty = EnumTy->getDecl()->getIntegerType();
1643
1644 return (Ty->isPromotableIntegerType() ?
1645 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1646 }
1647
1648 return ABIArgInfo::getIndirect(0);
1649}
1650
Eli Friedmanee1ad992011-12-02 00:11:43 +00001651bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1652 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1653 uint64_t Size = getContext().getTypeSize(VecTy);
1654 unsigned LargestVector = HasAVX ? 256 : 128;
1655 if (Size <= 64 || Size > LargestVector)
1656 return true;
1657 }
1658
1659 return false;
1660}
1661
Daniel Dunbaredfac032012-03-10 01:03:58 +00001662ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1663 unsigned freeIntRegs) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001664 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1665 // place naturally.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001666 //
1667 // This assumption is optimistic, as there could be free registers available
1668 // when we need to pass this argument in memory, and LLVM could try to pass
1669 // the argument in the free register. This does not seem to happen currently,
1670 // but this code would be much safer if we could mark the argument with
1671 // 'onstack'. See PR12193.
Eli Friedmanee1ad992011-12-02 00:11:43 +00001672 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001673 // Treat an enum type as its underlying type.
1674 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1675 Ty = EnumTy->getDecl()->getIntegerType();
1676
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001677 return (Ty->isPromotableIntegerType() ?
1678 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001679 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001680
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001681 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1682 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001683
Chris Lattner855d2272011-05-22 23:21:23 +00001684 // Compute the byval alignment. We specify the alignment of the byval in all
1685 // cases so that the mid-level optimizer knows the alignment of the byval.
1686 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
Daniel Dunbaredfac032012-03-10 01:03:58 +00001687
1688 // Attempt to avoid passing indirect results using byval when possible. This
1689 // is important for good codegen.
1690 //
1691 // We do this by coercing the value into a scalar type which the backend can
1692 // handle naturally (i.e., without using byval).
1693 //
1694 // For simplicity, we currently only do this when we have exhausted all of the
1695 // free integer registers. Doing this when there are free integer registers
1696 // would require more care, as we would have to ensure that the coerced value
1697 // did not claim the unused register. That would require either reording the
1698 // arguments to the function (so that any subsequent inreg values came first),
1699 // or only doing this optimization when there were no following arguments that
1700 // might be inreg.
1701 //
1702 // We currently expect it to be rare (particularly in well written code) for
1703 // arguments to be passed on the stack when there are still free integer
1704 // registers available (this would typically imply large structs being passed
1705 // by value), so this seems like a fair tradeoff for now.
1706 //
1707 // We can revisit this if the backend grows support for 'onstack' parameter
1708 // attributes. See PR12193.
1709 if (freeIntRegs == 0) {
1710 uint64_t Size = getContext().getTypeSize(Ty);
1711
1712 // If this type fits in an eightbyte, coerce it into the matching integral
1713 // type, which will end up on the stack (with alignment 8).
1714 if (Align == 8 && Size <= 64)
1715 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1716 Size));
1717 }
1718
Chris Lattner855d2272011-05-22 23:21:23 +00001719 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001720}
1721
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001722/// GetByteVectorType - The ABI specifies that a value should be passed in an
1723/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner0f408f52010-07-29 04:56:46 +00001724/// vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001725llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001726 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001727
Chris Lattner15842bd2010-07-29 05:02:29 +00001728 // Wrapper structs that just contain vectors are passed just like vectors,
1729 // strip them off if present.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001730 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner15842bd2010-07-29 05:02:29 +00001731 while (STy && STy->getNumElements() == 1) {
1732 IRType = STy->getElementType(0);
1733 STy = dyn_cast<llvm::StructType>(IRType);
1734 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001735
Bruno Cardoso Lopes528a8c72011-07-08 22:57:35 +00001736 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001737 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1738 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001739 unsigned BitWidth = VT->getBitWidth();
Tanya Lattnerce275672011-11-28 23:18:11 +00001740 if ((BitWidth >= 128 && BitWidth <= 256) &&
Chris Lattner0f408f52010-07-29 04:56:46 +00001741 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1742 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1743 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1744 EltTy->isIntegerTy(128)))
1745 return VT;
1746 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001747
Chris Lattner0f408f52010-07-29 04:56:46 +00001748 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1749}
1750
Chris Lattnere2962be2010-07-29 07:30:00 +00001751/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1752/// is known to either be off the end of the specified type or being in
1753/// alignment padding. The user type specified is known to be at most 128 bits
1754/// in size, and have passed through X86_64ABIInfo::classify with a successful
1755/// classification that put one of the two halves in the INTEGER class.
1756///
1757/// It is conservatively correct to return false.
1758static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1759 unsigned EndBit, ASTContext &Context) {
1760 // If the bytes being queried are off the end of the type, there is no user
1761 // data hiding here. This handles analysis of builtins, vectors and other
1762 // types that don't contain interesting padding.
1763 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1764 if (TySize <= StartBit)
1765 return true;
1766
Chris Lattner021c3a32010-07-29 07:43:55 +00001767 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1768 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1769 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1770
1771 // Check each element to see if the element overlaps with the queried range.
1772 for (unsigned i = 0; i != NumElts; ++i) {
1773 // If the element is after the span we care about, then we're done..
1774 unsigned EltOffset = i*EltSize;
1775 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001776
Chris Lattner021c3a32010-07-29 07:43:55 +00001777 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1778 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1779 EndBit-EltOffset, Context))
1780 return false;
1781 }
1782 // If it overlaps no elements, then it is safe to process as padding.
1783 return true;
1784 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001785
Chris Lattnere2962be2010-07-29 07:30:00 +00001786 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1787 const RecordDecl *RD = RT->getDecl();
1788 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001789
Chris Lattnere2962be2010-07-29 07:30:00 +00001790 // If this is a C++ record, check the bases first.
1791 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1792 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1793 e = CXXRD->bases_end(); i != e; ++i) {
1794 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1795 "Unexpected base class!");
1796 const CXXRecordDecl *Base =
1797 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001798
Chris Lattnere2962be2010-07-29 07:30:00 +00001799 // If the base is after the span we care about, ignore it.
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001800 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
Chris Lattnere2962be2010-07-29 07:30:00 +00001801 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001802
Chris Lattnere2962be2010-07-29 07:30:00 +00001803 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1804 if (!BitsContainNoUserData(i->getType(), BaseStart,
1805 EndBit-BaseOffset, Context))
1806 return false;
1807 }
1808 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001809
Chris Lattnere2962be2010-07-29 07:30:00 +00001810 // Verify that no field has data that overlaps the region of interest. Yes
1811 // this could be sped up a lot by being smarter about queried fields,
1812 // however we're only looking at structs up to 16 bytes, so we don't care
1813 // much.
1814 unsigned idx = 0;
1815 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1816 i != e; ++i, ++idx) {
1817 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001818
Chris Lattnere2962be2010-07-29 07:30:00 +00001819 // If we found a field after the region we care about, then we're done.
1820 if (FieldOffset >= EndBit) break;
1821
1822 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1823 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1824 Context))
1825 return false;
1826 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001827
Chris Lattnere2962be2010-07-29 07:30:00 +00001828 // If nothing in this record overlapped the area of interest, then we're
1829 // clean.
1830 return true;
1831 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001832
Chris Lattnere2962be2010-07-29 07:30:00 +00001833 return false;
1834}
1835
Chris Lattner0b362002010-07-29 18:39:32 +00001836/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1837/// float member at the specified offset. For example, {int,{float}} has a
1838/// float at offset 4. It is conservatively correct for this routine to return
1839/// false.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001840static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Micah Villmow25a6a842012-10-08 16:25:52 +00001841 const llvm::DataLayout &TD) {
Chris Lattner0b362002010-07-29 18:39:32 +00001842 // Base case if we find a float.
1843 if (IROffset == 0 && IRType->isFloatTy())
1844 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001845
Chris Lattner0b362002010-07-29 18:39:32 +00001846 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001847 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner0b362002010-07-29 18:39:32 +00001848 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1849 unsigned Elt = SL->getElementContainingOffset(IROffset);
1850 IROffset -= SL->getElementOffset(Elt);
1851 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1852 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001853
Chris Lattner0b362002010-07-29 18:39:32 +00001854 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001855 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1856 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner0b362002010-07-29 18:39:32 +00001857 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1858 IROffset -= IROffset/EltSize*EltSize;
1859 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1860 }
1861
1862 return false;
1863}
1864
Chris Lattnerf47c9442010-07-29 18:13:09 +00001865
1866/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1867/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001868llvm::Type *X86_64ABIInfo::
1869GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00001870 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001871 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001872 // pass as float if the last 4 bytes is just padding. This happens for
1873 // structs that contain 3 floats.
1874 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1875 SourceOffset*8+64, getContext()))
1876 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001877
Chris Lattner0b362002010-07-29 18:39:32 +00001878 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1879 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1880 // case.
Micah Villmow25a6a842012-10-08 16:25:52 +00001881 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
1882 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001883 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001884
Chris Lattnerf47c9442010-07-29 18:13:09 +00001885 return llvm::Type::getDoubleTy(getVMContext());
1886}
1887
1888
Chris Lattner0d2656d2010-07-29 17:40:35 +00001889/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1890/// an 8-byte GPR. This means that we either have a scalar or we are talking
1891/// about the high or low part of an up-to-16-byte struct. This routine picks
1892/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001893/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1894/// etc).
1895///
1896/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1897/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1898/// the 8-byte value references. PrefType may be null.
1899///
1900/// SourceTy is the source level type for the entire argument. SourceOffset is
1901/// an offset into this that we're processing (which is always either 0 or 8).
1902///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001903llvm::Type *X86_64ABIInfo::
1904GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00001905 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001906 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1907 // returning an 8-byte unit starting with it. See if we can safely use it.
1908 if (IROffset == 0) {
1909 // Pointers and int64's always fill the 8-byte unit.
Derek Schuffbabaf312012-10-11 15:52:22 +00001910 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
1911 IRType->isIntegerTy(64))
Chris Lattnere2962be2010-07-29 07:30:00 +00001912 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001913
Chris Lattnere2962be2010-07-29 07:30:00 +00001914 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1915 // goodness in the source type is just tail padding. This is allowed to
1916 // kick in for struct {double,int} on the int, but not on
1917 // struct{double,int,int} because we wouldn't return the second int. We
1918 // have to do this analysis on the source type because we can't depend on
1919 // unions being lowered a specific way etc.
1920 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
Derek Schuffbabaf312012-10-11 15:52:22 +00001921 IRType->isIntegerTy(32) ||
1922 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
1923 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
1924 cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001925
Chris Lattnere2962be2010-07-29 07:30:00 +00001926 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1927 SourceOffset*8+64, getContext()))
1928 return IRType;
1929 }
1930 }
Chris Lattner49382de2010-07-28 22:44:07 +00001931
Chris Lattner2acc6e32011-07-18 04:24:23 +00001932 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001933 // If this is a struct, recurse into the field at the specified offset.
Micah Villmow25a6a842012-10-08 16:25:52 +00001934 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001935 if (IROffset < SL->getSizeInBytes()) {
1936 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1937 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001938
Chris Lattner0d2656d2010-07-29 17:40:35 +00001939 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1940 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001941 }
Chris Lattner49382de2010-07-28 22:44:07 +00001942 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001943
Chris Lattner2acc6e32011-07-18 04:24:23 +00001944 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001945 llvm::Type *EltTy = ATy->getElementType();
Micah Villmow25a6a842012-10-08 16:25:52 +00001946 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
Chris Lattner021c3a32010-07-29 07:43:55 +00001947 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00001948 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1949 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00001950 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001951
Chris Lattner49382de2010-07-28 22:44:07 +00001952 // Okay, we don't have any better idea of what to pass, so we pass this in an
1953 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001954 unsigned TySizeInBytes =
1955 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00001956
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001957 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001958
Chris Lattner49382de2010-07-28 22:44:07 +00001959 // It is always safe to classify this as an integer type up to i64 that
1960 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001961 return llvm::IntegerType::get(getVMContext(),
1962 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00001963}
1964
Chris Lattner66e7b682010-09-01 00:50:20 +00001965
1966/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1967/// be used as elements of a two register pair to pass or return, return a
1968/// first class aggregate to represent them. For example, if the low part of
1969/// a by-value argument should be passed as i32* and the high part as float,
1970/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001971static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00001972GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Micah Villmow25a6a842012-10-08 16:25:52 +00001973 const llvm::DataLayout &TD) {
Chris Lattner66e7b682010-09-01 00:50:20 +00001974 // In order to correctly satisfy the ABI, we need to the high part to start
1975 // at offset 8. If the high and low parts we inferred are both 4-byte types
1976 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1977 // the second element at offset 8. Check for this:
1978 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1979 unsigned HiAlign = TD.getABITypeAlignment(Hi);
Micah Villmow25a6a842012-10-08 16:25:52 +00001980 unsigned HiStart = llvm::DataLayout::RoundUpAlignment(LoSize, HiAlign);
Chris Lattner66e7b682010-09-01 00:50:20 +00001981 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001982
Chris Lattner66e7b682010-09-01 00:50:20 +00001983 // To handle this, we have to increase the size of the low part so that the
1984 // second element will start at an 8 byte offset. We can't increase the size
1985 // of the second element because it might make us access off the end of the
1986 // struct.
1987 if (HiStart != 8) {
1988 // There are only two sorts of types the ABI generation code can produce for
1989 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1990 // Promote these to a larger type.
1991 if (Lo->isFloatTy())
1992 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1993 else {
1994 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1995 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1996 }
1997 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001998
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001999 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002000
2001
Chris Lattner66e7b682010-09-01 00:50:20 +00002002 // Verify that the second element is at an 8-byte offset.
2003 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
2004 "Invalid x86-64 argument pair!");
2005 return Result;
2006}
2007
Chris Lattner519f68c2010-07-28 23:06:14 +00002008ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00002009classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00002010 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
2011 // classification algorithm.
2012 X86_64ABIInfo::Class Lo, Hi;
2013 classify(RetTy, 0, Lo, Hi);
2014
2015 // Check some invariants.
2016 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002017 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2018
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002019 llvm::Type *ResType = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002020 switch (Lo) {
2021 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002022 if (Hi == NoClass)
2023 return ABIArgInfo::getIgnore();
2024 // If the low part is just padding, it takes no register, leave ResType
2025 // null.
2026 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2027 "Unknown missing lo part");
2028 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002029
2030 case SSEUp:
2031 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002032 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002033
2034 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
2035 // hidden argument.
2036 case Memory:
2037 return getIndirectReturnResult(RetTy);
2038
2039 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
2040 // available register of the sequence %rax, %rdx is used.
2041 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002042 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002043
Chris Lattnereb518b42010-07-29 21:42:50 +00002044 // If we have a sign or zero extended integer, make sure to return Extend
2045 // so that the parameter gets the right LLVM IR attributes.
2046 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2047 // Treat an enum type as its underlying type.
2048 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2049 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002050
Chris Lattnereb518b42010-07-29 21:42:50 +00002051 if (RetTy->isIntegralOrEnumerationType() &&
2052 RetTy->isPromotableIntegerType())
2053 return ABIArgInfo::getExtend();
2054 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002055 break;
2056
2057 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
2058 // available SSE register of the sequence %xmm0, %xmm1 is used.
2059 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002060 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00002061 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002062
2063 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
2064 // returned on the X87 stack in %st0 as 80-bit x87 number.
2065 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00002066 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00002067 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002068
2069 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
2070 // part of the value is returned in %st0 and the imaginary part in
2071 // %st1.
2072 case ComplexX87:
2073 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00002074 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00002075 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00002076 NULL);
2077 break;
2078 }
2079
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002080 llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002081 switch (Hi) {
2082 // Memory was handled previously and X87 should
2083 // never occur as a hi class.
2084 case Memory:
2085 case X87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002086 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002087
2088 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00002089 case NoClass:
2090 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002091
Chris Lattner3db4dde2010-09-01 00:20:33 +00002092 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002093 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002094 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2095 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002096 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00002097 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002098 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002099 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2100 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002101 break;
2102
2103 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002104 // is passed in the next available eightbyte chunk if the last used
2105 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00002106 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002107 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00002108 case SSEUp:
2109 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002110 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00002111 break;
2112
2113 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
2114 // returned together with the previous X87 value in %st0.
2115 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002116 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00002117 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002118 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00002119 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00002120 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002121 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002122 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2123 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00002124 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002125 break;
2126 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002127
Chris Lattner3db4dde2010-09-01 00:20:33 +00002128 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00002129 // known to pass in the high eightbyte of the result. We do this by forming a
2130 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00002131 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002132 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Chris Lattner519f68c2010-07-28 23:06:14 +00002133
Chris Lattnereb518b42010-07-29 21:42:50 +00002134 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00002135}
2136
Daniel Dunbaredfac032012-03-10 01:03:58 +00002137ABIArgInfo X86_64ABIInfo::classifyArgumentType(
2138 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE)
2139 const
2140{
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002141 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00002142 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002143
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002144 // Check some invariants.
2145 // FIXME: Enforce these by construction.
2146 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002147 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2148
2149 neededInt = 0;
2150 neededSSE = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002151 llvm::Type *ResType = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002152 switch (Lo) {
2153 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002154 if (Hi == NoClass)
2155 return ABIArgInfo::getIgnore();
2156 // If the low part is just padding, it takes no register, leave ResType
2157 // null.
2158 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2159 "Unknown missing lo part");
2160 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002161
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002162 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
2163 // on the stack.
2164 case Memory:
2165
2166 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
2167 // COMPLEX_X87, it is passed in memory.
2168 case X87:
2169 case ComplexX87:
Eli Friedmanded137f2011-06-29 07:04:55 +00002170 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2171 ++neededInt;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002172 return getIndirectResult(Ty, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002173
2174 case SSEUp:
2175 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002176 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002177
2178 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
2179 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
2180 // and %r9 is used.
2181 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00002182 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002183
Chris Lattner49382de2010-07-28 22:44:07 +00002184 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002185 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00002186
2187 // If we have a sign or zero extended integer, make sure to return Extend
2188 // so that the parameter gets the right LLVM IR attributes.
2189 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2190 // Treat an enum type as its underlying type.
2191 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2192 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002193
Chris Lattnereb518b42010-07-29 21:42:50 +00002194 if (Ty->isIntegralOrEnumerationType() &&
2195 Ty->isPromotableIntegerType())
2196 return ABIArgInfo::getExtend();
2197 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002198
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002199 break;
2200
2201 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
2202 // available SSE register is used, the registers are taken in the
2203 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00002204 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002205 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00002206 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00002207 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002208 break;
2209 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00002210 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002211
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002212 llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002213 switch (Hi) {
2214 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002215 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002216 // which is passed in memory.
2217 case Memory:
2218 case X87:
2219 case ComplexX87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002220 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002221
2222 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002223
Chris Lattner645406a2010-09-01 00:24:35 +00002224 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002225 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00002226 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002227 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002228
Chris Lattner645406a2010-09-01 00:24:35 +00002229 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2230 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002231 break;
2232
2233 // X87Up generally doesn't occur here (long double is passed in
2234 // memory), except in situations involving unions.
2235 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00002236 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002237 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002238
Chris Lattner645406a2010-09-01 00:24:35 +00002239 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2240 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00002241
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002242 ++neededSSE;
2243 break;
2244
2245 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
2246 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002247 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002248 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00002249 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002250 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002251 break;
2252 }
2253
Chris Lattner645406a2010-09-01 00:24:35 +00002254 // If a high part was specified, merge it together with the low part. It is
2255 // known to pass in the high eightbyte of the result. We do this by forming a
2256 // first class struct aggregate with the high and low part: {low, high}
2257 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002258 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002259
Chris Lattnereb518b42010-07-29 21:42:50 +00002260 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002261}
2262
Chris Lattneree5dcd02010-07-29 02:31:05 +00002263void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002264
Chris Lattnera3c109b2010-07-29 02:16:43 +00002265 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002266
2267 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00002268 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002269
2270 // If the return value is indirect, then the hidden argument is consuming one
2271 // integer register.
2272 if (FI.getReturnInfo().isIndirect())
2273 --freeIntRegs;
2274
2275 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2276 // get assigned (in left-to-right order) for passing as follows...
2277 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2278 it != ie; ++it) {
Bill Wendling99aaae82010-10-18 23:51:38 +00002279 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002280 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
2281 neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002282
2283 // AMD64-ABI 3.2.3p3: If there are no registers available for any
2284 // eightbyte of an argument, the whole argument is passed on the
2285 // stack. If registers have already been assigned for some
2286 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00002287 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002288 freeIntRegs -= neededInt;
2289 freeSSERegs -= neededSSE;
2290 } else {
Daniel Dunbaredfac032012-03-10 01:03:58 +00002291 it->info = getIndirectResult(it->type, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002292 }
2293 }
2294}
2295
2296static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2297 QualType Ty,
2298 CodeGenFunction &CGF) {
2299 llvm::Value *overflow_arg_area_p =
2300 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2301 llvm::Value *overflow_arg_area =
2302 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2303
2304 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2305 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedman8d2fe422011-11-18 02:44:19 +00002306 // It isn't stated explicitly in the standard, but in practice we use
2307 // alignment greater than 16 where necessary.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002308 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2309 if (Align > 8) {
Eli Friedman8d2fe422011-11-18 02:44:19 +00002310 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
Owen Anderson0032b272009-08-13 21:57:51 +00002311 llvm::Value *Offset =
Eli Friedman8d2fe422011-11-18 02:44:19 +00002312 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002313 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2314 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00002315 CGF.Int64Ty);
Eli Friedman8d2fe422011-11-18 02:44:19 +00002316 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002317 overflow_arg_area =
2318 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2319 overflow_arg_area->getType(),
2320 "overflow_arg_area.align");
2321 }
2322
2323 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002324 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002325 llvm::Value *Res =
2326 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002327 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002328
2329 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2330 // l->overflow_arg_area + sizeof(type).
2331 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2332 // an 8 byte boundary.
2333
2334 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00002335 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00002336 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002337 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2338 "overflow_arg_area.next");
2339 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2340
2341 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2342 return Res;
2343}
2344
2345llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2346 CodeGenFunction &CGF) const {
2347 // Assume that va_list type is correct; should be pointer to LLVM type:
2348 // struct {
2349 // i32 gp_offset;
2350 // i32 fp_offset;
2351 // i8* overflow_arg_area;
2352 // i8* reg_save_area;
2353 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00002354 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002355
Chris Lattnera14db752010-03-11 18:19:55 +00002356 Ty = CGF.getContext().getCanonicalType(Ty);
Daniel Dunbaredfac032012-03-10 01:03:58 +00002357 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002358
2359 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2360 // in the registers. If not go to step 7.
2361 if (!neededInt && !neededSSE)
2362 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2363
2364 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2365 // general purpose registers needed to pass type and num_fp to hold
2366 // the number of floating point registers needed.
2367
2368 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2369 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2370 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2371 //
2372 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2373 // register save space).
2374
2375 llvm::Value *InRegs = 0;
2376 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2377 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2378 if (neededInt) {
2379 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2380 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002381 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2382 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002383 }
2384
2385 if (neededSSE) {
2386 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2387 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2388 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00002389 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2390 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002391 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2392 }
2393
2394 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2395 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2396 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2397 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2398
2399 // Emit code to load the value if it was passed in registers.
2400
2401 CGF.EmitBlock(InRegBlock);
2402
2403 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2404 // an offset of l->gp_offset and/or l->fp_offset. This may require
2405 // copying to a temporary location in case the parameter is passed
2406 // in different register classes or requires an alignment greater
2407 // than 8 for general purpose registers and 16 for XMM registers.
2408 //
2409 // FIXME: This really results in shameful code when we end up needing to
2410 // collect arguments from different places; often what should result in a
2411 // simple assembling of a structure from scattered addresses has many more
2412 // loads than necessary. Can we clean this up?
Chris Lattner2acc6e32011-07-18 04:24:23 +00002413 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002414 llvm::Value *RegAddr =
2415 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2416 "reg_save_area");
2417 if (neededInt && neededSSE) {
2418 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002419 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002420 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002421 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2422 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002423 llvm::Type *TyLo = ST->getElementType(0);
2424 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002425 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002426 "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002427 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2428 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002429 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2430 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002431 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2432 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002433 llvm::Value *V =
2434 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2435 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2436 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2437 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2438
Owen Andersona1cf15f2009-07-14 23:10:40 +00002439 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002440 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002441 } else if (neededInt) {
2442 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2443 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002444 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002445 } else if (neededSSE == 1) {
2446 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2447 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2448 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002449 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002450 assert(neededSSE == 2 && "Invalid number of needed registers!");
2451 // SSE registers are spaced 16 bytes apart in the register save
2452 // area, we need to collect the two eightbytes together.
2453 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002454 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner8b418682012-02-07 00:39:47 +00002455 llvm::Type *DoubleTy = CGF.DoubleTy;
Chris Lattner2acc6e32011-07-18 04:24:23 +00002456 llvm::Type *DblPtrTy =
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002457 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002458 llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002459 DoubleTy, NULL);
2460 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2461 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2462 DblPtrTy));
2463 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2464 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2465 DblPtrTy));
2466 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2467 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2468 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002469 }
2470
2471 // AMD64-ABI 3.5.7p5: Step 5. Set:
2472 // l->gp_offset = l->gp_offset + num_gp * 8
2473 // l->fp_offset = l->fp_offset + num_fp * 16.
2474 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002475 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002476 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2477 gp_offset_p);
2478 }
2479 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002480 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002481 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2482 fp_offset_p);
2483 }
2484 CGF.EmitBranch(ContBlock);
2485
2486 // Emit code to load the value if it was passed in memory.
2487
2488 CGF.EmitBlock(InMemBlock);
2489 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2490
2491 // Return the appropriate result.
2492
2493 CGF.EmitBlock(ContBlock);
Jay Foadbbf3bac2011-03-30 11:28:58 +00002494 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002495 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002496 ResAddr->addIncoming(RegAddr, InRegBlock);
2497 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002498 return ResAddr;
2499}
2500
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002501ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2502
2503 if (Ty->isVoidType())
2504 return ABIArgInfo::getIgnore();
2505
2506 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2507 Ty = EnumTy->getDecl()->getIntegerType();
2508
2509 uint64_t Size = getContext().getTypeSize(Ty);
2510
2511 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002512 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2513 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002514 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2515
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002516 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
John McCall64aa4b32013-04-16 22:48:15 +00002517 if (Size == 128 && getTarget().getTriple().getOS() == llvm::Triple::MinGW32)
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002518 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2519 Size));
2520
2521 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2522 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2523 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002524 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002525 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2526 Size));
2527
2528 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2529 }
2530
2531 if (Ty->isPromotableIntegerType())
2532 return ABIArgInfo::getExtend();
2533
2534 return ABIArgInfo::getDirect();
2535}
2536
2537void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2538
2539 QualType RetTy = FI.getReturnType();
2540 FI.getReturnInfo() = classify(RetTy);
2541
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002542 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2543 it != ie; ++it)
2544 it->info = classify(it->type);
2545}
2546
Chris Lattnerf13721d2010-08-31 16:44:54 +00002547llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2548 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002549 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002550
Chris Lattnerf13721d2010-08-31 16:44:54 +00002551 CGBuilderTy &Builder = CGF.Builder;
2552 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2553 "ap");
2554 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2555 llvm::Type *PTy =
2556 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2557 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2558
2559 uint64_t Offset =
2560 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2561 llvm::Value *NextAddr =
2562 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2563 "ap.next");
2564 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2565
2566 return AddrTyped;
2567}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002568
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00002569namespace {
2570
Derek Schuff263366f2012-10-16 22:30:41 +00002571class NaClX86_64ABIInfo : public ABIInfo {
2572 public:
2573 NaClX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2574 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, HasAVX) {}
2575 virtual void computeInfo(CGFunctionInfo &FI) const;
2576 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2577 CodeGenFunction &CGF) const;
2578 private:
2579 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
2580 X86_64ABIInfo NInfo; // Used for everything else.
2581};
2582
2583class NaClX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2584 public:
2585 NaClX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2586 : TargetCodeGenInfo(new NaClX86_64ABIInfo(CGT, HasAVX)) {}
2587};
2588
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00002589}
2590
Derek Schuff263366f2012-10-16 22:30:41 +00002591void NaClX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2592 if (FI.getASTCallingConvention() == CC_PnaclCall)
2593 PInfo.computeInfo(FI);
2594 else
2595 NInfo.computeInfo(FI);
2596}
2597
2598llvm::Value *NaClX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2599 CodeGenFunction &CGF) const {
2600 // Always use the native convention; calling pnacl-style varargs functions
2601 // is unuspported.
2602 return NInfo.EmitVAArg(VAListAddr, Ty, CGF);
2603}
2604
2605
John McCallec853ba2010-03-11 00:10:12 +00002606// PowerPC-32
2607
2608namespace {
2609class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2610public:
Chris Lattnerea044322010-07-29 02:01:43 +00002611 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002612
John McCallec853ba2010-03-11 00:10:12 +00002613 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2614 // This is recovered from gcc output.
2615 return 1; // r1 is the dedicated stack pointer
2616 }
2617
2618 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002619 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002620};
2621
2622}
2623
2624bool
2625PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2626 llvm::Value *Address) const {
2627 // This is calculated from the LLVM and GCC tables and verified
2628 // against gcc output. AFAIK all ABIs use the same encoding.
2629
2630 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallec853ba2010-03-11 00:10:12 +00002631
Chris Lattner8b418682012-02-07 00:39:47 +00002632 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallec853ba2010-03-11 00:10:12 +00002633 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2634 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2635 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2636
2637 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002638 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002639
2640 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002641 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002642
2643 // 64-76 are various 4-byte special-purpose registers:
2644 // 64: mq
2645 // 65: lr
2646 // 66: ctr
2647 // 67: ap
2648 // 68-75 cr0-7
2649 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002650 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002651
2652 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002653 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002654
2655 // 109: vrsave
2656 // 110: vscr
2657 // 111: spe_acc
2658 // 112: spefscr
2659 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002660 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002661
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002662 return false;
John McCallec853ba2010-03-11 00:10:12 +00002663}
2664
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002665// PowerPC-64
2666
2667namespace {
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002668/// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
2669class PPC64_SVR4_ABIInfo : public DefaultABIInfo {
2670
2671public:
2672 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
2673
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002674 bool isPromotableTypeForABI(QualType Ty) const;
2675
2676 ABIArgInfo classifyReturnType(QualType RetTy) const;
2677 ABIArgInfo classifyArgumentType(QualType Ty) const;
2678
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00002679 // TODO: We can add more logic to computeInfo to improve performance.
2680 // Example: For aggregate arguments that fit in a register, we could
2681 // use getDirectInReg (as is done below for structs containing a single
2682 // floating-point value) to avoid pushing them to memory on function
2683 // entry. This would require changing the logic in PPCISelLowering
2684 // when lowering the parameters in the caller and args in the callee.
2685 virtual void computeInfo(CGFunctionInfo &FI) const {
2686 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2687 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2688 it != ie; ++it) {
2689 // We rely on the default argument classification for the most part.
2690 // One exception: An aggregate containing a single floating-point
2691 // item must be passed in a register if one is available.
2692 const Type *T = isSingleElementStruct(it->type, getContext());
2693 if (T) {
2694 const BuiltinType *BT = T->getAs<BuiltinType>();
2695 if (BT && BT->isFloatingPoint()) {
2696 QualType QT(T, 0);
2697 it->info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
2698 continue;
2699 }
2700 }
2701 it->info = classifyArgumentType(it->type);
2702 }
2703 }
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002704
2705 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr,
2706 QualType Ty,
2707 CodeGenFunction &CGF) const;
2708};
2709
2710class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
2711public:
2712 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT)
2713 : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT)) {}
2714
2715 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2716 // This is recovered from gcc output.
2717 return 1; // r1 is the dedicated stack pointer
2718 }
2719
2720 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2721 llvm::Value *Address) const;
2722};
2723
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002724class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2725public:
2726 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
2727
2728 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2729 // This is recovered from gcc output.
2730 return 1; // r1 is the dedicated stack pointer
2731 }
2732
2733 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2734 llvm::Value *Address) const;
2735};
2736
2737}
2738
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002739// Return true if the ABI requires Ty to be passed sign- or zero-
2740// extended to 64 bits.
2741bool
2742PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
2743 // Treat an enum type as its underlying type.
2744 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2745 Ty = EnumTy->getDecl()->getIntegerType();
2746
2747 // Promotable integer types are required to be promoted by the ABI.
2748 if (Ty->isPromotableIntegerType())
2749 return true;
2750
2751 // In addition to the usual promotable integer types, we also need to
2752 // extend all 32-bit types, since the ABI requires promotion to 64 bits.
2753 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2754 switch (BT->getKind()) {
2755 case BuiltinType::Int:
2756 case BuiltinType::UInt:
2757 return true;
2758 default:
2759 break;
2760 }
2761
2762 return false;
2763}
2764
2765ABIArgInfo
2766PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
Bill Schmidtc9715fc2012-11-27 02:46:43 +00002767 if (Ty->isAnyComplexType())
2768 return ABIArgInfo::getDirect();
2769
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002770 if (isAggregateTypeForABI(Ty)) {
2771 // Records with non trivial destructors/constructors should not be passed
2772 // by value.
2773 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2774 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2775
2776 return ABIArgInfo::getIndirect(0);
2777 }
2778
2779 return (isPromotableTypeForABI(Ty) ?
2780 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2781}
2782
2783ABIArgInfo
2784PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
2785 if (RetTy->isVoidType())
2786 return ABIArgInfo::getIgnore();
2787
Bill Schmidt9e6111a2012-12-17 04:20:17 +00002788 if (RetTy->isAnyComplexType())
2789 return ABIArgInfo::getDirect();
2790
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002791 if (isAggregateTypeForABI(RetTy))
2792 return ABIArgInfo::getIndirect(0);
2793
2794 return (isPromotableTypeForABI(RetTy) ?
2795 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2796}
2797
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002798// Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
2799llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr,
2800 QualType Ty,
2801 CodeGenFunction &CGF) const {
2802 llvm::Type *BP = CGF.Int8PtrTy;
2803 llvm::Type *BPP = CGF.Int8PtrPtrTy;
2804
2805 CGBuilderTy &Builder = CGF.Builder;
2806 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
2807 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2808
Bill Schmidt19f8e852013-01-14 17:45:36 +00002809 // Update the va_list pointer. The pointer should be bumped by the
2810 // size of the object. We can trust getTypeSize() except for a complex
2811 // type whose base type is smaller than a doubleword. For these, the
2812 // size of the object is 16 bytes; see below for further explanation.
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002813 unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8;
Bill Schmidt19f8e852013-01-14 17:45:36 +00002814 QualType BaseTy;
2815 unsigned CplxBaseSize = 0;
2816
2817 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
2818 BaseTy = CTy->getElementType();
2819 CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8;
2820 if (CplxBaseSize < 8)
2821 SizeInBytes = 16;
2822 }
2823
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002824 unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8);
2825 llvm::Value *NextAddr =
2826 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset),
2827 "ap.next");
2828 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2829
Bill Schmidt19f8e852013-01-14 17:45:36 +00002830 // If we have a complex type and the base type is smaller than 8 bytes,
2831 // the ABI calls for the real and imaginary parts to be right-adjusted
2832 // in separate doublewords. However, Clang expects us to produce a
2833 // pointer to a structure with the two parts packed tightly. So generate
2834 // loads of the real and imaginary parts relative to the va_list pointer,
2835 // and store them to a temporary structure.
2836 if (CplxBaseSize && CplxBaseSize < 8) {
2837 llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2838 llvm::Value *ImagAddr = RealAddr;
2839 RealAddr = Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize));
2840 ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize));
2841 llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy));
2842 RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy);
2843 ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy);
2844 llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal");
2845 llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag");
2846 llvm::Value *Ptr = CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty),
2847 "vacplx");
2848 llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, ".real");
2849 llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, ".imag");
2850 Builder.CreateStore(Real, RealPtr, false);
2851 Builder.CreateStore(Imag, ImagPtr, false);
2852 return Ptr;
2853 }
2854
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002855 // If the argument is smaller than 8 bytes, it is right-adjusted in
2856 // its doubleword slot. Adjust the pointer to pick it up from the
2857 // correct offset.
2858 if (SizeInBytes < 8) {
2859 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2860 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes));
2861 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2862 }
2863
2864 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2865 return Builder.CreateBitCast(Addr, PTy);
2866}
2867
2868static bool
2869PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2870 llvm::Value *Address) {
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002871 // This is calculated from the LLVM and GCC tables and verified
2872 // against gcc output. AFAIK all ABIs use the same encoding.
2873
2874 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2875
2876 llvm::IntegerType *i8 = CGF.Int8Ty;
2877 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2878 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2879 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2880
2881 // 0-31: r0-31, the 8-byte general-purpose registers
2882 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
2883
2884 // 32-63: fp0-31, the 8-byte floating-point registers
2885 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
2886
2887 // 64-76 are various 4-byte special-purpose registers:
2888 // 64: mq
2889 // 65: lr
2890 // 66: ctr
2891 // 67: ap
2892 // 68-75 cr0-7
2893 // 76: xer
2894 AssignToArrayRange(Builder, Address, Four8, 64, 76);
2895
2896 // 77-108: v0-31, the 16-byte vector registers
2897 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
2898
2899 // 109: vrsave
2900 // 110: vscr
2901 // 111: spe_acc
2902 // 112: spefscr
2903 // 113: sfp
2904 AssignToArrayRange(Builder, Address, Four8, 109, 113);
2905
2906 return false;
2907}
John McCallec853ba2010-03-11 00:10:12 +00002908
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002909bool
2910PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
2911 CodeGen::CodeGenFunction &CGF,
2912 llvm::Value *Address) const {
2913
2914 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
2915}
2916
2917bool
2918PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2919 llvm::Value *Address) const {
2920
2921 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
2922}
2923
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002924//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002925// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002926//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002927
2928namespace {
2929
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002930class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002931public:
2932 enum ABIKind {
2933 APCS = 0,
2934 AAPCS = 1,
2935 AAPCS_VFP
2936 };
2937
2938private:
2939 ABIKind Kind;
2940
2941public:
John McCallbd7370a2013-02-28 19:01:20 +00002942 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {
2943 setRuntimeCC();
2944 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002945
John McCall49e34be2011-08-30 01:42:09 +00002946 bool isEABI() const {
John McCall64aa4b32013-04-16 22:48:15 +00002947 StringRef Env = getTarget().getTriple().getEnvironmentName();
Logan Chien94a71422012-09-02 09:30:11 +00002948 return (Env == "gnueabi" || Env == "eabi" ||
2949 Env == "android" || Env == "androideabi");
John McCall49e34be2011-08-30 01:42:09 +00002950 }
2951
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002952private:
2953 ABIKind getABIKind() const { return Kind; }
2954
Chris Lattnera3c109b2010-07-29 02:16:43 +00002955 ABIArgInfo classifyReturnType(QualType RetTy) const;
Manman Ren710c5172012-10-31 19:02:26 +00002956 ABIArgInfo classifyArgumentType(QualType RetTy, int *VFPRegs,
2957 unsigned &AllocatedVFP,
Manman Renb3fa55f2012-10-30 23:21:41 +00002958 bool &IsHA) const;
Manman Ren97f81572012-10-16 19:18:39 +00002959 bool isIllegalVectorType(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002960
Chris Lattneree5dcd02010-07-29 02:31:05 +00002961 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002962
2963 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2964 CodeGenFunction &CGF) const;
John McCallbd7370a2013-02-28 19:01:20 +00002965
2966 llvm::CallingConv::ID getLLVMDefaultCC() const;
2967 llvm::CallingConv::ID getABIDefaultCC() const;
2968 void setRuntimeCC();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002969};
2970
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002971class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2972public:
Chris Lattnerea044322010-07-29 02:01:43 +00002973 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2974 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00002975
John McCall49e34be2011-08-30 01:42:09 +00002976 const ARMABIInfo &getABIInfo() const {
2977 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
2978 }
2979
John McCall6374c332010-03-06 00:35:14 +00002980 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2981 return 13;
2982 }
Roman Divacky09345d12011-05-18 19:36:54 +00002983
Chris Lattner5f9e2722011-07-23 10:55:15 +00002984 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCallf85e1932011-06-15 23:02:42 +00002985 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2986 }
2987
Roman Divacky09345d12011-05-18 19:36:54 +00002988 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2989 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002990 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divacky09345d12011-05-18 19:36:54 +00002991
2992 // 0-15 are the 16 integer registers.
Chris Lattner8b418682012-02-07 00:39:47 +00002993 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divacky09345d12011-05-18 19:36:54 +00002994 return false;
2995 }
John McCall49e34be2011-08-30 01:42:09 +00002996
2997 unsigned getSizeOfUnwindException() const {
2998 if (getABIInfo().isEABI()) return 88;
2999 return TargetCodeGenInfo::getSizeOfUnwindException();
3000 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003001};
3002
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003003}
3004
Chris Lattneree5dcd02010-07-29 02:31:05 +00003005void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Manman Renb3fa55f2012-10-30 23:21:41 +00003006 // To correctly handle Homogeneous Aggregate, we need to keep track of the
Manman Ren710c5172012-10-31 19:02:26 +00003007 // VFP registers allocated so far.
Manman Renb3fa55f2012-10-30 23:21:41 +00003008 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
3009 // VFP registers of the appropriate type unallocated then the argument is
3010 // allocated to the lowest-numbered sequence of such registers.
3011 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
3012 // unallocated are marked as unavailable.
3013 unsigned AllocatedVFP = 0;
Manman Ren710c5172012-10-31 19:02:26 +00003014 int VFPRegs[16] = { 0 };
Chris Lattnera3c109b2010-07-29 02:16:43 +00003015 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003016 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Manman Renb3fa55f2012-10-30 23:21:41 +00003017 it != ie; ++it) {
3018 unsigned PreAllocation = AllocatedVFP;
3019 bool IsHA = false;
3020 // 6.1.2.3 There is one VFP co-processor register class using registers
3021 // s0-s15 (d0-d7) for passing arguments.
3022 const unsigned NumVFPs = 16;
Manman Ren710c5172012-10-31 19:02:26 +00003023 it->info = classifyArgumentType(it->type, VFPRegs, AllocatedVFP, IsHA);
Manman Renb3fa55f2012-10-30 23:21:41 +00003024 // If we do not have enough VFP registers for the HA, any VFP registers
3025 // that are unallocated are marked as unavailable. To achieve this, we add
3026 // padding of (NumVFPs - PreAllocation) floats.
3027 if (IsHA && AllocatedVFP > NumVFPs && PreAllocation < NumVFPs) {
3028 llvm::Type *PaddingTy = llvm::ArrayType::get(
3029 llvm::Type::getFloatTy(getVMContext()), NumVFPs - PreAllocation);
3030 it->info = ABIArgInfo::getExpandWithPadding(false, PaddingTy);
3031 }
3032 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003033
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003034 // Always honor user-specified calling convention.
3035 if (FI.getCallingConvention() != llvm::CallingConv::C)
3036 return;
3037
John McCallbd7370a2013-02-28 19:01:20 +00003038 llvm::CallingConv::ID cc = getRuntimeCC();
3039 if (cc != llvm::CallingConv::C)
3040 FI.setEffectiveCallingConvention(cc);
3041}
Rafael Espindola25117ab2010-06-16 16:13:39 +00003042
John McCallbd7370a2013-02-28 19:01:20 +00003043/// Return the default calling convention that LLVM will use.
3044llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
3045 // The default calling convention that LLVM will infer.
John McCall64aa4b32013-04-16 22:48:15 +00003046 if (getTarget().getTriple().getEnvironmentName()=="gnueabihf")
John McCallbd7370a2013-02-28 19:01:20 +00003047 return llvm::CallingConv::ARM_AAPCS_VFP;
3048 else if (isEABI())
3049 return llvm::CallingConv::ARM_AAPCS;
3050 else
3051 return llvm::CallingConv::ARM_APCS;
3052}
3053
3054/// Return the calling convention that our ABI would like us to use
3055/// as the C calling convention.
3056llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003057 switch (getABIKind()) {
John McCallbd7370a2013-02-28 19:01:20 +00003058 case APCS: return llvm::CallingConv::ARM_APCS;
3059 case AAPCS: return llvm::CallingConv::ARM_AAPCS;
3060 case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003061 }
John McCallbd7370a2013-02-28 19:01:20 +00003062 llvm_unreachable("bad ABI kind");
3063}
3064
3065void ARMABIInfo::setRuntimeCC() {
3066 assert(getRuntimeCC() == llvm::CallingConv::C);
3067
3068 // Don't muddy up the IR with a ton of explicit annotations if
3069 // they'd just match what LLVM will infer from the triple.
3070 llvm::CallingConv::ID abiCC = getABIDefaultCC();
3071 if (abiCC != getLLVMDefaultCC())
3072 RuntimeCC = abiCC;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003073}
3074
Bob Wilson194f06a2011-08-03 05:58:22 +00003075/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
3076/// aggregate. If HAMembers is non-null, the number of base elements
3077/// contained in the type is returned through it; this is used for the
3078/// recursive calls that check aggregate component types.
3079static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
3080 ASTContext &Context,
3081 uint64_t *HAMembers = 0) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003082 uint64_t Members = 0;
Bob Wilson194f06a2011-08-03 05:58:22 +00003083 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
3084 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
3085 return false;
3086 Members *= AT->getSize().getZExtValue();
3087 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
3088 const RecordDecl *RD = RT->getDecl();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003089 if (RD->hasFlexibleArrayMember())
Bob Wilson194f06a2011-08-03 05:58:22 +00003090 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003091
Bob Wilson194f06a2011-08-03 05:58:22 +00003092 Members = 0;
3093 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3094 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00003095 const FieldDecl *FD = *i;
Bob Wilson194f06a2011-08-03 05:58:22 +00003096 uint64_t FldMembers;
3097 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
3098 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003099
3100 Members = (RD->isUnion() ?
3101 std::max(Members, FldMembers) : Members + FldMembers);
Bob Wilson194f06a2011-08-03 05:58:22 +00003102 }
3103 } else {
3104 Members = 1;
3105 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
3106 Members = 2;
3107 Ty = CT->getElementType();
3108 }
3109
3110 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
3111 // double, or 64-bit or 128-bit vectors.
3112 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3113 if (BT->getKind() != BuiltinType::Float &&
Tim Northoveradfa45f2012-07-20 22:29:29 +00003114 BT->getKind() != BuiltinType::Double &&
3115 BT->getKind() != BuiltinType::LongDouble)
Bob Wilson194f06a2011-08-03 05:58:22 +00003116 return false;
3117 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
3118 unsigned VecSize = Context.getTypeSize(VT);
3119 if (VecSize != 64 && VecSize != 128)
3120 return false;
3121 } else {
3122 return false;
3123 }
3124
3125 // The base type must be the same for all members. Vector types of the
3126 // same total size are treated as being equivalent here.
3127 const Type *TyPtr = Ty.getTypePtr();
3128 if (!Base)
3129 Base = TyPtr;
3130 if (Base != TyPtr &&
3131 (!Base->isVectorType() || !TyPtr->isVectorType() ||
3132 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
3133 return false;
3134 }
3135
3136 // Homogeneous Aggregates can have at most 4 members of the base type.
3137 if (HAMembers)
3138 *HAMembers = Members;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003139
3140 return (Members > 0 && Members <= 4);
Bob Wilson194f06a2011-08-03 05:58:22 +00003141}
3142
Manman Ren710c5172012-10-31 19:02:26 +00003143/// markAllocatedVFPs - update VFPRegs according to the alignment and
3144/// number of VFP registers (unit is S register) requested.
3145static void markAllocatedVFPs(int *VFPRegs, unsigned &AllocatedVFP,
3146 unsigned Alignment,
3147 unsigned NumRequired) {
3148 // Early Exit.
3149 if (AllocatedVFP >= 16)
3150 return;
3151 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
3152 // VFP registers of the appropriate type unallocated then the argument is
3153 // allocated to the lowest-numbered sequence of such registers.
3154 for (unsigned I = 0; I < 16; I += Alignment) {
3155 bool FoundSlot = true;
3156 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3157 if (J >= 16 || VFPRegs[J]) {
3158 FoundSlot = false;
3159 break;
3160 }
3161 if (FoundSlot) {
3162 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3163 VFPRegs[J] = 1;
3164 AllocatedVFP += NumRequired;
3165 return;
3166 }
3167 }
3168 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
3169 // unallocated are marked as unavailable.
3170 for (unsigned I = 0; I < 16; I++)
3171 VFPRegs[I] = 1;
3172 AllocatedVFP = 17; // We do not have enough VFP registers.
3173}
3174
3175ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, int *VFPRegs,
3176 unsigned &AllocatedVFP,
Manman Renb3fa55f2012-10-30 23:21:41 +00003177 bool &IsHA) const {
3178 // We update number of allocated VFPs according to
3179 // 6.1.2.1 The following argument types are VFP CPRCs:
3180 // A single-precision floating-point type (including promoted
3181 // half-precision types); A double-precision floating-point type;
3182 // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
3183 // with a Base Type of a single- or double-precision floating-point type,
3184 // 64-bit containerized vectors or 128-bit containerized vectors with one
3185 // to four Elements.
3186
Manman Ren97f81572012-10-16 19:18:39 +00003187 // Handle illegal vector types here.
3188 if (isIllegalVectorType(Ty)) {
3189 uint64_t Size = getContext().getTypeSize(Ty);
3190 if (Size <= 32) {
3191 llvm::Type *ResType =
3192 llvm::Type::getInt32Ty(getVMContext());
3193 return ABIArgInfo::getDirect(ResType);
3194 }
3195 if (Size == 64) {
3196 llvm::Type *ResType = llvm::VectorType::get(
3197 llvm::Type::getInt32Ty(getVMContext()), 2);
Manman Ren710c5172012-10-31 19:02:26 +00003198 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
Manman Ren97f81572012-10-16 19:18:39 +00003199 return ABIArgInfo::getDirect(ResType);
3200 }
3201 if (Size == 128) {
3202 llvm::Type *ResType = llvm::VectorType::get(
3203 llvm::Type::getInt32Ty(getVMContext()), 4);
Manman Ren710c5172012-10-31 19:02:26 +00003204 markAllocatedVFPs(VFPRegs, AllocatedVFP, 4, 4);
Manman Ren97f81572012-10-16 19:18:39 +00003205 return ABIArgInfo::getDirect(ResType);
3206 }
3207 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3208 }
Manman Ren710c5172012-10-31 19:02:26 +00003209 // Update VFPRegs for legal vector types.
Manman Renb3fa55f2012-10-30 23:21:41 +00003210 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3211 uint64_t Size = getContext().getTypeSize(VT);
3212 // Size of a legal vector should be power of 2 and above 64.
Manman Ren710c5172012-10-31 19:02:26 +00003213 markAllocatedVFPs(VFPRegs, AllocatedVFP, Size >= 128 ? 4 : 2, Size / 32);
Manman Renb3fa55f2012-10-30 23:21:41 +00003214 }
Manman Ren710c5172012-10-31 19:02:26 +00003215 // Update VFPRegs for floating point types.
Manman Renb3fa55f2012-10-30 23:21:41 +00003216 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3217 if (BT->getKind() == BuiltinType::Half ||
3218 BT->getKind() == BuiltinType::Float)
Manman Ren710c5172012-10-31 19:02:26 +00003219 markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, 1);
Manman Renb3fa55f2012-10-30 23:21:41 +00003220 if (BT->getKind() == BuiltinType::Double ||
Manman Ren710c5172012-10-31 19:02:26 +00003221 BT->getKind() == BuiltinType::LongDouble)
3222 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
Manman Renb3fa55f2012-10-30 23:21:41 +00003223 }
Manman Ren97f81572012-10-16 19:18:39 +00003224
John McCalld608cdb2010-08-22 10:59:02 +00003225 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003226 // Treat an enum type as its underlying type.
3227 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3228 Ty = EnumTy->getDecl()->getIntegerType();
3229
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003230 return (Ty->isPromotableIntegerType() ?
3231 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003232 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003233
Daniel Dunbar42025572009-09-14 21:54:03 +00003234 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003235 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00003236 return ABIArgInfo::getIgnore();
3237
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003238 // Structures with either a non-trivial destructor or a non-trivial
3239 // copy constructor are always indirect.
3240 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
3241 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3242
Bob Wilson194f06a2011-08-03 05:58:22 +00003243 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
Manman Renb3fa55f2012-10-30 23:21:41 +00003244 // Homogeneous Aggregates need to be expanded when we can fit the aggregate
3245 // into VFP registers.
Bob Wilson194f06a2011-08-03 05:58:22 +00003246 const Type *Base = 0;
Manman Renb3fa55f2012-10-30 23:21:41 +00003247 uint64_t Members = 0;
3248 if (isHomogeneousAggregate(Ty, Base, getContext(), &Members)) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003249 assert(Base && "Base class should be set for homogeneous aggregate");
Manman Renb3fa55f2012-10-30 23:21:41 +00003250 // Base can be a floating-point or a vector.
3251 if (Base->isVectorType()) {
3252 // ElementSize is in number of floats.
3253 unsigned ElementSize = getContext().getTypeSize(Base) == 64 ? 2 : 4;
Manman Rencb489dd2012-11-06 19:05:29 +00003254 markAllocatedVFPs(VFPRegs, AllocatedVFP, ElementSize,
3255 Members * ElementSize);
Manman Renb3fa55f2012-10-30 23:21:41 +00003256 } else if (Base->isSpecificBuiltinType(BuiltinType::Float))
Manman Ren710c5172012-10-31 19:02:26 +00003257 markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, Members);
Manman Renb3fa55f2012-10-30 23:21:41 +00003258 else {
3259 assert(Base->isSpecificBuiltinType(BuiltinType::Double) ||
3260 Base->isSpecificBuiltinType(BuiltinType::LongDouble));
Manman Ren710c5172012-10-31 19:02:26 +00003261 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, Members * 2);
Manman Renb3fa55f2012-10-30 23:21:41 +00003262 }
3263 IsHA = true;
Bob Wilson194f06a2011-08-03 05:58:22 +00003264 return ABIArgInfo::getExpand();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003265 }
Bob Wilson194f06a2011-08-03 05:58:22 +00003266 }
3267
Manman Ren634b3d22012-08-13 21:23:55 +00003268 // Support byval for ARM.
Manman Rencb489dd2012-11-06 19:05:29 +00003269 // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
3270 // most 8-byte. We realign the indirect argument if type alignment is bigger
3271 // than ABI alignment.
Manman Renfd1ba912012-11-05 22:42:46 +00003272 uint64_t ABIAlign = 4;
3273 uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8;
3274 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3275 getABIKind() == ARMABIInfo::AAPCS)
3276 ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
Manman Ren885ad692012-11-06 04:58:01 +00003277 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
3278 return ABIArgInfo::getIndirect(0, /*ByVal=*/true,
Manman Rencb489dd2012-11-06 19:05:29 +00003279 /*Realign=*/TyAlign > ABIAlign);
Eli Friedman79f30982012-08-09 00:31:40 +00003280 }
3281
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00003282 // Otherwise, pass by coercing to a structure of the appropriate size.
Chris Lattner2acc6e32011-07-18 04:24:23 +00003283 llvm::Type* ElemTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003284 unsigned SizeRegs;
Eli Friedman79f30982012-08-09 00:31:40 +00003285 // FIXME: Try to match the types of the arguments more accurately where
3286 // we can.
3287 if (getContext().getTypeAlign(Ty) <= 32) {
Bob Wilson53fc1a62011-08-01 23:39:04 +00003288 ElemTy = llvm::Type::getInt32Ty(getVMContext());
3289 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Manman Ren78eb76e2012-06-25 22:04:00 +00003290 } else {
Manman Ren78eb76e2012-06-25 22:04:00 +00003291 ElemTy = llvm::Type::getInt64Ty(getVMContext());
3292 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastings67d097e2011-04-27 17:24:02 +00003293 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003294
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003295 llvm::Type *STy =
Chris Lattner7650d952011-06-18 22:49:11 +00003296 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003297 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003298}
3299
Chris Lattnera3c109b2010-07-29 02:16:43 +00003300static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00003301 llvm::LLVMContext &VMContext) {
3302 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
3303 // is called integer-like if its size is less than or equal to one word, and
3304 // the offset of each of its addressable sub-fields is zero.
3305
3306 uint64_t Size = Context.getTypeSize(Ty);
3307
3308 // Check that the type fits in a word.
3309 if (Size > 32)
3310 return false;
3311
3312 // FIXME: Handle vector types!
3313 if (Ty->isVectorType())
3314 return false;
3315
Daniel Dunbarb0d58192009-09-14 02:20:34 +00003316 // Float types are never treated as "integer like".
3317 if (Ty->isRealFloatingType())
3318 return false;
3319
Daniel Dunbar98303b92009-09-13 08:03:58 +00003320 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00003321 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00003322 return true;
3323
Daniel Dunbar45815812010-02-01 23:31:26 +00003324 // Small complex integer types are "integer like".
3325 if (const ComplexType *CT = Ty->getAs<ComplexType>())
3326 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003327
3328 // Single element and zero sized arrays should be allowed, by the definition
3329 // above, but they are not.
3330
3331 // Otherwise, it must be a record type.
3332 const RecordType *RT = Ty->getAs<RecordType>();
3333 if (!RT) return false;
3334
3335 // Ignore records with flexible arrays.
3336 const RecordDecl *RD = RT->getDecl();
3337 if (RD->hasFlexibleArrayMember())
3338 return false;
3339
3340 // Check that all sub-fields are at offset 0, and are themselves "integer
3341 // like".
3342 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3343
3344 bool HadField = false;
3345 unsigned idx = 0;
3346 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3347 i != e; ++i, ++idx) {
David Blaikie581deb32012-06-06 20:45:41 +00003348 const FieldDecl *FD = *i;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003349
Daniel Dunbar679855a2010-01-29 03:22:29 +00003350 // Bit-fields are not addressable, we only need to verify they are "integer
3351 // like". We still have to disallow a subsequent non-bitfield, for example:
3352 // struct { int : 0; int x }
3353 // is non-integer like according to gcc.
3354 if (FD->isBitField()) {
3355 if (!RD->isUnion())
3356 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003357
Daniel Dunbar679855a2010-01-29 03:22:29 +00003358 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3359 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003360
Daniel Dunbar679855a2010-01-29 03:22:29 +00003361 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003362 }
3363
Daniel Dunbar679855a2010-01-29 03:22:29 +00003364 // Check if this field is at offset 0.
3365 if (Layout.getFieldOffset(idx) != 0)
3366 return false;
3367
Daniel Dunbar98303b92009-09-13 08:03:58 +00003368 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3369 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003370
Daniel Dunbar679855a2010-01-29 03:22:29 +00003371 // Only allow at most one field in a structure. This doesn't match the
3372 // wording above, but follows gcc in situations with a field following an
3373 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00003374 if (!RD->isUnion()) {
3375 if (HadField)
3376 return false;
3377
3378 HadField = true;
3379 }
3380 }
3381
3382 return true;
3383}
3384
Chris Lattnera3c109b2010-07-29 02:16:43 +00003385ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003386 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003387 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00003388
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00003389 // Large vector types should be returned via memory.
3390 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
3391 return ABIArgInfo::getIndirect(0);
3392
John McCalld608cdb2010-08-22 10:59:02 +00003393 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003394 // Treat an enum type as its underlying type.
3395 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3396 RetTy = EnumTy->getDecl()->getIntegerType();
3397
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003398 return (RetTy->isPromotableIntegerType() ?
3399 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003400 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003401
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003402 // Structures with either a non-trivial destructor or a non-trivial
3403 // copy constructor are always indirect.
3404 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
3405 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3406
Daniel Dunbar98303b92009-09-13 08:03:58 +00003407 // Are we following APCS?
3408 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00003409 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00003410 return ABIArgInfo::getIgnore();
3411
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003412 // Complex types are all returned as packed integers.
3413 //
3414 // FIXME: Consider using 2 x vector types if the back end handles them
3415 // correctly.
3416 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00003417 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00003418 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003419
Daniel Dunbar98303b92009-09-13 08:03:58 +00003420 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003421 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003422 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003423 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003424 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003425 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003426 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003427 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3428 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003429 }
3430
3431 // Otherwise return in memory.
3432 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003433 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003434
3435 // Otherwise this is an AAPCS variant.
3436
Chris Lattnera3c109b2010-07-29 02:16:43 +00003437 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00003438 return ABIArgInfo::getIgnore();
3439
Bob Wilson3b694fa2011-11-02 04:51:36 +00003440 // Check for homogeneous aggregates with AAPCS-VFP.
3441 if (getABIKind() == AAPCS_VFP) {
3442 const Type *Base = 0;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003443 if (isHomogeneousAggregate(RetTy, Base, getContext())) {
3444 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson3b694fa2011-11-02 04:51:36 +00003445 // Homogeneous Aggregates are returned directly.
3446 return ABIArgInfo::getDirect();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003447 }
Bob Wilson3b694fa2011-11-02 04:51:36 +00003448 }
3449
Daniel Dunbar98303b92009-09-13 08:03:58 +00003450 // Aggregates <= 4 bytes are returned in r0; other aggregates
3451 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003452 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00003453 if (Size <= 32) {
3454 // Return in the smallest viable integer type.
3455 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003456 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003457 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003458 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3459 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003460 }
3461
Daniel Dunbar98303b92009-09-13 08:03:58 +00003462 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003463}
3464
Manman Ren97f81572012-10-16 19:18:39 +00003465/// isIllegalVector - check whether Ty is an illegal vector type.
3466bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
3467 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3468 // Check whether VT is legal.
3469 unsigned NumElements = VT->getNumElements();
3470 uint64_t Size = getContext().getTypeSize(VT);
3471 // NumElements should be power of 2.
3472 if ((NumElements & (NumElements - 1)) != 0)
3473 return true;
3474 // Size should be greater than 32 bits.
3475 return Size <= 32;
3476 }
3477 return false;
3478}
3479
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003480llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00003481 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003482 llvm::Type *BP = CGF.Int8PtrTy;
3483 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003484
3485 CGBuilderTy &Builder = CGF.Builder;
Chris Lattner8b418682012-02-07 00:39:47 +00003486 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003487 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Manman Rend105e732012-10-16 19:01:37 +00003488
3489 uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8;
Rafael Espindolae164c182011-08-02 22:33:37 +00003490 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
Manman Ren97f81572012-10-16 19:18:39 +00003491 bool IsIndirect = false;
Manman Rend105e732012-10-16 19:01:37 +00003492
3493 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
3494 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
Manman Ren93371022012-10-16 19:51:48 +00003495 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3496 getABIKind() == ARMABIInfo::AAPCS)
3497 TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
3498 else
3499 TyAlign = 4;
Manman Ren97f81572012-10-16 19:18:39 +00003500 // Use indirect if size of the illegal vector is bigger than 16 bytes.
3501 if (isIllegalVectorType(Ty) && Size > 16) {
3502 IsIndirect = true;
3503 Size = 4;
3504 TyAlign = 4;
3505 }
Manman Rend105e732012-10-16 19:01:37 +00003506
3507 // Handle address alignment for ABI alignment > 4 bytes.
Rafael Espindolae164c182011-08-02 22:33:37 +00003508 if (TyAlign > 4) {
3509 assert((TyAlign & (TyAlign - 1)) == 0 &&
3510 "Alignment is not power of 2!");
3511 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3512 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
3513 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
Manman Rend105e732012-10-16 19:01:37 +00003514 Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align");
Rafael Espindolae164c182011-08-02 22:33:37 +00003515 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003516
3517 uint64_t Offset =
Manman Rend105e732012-10-16 19:01:37 +00003518 llvm::RoundUpToAlignment(Size, 4);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003519 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00003520 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003521 "ap.next");
3522 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3523
Manman Ren97f81572012-10-16 19:18:39 +00003524 if (IsIndirect)
3525 Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP));
Manman Ren93371022012-10-16 19:51:48 +00003526 else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) {
Manman Rend105e732012-10-16 19:01:37 +00003527 // We can't directly cast ap.cur to pointer to a vector type, since ap.cur
3528 // may not be correctly aligned for the vector type. We create an aligned
3529 // temporary space and copy the content over from ap.cur to the temporary
3530 // space. This is necessary if the natural alignment of the type is greater
3531 // than the ABI alignment.
3532 llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
3533 CharUnits CharSize = getContext().getTypeSizeInChars(Ty);
3534 llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty),
3535 "var.align");
3536 llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
3537 llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy);
3538 Builder.CreateMemCpy(Dst, Src,
3539 llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()),
3540 TyAlign, false);
3541 Addr = AlignedTemp; //The content is in aligned location.
3542 }
3543 llvm::Type *PTy =
3544 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3545 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3546
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003547 return AddrTyped;
3548}
3549
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00003550namespace {
3551
Derek Schuff263366f2012-10-16 22:30:41 +00003552class NaClARMABIInfo : public ABIInfo {
3553 public:
3554 NaClARMABIInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3555 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, Kind) {}
3556 virtual void computeInfo(CGFunctionInfo &FI) const;
3557 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3558 CodeGenFunction &CGF) const;
3559 private:
3560 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
3561 ARMABIInfo NInfo; // Used for everything else.
3562};
3563
3564class NaClARMTargetCodeGenInfo : public TargetCodeGenInfo {
3565 public:
3566 NaClARMTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3567 : TargetCodeGenInfo(new NaClARMABIInfo(CGT, Kind)) {}
3568};
3569
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00003570}
3571
Derek Schuff263366f2012-10-16 22:30:41 +00003572void NaClARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
3573 if (FI.getASTCallingConvention() == CC_PnaclCall)
3574 PInfo.computeInfo(FI);
3575 else
3576 static_cast<const ABIInfo&>(NInfo).computeInfo(FI);
3577}
3578
3579llvm::Value *NaClARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3580 CodeGenFunction &CGF) const {
3581 // Always use the native convention; calling pnacl-style varargs functions
3582 // is unsupported.
3583 return static_cast<const ABIInfo&>(NInfo).EmitVAArg(VAListAddr, Ty, CGF);
3584}
3585
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003586//===----------------------------------------------------------------------===//
Tim Northoverc264e162013-01-31 12:13:10 +00003587// AArch64 ABI Implementation
3588//===----------------------------------------------------------------------===//
3589
3590namespace {
3591
3592class AArch64ABIInfo : public ABIInfo {
3593public:
3594 AArch64ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3595
3596private:
3597 // The AArch64 PCS is explicit about return types and argument types being
3598 // handled identically, so we don't need to draw a distinction between
3599 // Argument and Return classification.
3600 ABIArgInfo classifyGenericType(QualType Ty, int &FreeIntRegs,
3601 int &FreeVFPRegs) const;
3602
3603 ABIArgInfo tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded, bool IsInt,
3604 llvm::Type *DirectTy = 0) const;
3605
3606 virtual void computeInfo(CGFunctionInfo &FI) const;
3607
3608 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3609 CodeGenFunction &CGF) const;
3610};
3611
3612class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
3613public:
3614 AArch64TargetCodeGenInfo(CodeGenTypes &CGT)
3615 :TargetCodeGenInfo(new AArch64ABIInfo(CGT)) {}
3616
3617 const AArch64ABIInfo &getABIInfo() const {
3618 return static_cast<const AArch64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
3619 }
3620
3621 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3622 return 31;
3623 }
3624
3625 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3626 llvm::Value *Address) const {
3627 // 0-31 are x0-x30 and sp: 8 bytes each
3628 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
3629 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 31);
3630
3631 // 64-95 are v0-v31: 16 bytes each
3632 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
3633 AssignToArrayRange(CGF.Builder, Address, Sixteen8, 64, 95);
3634
3635 return false;
3636 }
3637
3638};
3639
3640}
3641
3642void AArch64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
3643 int FreeIntRegs = 8, FreeVFPRegs = 8;
3644
3645 FI.getReturnInfo() = classifyGenericType(FI.getReturnType(),
3646 FreeIntRegs, FreeVFPRegs);
3647
3648 FreeIntRegs = FreeVFPRegs = 8;
3649 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3650 it != ie; ++it) {
3651 it->info = classifyGenericType(it->type, FreeIntRegs, FreeVFPRegs);
3652
3653 }
3654}
3655
3656ABIArgInfo
3657AArch64ABIInfo::tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded,
3658 bool IsInt, llvm::Type *DirectTy) const {
3659 if (FreeRegs >= RegsNeeded) {
3660 FreeRegs -= RegsNeeded;
3661 return ABIArgInfo::getDirect(DirectTy);
3662 }
3663
3664 llvm::Type *Padding = 0;
3665
3666 // We need padding so that later arguments don't get filled in anyway. That
3667 // wouldn't happen if only ByVal arguments followed in the same category, but
3668 // a large structure will simply seem to be a pointer as far as LLVM is
3669 // concerned.
3670 if (FreeRegs > 0) {
3671 if (IsInt)
3672 Padding = llvm::Type::getInt64Ty(getVMContext());
3673 else
3674 Padding = llvm::Type::getFloatTy(getVMContext());
3675
3676 // Either [N x i64] or [N x float].
3677 Padding = llvm::ArrayType::get(Padding, FreeRegs);
3678 FreeRegs = 0;
3679 }
3680
3681 return ABIArgInfo::getIndirect(getContext().getTypeAlign(Ty) / 8,
3682 /*IsByVal=*/ true, /*Realign=*/ false,
3683 Padding);
3684}
3685
3686
3687ABIArgInfo AArch64ABIInfo::classifyGenericType(QualType Ty,
3688 int &FreeIntRegs,
3689 int &FreeVFPRegs) const {
3690 // Can only occurs for return, but harmless otherwise.
3691 if (Ty->isVoidType())
3692 return ABIArgInfo::getIgnore();
3693
3694 // Large vector types should be returned via memory. There's no such concept
3695 // in the ABI, but they'd be over 16 bytes anyway so no matter how they're
3696 // classified they'd go into memory (see B.3).
3697 if (Ty->isVectorType() && getContext().getTypeSize(Ty) > 128) {
3698 if (FreeIntRegs > 0)
3699 --FreeIntRegs;
3700 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3701 }
3702
3703 // All non-aggregate LLVM types have a concrete ABI representation so they can
3704 // be passed directly. After this block we're guaranteed to be in a
3705 // complicated case.
3706 if (!isAggregateTypeForABI(Ty)) {
3707 // Treat an enum type as its underlying type.
3708 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3709 Ty = EnumTy->getDecl()->getIntegerType();
3710
3711 if (Ty->isFloatingType() || Ty->isVectorType())
3712 return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ false);
3713
3714 assert(getContext().getTypeSize(Ty) <= 128 &&
3715 "unexpectedly large scalar type");
3716
3717 int RegsNeeded = getContext().getTypeSize(Ty) > 64 ? 2 : 1;
3718
3719 // If the type may need padding registers to ensure "alignment", we must be
3720 // careful when this is accounted for. Increasing the effective size covers
3721 // all cases.
3722 if (getContext().getTypeAlign(Ty) == 128)
3723 RegsNeeded += FreeIntRegs % 2 != 0;
3724
3725 return tryUseRegs(Ty, FreeIntRegs, RegsNeeded, /*IsInt=*/ true);
3726 }
3727
3728 // Structures with either a non-trivial destructor or a non-trivial
3729 // copy constructor are always indirect.
3730 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) {
3731 if (FreeIntRegs > 0)
3732 --FreeIntRegs;
3733 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3734 }
3735
3736 if (isEmptyRecord(getContext(), Ty, true)) {
3737 if (!getContext().getLangOpts().CPlusPlus) {
3738 // Empty structs outside C++ mode are a GNU extension, so no ABI can
3739 // possibly tell us what to do. It turns out (I believe) that GCC ignores
3740 // the object for parameter-passsing purposes.
3741 return ABIArgInfo::getIgnore();
3742 }
3743
3744 // The combination of C++98 9p5 (sizeof(struct) != 0) and the pseudocode
3745 // description of va_arg in the PCS require that an empty struct does
3746 // actually occupy space for parameter-passing. I'm hoping for a
3747 // clarification giving an explicit paragraph to point to in future.
3748 return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ true,
3749 llvm::Type::getInt8Ty(getVMContext()));
3750 }
3751
3752 // Homogeneous vector aggregates get passed in registers or on the stack.
3753 const Type *Base = 0;
3754 uint64_t NumMembers = 0;
3755 if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)) {
3756 assert(Base && "Base class should be set for homogeneous aggregate");
3757 // Homogeneous aggregates are passed and returned directly.
3758 return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ NumMembers,
3759 /*IsInt=*/ false);
3760 }
3761
3762 uint64_t Size = getContext().getTypeSize(Ty);
3763 if (Size <= 128) {
3764 // Small structs can use the same direct type whether they're in registers
3765 // or on the stack.
3766 llvm::Type *BaseTy;
3767 unsigned NumBases;
3768 int SizeInRegs = (Size + 63) / 64;
3769
3770 if (getContext().getTypeAlign(Ty) == 128) {
3771 BaseTy = llvm::Type::getIntNTy(getVMContext(), 128);
3772 NumBases = 1;
3773
3774 // If the type may need padding registers to ensure "alignment", we must
3775 // be careful when this is accounted for. Increasing the effective size
3776 // covers all cases.
3777 SizeInRegs += FreeIntRegs % 2 != 0;
3778 } else {
3779 BaseTy = llvm::Type::getInt64Ty(getVMContext());
3780 NumBases = SizeInRegs;
3781 }
3782 llvm::Type *DirectTy = llvm::ArrayType::get(BaseTy, NumBases);
3783
3784 return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ SizeInRegs,
3785 /*IsInt=*/ true, DirectTy);
3786 }
3787
3788 // If the aggregate is > 16 bytes, it's passed and returned indirectly. In
3789 // LLVM terms the return uses an "sret" pointer, but that's handled elsewhere.
3790 --FreeIntRegs;
3791 return ABIArgInfo::getIndirect(0, /* byVal = */ false);
3792}
3793
3794llvm::Value *AArch64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3795 CodeGenFunction &CGF) const {
3796 // The AArch64 va_list type and handling is specified in the Procedure Call
3797 // Standard, section B.4:
3798 //
3799 // struct {
3800 // void *__stack;
3801 // void *__gr_top;
3802 // void *__vr_top;
3803 // int __gr_offs;
3804 // int __vr_offs;
3805 // };
3806
3807 assert(!CGF.CGM.getDataLayout().isBigEndian()
3808 && "va_arg not implemented for big-endian AArch64");
3809
3810 int FreeIntRegs = 8, FreeVFPRegs = 8;
3811 Ty = CGF.getContext().getCanonicalType(Ty);
3812 ABIArgInfo AI = classifyGenericType(Ty, FreeIntRegs, FreeVFPRegs);
3813
3814 llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
3815 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
3816 llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
3817 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
3818
3819 llvm::Value *reg_offs_p = 0, *reg_offs = 0;
3820 int reg_top_index;
3821 int RegSize;
3822 if (FreeIntRegs < 8) {
3823 assert(FreeVFPRegs == 8 && "Arguments never split between int & VFP regs");
3824 // 3 is the field number of __gr_offs
3825 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p");
3826 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
3827 reg_top_index = 1; // field number for __gr_top
3828 RegSize = 8 * (8 - FreeIntRegs);
3829 } else {
3830 assert(FreeVFPRegs < 8 && "Argument must go in VFP or int regs");
3831 // 4 is the field number of __vr_offs.
3832 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p");
3833 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
3834 reg_top_index = 2; // field number for __vr_top
3835 RegSize = 16 * (8 - FreeVFPRegs);
3836 }
3837
3838 //=======================================
3839 // Find out where argument was passed
3840 //=======================================
3841
3842 // If reg_offs >= 0 we're already using the stack for this type of
3843 // argument. We don't want to keep updating reg_offs (in case it overflows,
3844 // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
3845 // whatever they get).
3846 llvm::Value *UsingStack = 0;
3847 UsingStack = CGF.Builder.CreateICmpSGE(reg_offs,
3848 llvm::ConstantInt::get(CGF.Int32Ty, 0));
3849
3850 CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
3851
3852 // Otherwise, at least some kind of argument could go in these registers, the
3853 // quesiton is whether this particular type is too big.
3854 CGF.EmitBlock(MaybeRegBlock);
3855
3856 // Integer arguments may need to correct register alignment (for example a
3857 // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
3858 // align __gr_offs to calculate the potential address.
3859 if (FreeIntRegs < 8 && AI.isDirect() && getContext().getTypeAlign(Ty) > 64) {
3860 int Align = getContext().getTypeAlign(Ty) / 8;
3861
3862 reg_offs = CGF.Builder.CreateAdd(reg_offs,
3863 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
3864 "align_regoffs");
3865 reg_offs = CGF.Builder.CreateAnd(reg_offs,
3866 llvm::ConstantInt::get(CGF.Int32Ty, -Align),
3867 "aligned_regoffs");
3868 }
3869
3870 // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
3871 llvm::Value *NewOffset = 0;
3872 NewOffset = CGF.Builder.CreateAdd(reg_offs,
3873 llvm::ConstantInt::get(CGF.Int32Ty, RegSize),
3874 "new_reg_offs");
3875 CGF.Builder.CreateStore(NewOffset, reg_offs_p);
3876
3877 // Now we're in a position to decide whether this argument really was in
3878 // registers or not.
3879 llvm::Value *InRegs = 0;
3880 InRegs = CGF.Builder.CreateICmpSLE(NewOffset,
3881 llvm::ConstantInt::get(CGF.Int32Ty, 0),
3882 "inreg");
3883
3884 CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
3885
3886 //=======================================
3887 // Argument was in registers
3888 //=======================================
3889
3890 // Now we emit the code for if the argument was originally passed in
3891 // registers. First start the appropriate block:
3892 CGF.EmitBlock(InRegBlock);
3893
3894 llvm::Value *reg_top_p = 0, *reg_top = 0;
3895 reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p");
3896 reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
3897 llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs);
3898 llvm::Value *RegAddr = 0;
3899 llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
3900
3901 if (!AI.isDirect()) {
3902 // If it's been passed indirectly (actually a struct), whatever we find from
3903 // stored registers or on the stack will actually be a struct **.
3904 MemTy = llvm::PointerType::getUnqual(MemTy);
3905 }
3906
3907 const Type *Base = 0;
3908 uint64_t NumMembers;
3909 if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)
3910 && NumMembers > 1) {
3911 // Homogeneous aggregates passed in registers will have their elements split
3912 // and stored 16-bytes apart regardless of size (they're notionally in qN,
3913 // qN+1, ...). We reload and store into a temporary local variable
3914 // contiguously.
3915 assert(AI.isDirect() && "Homogeneous aggregates should be passed directly");
3916 llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
3917 llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
3918 llvm::Value *Tmp = CGF.CreateTempAlloca(HFATy);
3919
3920 for (unsigned i = 0; i < NumMembers; ++i) {
3921 llvm::Value *BaseOffset = llvm::ConstantInt::get(CGF.Int32Ty, 16 * i);
3922 llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset);
3923 LoadAddr = CGF.Builder.CreateBitCast(LoadAddr,
3924 llvm::PointerType::getUnqual(BaseTy));
3925 llvm::Value *StoreAddr = CGF.Builder.CreateStructGEP(Tmp, i);
3926
3927 llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
3928 CGF.Builder.CreateStore(Elem, StoreAddr);
3929 }
3930
3931 RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy);
3932 } else {
3933 // Otherwise the object is contiguous in memory
3934 RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy);
3935 }
3936
3937 CGF.EmitBranch(ContBlock);
3938
3939 //=======================================
3940 // Argument was on the stack
3941 //=======================================
3942 CGF.EmitBlock(OnStackBlock);
3943
3944 llvm::Value *stack_p = 0, *OnStackAddr = 0;
3945 stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p");
3946 OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack");
3947
3948 // Again, stack arguments may need realigmnent. In this case both integer and
3949 // floating-point ones might be affected.
3950 if (AI.isDirect() && getContext().getTypeAlign(Ty) > 64) {
3951 int Align = getContext().getTypeAlign(Ty) / 8;
3952
3953 OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty);
3954
3955 OnStackAddr = CGF.Builder.CreateAdd(OnStackAddr,
3956 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
3957 "align_stack");
3958 OnStackAddr = CGF.Builder.CreateAnd(OnStackAddr,
3959 llvm::ConstantInt::get(CGF.Int64Ty, -Align),
3960 "align_stack");
3961
3962 OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy);
3963 }
3964
3965 uint64_t StackSize;
3966 if (AI.isDirect())
3967 StackSize = getContext().getTypeSize(Ty) / 8;
3968 else
3969 StackSize = 8;
3970
3971 // All stack slots are 8 bytes
3972 StackSize = llvm::RoundUpToAlignment(StackSize, 8);
3973
3974 llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize);
3975 llvm::Value *NewStack = CGF.Builder.CreateGEP(OnStackAddr, StackSizeC,
3976 "new_stack");
3977
3978 // Write the new value of __stack for the next call to va_arg
3979 CGF.Builder.CreateStore(NewStack, stack_p);
3980
3981 OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy);
3982
3983 CGF.EmitBranch(ContBlock);
3984
3985 //=======================================
3986 // Tidy up
3987 //=======================================
3988 CGF.EmitBlock(ContBlock);
3989
3990 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr");
3991 ResAddr->addIncoming(RegAddr, InRegBlock);
3992 ResAddr->addIncoming(OnStackAddr, OnStackBlock);
3993
3994 if (AI.isDirect())
3995 return ResAddr;
3996
3997 return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr");
3998}
3999
4000//===----------------------------------------------------------------------===//
Justin Holewinski2c585b92012-05-24 17:43:12 +00004001// NVPTX ABI Implementation
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004002//===----------------------------------------------------------------------===//
4003
4004namespace {
4005
Justin Holewinski2c585b92012-05-24 17:43:12 +00004006class NVPTXABIInfo : public ABIInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004007public:
Justin Holewinskidca8f332013-03-30 14:38:24 +00004008 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004009
4010 ABIArgInfo classifyReturnType(QualType RetTy) const;
4011 ABIArgInfo classifyArgumentType(QualType Ty) const;
4012
4013 virtual void computeInfo(CGFunctionInfo &FI) const;
4014 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4015 CodeGenFunction &CFG) const;
4016};
4017
Justin Holewinski2c585b92012-05-24 17:43:12 +00004018class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004019public:
Justin Holewinski2c585b92012-05-24 17:43:12 +00004020 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
4021 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
Justin Holewinski818eafb2011-10-05 17:58:44 +00004022
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004023 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4024 CodeGen::CodeGenModule &M) const;
Justin Holewinskidca8f332013-03-30 14:38:24 +00004025private:
4026 static void addKernelMetadata(llvm::Function *F);
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004027};
4028
Justin Holewinski2c585b92012-05-24 17:43:12 +00004029ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004030 if (RetTy->isVoidType())
4031 return ABIArgInfo::getIgnore();
4032 if (isAggregateTypeForABI(RetTy))
4033 return ABIArgInfo::getIndirect(0);
4034 return ABIArgInfo::getDirect();
4035}
4036
Justin Holewinski2c585b92012-05-24 17:43:12 +00004037ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004038 if (isAggregateTypeForABI(Ty))
4039 return ABIArgInfo::getIndirect(0);
4040
4041 return ABIArgInfo::getDirect();
4042}
4043
Justin Holewinski2c585b92012-05-24 17:43:12 +00004044void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004045 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4046 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4047 it != ie; ++it)
4048 it->info = classifyArgumentType(it->type);
4049
4050 // Always honor user-specified calling convention.
4051 if (FI.getCallingConvention() != llvm::CallingConv::C)
4052 return;
4053
John McCallbd7370a2013-02-28 19:01:20 +00004054 FI.setEffectiveCallingConvention(getRuntimeCC());
4055}
4056
Justin Holewinski2c585b92012-05-24 17:43:12 +00004057llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4058 CodeGenFunction &CFG) const {
4059 llvm_unreachable("NVPTX does not support varargs");
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004060}
4061
Justin Holewinski2c585b92012-05-24 17:43:12 +00004062void NVPTXTargetCodeGenInfo::
4063SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4064 CodeGen::CodeGenModule &M) const{
Justin Holewinski818eafb2011-10-05 17:58:44 +00004065 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4066 if (!FD) return;
4067
4068 llvm::Function *F = cast<llvm::Function>(GV);
4069
4070 // Perform special handling in OpenCL mode
David Blaikie4e4d0842012-03-11 07:00:24 +00004071 if (M.getLangOpts().OpenCL) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00004072 // Use OpenCL function attributes to check for kernel functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00004073 // By default, all functions are device functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00004074 if (FD->hasAttr<OpenCLKernelAttr>()) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00004075 // OpenCL __kernel functions get kernel metadata
4076 addKernelMetadata(F);
Justin Holewinski818eafb2011-10-05 17:58:44 +00004077 // And kernel functions are not subject to inlining
Bill Wendling72390b32012-12-20 19:27:06 +00004078 F->addFnAttr(llvm::Attribute::NoInline);
Justin Holewinski818eafb2011-10-05 17:58:44 +00004079 }
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004080 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00004081
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004082 // Perform special handling in CUDA mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00004083 if (M.getLangOpts().CUDA) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00004084 // CUDA __global__ functions get a kernel metadata entry. Since
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004085 // __global__ functions cannot be called from the device, we do not
4086 // need to set the noinline attribute.
4087 if (FD->getAttr<CUDAGlobalAttr>())
Justin Holewinskidca8f332013-03-30 14:38:24 +00004088 addKernelMetadata(F);
Justin Holewinski818eafb2011-10-05 17:58:44 +00004089 }
4090}
4091
Justin Holewinskidca8f332013-03-30 14:38:24 +00004092void NVPTXTargetCodeGenInfo::addKernelMetadata(llvm::Function *F) {
4093 llvm::Module *M = F->getParent();
4094 llvm::LLVMContext &Ctx = M->getContext();
4095
4096 // Get "nvvm.annotations" metadata node
4097 llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
4098
4099 // Create !{<func-ref>, metadata !"kernel", i32 1} node
4100 llvm::SmallVector<llvm::Value *, 3> MDVals;
4101 MDVals.push_back(F);
4102 MDVals.push_back(llvm::MDString::get(Ctx, "kernel"));
4103 MDVals.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), 1));
4104
4105 // Append metadata to nvvm.annotations
4106 MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
4107}
4108
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004109}
4110
4111//===----------------------------------------------------------------------===//
Wesley Peck276fdf42010-12-19 19:57:51 +00004112// MBlaze ABI Implementation
4113//===----------------------------------------------------------------------===//
4114
4115namespace {
4116
4117class MBlazeABIInfo : public ABIInfo {
4118public:
4119 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
4120
4121 bool isPromotableIntegerType(QualType Ty) const;
4122
4123 ABIArgInfo classifyReturnType(QualType RetTy) const;
4124 ABIArgInfo classifyArgumentType(QualType RetTy) const;
4125
4126 virtual void computeInfo(CGFunctionInfo &FI) const {
4127 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4128 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4129 it != ie; ++it)
4130 it->info = classifyArgumentType(it->type);
4131 }
4132
4133 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4134 CodeGenFunction &CGF) const;
4135};
4136
4137class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
4138public:
4139 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
4140 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
4141 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4142 CodeGen::CodeGenModule &M) const;
4143};
4144
4145}
4146
4147bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
4148 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
4149 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4150 switch (BT->getKind()) {
4151 case BuiltinType::Bool:
4152 case BuiltinType::Char_S:
4153 case BuiltinType::Char_U:
4154 case BuiltinType::SChar:
4155 case BuiltinType::UChar:
4156 case BuiltinType::Short:
4157 case BuiltinType::UShort:
4158 return true;
4159 default:
4160 return false;
4161 }
4162 return false;
4163}
4164
4165llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4166 CodeGenFunction &CGF) const {
4167 // FIXME: Implement
4168 return 0;
4169}
4170
4171
4172ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
4173 if (RetTy->isVoidType())
4174 return ABIArgInfo::getIgnore();
4175 if (isAggregateTypeForABI(RetTy))
4176 return ABIArgInfo::getIndirect(0);
4177
4178 return (isPromotableIntegerType(RetTy) ?
4179 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4180}
4181
4182ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
4183 if (isAggregateTypeForABI(Ty))
4184 return ABIArgInfo::getIndirect(0);
4185
4186 return (isPromotableIntegerType(Ty) ?
4187 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4188}
4189
4190void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4191 llvm::GlobalValue *GV,
4192 CodeGen::CodeGenModule &M)
4193 const {
4194 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4195 if (!FD) return;
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00004196
Wesley Peck276fdf42010-12-19 19:57:51 +00004197 llvm::CallingConv::ID CC = llvm::CallingConv::C;
4198 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
4199 CC = llvm::CallingConv::MBLAZE_INTR;
4200 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
4201 CC = llvm::CallingConv::MBLAZE_SVOL;
4202
4203 if (CC != llvm::CallingConv::C) {
4204 // Handle 'interrupt_handler' attribute:
4205 llvm::Function *F = cast<llvm::Function>(GV);
4206
4207 // Step 1: Set ISR calling convention.
4208 F->setCallingConv(CC);
4209
4210 // Step 2: Add attributes goodness.
Bill Wendling72390b32012-12-20 19:27:06 +00004211 F->addFnAttr(llvm::Attribute::NoInline);
Wesley Peck276fdf42010-12-19 19:57:51 +00004212 }
4213
4214 // Step 3: Emit _interrupt_handler alias.
4215 if (CC == llvm::CallingConv::MBLAZE_INTR)
4216 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
4217 "_interrupt_handler", GV, &M.getModule());
4218}
4219
4220
4221//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004222// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004223//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004224
4225namespace {
4226
4227class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
4228public:
Chris Lattnerea044322010-07-29 02:01:43 +00004229 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
4230 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004231 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4232 CodeGen::CodeGenModule &M) const;
4233};
4234
4235}
4236
4237void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4238 llvm::GlobalValue *GV,
4239 CodeGen::CodeGenModule &M) const {
4240 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4241 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
4242 // Handle 'interrupt' attribute:
4243 llvm::Function *F = cast<llvm::Function>(GV);
4244
4245 // Step 1: Set ISR calling convention.
4246 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
4247
4248 // Step 2: Add attributes goodness.
Bill Wendling72390b32012-12-20 19:27:06 +00004249 F->addFnAttr(llvm::Attribute::NoInline);
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004250
4251 // Step 3: Emit ISR vector alias.
Anton Korobeynikovf419a852012-11-26 18:59:10 +00004252 unsigned Num = attr->getNumber() / 2;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004253 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Anton Korobeynikovf419a852012-11-26 18:59:10 +00004254 "__isr_" + Twine(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004255 GV, &M.getModule());
4256 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004257 }
4258}
4259
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004260//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00004261// MIPS ABI Implementation. This works for both little-endian and
4262// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004263//===----------------------------------------------------------------------===//
4264
John McCallaeeb7012010-05-27 06:19:26 +00004265namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00004266class MipsABIInfo : public ABIInfo {
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004267 bool IsO32;
Akira Hatanakac359f202012-07-03 19:24:06 +00004268 unsigned MinABIStackAlignInBytes, StackAlignInBytes;
4269 void CoerceToIntArgs(uint64_t TySize,
4270 SmallVector<llvm::Type*, 8> &ArgList) const;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004271 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004272 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004273 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004274public:
Akira Hatanakab551dd32011-11-03 00:05:50 +00004275 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
Akira Hatanakac359f202012-07-03 19:24:06 +00004276 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
4277 StackAlignInBytes(IsO32 ? 8 : 16) {}
Akira Hatanaka619e8872011-06-02 00:09:17 +00004278
4279 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004280 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004281 virtual void computeInfo(CGFunctionInfo &FI) const;
4282 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4283 CodeGenFunction &CGF) const;
4284};
4285
John McCallaeeb7012010-05-27 06:19:26 +00004286class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanakae624fa02011-09-20 18:23:28 +00004287 unsigned SizeOfUnwindException;
John McCallaeeb7012010-05-27 06:19:26 +00004288public:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004289 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
4290 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
4291 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCallaeeb7012010-05-27 06:19:26 +00004292
4293 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
4294 return 29;
4295 }
4296
Reed Kotler7dfd1822013-01-16 17:10:28 +00004297 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4298 CodeGen::CodeGenModule &CGM) const {
Reed Kotlerad4b8b42013-03-13 20:40:30 +00004299 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4300 if (!FD) return;
Rafael Espindolad8e6d6d2013-03-19 14:32:23 +00004301 llvm::Function *Fn = cast<llvm::Function>(GV);
Reed Kotlerad4b8b42013-03-13 20:40:30 +00004302 if (FD->hasAttr<Mips16Attr>()) {
4303 Fn->addFnAttr("mips16");
4304 }
4305 else if (FD->hasAttr<NoMips16Attr>()) {
4306 Fn->addFnAttr("nomips16");
4307 }
Reed Kotler7dfd1822013-01-16 17:10:28 +00004308 }
Reed Kotlerad4b8b42013-03-13 20:40:30 +00004309
John McCallaeeb7012010-05-27 06:19:26 +00004310 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00004311 llvm::Value *Address) const;
John McCall49e34be2011-08-30 01:42:09 +00004312
4313 unsigned getSizeOfUnwindException() const {
Akira Hatanakae624fa02011-09-20 18:23:28 +00004314 return SizeOfUnwindException;
John McCall49e34be2011-08-30 01:42:09 +00004315 }
John McCallaeeb7012010-05-27 06:19:26 +00004316};
4317}
4318
Akira Hatanakac359f202012-07-03 19:24:06 +00004319void MipsABIInfo::CoerceToIntArgs(uint64_t TySize,
4320 SmallVector<llvm::Type*, 8> &ArgList) const {
4321 llvm::IntegerType *IntTy =
4322 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004323
4324 // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
4325 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
4326 ArgList.push_back(IntTy);
4327
4328 // If necessary, add one more integer type to ArgList.
4329 unsigned R = TySize % (MinABIStackAlignInBytes * 8);
4330
4331 if (R)
4332 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004333}
4334
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004335// In N32/64, an aligned double precision floating point field is passed in
4336// a register.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004337llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
Akira Hatanakac359f202012-07-03 19:24:06 +00004338 SmallVector<llvm::Type*, 8> ArgList, IntArgList;
4339
4340 if (IsO32) {
4341 CoerceToIntArgs(TySize, ArgList);
4342 return llvm::StructType::get(getVMContext(), ArgList);
4343 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004344
Akira Hatanaka2afd23d2012-01-12 00:52:17 +00004345 if (Ty->isComplexType())
4346 return CGT.ConvertType(Ty);
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00004347
Akira Hatanakaa34e9212012-02-09 19:54:16 +00004348 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004349
Akira Hatanakac359f202012-07-03 19:24:06 +00004350 // Unions/vectors are passed in integer registers.
4351 if (!RT || !RT->isStructureOrClassType()) {
4352 CoerceToIntArgs(TySize, ArgList);
4353 return llvm::StructType::get(getVMContext(), ArgList);
4354 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004355
4356 const RecordDecl *RD = RT->getDecl();
4357 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004358 assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004359
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004360 uint64_t LastOffset = 0;
4361 unsigned idx = 0;
4362 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
4363
Akira Hatanakaa34e9212012-02-09 19:54:16 +00004364 // Iterate over fields in the struct/class and check if there are any aligned
4365 // double fields.
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004366 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
4367 i != e; ++i, ++idx) {
David Blaikie262bc182012-04-30 02:36:29 +00004368 const QualType Ty = i->getType();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004369 const BuiltinType *BT = Ty->getAs<BuiltinType>();
4370
4371 if (!BT || BT->getKind() != BuiltinType::Double)
4372 continue;
4373
4374 uint64_t Offset = Layout.getFieldOffset(idx);
4375 if (Offset % 64) // Ignore doubles that are not aligned.
4376 continue;
4377
4378 // Add ((Offset - LastOffset) / 64) args of type i64.
4379 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
4380 ArgList.push_back(I64);
4381
4382 // Add double type.
4383 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
4384 LastOffset = Offset + 64;
4385 }
4386
Akira Hatanakac359f202012-07-03 19:24:06 +00004387 CoerceToIntArgs(TySize - LastOffset, IntArgList);
4388 ArgList.append(IntArgList.begin(), IntArgList.end());
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004389
4390 return llvm::StructType::get(getVMContext(), ArgList);
4391}
4392
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004393llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004394 assert((Offset % MinABIStackAlignInBytes) == 0);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004395
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004396 if ((Align - 1) & Offset)
4397 return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
4398
4399 return 0;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004400}
Akira Hatanaka9659d592012-01-10 22:44:52 +00004401
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004402ABIArgInfo
4403MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004404 uint64_t OrigOffset = Offset;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004405 uint64_t TySize = getContext().getTypeSize(Ty);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004406 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004407
Akira Hatanakac359f202012-07-03 19:24:06 +00004408 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
4409 (uint64_t)StackAlignInBytes);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004410 Offset = llvm::RoundUpToAlignment(Offset, Align);
4411 Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004412
Akira Hatanakac359f202012-07-03 19:24:06 +00004413 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
Akira Hatanaka619e8872011-06-02 00:09:17 +00004414 // Ignore empty aggregates.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004415 if (TySize == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00004416 return ABIArgInfo::getIgnore();
4417
Akira Hatanaka511949b2011-08-01 18:09:58 +00004418 // Records with non trivial destructors/constructors should not be passed
4419 // by value.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004420 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004421 Offset = OrigOffset + MinABIStackAlignInBytes;
Akira Hatanaka511949b2011-08-01 18:09:58 +00004422 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004423 }
Akira Hatanaka511949b2011-08-01 18:09:58 +00004424
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004425 // If we have reached here, aggregates are passed directly by coercing to
4426 // another structure type. Padding is inserted if the offset of the
4427 // aggregate is unaligned.
4428 return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
4429 getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00004430 }
4431
4432 // Treat an enum type as its underlying type.
4433 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4434 Ty = EnumTy->getDecl()->getIntegerType();
4435
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004436 if (Ty->isPromotableIntegerType())
4437 return ABIArgInfo::getExtend();
4438
Akira Hatanaka4055cfc2013-01-24 21:47:33 +00004439 return ABIArgInfo::getDirect(0, 0,
4440 IsO32 ? 0 : getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00004441}
4442
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004443llvm::Type*
4444MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakada54ff32012-02-09 18:49:26 +00004445 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakac359f202012-07-03 19:24:06 +00004446 SmallVector<llvm::Type*, 8> RTList;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004447
Akira Hatanakada54ff32012-02-09 18:49:26 +00004448 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004449 const RecordDecl *RD = RT->getDecl();
Akira Hatanakada54ff32012-02-09 18:49:26 +00004450 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
4451 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004452
Akira Hatanakada54ff32012-02-09 18:49:26 +00004453 // N32/64 returns struct/classes in floating point registers if the
4454 // following conditions are met:
4455 // 1. The size of the struct/class is no larger than 128-bit.
4456 // 2. The struct/class has one or two fields all of which are floating
4457 // point types.
4458 // 3. The offset of the first field is zero (this follows what gcc does).
4459 //
4460 // Any other composite results are returned in integer registers.
4461 //
4462 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
4463 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
4464 for (; b != e; ++b) {
David Blaikie262bc182012-04-30 02:36:29 +00004465 const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004466
Akira Hatanakada54ff32012-02-09 18:49:26 +00004467 if (!BT || !BT->isFloatingPoint())
4468 break;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004469
David Blaikie262bc182012-04-30 02:36:29 +00004470 RTList.push_back(CGT.ConvertType(b->getType()));
Akira Hatanakada54ff32012-02-09 18:49:26 +00004471 }
4472
4473 if (b == e)
4474 return llvm::StructType::get(getVMContext(), RTList,
4475 RD->hasAttr<PackedAttr>());
4476
4477 RTList.clear();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004478 }
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004479 }
4480
Akira Hatanakac359f202012-07-03 19:24:06 +00004481 CoerceToIntArgs(Size, RTList);
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004482 return llvm::StructType::get(getVMContext(), RTList);
4483}
4484
Akira Hatanaka619e8872011-06-02 00:09:17 +00004485ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanakaa8536c02012-01-23 23:18:57 +00004486 uint64_t Size = getContext().getTypeSize(RetTy);
4487
4488 if (RetTy->isVoidType() || Size == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00004489 return ABIArgInfo::getIgnore();
4490
Akira Hatanaka8aeb1472012-05-11 21:01:17 +00004491 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004492 if (Size <= 128) {
4493 if (RetTy->isAnyComplexType())
4494 return ABIArgInfo::getDirect();
4495
Akira Hatanakac359f202012-07-03 19:24:06 +00004496 // O32 returns integer vectors in registers.
4497 if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())
4498 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
4499
Akira Hatanaka526cdfb2012-02-08 01:31:22 +00004500 if (!IsO32 && !isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004501 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
4502 }
Akira Hatanaka619e8872011-06-02 00:09:17 +00004503
4504 return ABIArgInfo::getIndirect(0);
4505 }
4506
4507 // Treat an enum type as its underlying type.
4508 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4509 RetTy = EnumTy->getDecl()->getIntegerType();
4510
4511 return (RetTy->isPromotableIntegerType() ?
4512 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4513}
4514
4515void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanakacc662542012-01-12 01:10:09 +00004516 ABIArgInfo &RetInfo = FI.getReturnInfo();
4517 RetInfo = classifyReturnType(FI.getReturnType());
4518
4519 // Check if a pointer to an aggregate is passed as a hidden argument.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004520 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
Akira Hatanakacc662542012-01-12 01:10:09 +00004521
Akira Hatanaka619e8872011-06-02 00:09:17 +00004522 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4523 it != ie; ++it)
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004524 it->info = classifyArgumentType(it->type, Offset);
Akira Hatanaka619e8872011-06-02 00:09:17 +00004525}
4526
4527llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4528 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00004529 llvm::Type *BP = CGF.Int8PtrTy;
4530 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004531
4532 CGBuilderTy &Builder = CGF.Builder;
4533 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
4534 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004535 int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004536 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
4537 llvm::Value *AddrTyped;
John McCall64aa4b32013-04-16 22:48:15 +00004538 unsigned PtrWidth = getTarget().getPointerWidth(0);
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004539 llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004540
4541 if (TypeAlign > MinABIStackAlignInBytes) {
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004542 llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
4543 llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
4544 llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
4545 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004546 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
4547 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
4548 }
4549 else
4550 AddrTyped = Builder.CreateBitCast(Addr, PTy);
4551
4552 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004553 TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004554 uint64_t Offset =
4555 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
4556 llvm::Value *NextAddr =
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004557 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004558 "ap.next");
4559 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
4560
4561 return AddrTyped;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004562}
4563
John McCallaeeb7012010-05-27 06:19:26 +00004564bool
4565MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4566 llvm::Value *Address) const {
4567 // This information comes from gcc's implementation, which seems to
4568 // as canonical as it gets.
4569
John McCallaeeb7012010-05-27 06:19:26 +00004570 // Everything on MIPS is 4 bytes. Double-precision FP registers
4571 // are aliased to pairs of single-precision FP registers.
Chris Lattner8b418682012-02-07 00:39:47 +00004572 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCallaeeb7012010-05-27 06:19:26 +00004573
4574 // 0-31 are the general purpose registers, $0 - $31.
4575 // 32-63 are the floating-point registers, $f0 - $f31.
4576 // 64 and 65 are the multiply/divide registers, $hi and $lo.
4577 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattner8b418682012-02-07 00:39:47 +00004578 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCallaeeb7012010-05-27 06:19:26 +00004579
4580 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
4581 // They are one bit wide and ignored here.
4582
4583 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
4584 // (coprocessor 1 is the FP unit)
4585 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
4586 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
4587 // 176-181 are the DSP accumulator registers.
Chris Lattner8b418682012-02-07 00:39:47 +00004588 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCallaeeb7012010-05-27 06:19:26 +00004589 return false;
4590}
4591
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004592//===----------------------------------------------------------------------===//
4593// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
4594// Currently subclassed only to implement custom OpenCL C function attribute
4595// handling.
4596//===----------------------------------------------------------------------===//
4597
4598namespace {
4599
4600class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
4601public:
4602 TCETargetCodeGenInfo(CodeGenTypes &CGT)
4603 : DefaultTargetCodeGenInfo(CGT) {}
4604
4605 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4606 CodeGen::CodeGenModule &M) const;
4607};
4608
4609void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4610 llvm::GlobalValue *GV,
4611 CodeGen::CodeGenModule &M) const {
4612 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4613 if (!FD) return;
4614
4615 llvm::Function *F = cast<llvm::Function>(GV);
4616
David Blaikie4e4d0842012-03-11 07:00:24 +00004617 if (M.getLangOpts().OpenCL) {
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004618 if (FD->hasAttr<OpenCLKernelAttr>()) {
4619 // OpenCL C Kernel functions are not subject to inlining
Bill Wendling72390b32012-12-20 19:27:06 +00004620 F->addFnAttr(llvm::Attribute::NoInline);
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004621
4622 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
4623
4624 // Convert the reqd_work_group_size() attributes to metadata.
4625 llvm::LLVMContext &Context = F->getContext();
4626 llvm::NamedMDNode *OpenCLMetadata =
4627 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
4628
4629 SmallVector<llvm::Value*, 5> Operands;
4630 Operands.push_back(F);
4631
Chris Lattner8b418682012-02-07 00:39:47 +00004632 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4633 llvm::APInt(32,
4634 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
4635 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4636 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004637 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
Chris Lattner8b418682012-02-07 00:39:47 +00004638 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4639 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004640 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
4641
4642 // Add a boolean constant operand for "required" (true) or "hint" (false)
4643 // for implementing the work_group_size_hint attr later. Currently
4644 // always true as the hint is not yet implemented.
Chris Lattner8b418682012-02-07 00:39:47 +00004645 Operands.push_back(llvm::ConstantInt::getTrue(Context));
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004646 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
4647 }
4648 }
4649 }
4650}
4651
4652}
John McCallaeeb7012010-05-27 06:19:26 +00004653
Tony Linthicum96319392011-12-12 21:14:55 +00004654//===----------------------------------------------------------------------===//
4655// Hexagon ABI Implementation
4656//===----------------------------------------------------------------------===//
4657
4658namespace {
4659
4660class HexagonABIInfo : public ABIInfo {
4661
4662
4663public:
4664 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
4665
4666private:
4667
4668 ABIArgInfo classifyReturnType(QualType RetTy) const;
4669 ABIArgInfo classifyArgumentType(QualType RetTy) const;
4670
4671 virtual void computeInfo(CGFunctionInfo &FI) const;
4672
4673 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4674 CodeGenFunction &CGF) const;
4675};
4676
4677class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
4678public:
4679 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
4680 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
4681
4682 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
4683 return 29;
4684 }
4685};
4686
4687}
4688
4689void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
4690 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4691 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4692 it != ie; ++it)
4693 it->info = classifyArgumentType(it->type);
4694}
4695
4696ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
4697 if (!isAggregateTypeForABI(Ty)) {
4698 // Treat an enum type as its underlying type.
4699 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4700 Ty = EnumTy->getDecl()->getIntegerType();
4701
4702 return (Ty->isPromotableIntegerType() ?
4703 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4704 }
4705
4706 // Ignore empty records.
4707 if (isEmptyRecord(getContext(), Ty, true))
4708 return ABIArgInfo::getIgnore();
4709
4710 // Structures with either a non-trivial destructor or a non-trivial
4711 // copy constructor are always indirect.
4712 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
4713 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
4714
4715 uint64_t Size = getContext().getTypeSize(Ty);
4716 if (Size > 64)
4717 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
4718 // Pass in the smallest viable integer type.
4719 else if (Size > 32)
4720 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
4721 else if (Size > 16)
4722 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
4723 else if (Size > 8)
4724 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
4725 else
4726 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
4727}
4728
4729ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
4730 if (RetTy->isVoidType())
4731 return ABIArgInfo::getIgnore();
4732
4733 // Large vector types should be returned via memory.
4734 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
4735 return ABIArgInfo::getIndirect(0);
4736
4737 if (!isAggregateTypeForABI(RetTy)) {
4738 // Treat an enum type as its underlying type.
4739 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4740 RetTy = EnumTy->getDecl()->getIntegerType();
4741
4742 return (RetTy->isPromotableIntegerType() ?
4743 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4744 }
4745
4746 // Structures with either a non-trivial destructor or a non-trivial
4747 // copy constructor are always indirect.
4748 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
4749 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
4750
4751 if (isEmptyRecord(getContext(), RetTy, true))
4752 return ABIArgInfo::getIgnore();
4753
4754 // Aggregates <= 8 bytes are returned in r0; other aggregates
4755 // are returned indirectly.
4756 uint64_t Size = getContext().getTypeSize(RetTy);
4757 if (Size <= 64) {
4758 // Return in the smallest viable integer type.
4759 if (Size <= 8)
4760 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
4761 if (Size <= 16)
4762 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
4763 if (Size <= 32)
4764 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
4765 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
4766 }
4767
4768 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
4769}
4770
4771llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner8b418682012-02-07 00:39:47 +00004772 CodeGenFunction &CGF) const {
Tony Linthicum96319392011-12-12 21:14:55 +00004773 // FIXME: Need to handle alignment
Chris Lattner8b418682012-02-07 00:39:47 +00004774 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Tony Linthicum96319392011-12-12 21:14:55 +00004775
4776 CGBuilderTy &Builder = CGF.Builder;
4777 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
4778 "ap");
4779 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
4780 llvm::Type *PTy =
4781 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
4782 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
4783
4784 uint64_t Offset =
4785 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
4786 llvm::Value *NextAddr =
4787 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
4788 "ap.next");
4789 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
4790
4791 return AddrTyped;
4792}
4793
4794
Chris Lattnerea044322010-07-29 02:01:43 +00004795const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004796 if (TheTargetCodeGenInfo)
4797 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004798
John McCall64aa4b32013-04-16 22:48:15 +00004799 const llvm::Triple &Triple = getTarget().getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00004800 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004801 default:
Chris Lattnerea044322010-07-29 02:01:43 +00004802 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004803
Derek Schuff9ed63f82012-09-06 17:37:28 +00004804 case llvm::Triple::le32:
4805 return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
John McCallaeeb7012010-05-27 06:19:26 +00004806 case llvm::Triple::mips:
4807 case llvm::Triple::mipsel:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004808 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCallaeeb7012010-05-27 06:19:26 +00004809
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00004810 case llvm::Triple::mips64:
4811 case llvm::Triple::mips64el:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004812 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00004813
Tim Northoverc264e162013-01-31 12:13:10 +00004814 case llvm::Triple::aarch64:
4815 return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types));
4816
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004817 case llvm::Triple::arm:
4818 case llvm::Triple::thumb:
Sandeep Patel34c1af82011-04-05 00:23:47 +00004819 {
4820 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
John McCall64aa4b32013-04-16 22:48:15 +00004821 if (strcmp(getTarget().getABI(), "apcs-gnu") == 0)
Sandeep Patel34c1af82011-04-05 00:23:47 +00004822 Kind = ARMABIInfo::APCS;
David Tweedb16abb12012-10-25 13:33:01 +00004823 else if (CodeGenOpts.FloatABI == "hard" ||
John McCall64aa4b32013-04-16 22:48:15 +00004824 (CodeGenOpts.FloatABI != "soft" &&
4825 Triple.getEnvironment() == llvm::Triple::GNUEABIHF))
Sandeep Patel34c1af82011-04-05 00:23:47 +00004826 Kind = ARMABIInfo::AAPCS_VFP;
4827
Derek Schuff263366f2012-10-16 22:30:41 +00004828 switch (Triple.getOS()) {
Eli Bendersky441d9f72012-12-04 18:38:10 +00004829 case llvm::Triple::NaCl:
Derek Schuff263366f2012-10-16 22:30:41 +00004830 return *(TheTargetCodeGenInfo =
4831 new NaClARMTargetCodeGenInfo(Types, Kind));
4832 default:
4833 return *(TheTargetCodeGenInfo =
4834 new ARMTargetCodeGenInfo(Types, Kind));
4835 }
Sandeep Patel34c1af82011-04-05 00:23:47 +00004836 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004837
John McCallec853ba2010-03-11 00:10:12 +00004838 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00004839 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
Roman Divacky0fbc4b92012-05-09 18:22:46 +00004840 case llvm::Triple::ppc64:
Bill Schmidt2fc107f2012-10-03 19:18:57 +00004841 if (Triple.isOSBinFormatELF())
4842 return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types));
4843 else
4844 return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00004845
Peter Collingbourneedb66f32012-05-20 23:28:41 +00004846 case llvm::Triple::nvptx:
4847 case llvm::Triple::nvptx64:
Justin Holewinski2c585b92012-05-24 17:43:12 +00004848 return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types));
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004849
Wesley Peck276fdf42010-12-19 19:57:51 +00004850 case llvm::Triple::mblaze:
4851 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
4852
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004853 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00004854 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004855
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004856 case llvm::Triple::tce:
4857 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
4858
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004859 case llvm::Triple::x86: {
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00004860 if (Triple.isOSDarwin())
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004861 return *(TheTargetCodeGenInfo =
Chad Rosier1f1df1f2013-03-25 21:00:27 +00004862 new X86_32TargetCodeGenInfo(Types, true, true, false,
Rafael Espindolab48280b2012-07-31 02:44:24 +00004863 CodeGenOpts.NumRegisterParameters));
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00004864
4865 switch (Triple.getOS()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004866 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004867 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00004868 case llvm::Triple::AuroraUX:
4869 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00004870 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004871 case llvm::Triple::OpenBSD:
Eli Friedman42f74f22012-08-08 23:57:20 +00004872 case llvm::Triple::Bitrig:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004873 return *(TheTargetCodeGenInfo =
Chad Rosier1f1df1f2013-03-25 21:00:27 +00004874 new X86_32TargetCodeGenInfo(Types, false, true, false,
Rafael Espindolab48280b2012-07-31 02:44:24 +00004875 CodeGenOpts.NumRegisterParameters));
Eli Friedman55fc7e22012-01-25 22:46:34 +00004876
4877 case llvm::Triple::Win32:
4878 return *(TheTargetCodeGenInfo =
Chad Rosier1f1df1f2013-03-25 21:00:27 +00004879 new X86_32TargetCodeGenInfo(Types, false, true, true,
Rafael Espindolab48280b2012-07-31 02:44:24 +00004880 CodeGenOpts.NumRegisterParameters));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004881
4882 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004883 return *(TheTargetCodeGenInfo =
Chad Rosier1f1df1f2013-03-25 21:00:27 +00004884 new X86_32TargetCodeGenInfo(Types, false, false, false,
Rafael Espindolab48280b2012-07-31 02:44:24 +00004885 CodeGenOpts.NumRegisterParameters));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004886 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004887 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004888
Eli Friedmanee1ad992011-12-02 00:11:43 +00004889 case llvm::Triple::x86_64: {
John McCall64aa4b32013-04-16 22:48:15 +00004890 bool HasAVX = strcmp(getTarget().getABI(), "avx") == 0;
Eli Friedmanee1ad992011-12-02 00:11:43 +00004891
Chris Lattnerf13721d2010-08-31 16:44:54 +00004892 switch (Triple.getOS()) {
4893 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00004894 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00004895 case llvm::Triple::Cygwin:
4896 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
Eli Bendersky441d9f72012-12-04 18:38:10 +00004897 case llvm::Triple::NaCl:
John McCall64aa4b32013-04-16 22:48:15 +00004898 return *(TheTargetCodeGenInfo = new NaClX86_64TargetCodeGenInfo(Types,
4899 HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00004900 default:
Eli Friedmanee1ad992011-12-02 00:11:43 +00004901 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
4902 HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00004903 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004904 }
Tony Linthicum96319392011-12-12 21:14:55 +00004905 case llvm::Triple::hexagon:
4906 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
Eli Friedmanee1ad992011-12-02 00:11:43 +00004907 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004908}