blob: 7cc63b7db15d4b17de6a67352d04e511d3409dd1 [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
58
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000059void ABIArgInfo::dump() const {
Chris Lattner5f9e2722011-07-23 10:55:15 +000060 raw_ostream &OS = llvm::errs();
Daniel Dunbar28df7a52009-12-03 09:13:49 +000061 OS << "(ABIArgInfo Kind=";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000062 switch (TheKind) {
63 case Direct:
Chris Lattner800588f2010-07-29 06:26:06 +000064 OS << "Direct Type=";
Chris Lattner2acc6e32011-07-18 04:24:23 +000065 if (llvm::Type *Ty = getCoerceToType())
Chris Lattner800588f2010-07-29 06:26:06 +000066 Ty->print(OS);
67 else
68 OS << "null";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000069 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000070 case Extend:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000071 OS << "Extend";
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000072 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000073 case Ignore:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000074 OS << "Ignore";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000075 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000076 case Indirect:
Daniel Dunbardc6d5742010-04-21 19:10:51 +000077 OS << "Indirect Align=" << getIndirectAlign()
Joerg Sonnenbergere9b5d772011-07-15 18:23:44 +000078 << " ByVal=" << getIndirectByVal()
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +000079 << " Realign=" << getIndirectRealign();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000080 break;
81 case Expand:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000082 OS << "Expand";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000083 break;
84 }
Daniel Dunbar28df7a52009-12-03 09:13:49 +000085 OS << ")\n";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000086}
87
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000088TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
89
John McCall49e34be2011-08-30 01:42:09 +000090// If someone can figure out a general rule for this, that would be great.
91// It's probably just doomed to be platform-dependent, though.
92unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
93 // Verified for:
94 // x86-64 FreeBSD, Linux, Darwin
95 // x86-32 FreeBSD, Linux, Darwin
96 // PowerPC Linux, Darwin
97 // ARM Darwin (*not* EABI)
Tim Northoverc264e162013-01-31 12:13:10 +000098 // AArch64 Linux
John McCall49e34be2011-08-30 01:42:09 +000099 return 32;
100}
101
John McCallde5d3c72012-02-17 03:33:10 +0000102bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
103 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +0000104 // The following conventions are known to require this to be false:
105 // x86_stdcall
106 // MIPS
107 // For everything else, we just prefer false unless we opt out.
108 return false;
109}
110
Daniel Dunbar98303b92009-09-13 08:03:58 +0000111static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000112
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000113/// isEmptyField - Return true iff a the field is "empty", that is it
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000114/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +0000115static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
116 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000117 if (FD->isUnnamedBitfield())
118 return true;
119
120 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000121
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000122 // Constant arrays of empty records count as empty, strip them off.
123 // Constant arrays of zero length always count as empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000124 if (AllowArrays)
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000125 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
126 if (AT->getSize() == 0)
127 return true;
Daniel Dunbar98303b92009-09-13 08:03:58 +0000128 FT = AT->getElementType();
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000129 }
Daniel Dunbar98303b92009-09-13 08:03:58 +0000130
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000131 const RecordType *RT = FT->getAs<RecordType>();
132 if (!RT)
133 return false;
134
135 // C++ record fields are never empty, at least in the Itanium ABI.
136 //
137 // FIXME: We should use a predicate for whether this behavior is true in the
138 // current ABI.
139 if (isa<CXXRecordDecl>(RT->getDecl()))
140 return false;
141
Daniel Dunbar98303b92009-09-13 08:03:58 +0000142 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000143}
144
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000145/// isEmptyRecord - Return true iff a structure contains only empty
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000146/// fields. Note that a structure with a flexible array member is not
147/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000148static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000149 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000150 if (!RT)
151 return 0;
152 const RecordDecl *RD = RT->getDecl();
153 if (RD->hasFlexibleArrayMember())
154 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000155
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000156 // If this is a C++ record, check the bases first.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000157 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000158 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
159 e = CXXRD->bases_end(); i != e; ++i)
160 if (!isEmptyRecord(Context, i->getType(), true))
161 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000162
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000163 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
164 i != e; ++i)
David Blaikie581deb32012-06-06 20:45:41 +0000165 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000166 return false;
167 return true;
168}
169
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000170/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
171/// a non-trivial destructor or a non-trivial copy constructor.
172static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
173 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
174 if (!RD)
175 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000176
Richard Smith426391c2012-11-16 00:53:38 +0000177 return !RD->hasTrivialDestructor() || RD->hasNonTrivialCopyConstructor();
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000178}
179
180/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
181/// a record type with either a non-trivial destructor or a non-trivial copy
182/// constructor.
183static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
184 const RecordType *RT = T->getAs<RecordType>();
185 if (!RT)
186 return false;
187
188 return hasNonTrivialDestructorOrCopyConstructor(RT);
189}
190
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000191/// isSingleElementStruct - Determine if a structure is a "single
192/// element struct", i.e. it has exactly one non-empty field or
193/// exactly one field which is itself a single element
194/// struct. Structures with flexible array members are never
195/// considered single element structs.
196///
197/// \return The field declaration for the single non-empty field, if
198/// it exists.
199static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
200 const RecordType *RT = T->getAsStructureType();
201 if (!RT)
202 return 0;
203
204 const RecordDecl *RD = RT->getDecl();
205 if (RD->hasFlexibleArrayMember())
206 return 0;
207
208 const Type *Found = 0;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000209
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000210 // If this is a C++ record, check the bases first.
211 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
212 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
213 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000214 // Ignore empty records.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000215 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000216 continue;
217
218 // If we already found an element then this isn't a single-element struct.
219 if (Found)
220 return 0;
221
222 // If this is non-empty and not a single element struct, the composite
223 // cannot be a single element struct.
224 Found = isSingleElementStruct(i->getType(), Context);
225 if (!Found)
226 return 0;
227 }
228 }
229
230 // Check for single element.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000231 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
232 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000233 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000234 QualType FT = FD->getType();
235
236 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000237 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000238 continue;
239
240 // If we already found an element then this isn't a single-element
241 // struct.
242 if (Found)
243 return 0;
244
245 // Treat single element arrays as the element.
246 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
247 if (AT->getSize().getZExtValue() != 1)
248 break;
249 FT = AT->getElementType();
250 }
251
John McCalld608cdb2010-08-22 10:59:02 +0000252 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000253 Found = FT.getTypePtr();
254 } else {
255 Found = isSingleElementStruct(FT, Context);
256 if (!Found)
257 return 0;
258 }
259 }
260
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000261 // We don't consider a struct a single-element struct if it has
262 // padding beyond the element type.
263 if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
264 return 0;
265
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000266 return Found;
267}
268
269static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Eli Friedmandb748a32012-11-29 23:21:04 +0000270 // Treat complex types as the element type.
271 if (const ComplexType *CTy = Ty->getAs<ComplexType>())
272 Ty = CTy->getElementType();
273
274 // Check for a type which we know has a simple scalar argument-passing
275 // convention without any padding. (We're specifically looking for 32
276 // and 64-bit integer and integer-equivalents, float, and double.)
Daniel Dunbara1842d32010-05-14 03:40:53 +0000277 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Eli Friedmandb748a32012-11-29 23:21:04 +0000278 !Ty->isEnumeralType() && !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000279 return false;
280
281 uint64_t Size = Context.getTypeSize(Ty);
282 return Size == 32 || Size == 64;
283}
284
Daniel Dunbar53012f42009-11-09 01:33:53 +0000285/// canExpandIndirectArgument - Test whether an argument type which is to be
286/// passed indirectly (on the stack) would have the equivalent layout if it was
287/// expanded into separate arguments. If so, we prefer to do the latter to avoid
288/// inhibiting optimizations.
289///
290// FIXME: This predicate is missing many cases, currently it just follows
291// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
292// should probably make this smarter, or better yet make the LLVM backend
293// capable of handling it.
294static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
295 // We can only expand structure types.
296 const RecordType *RT = Ty->getAs<RecordType>();
297 if (!RT)
298 return false;
299
300 // We can only expand (C) structures.
301 //
302 // FIXME: This needs to be generalized to handle classes as well.
303 const RecordDecl *RD = RT->getDecl();
304 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
305 return false;
306
Eli Friedman506d4e32011-11-18 01:32:26 +0000307 uint64_t Size = 0;
308
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000309 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
310 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000311 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000312
313 if (!is32Or64BitBasicType(FD->getType(), Context))
314 return false;
315
316 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
317 // how to expand them yet, and the predicate for telling if a bitfield still
318 // counts as "basic" is more complicated than what we were doing previously.
319 if (FD->isBitField())
320 return false;
Eli Friedman506d4e32011-11-18 01:32:26 +0000321
322 Size += Context.getTypeSize(FD->getType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000323 }
324
Eli Friedman506d4e32011-11-18 01:32:26 +0000325 // Make sure there are not any holes in the struct.
326 if (Size != Context.getTypeSize(Ty))
327 return false;
328
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000329 return true;
330}
331
332namespace {
333/// DefaultABIInfo - The default implementation for ABI specific
334/// details. This implementation provides information which results in
335/// self-consistent and sensible LLVM IR generation, but does not
336/// conform to any particular ABI.
337class DefaultABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +0000338public:
339 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000340
Chris Lattnera3c109b2010-07-29 02:16:43 +0000341 ABIArgInfo classifyReturnType(QualType RetTy) const;
342 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000343
Chris Lattneree5dcd02010-07-29 02:31:05 +0000344 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000345 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000346 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
347 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000348 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000349 }
350
351 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
352 CodeGenFunction &CGF) const;
353};
354
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000355class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
356public:
Chris Lattnerea044322010-07-29 02:01:43 +0000357 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
358 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000359};
360
361llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
362 CodeGenFunction &CGF) const {
363 return 0;
364}
365
Chris Lattnera3c109b2010-07-29 02:16:43 +0000366ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
Jan Wen Voung90306932011-11-03 00:59:44 +0000367 if (isAggregateTypeForABI(Ty)) {
368 // Records with non trivial destructors/constructors should not be passed
369 // by value.
370 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
371 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
372
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000373 return ABIArgInfo::getIndirect(0);
Jan Wen Voung90306932011-11-03 00:59:44 +0000374 }
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000375
Chris Lattnera14db752010-03-11 18:19:55 +0000376 // Treat an enum type as its underlying type.
377 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
378 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000379
Chris Lattnera14db752010-03-11 18:19:55 +0000380 return (Ty->isPromotableIntegerType() ?
381 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000382}
383
Bob Wilson0024f942011-01-10 23:54:17 +0000384ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
385 if (RetTy->isVoidType())
386 return ABIArgInfo::getIgnore();
387
388 if (isAggregateTypeForABI(RetTy))
389 return ABIArgInfo::getIndirect(0);
390
391 // Treat an enum type as its underlying type.
392 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
393 RetTy = EnumTy->getDecl()->getIntegerType();
394
395 return (RetTy->isPromotableIntegerType() ?
396 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
397}
398
Derek Schuff9ed63f82012-09-06 17:37:28 +0000399//===----------------------------------------------------------------------===//
400// le32/PNaCl bitcode ABI Implementation
401//===----------------------------------------------------------------------===//
402
403class PNaClABIInfo : public ABIInfo {
404 public:
405 PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
406
407 ABIArgInfo classifyReturnType(QualType RetTy) const;
408 ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &FreeRegs) const;
409
410 virtual void computeInfo(CGFunctionInfo &FI) const;
411 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
412 CodeGenFunction &CGF) const;
413};
414
415class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
416 public:
417 PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
418 : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {}
419};
420
421void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
422 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
423
Eli Benderskye45dfd12013-04-04 22:49:35 +0000424 // Obtain the initial number of registers available for passing integers
425 // from the function's regparm attribute.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000426 unsigned FreeRegs = FI.getHasRegParm() ? FI.getRegParm() : 0;
427
428 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
429 it != ie; ++it)
430 it->info = classifyArgumentType(it->type, FreeRegs);
431 }
432
433llvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
434 CodeGenFunction &CGF) const {
435 return 0;
436}
437
Eli Benderskye45dfd12013-04-04 22:49:35 +0000438// \brief Classify argument of given type \p Ty. \p FreeRegs is the number of
439// registers available for passing arguments - it can be updated by this
440// method.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000441ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty,
442 unsigned &FreeRegs) const {
443 if (isAggregateTypeForABI(Ty)) {
Eli Benderskye45dfd12013-04-04 22:49:35 +0000444 // In the PNaCl ABI we always pass records/structures on the stack. The
445 // byval attribute can be used if the record doesn't have non-trivial
446 // constructors/destructors.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000447 FreeRegs = 0;
448 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
449 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Derek Schuff9ed63f82012-09-06 17:37:28 +0000450 return ABIArgInfo::getIndirect(0);
451 }
452
453 // Treat an enum type as its underlying type.
454 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
455 Ty = EnumTy->getDecl()->getIntegerType();
456
457 ABIArgInfo BaseInfo = (Ty->isPromotableIntegerType() ?
458 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
459
Eli Benderskye45dfd12013-04-04 22:49:35 +0000460 // Figure out how many of the free registers can be occupied by this type.
461 // regparm registers are 32-bit.
462 unsigned NumRegsRequired = (getContext().getTypeSize(Ty) + 31) / 32;
463 if (NumRegsRequired == 0) return BaseInfo;
464 if (NumRegsRequired > FreeRegs) {
465 // If this type needs more registers than we have available, no more
466 // passing in-registers can happen.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000467 FreeRegs = 0;
468 return BaseInfo;
469 }
Eli Benderskye45dfd12013-04-04 22:49:35 +0000470 FreeRegs -= NumRegsRequired;
Derek Schuff9ed63f82012-09-06 17:37:28 +0000471 return BaseInfo.isDirect() ?
472 ABIArgInfo::getDirectInReg(BaseInfo.getCoerceToType()) :
473 ABIArgInfo::getExtendInReg(BaseInfo.getCoerceToType());
474}
475
476ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
477 if (RetTy->isVoidType())
478 return ABIArgInfo::getIgnore();
479
Eli Benderskye45dfd12013-04-04 22:49:35 +0000480 // In the PNaCl ABI we always return records/structures on the stack.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000481 if (isAggregateTypeForABI(RetTy))
482 return ABIArgInfo::getIndirect(0);
483
484 // Treat an enum type as its underlying type.
485 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
486 RetTy = EnumTy->getDecl()->getIntegerType();
487
488 return (RetTy->isPromotableIntegerType() ?
489 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
490}
491
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000492/// IsX86_MMXType - Return true if this is an MMX type.
493bool IsX86_MMXType(llvm::Type *IRType) {
494 // 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 +0000495 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
496 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
497 IRType->getScalarSizeInBits() != 64;
498}
499
Jay Foadef6de3d2011-07-11 09:56:20 +0000500static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000501 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000502 llvm::Type* Ty) {
Bill Wendling0507be62011-03-07 22:47:14 +0000503 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy())
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000504 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
505 return Ty;
506}
507
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000508//===----------------------------------------------------------------------===//
509// X86-32 ABI Implementation
510//===----------------------------------------------------------------------===//
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000511
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000512/// X86_32ABIInfo - The X86-32 ABI information.
513class X86_32ABIInfo : public ABIInfo {
Rafael Espindolab48280b2012-07-31 02:44:24 +0000514 enum Class {
515 Integer,
516 Float
517 };
518
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000519 static const unsigned MinABIStackAlignInBytes = 4;
520
David Chisnall1e4249c2009-08-17 23:08:21 +0000521 bool IsDarwinVectorABI;
522 bool IsSmallStructInRegABI;
Eli Friedman55fc7e22012-01-25 22:46:34 +0000523 bool IsWin32FloatStructABI;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000524 unsigned DefaultNumRegisterParameters;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000525
526 static bool isRegisterSize(unsigned Size) {
527 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
528 }
529
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000530 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context,
531 unsigned callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000532
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000533 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
534 /// such that the argument will be passed in memory.
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000535 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal,
536 unsigned &FreeRegs) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000537
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000538 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbare59d8582010-09-16 20:42:06 +0000539 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000540
Rafael Espindolab48280b2012-07-31 02:44:24 +0000541 Class classify(QualType Ty) const;
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000542 ABIArgInfo classifyReturnType(QualType RetTy,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000543 unsigned callingConvention) const;
Rafael Espindolab6932692012-10-24 01:58:58 +0000544 ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &FreeRegs,
545 bool IsFastCall) const;
546 bool shouldUseInReg(QualType Ty, unsigned &FreeRegs,
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000547 bool IsFastCall, bool &NeedsPadding) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000548
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000549public:
550
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000551 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000552 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
553 CodeGenFunction &CGF) const;
554
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000555 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool w,
Rafael Espindolab48280b2012-07-31 02:44:24 +0000556 unsigned r)
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000557 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000558 IsWin32FloatStructABI(w), DefaultNumRegisterParameters(r) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000559};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000560
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000561class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
562public:
Eli Friedman55fc7e22012-01-25 22:46:34 +0000563 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000564 bool d, bool p, bool w, unsigned r)
565 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, w, r)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000566
567 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
568 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000569
570 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
571 // Darwin uses different dwarf register numbers for EH.
572 if (CGM.isTargetDarwin()) return 5;
573
574 return 4;
575 }
576
577 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
578 llvm::Value *Address) const;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000579
Jay Foadef6de3d2011-07-11 09:56:20 +0000580 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000581 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000582 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000583 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
584 }
585
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000586};
587
588}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000589
590/// shouldReturnTypeInRegister - Determine if the given type should be
591/// passed in a register (for the Darwin ABI).
592bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000593 ASTContext &Context,
594 unsigned callingConvention) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000595 uint64_t Size = Context.getTypeSize(Ty);
596
597 // Type must be register sized.
598 if (!isRegisterSize(Size))
599 return false;
600
601 if (Ty->isVectorType()) {
602 // 64- and 128- bit vectors inside structures are not returned in
603 // registers.
604 if (Size == 64 || Size == 128)
605 return false;
606
607 return true;
608 }
609
Daniel Dunbar77115232010-05-15 00:00:30 +0000610 // If this is a builtin, pointer, enum, complex type, member pointer, or
611 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000612 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000613 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000614 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000615 return true;
616
617 // Arrays are treated like records.
618 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000619 return shouldReturnTypeInRegister(AT->getElementType(), Context,
620 callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000621
622 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000623 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000624 if (!RT) return false;
625
Anders Carlssona8874232010-01-27 03:25:19 +0000626 // FIXME: Traverse bases here too.
627
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000628 // For thiscall conventions, structures will never be returned in
629 // a register. This is for compatibility with the MSVC ABI
630 if (callingConvention == llvm::CallingConv::X86_ThisCall &&
631 RT->isStructureType()) {
632 return false;
633 }
634
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000635 // Structure types are passed in register if all fields would be
636 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000637 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
638 e = RT->getDecl()->field_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000639 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000640
641 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000642 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000643 continue;
644
645 // Check fields recursively.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000646 if (!shouldReturnTypeInRegister(FD->getType(), Context,
647 callingConvention))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000648 return false;
649 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000650 return true;
651}
652
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000653ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
654 unsigned callingConvention) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000655 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000656 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000657
Chris Lattnera3c109b2010-07-29 02:16:43 +0000658 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000659 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000660 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000661 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000662
663 // 128-bit vectors are a special case; they are returned in
664 // registers and we need to make sure to pick a type the LLVM
665 // backend will like.
666 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +0000667 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000668 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000669
670 // Always return in register if it fits in a general purpose
671 // register, or if it is 64 bits and has a single element.
672 if ((Size == 8 || Size == 16 || Size == 32) ||
673 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +0000674 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000675 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000676
677 return ABIArgInfo::getIndirect(0);
678 }
679
680 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000681 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000682
John McCalld608cdb2010-08-22 10:59:02 +0000683 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000684 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000685 // Structures with either a non-trivial destructor or a non-trivial
686 // copy constructor are always indirect.
687 if (hasNonTrivialDestructorOrCopyConstructor(RT))
688 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000689
Anders Carlsson40092972009-10-20 22:07:59 +0000690 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000691 if (RT->getDecl()->hasFlexibleArrayMember())
692 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000693 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000694
David Chisnall1e4249c2009-08-17 23:08:21 +0000695 // If specified, structs and unions are always indirect.
696 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000697 return ABIArgInfo::getIndirect(0);
698
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000699 // Small structures which are register sized are generally returned
700 // in a register.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000701 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(),
702 callingConvention)) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000703 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000704
705 // As a special-case, if the struct is a "single-element" struct, and
706 // the field is of type "float" or "double", return it in a
Eli Friedman55fc7e22012-01-25 22:46:34 +0000707 // floating-point register. (MSVC does not apply this special case.)
708 // We apply a similar transformation for pointer types to improve the
709 // quality of the generated IR.
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000710 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
Eli Friedman55fc7e22012-01-25 22:46:34 +0000711 if ((!IsWin32FloatStructABI && SeltTy->isRealFloatingType())
712 || SeltTy->hasPointerRepresentation())
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000713 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
714
715 // FIXME: We should be able to narrow this integer in cases with dead
716 // padding.
Chris Lattner800588f2010-07-29 06:26:06 +0000717 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000718 }
719
720 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000721 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000722
Chris Lattnera3c109b2010-07-29 02:16:43 +0000723 // Treat an enum type as its underlying type.
724 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
725 RetTy = EnumTy->getDecl()->getIntegerType();
726
727 return (RetTy->isPromotableIntegerType() ?
728 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000729}
730
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000731static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
732 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
733}
734
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000735static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
736 const RecordType *RT = Ty->getAs<RecordType>();
737 if (!RT)
738 return 0;
739 const RecordDecl *RD = RT->getDecl();
740
741 // If this is a C++ record, check the bases first.
742 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
743 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
744 e = CXXRD->bases_end(); i != e; ++i)
745 if (!isRecordWithSSEVectorType(Context, i->getType()))
746 return false;
747
748 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
749 i != e; ++i) {
750 QualType FT = i->getType();
751
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000752 if (isSSEVectorType(Context, FT))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000753 return true;
754
755 if (isRecordWithSSEVectorType(Context, FT))
756 return true;
757 }
758
759 return false;
760}
761
Daniel Dunbare59d8582010-09-16 20:42:06 +0000762unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
763 unsigned Align) const {
764 // Otherwise, if the alignment is less than or equal to the minimum ABI
765 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000766 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +0000767 return 0; // Use default alignment.
768
769 // On non-Darwin, the stack type alignment is always 4.
770 if (!IsDarwinVectorABI) {
771 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000772 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +0000773 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000774
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000775 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000776 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
777 isRecordWithSSEVectorType(getContext(), Ty)))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000778 return 16;
779
780 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000781}
782
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000783ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
784 unsigned &FreeRegs) const {
785 if (!ByVal) {
786 if (FreeRegs) {
787 --FreeRegs; // Non byval indirects just use one pointer.
788 return ABIArgInfo::getIndirectInReg(0, false);
789 }
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000790 return ABIArgInfo::getIndirect(0, false);
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000791 }
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000792
Daniel Dunbare59d8582010-09-16 20:42:06 +0000793 // Compute the byval alignment.
794 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
795 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
796 if (StackAlign == 0)
Chris Lattnerde92d732011-05-22 23:35:00 +0000797 return ABIArgInfo::getIndirect(4);
Daniel Dunbare59d8582010-09-16 20:42:06 +0000798
799 // If the stack alignment is less than the type alignment, realign the
800 // argument.
801 if (StackAlign < TypeAlign)
802 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
803 /*Realign=*/true);
804
805 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000806}
807
Rafael Espindolab48280b2012-07-31 02:44:24 +0000808X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
809 const Type *T = isSingleElementStruct(Ty, getContext());
810 if (!T)
811 T = Ty.getTypePtr();
812
813 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
814 BuiltinType::Kind K = BT->getKind();
815 if (K == BuiltinType::Float || K == BuiltinType::Double)
816 return Float;
817 }
818 return Integer;
819}
820
Rafael Espindolab6932692012-10-24 01:58:58 +0000821bool X86_32ABIInfo::shouldUseInReg(QualType Ty, unsigned &FreeRegs,
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000822 bool IsFastCall, bool &NeedsPadding) const {
823 NeedsPadding = false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000824 Class C = classify(Ty);
825 if (C == Float)
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000826 return false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000827
Rafael Espindolab6932692012-10-24 01:58:58 +0000828 unsigned Size = getContext().getTypeSize(Ty);
829 unsigned SizeInRegs = (Size + 31) / 32;
Rafael Espindola5f14fcb2012-10-23 02:04:01 +0000830
831 if (SizeInRegs == 0)
832 return false;
833
Rafael Espindolab48280b2012-07-31 02:44:24 +0000834 if (SizeInRegs > FreeRegs) {
835 FreeRegs = 0;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000836 return false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000837 }
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000838
Rafael Espindolab48280b2012-07-31 02:44:24 +0000839 FreeRegs -= SizeInRegs;
Rafael Espindolab6932692012-10-24 01:58:58 +0000840
841 if (IsFastCall) {
842 if (Size > 32)
843 return false;
844
845 if (Ty->isIntegralOrEnumerationType())
846 return true;
847
848 if (Ty->isPointerType())
849 return true;
850
851 if (Ty->isReferenceType())
852 return true;
853
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000854 if (FreeRegs)
855 NeedsPadding = true;
856
Rafael Espindolab6932692012-10-24 01:58:58 +0000857 return false;
858 }
859
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000860 return true;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000861}
862
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000863ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Rafael Espindolab6932692012-10-24 01:58:58 +0000864 unsigned &FreeRegs,
865 bool IsFastCall) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000866 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000867 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000868 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000869 if (const RecordType *RT = Ty->getAs<RecordType>()) {
870 // Structures with either a non-trivial destructor or a non-trivial
871 // copy constructor are always indirect.
872 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000873 return getIndirectResult(Ty, false, FreeRegs);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000874
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000875 if (RT->getDecl()->hasFlexibleArrayMember())
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000876 return getIndirectResult(Ty, true, FreeRegs);
Anders Carlssona8874232010-01-27 03:25:19 +0000877 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000878
Eli Friedman5a4d3522011-11-18 00:28:11 +0000879 // Ignore empty structs/unions.
Eli Friedman5a1ac892011-11-18 04:01:36 +0000880 if (isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000881 return ABIArgInfo::getIgnore();
882
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000883 llvm::LLVMContext &LLVMContext = getVMContext();
884 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
885 bool NeedsPadding;
886 if (shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding)) {
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000887 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000888 SmallVector<llvm::Type*, 3> Elements;
889 for (unsigned I = 0; I < SizeInRegs; ++I)
890 Elements.push_back(Int32);
891 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
892 return ABIArgInfo::getDirectInReg(Result);
893 }
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000894 llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : 0;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000895
Daniel Dunbar53012f42009-11-09 01:33:53 +0000896 // Expand small (<= 128-bit) record types when we know that the stack layout
897 // of those arguments will match the struct. This is important because the
898 // LLVM backend isn't smart enough to remove byval, which inhibits many
899 // optimizations.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000900 if (getContext().getTypeSize(Ty) <= 4*32 &&
901 canExpandIndirectArgument(Ty, getContext()))
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000902 return ABIArgInfo::getExpandWithPadding(IsFastCall, PaddingType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000903
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000904 return getIndirectResult(Ty, true, FreeRegs);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000905 }
906
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000907 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +0000908 // On Darwin, some vectors are passed in memory, we handle this by passing
909 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000910 if (IsDarwinVectorABI) {
911 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000912 if ((Size == 8 || Size == 16 || Size == 32) ||
913 (Size == 64 && VT->getNumElements() == 1))
914 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
915 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000916 }
Bill Wendlingbb465d72010-10-18 03:41:31 +0000917
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000918 if (IsX86_MMXType(CGT.ConvertType(Ty)))
919 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64));
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000920
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000921 return ABIArgInfo::getDirect();
922 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000923
924
Chris Lattnera3c109b2010-07-29 02:16:43 +0000925 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
926 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000927
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000928 bool NeedsPadding;
929 bool InReg = shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding);
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000930
931 if (Ty->isPromotableIntegerType()) {
932 if (InReg)
933 return ABIArgInfo::getExtendInReg();
934 return ABIArgInfo::getExtend();
935 }
936 if (InReg)
937 return ABIArgInfo::getDirectInReg();
938 return ABIArgInfo::getDirect();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000939}
940
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000941void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
942 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
943 FI.getCallingConvention());
Rafael Espindolab48280b2012-07-31 02:44:24 +0000944
Rafael Espindolab6932692012-10-24 01:58:58 +0000945 unsigned CC = FI.getCallingConvention();
946 bool IsFastCall = CC == llvm::CallingConv::X86_FastCall;
947 unsigned FreeRegs;
948 if (IsFastCall)
949 FreeRegs = 2;
950 else if (FI.getHasRegParm())
951 FreeRegs = FI.getRegParm();
952 else
953 FreeRegs = DefaultNumRegisterParameters;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000954
955 // If the return value is indirect, then the hidden argument is consuming one
956 // integer register.
957 if (FI.getReturnInfo().isIndirect() && FreeRegs) {
958 --FreeRegs;
959 ABIArgInfo &Old = FI.getReturnInfo();
960 Old = ABIArgInfo::getIndirectInReg(Old.getIndirectAlign(),
961 Old.getIndirectByVal(),
962 Old.getIndirectRealign());
963 }
964
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000965 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
966 it != ie; ++it)
Rafael Espindolab6932692012-10-24 01:58:58 +0000967 it->info = classifyArgumentType(it->type, FreeRegs, IsFastCall);
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000968}
969
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000970llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
971 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +0000972 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000973
974 CGBuilderTy &Builder = CGF.Builder;
975 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
976 "ap");
977 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Eli Friedman7b1fb812011-11-18 02:12:09 +0000978
979 // Compute if the address needs to be aligned
980 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
981 Align = getTypeStackAlignInBytes(Ty, Align);
982 Align = std::max(Align, 4U);
983 if (Align > 4) {
984 // addr = (addr + align - 1) & -align;
985 llvm::Value *Offset =
986 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
987 Addr = CGF.Builder.CreateGEP(Addr, Offset);
988 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
989 CGF.Int32Ty);
990 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
991 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
992 Addr->getType(),
993 "ap.cur.aligned");
994 }
995
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000996 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000997 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000998 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
999
1000 uint64_t Offset =
Eli Friedman7b1fb812011-11-18 02:12:09 +00001001 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001002 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00001003 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001004 "ap.next");
1005 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1006
1007 return AddrTyped;
1008}
1009
Charles Davis74f72932010-02-13 15:54:06 +00001010void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
1011 llvm::GlobalValue *GV,
1012 CodeGen::CodeGenModule &CGM) const {
1013 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1014 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
1015 // Get the LLVM function.
1016 llvm::Function *Fn = cast<llvm::Function>(GV);
1017
1018 // Now add the 'alignstack' attribute with a value of 16.
Bill Wendling0d583392012-10-15 20:36:26 +00001019 llvm::AttrBuilder B;
Bill Wendlinge91e9ec2012-10-14 03:28:14 +00001020 B.addStackAlignmentAttr(16);
Bill Wendling909b6de2013-01-23 00:21:06 +00001021 Fn->addAttributes(llvm::AttributeSet::FunctionIndex,
1022 llvm::AttributeSet::get(CGM.getLLVMContext(),
1023 llvm::AttributeSet::FunctionIndex,
1024 B));
Charles Davis74f72932010-02-13 15:54:06 +00001025 }
1026 }
1027}
1028
John McCall6374c332010-03-06 00:35:14 +00001029bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
1030 CodeGen::CodeGenFunction &CGF,
1031 llvm::Value *Address) const {
1032 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCall6374c332010-03-06 00:35:14 +00001033
Chris Lattner8b418682012-02-07 00:39:47 +00001034 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001035
John McCall6374c332010-03-06 00:35:14 +00001036 // 0-7 are the eight integer registers; the order is different
1037 // on Darwin (for EH), but the range is the same.
1038 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +00001039 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +00001040
1041 if (CGF.CGM.isTargetDarwin()) {
1042 // 12-16 are st(0..4). Not sure why we stop at 4.
1043 // These have size 16, which is sizeof(long double) on
1044 // platforms with 8-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001045 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCallaeeb7012010-05-27 06:19:26 +00001046 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001047
John McCall6374c332010-03-06 00:35:14 +00001048 } else {
1049 // 9 is %eflags, which doesn't get a size on Darwin for some
1050 // reason.
1051 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
1052
1053 // 11-16 are st(0..5). Not sure why we stop at 5.
1054 // These have size 12, which is sizeof(long double) on
1055 // platforms with 4-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001056 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCallaeeb7012010-05-27 06:19:26 +00001057 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1058 }
John McCall6374c332010-03-06 00:35:14 +00001059
1060 return false;
1061}
1062
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001063//===----------------------------------------------------------------------===//
1064// X86-64 ABI Implementation
1065//===----------------------------------------------------------------------===//
1066
1067
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001068namespace {
1069/// X86_64ABIInfo - The X86_64 ABI information.
1070class X86_64ABIInfo : public ABIInfo {
1071 enum Class {
1072 Integer = 0,
1073 SSE,
1074 SSEUp,
1075 X87,
1076 X87Up,
1077 ComplexX87,
1078 NoClass,
1079 Memory
1080 };
1081
1082 /// merge - Implement the X86_64 ABI merging algorithm.
1083 ///
1084 /// Merge an accumulating classification \arg Accum with a field
1085 /// classification \arg Field.
1086 ///
1087 /// \param Accum - The accumulating classification. This should
1088 /// always be either NoClass or the result of a previous merge
1089 /// call. In addition, this should never be Memory (the caller
1090 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001091 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001092
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001093 /// postMerge - Implement the X86_64 ABI post merging algorithm.
1094 ///
1095 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
1096 /// final MEMORY or SSE classes when necessary.
1097 ///
1098 /// \param AggregateSize - The size of the current aggregate in
1099 /// the classification process.
1100 ///
1101 /// \param Lo - The classification for the parts of the type
1102 /// residing in the low word of the containing object.
1103 ///
1104 /// \param Hi - The classification for the parts of the type
1105 /// residing in the higher words of the containing object.
1106 ///
1107 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
1108
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001109 /// classify - Determine the x86_64 register classes in which the
1110 /// given type T should be passed.
1111 ///
1112 /// \param Lo - The classification for the parts of the type
1113 /// residing in the low word of the containing object.
1114 ///
1115 /// \param Hi - The classification for the parts of the type
1116 /// residing in the high word of the containing object.
1117 ///
1118 /// \param OffsetBase - The bit offset of this type in the
1119 /// containing object. Some parameters are classified different
1120 /// depending on whether they straddle an eightbyte boundary.
1121 ///
1122 /// If a word is unused its result will be NoClass; if a type should
1123 /// be passed in Memory then at least the classification of \arg Lo
1124 /// will be Memory.
1125 ///
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001126 /// The \arg Lo class will be NoClass iff the argument is ignored.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001127 ///
1128 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
1129 /// also be ComplexX87.
Chris Lattner9c254f02010-06-29 06:01:59 +00001130 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001131
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001132 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001133 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
1134 unsigned IROffset, QualType SourceTy,
1135 unsigned SourceOffset) const;
1136 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
1137 unsigned IROffset, QualType SourceTy,
1138 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001139
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001140 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001141 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +00001142 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001143
1144 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001145 /// such that the argument will be passed in memory.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001146 ///
1147 /// \param freeIntRegs - The number of free integer registers remaining
1148 /// available.
1149 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001150
Chris Lattnera3c109b2010-07-29 02:16:43 +00001151 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001152
Bill Wendlingbb465d72010-10-18 03:41:31 +00001153 ABIArgInfo classifyArgumentType(QualType Ty,
Daniel Dunbaredfac032012-03-10 01:03:58 +00001154 unsigned freeIntRegs,
Bill Wendlingbb465d72010-10-18 03:41:31 +00001155 unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +00001156 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001157
Eli Friedmanee1ad992011-12-02 00:11:43 +00001158 bool IsIllegalVectorType(QualType Ty) const;
1159
John McCall67a57732011-04-21 01:20:55 +00001160 /// The 0.98 ABI revision clarified a lot of ambiguities,
1161 /// unfortunately in ways that were not always consistent with
1162 /// certain previous compilers. In particular, platforms which
1163 /// required strict binary compatibility with older versions of GCC
1164 /// may need to exempt themselves.
1165 bool honorsRevision0_98() const {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001166 return !getContext().getTargetInfo().getTriple().isOSDarwin();
John McCall67a57732011-04-21 01:20:55 +00001167 }
1168
Eli Friedmanee1ad992011-12-02 00:11:43 +00001169 bool HasAVX;
Derek Schuffbabaf312012-10-11 15:52:22 +00001170 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
1171 // 64-bit hardware.
1172 bool Has64BitPointers;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001173
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001174public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001175 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
Derek Schuffbabaf312012-10-11 15:52:22 +00001176 ABIInfo(CGT), HasAVX(hasavx),
Derek Schuff90da80c2012-10-11 18:21:13 +00001177 Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001178 }
Chris Lattner9c254f02010-06-29 06:01:59 +00001179
John McCallde5d3c72012-02-17 03:33:10 +00001180 bool isPassedUsingAVXType(QualType type) const {
1181 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00001182 // The freeIntRegs argument doesn't matter here.
1183 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE);
John McCallde5d3c72012-02-17 03:33:10 +00001184 if (info.isDirect()) {
1185 llvm::Type *ty = info.getCoerceToType();
1186 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
1187 return (vectorTy->getBitWidth() > 128);
1188 }
1189 return false;
1190 }
1191
Chris Lattneree5dcd02010-07-29 02:31:05 +00001192 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001193
1194 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1195 CodeGenFunction &CGF) const;
1196};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001197
Chris Lattnerf13721d2010-08-31 16:44:54 +00001198/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001199class WinX86_64ABIInfo : public ABIInfo {
1200
1201 ABIArgInfo classify(QualType Ty) const;
1202
Chris Lattnerf13721d2010-08-31 16:44:54 +00001203public:
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001204 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
1205
1206 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattnerf13721d2010-08-31 16:44:54 +00001207
1208 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1209 CodeGenFunction &CGF) const;
1210};
1211
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001212class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1213public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001214 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
Derek Schuffbabaf312012-10-11 15:52:22 +00001215 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
John McCall6374c332010-03-06 00:35:14 +00001216
John McCallde5d3c72012-02-17 03:33:10 +00001217 const X86_64ABIInfo &getABIInfo() const {
1218 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
1219 }
1220
John McCall6374c332010-03-06 00:35:14 +00001221 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1222 return 7;
1223 }
1224
1225 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1226 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001227 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001228
John McCallaeeb7012010-05-27 06:19:26 +00001229 // 0-15 are the 16 integer registers.
1230 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001231 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +00001232 return false;
1233 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001234
Jay Foadef6de3d2011-07-11 09:56:20 +00001235 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001236 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +00001237 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001238 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1239 }
1240
John McCallde5d3c72012-02-17 03:33:10 +00001241 bool isNoProtoCallVariadic(const CallArgList &args,
1242 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +00001243 // The default CC on x86-64 sets %al to the number of SSA
1244 // registers used, and GCC sets this when calling an unprototyped
Eli Friedman3ed79032011-12-01 04:53:19 +00001245 // function, so we override the default behavior. However, don't do
Eli Friedman68805fe2011-12-06 03:08:26 +00001246 // that when AVX types are involved: the ABI explicitly states it is
1247 // undefined, and it doesn't work in practice because of how the ABI
1248 // defines varargs anyway.
John McCallde5d3c72012-02-17 03:33:10 +00001249 if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) {
Eli Friedman3ed79032011-12-01 04:53:19 +00001250 bool HasAVXType = false;
John McCallde5d3c72012-02-17 03:33:10 +00001251 for (CallArgList::const_iterator
1252 it = args.begin(), ie = args.end(); it != ie; ++it) {
1253 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1254 HasAVXType = true;
1255 break;
Eli Friedman3ed79032011-12-01 04:53:19 +00001256 }
1257 }
John McCallde5d3c72012-02-17 03:33:10 +00001258
Eli Friedman3ed79032011-12-01 04:53:19 +00001259 if (!HasAVXType)
1260 return true;
1261 }
John McCall01f151e2011-09-21 08:08:30 +00001262
John McCallde5d3c72012-02-17 03:33:10 +00001263 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
John McCall01f151e2011-09-21 08:08:30 +00001264 }
1265
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001266};
1267
Chris Lattnerf13721d2010-08-31 16:44:54 +00001268class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1269public:
1270 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1271 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1272
1273 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1274 return 7;
1275 }
1276
1277 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1278 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001279 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001280
Chris Lattnerf13721d2010-08-31 16:44:54 +00001281 // 0-15 are the 16 integer registers.
1282 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001283 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattnerf13721d2010-08-31 16:44:54 +00001284 return false;
1285 }
1286};
1287
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001288}
1289
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001290void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1291 Class &Hi) const {
1292 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1293 //
1294 // (a) If one of the classes is Memory, the whole argument is passed in
1295 // memory.
1296 //
1297 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1298 // memory.
1299 //
1300 // (c) If the size of the aggregate exceeds two eightbytes and the first
1301 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1302 // argument is passed in memory. NOTE: This is necessary to keep the
1303 // ABI working for processors that don't support the __m256 type.
1304 //
1305 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1306 //
1307 // Some of these are enforced by the merging logic. Others can arise
1308 // only with unions; for example:
1309 // union { _Complex double; unsigned; }
1310 //
1311 // Note that clauses (b) and (c) were added in 0.98.
1312 //
1313 if (Hi == Memory)
1314 Lo = Memory;
1315 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1316 Lo = Memory;
1317 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1318 Lo = Memory;
1319 if (Hi == SSEUp && Lo != SSE)
1320 Hi = SSE;
1321}
1322
Chris Lattner1090a9b2010-06-28 21:43:59 +00001323X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001324 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1325 // classified recursively so that always two fields are
1326 // considered. The resulting class is calculated according to
1327 // the classes of the fields in the eightbyte:
1328 //
1329 // (a) If both classes are equal, this is the resulting class.
1330 //
1331 // (b) If one of the classes is NO_CLASS, the resulting class is
1332 // the other class.
1333 //
1334 // (c) If one of the classes is MEMORY, the result is the MEMORY
1335 // class.
1336 //
1337 // (d) If one of the classes is INTEGER, the result is the
1338 // INTEGER.
1339 //
1340 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1341 // MEMORY is used as class.
1342 //
1343 // (f) Otherwise class SSE is used.
1344
1345 // Accum should never be memory (we should have returned) or
1346 // ComplexX87 (because this cannot be passed in a structure).
1347 assert((Accum != Memory && Accum != ComplexX87) &&
1348 "Invalid accumulated classification during merge.");
1349 if (Accum == Field || Field == NoClass)
1350 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001351 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001352 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001353 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001354 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001355 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001356 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001357 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1358 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001359 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001360 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001361}
1362
Chris Lattnerbcaedae2010-06-30 19:14:05 +00001363void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001364 Class &Lo, Class &Hi) const {
1365 // FIXME: This code can be simplified by introducing a simple value class for
1366 // Class pairs with appropriate constructor methods for the various
1367 // situations.
1368
1369 // FIXME: Some of the split computations are wrong; unaligned vectors
1370 // shouldn't be passed in registers for example, so there is no chance they
1371 // can straddle an eightbyte. Verify & simplify.
1372
1373 Lo = Hi = NoClass;
1374
1375 Class &Current = OffsetBase < 64 ? Lo : Hi;
1376 Current = Memory;
1377
John McCall183700f2009-09-21 23:43:11 +00001378 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001379 BuiltinType::Kind k = BT->getKind();
1380
1381 if (k == BuiltinType::Void) {
1382 Current = NoClass;
1383 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1384 Lo = Integer;
1385 Hi = Integer;
1386 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1387 Current = Integer;
Derek Schuff7da46f92012-10-11 16:55:58 +00001388 } else if ((k == BuiltinType::Float || k == BuiltinType::Double) ||
1389 (k == BuiltinType::LongDouble &&
1390 getContext().getTargetInfo().getTriple().getOS() ==
Eli Bendersky441d9f72012-12-04 18:38:10 +00001391 llvm::Triple::NaCl)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001392 Current = SSE;
1393 } else if (k == BuiltinType::LongDouble) {
1394 Lo = X87;
1395 Hi = X87Up;
1396 }
1397 // FIXME: _Decimal32 and _Decimal64 are SSE.
1398 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001399 return;
1400 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001401
Chris Lattner1090a9b2010-06-28 21:43:59 +00001402 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001403 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +00001404 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001405 return;
1406 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001407
Chris Lattner1090a9b2010-06-28 21:43:59 +00001408 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001409 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001410 return;
1411 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001412
Chris Lattner1090a9b2010-06-28 21:43:59 +00001413 if (Ty->isMemberPointerType()) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001414 if (Ty->isMemberFunctionPointerType() && Has64BitPointers)
Daniel Dunbar67d438d2010-05-15 00:00:37 +00001415 Lo = Hi = Integer;
1416 else
1417 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001418 return;
1419 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001420
Chris Lattner1090a9b2010-06-28 21:43:59 +00001421 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001422 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001423 if (Size == 32) {
1424 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1425 // float> as integer.
1426 Current = Integer;
1427
1428 // If this type crosses an eightbyte boundary, it should be
1429 // split.
1430 uint64_t EB_Real = (OffsetBase) / 64;
1431 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1432 if (EB_Real != EB_Imag)
1433 Hi = Lo;
1434 } else if (Size == 64) {
1435 // gcc passes <1 x double> in memory. :(
1436 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1437 return;
1438
1439 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001440 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001441 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1442 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1443 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001444 Current = Integer;
1445 else
1446 Current = SSE;
1447
1448 // If this type crosses an eightbyte boundary, it should be
1449 // split.
1450 if (OffsetBase && OffsetBase != 64)
1451 Hi = Lo;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001452 } else if (Size == 128 || (HasAVX && Size == 256)) {
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001453 // Arguments of 256-bits are split into four eightbyte chunks. The
1454 // least significant one belongs to class SSE and all the others to class
1455 // SSEUP. The original Lo and Hi design considers that types can't be
1456 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1457 // This design isn't correct for 256-bits, but since there're no cases
1458 // where the upper parts would need to be inspected, avoid adding
1459 // complexity and just consider Hi to match the 64-256 part.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001460 Lo = SSE;
1461 Hi = SSEUp;
1462 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001463 return;
1464 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001465
Chris Lattner1090a9b2010-06-28 21:43:59 +00001466 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001467 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001468
Chris Lattnerea044322010-07-29 02:01:43 +00001469 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001470 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001471 if (Size <= 64)
1472 Current = Integer;
1473 else if (Size <= 128)
1474 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001475 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001476 Current = SSE;
Derek Schuff7da46f92012-10-11 16:55:58 +00001477 else if (ET == getContext().DoubleTy ||
1478 (ET == getContext().LongDoubleTy &&
1479 getContext().getTargetInfo().getTriple().getOS() ==
Eli Bendersky441d9f72012-12-04 18:38:10 +00001480 llvm::Triple::NaCl))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001481 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001482 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001483 Current = ComplexX87;
1484
1485 // If this complex type crosses an eightbyte boundary then it
1486 // should be split.
1487 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001488 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001489 if (Hi == NoClass && EB_Real != EB_Imag)
1490 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001491
Chris Lattner1090a9b2010-06-28 21:43:59 +00001492 return;
1493 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001494
Chris Lattnerea044322010-07-29 02:01:43 +00001495 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001496 // Arrays are treated like structures.
1497
Chris Lattnerea044322010-07-29 02:01:43 +00001498 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001499
1500 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001501 // than four eightbytes, ..., it has class MEMORY.
1502 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001503 return;
1504
1505 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1506 // fields, it has class MEMORY.
1507 //
1508 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001509 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001510 return;
1511
1512 // Otherwise implement simplified merge. We could be smarter about
1513 // this, but it isn't worth it and would be harder to verify.
1514 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001515 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001516 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes089d8922011-07-12 01:27:38 +00001517
1518 // The only case a 256-bit wide vector could be used is when the array
1519 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1520 // to work for sizes wider than 128, early check and fallback to memory.
1521 if (Size > 128 && EltSize != 256)
1522 return;
1523
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001524 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1525 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001526 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001527 Lo = merge(Lo, FieldLo);
1528 Hi = merge(Hi, FieldHi);
1529 if (Lo == Memory || Hi == Memory)
1530 break;
1531 }
1532
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001533 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001534 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001535 return;
1536 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001537
Chris Lattner1090a9b2010-06-28 21:43:59 +00001538 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001539 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001540
1541 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001542 // than four eightbytes, ..., it has class MEMORY.
1543 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001544 return;
1545
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001546 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1547 // copy constructor or a non-trivial destructor, it is passed by invisible
1548 // reference.
1549 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1550 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001551
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001552 const RecordDecl *RD = RT->getDecl();
1553
1554 // Assume variable sized types are passed in memory.
1555 if (RD->hasFlexibleArrayMember())
1556 return;
1557
Chris Lattnerea044322010-07-29 02:01:43 +00001558 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001559
1560 // Reset Lo class, this will be recomputed.
1561 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001562
1563 // If this is a C++ record, classify the bases first.
1564 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1565 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1566 e = CXXRD->bases_end(); i != e; ++i) {
1567 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1568 "Unexpected base class!");
1569 const CXXRecordDecl *Base =
1570 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1571
1572 // Classify this field.
1573 //
1574 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1575 // single eightbyte, each is classified separately. Each eightbyte gets
1576 // initialized to class NO_CLASS.
1577 Class FieldLo, FieldHi;
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001578 uint64_t Offset =
1579 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
Chris Lattner9c254f02010-06-29 06:01:59 +00001580 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001581 Lo = merge(Lo, FieldLo);
1582 Hi = merge(Hi, FieldHi);
1583 if (Lo == Memory || Hi == Memory)
1584 break;
1585 }
1586 }
1587
1588 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001589 unsigned idx = 0;
Bruno Cardoso Lopes548e4782011-07-12 22:30:58 +00001590 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001591 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001592 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1593 bool BitField = i->isBitField();
1594
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001595 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1596 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001597 //
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001598 // The only case a 256-bit wide vector could be used is when the struct
1599 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1600 // to work for sizes wider than 128, early check and fallback to memory.
1601 //
1602 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1603 Lo = Memory;
1604 return;
1605 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001606 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001607 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001608 Lo = Memory;
1609 return;
1610 }
1611
1612 // Classify this field.
1613 //
1614 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1615 // exceeds a single eightbyte, each is classified
1616 // separately. Each eightbyte gets initialized to class
1617 // NO_CLASS.
1618 Class FieldLo, FieldHi;
1619
1620 // Bit-fields require special handling, they do not force the
1621 // structure to be passed in memory even if unaligned, and
1622 // therefore they can straddle an eightbyte.
1623 if (BitField) {
1624 // Ignore padding bit-fields.
1625 if (i->isUnnamedBitfield())
1626 continue;
1627
1628 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001629 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001630
1631 uint64_t EB_Lo = Offset / 64;
1632 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1633 FieldLo = FieldHi = NoClass;
1634 if (EB_Lo) {
1635 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1636 FieldLo = NoClass;
1637 FieldHi = Integer;
1638 } else {
1639 FieldLo = Integer;
1640 FieldHi = EB_Hi ? Integer : NoClass;
1641 }
1642 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001643 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001644 Lo = merge(Lo, FieldLo);
1645 Hi = merge(Hi, FieldHi);
1646 if (Lo == Memory || Hi == Memory)
1647 break;
1648 }
1649
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001650 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001651 }
1652}
1653
Chris Lattner9c254f02010-06-29 06:01:59 +00001654ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001655 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1656 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001657 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001658 // Treat an enum type as its underlying type.
1659 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1660 Ty = EnumTy->getDecl()->getIntegerType();
1661
1662 return (Ty->isPromotableIntegerType() ?
1663 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1664 }
1665
1666 return ABIArgInfo::getIndirect(0);
1667}
1668
Eli Friedmanee1ad992011-12-02 00:11:43 +00001669bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1670 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1671 uint64_t Size = getContext().getTypeSize(VecTy);
1672 unsigned LargestVector = HasAVX ? 256 : 128;
1673 if (Size <= 64 || Size > LargestVector)
1674 return true;
1675 }
1676
1677 return false;
1678}
1679
Daniel Dunbaredfac032012-03-10 01:03:58 +00001680ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1681 unsigned freeIntRegs) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001682 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1683 // place naturally.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001684 //
1685 // This assumption is optimistic, as there could be free registers available
1686 // when we need to pass this argument in memory, and LLVM could try to pass
1687 // the argument in the free register. This does not seem to happen currently,
1688 // but this code would be much safer if we could mark the argument with
1689 // 'onstack'. See PR12193.
Eli Friedmanee1ad992011-12-02 00:11:43 +00001690 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001691 // Treat an enum type as its underlying type.
1692 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1693 Ty = EnumTy->getDecl()->getIntegerType();
1694
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001695 return (Ty->isPromotableIntegerType() ?
1696 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001697 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001698
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001699 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1700 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001701
Chris Lattner855d2272011-05-22 23:21:23 +00001702 // Compute the byval alignment. We specify the alignment of the byval in all
1703 // cases so that the mid-level optimizer knows the alignment of the byval.
1704 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
Daniel Dunbaredfac032012-03-10 01:03:58 +00001705
1706 // Attempt to avoid passing indirect results using byval when possible. This
1707 // is important for good codegen.
1708 //
1709 // We do this by coercing the value into a scalar type which the backend can
1710 // handle naturally (i.e., without using byval).
1711 //
1712 // For simplicity, we currently only do this when we have exhausted all of the
1713 // free integer registers. Doing this when there are free integer registers
1714 // would require more care, as we would have to ensure that the coerced value
1715 // did not claim the unused register. That would require either reording the
1716 // arguments to the function (so that any subsequent inreg values came first),
1717 // or only doing this optimization when there were no following arguments that
1718 // might be inreg.
1719 //
1720 // We currently expect it to be rare (particularly in well written code) for
1721 // arguments to be passed on the stack when there are still free integer
1722 // registers available (this would typically imply large structs being passed
1723 // by value), so this seems like a fair tradeoff for now.
1724 //
1725 // We can revisit this if the backend grows support for 'onstack' parameter
1726 // attributes. See PR12193.
1727 if (freeIntRegs == 0) {
1728 uint64_t Size = getContext().getTypeSize(Ty);
1729
1730 // If this type fits in an eightbyte, coerce it into the matching integral
1731 // type, which will end up on the stack (with alignment 8).
1732 if (Align == 8 && Size <= 64)
1733 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1734 Size));
1735 }
1736
Chris Lattner855d2272011-05-22 23:21:23 +00001737 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001738}
1739
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001740/// GetByteVectorType - The ABI specifies that a value should be passed in an
1741/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner0f408f52010-07-29 04:56:46 +00001742/// vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001743llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001744 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001745
Chris Lattner15842bd2010-07-29 05:02:29 +00001746 // Wrapper structs that just contain vectors are passed just like vectors,
1747 // strip them off if present.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001748 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner15842bd2010-07-29 05:02:29 +00001749 while (STy && STy->getNumElements() == 1) {
1750 IRType = STy->getElementType(0);
1751 STy = dyn_cast<llvm::StructType>(IRType);
1752 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001753
Bruno Cardoso Lopes528a8c72011-07-08 22:57:35 +00001754 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001755 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1756 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001757 unsigned BitWidth = VT->getBitWidth();
Tanya Lattnerce275672011-11-28 23:18:11 +00001758 if ((BitWidth >= 128 && BitWidth <= 256) &&
Chris Lattner0f408f52010-07-29 04:56:46 +00001759 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1760 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1761 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1762 EltTy->isIntegerTy(128)))
1763 return VT;
1764 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001765
Chris Lattner0f408f52010-07-29 04:56:46 +00001766 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1767}
1768
Chris Lattnere2962be2010-07-29 07:30:00 +00001769/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1770/// is known to either be off the end of the specified type or being in
1771/// alignment padding. The user type specified is known to be at most 128 bits
1772/// in size, and have passed through X86_64ABIInfo::classify with a successful
1773/// classification that put one of the two halves in the INTEGER class.
1774///
1775/// It is conservatively correct to return false.
1776static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1777 unsigned EndBit, ASTContext &Context) {
1778 // If the bytes being queried are off the end of the type, there is no user
1779 // data hiding here. This handles analysis of builtins, vectors and other
1780 // types that don't contain interesting padding.
1781 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1782 if (TySize <= StartBit)
1783 return true;
1784
Chris Lattner021c3a32010-07-29 07:43:55 +00001785 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1786 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1787 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1788
1789 // Check each element to see if the element overlaps with the queried range.
1790 for (unsigned i = 0; i != NumElts; ++i) {
1791 // If the element is after the span we care about, then we're done..
1792 unsigned EltOffset = i*EltSize;
1793 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001794
Chris Lattner021c3a32010-07-29 07:43:55 +00001795 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1796 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1797 EndBit-EltOffset, Context))
1798 return false;
1799 }
1800 // If it overlaps no elements, then it is safe to process as padding.
1801 return true;
1802 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001803
Chris Lattnere2962be2010-07-29 07:30:00 +00001804 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1805 const RecordDecl *RD = RT->getDecl();
1806 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001807
Chris Lattnere2962be2010-07-29 07:30:00 +00001808 // If this is a C++ record, check the bases first.
1809 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1810 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1811 e = CXXRD->bases_end(); i != e; ++i) {
1812 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1813 "Unexpected base class!");
1814 const CXXRecordDecl *Base =
1815 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001816
Chris Lattnere2962be2010-07-29 07:30:00 +00001817 // If the base is after the span we care about, ignore it.
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001818 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
Chris Lattnere2962be2010-07-29 07:30:00 +00001819 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001820
Chris Lattnere2962be2010-07-29 07:30:00 +00001821 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1822 if (!BitsContainNoUserData(i->getType(), BaseStart,
1823 EndBit-BaseOffset, Context))
1824 return false;
1825 }
1826 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001827
Chris Lattnere2962be2010-07-29 07:30:00 +00001828 // Verify that no field has data that overlaps the region of interest. Yes
1829 // this could be sped up a lot by being smarter about queried fields,
1830 // however we're only looking at structs up to 16 bytes, so we don't care
1831 // much.
1832 unsigned idx = 0;
1833 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1834 i != e; ++i, ++idx) {
1835 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001836
Chris Lattnere2962be2010-07-29 07:30:00 +00001837 // If we found a field after the region we care about, then we're done.
1838 if (FieldOffset >= EndBit) break;
1839
1840 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1841 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1842 Context))
1843 return false;
1844 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001845
Chris Lattnere2962be2010-07-29 07:30:00 +00001846 // If nothing in this record overlapped the area of interest, then we're
1847 // clean.
1848 return true;
1849 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001850
Chris Lattnere2962be2010-07-29 07:30:00 +00001851 return false;
1852}
1853
Chris Lattner0b362002010-07-29 18:39:32 +00001854/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1855/// float member at the specified offset. For example, {int,{float}} has a
1856/// float at offset 4. It is conservatively correct for this routine to return
1857/// false.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001858static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Micah Villmow25a6a842012-10-08 16:25:52 +00001859 const llvm::DataLayout &TD) {
Chris Lattner0b362002010-07-29 18:39:32 +00001860 // Base case if we find a float.
1861 if (IROffset == 0 && IRType->isFloatTy())
1862 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001863
Chris Lattner0b362002010-07-29 18:39:32 +00001864 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001865 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner0b362002010-07-29 18:39:32 +00001866 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1867 unsigned Elt = SL->getElementContainingOffset(IROffset);
1868 IROffset -= SL->getElementOffset(Elt);
1869 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1870 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001871
Chris Lattner0b362002010-07-29 18:39:32 +00001872 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001873 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1874 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner0b362002010-07-29 18:39:32 +00001875 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1876 IROffset -= IROffset/EltSize*EltSize;
1877 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1878 }
1879
1880 return false;
1881}
1882
Chris Lattnerf47c9442010-07-29 18:13:09 +00001883
1884/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1885/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001886llvm::Type *X86_64ABIInfo::
1887GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00001888 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001889 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001890 // pass as float if the last 4 bytes is just padding. This happens for
1891 // structs that contain 3 floats.
1892 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1893 SourceOffset*8+64, getContext()))
1894 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001895
Chris Lattner0b362002010-07-29 18:39:32 +00001896 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1897 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1898 // case.
Micah Villmow25a6a842012-10-08 16:25:52 +00001899 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
1900 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001901 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001902
Chris Lattnerf47c9442010-07-29 18:13:09 +00001903 return llvm::Type::getDoubleTy(getVMContext());
1904}
1905
1906
Chris Lattner0d2656d2010-07-29 17:40:35 +00001907/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1908/// an 8-byte GPR. This means that we either have a scalar or we are talking
1909/// about the high or low part of an up-to-16-byte struct. This routine picks
1910/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001911/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1912/// etc).
1913///
1914/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1915/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1916/// the 8-byte value references. PrefType may be null.
1917///
1918/// SourceTy is the source level type for the entire argument. SourceOffset is
1919/// an offset into this that we're processing (which is always either 0 or 8).
1920///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001921llvm::Type *X86_64ABIInfo::
1922GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00001923 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001924 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1925 // returning an 8-byte unit starting with it. See if we can safely use it.
1926 if (IROffset == 0) {
1927 // Pointers and int64's always fill the 8-byte unit.
Derek Schuffbabaf312012-10-11 15:52:22 +00001928 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
1929 IRType->isIntegerTy(64))
Chris Lattnere2962be2010-07-29 07:30:00 +00001930 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001931
Chris Lattnere2962be2010-07-29 07:30:00 +00001932 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1933 // goodness in the source type is just tail padding. This is allowed to
1934 // kick in for struct {double,int} on the int, but not on
1935 // struct{double,int,int} because we wouldn't return the second int. We
1936 // have to do this analysis on the source type because we can't depend on
1937 // unions being lowered a specific way etc.
1938 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
Derek Schuffbabaf312012-10-11 15:52:22 +00001939 IRType->isIntegerTy(32) ||
1940 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
1941 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
1942 cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001943
Chris Lattnere2962be2010-07-29 07:30:00 +00001944 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1945 SourceOffset*8+64, getContext()))
1946 return IRType;
1947 }
1948 }
Chris Lattner49382de2010-07-28 22:44:07 +00001949
Chris Lattner2acc6e32011-07-18 04:24:23 +00001950 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001951 // If this is a struct, recurse into the field at the specified offset.
Micah Villmow25a6a842012-10-08 16:25:52 +00001952 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001953 if (IROffset < SL->getSizeInBytes()) {
1954 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1955 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001956
Chris Lattner0d2656d2010-07-29 17:40:35 +00001957 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1958 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001959 }
Chris Lattner49382de2010-07-28 22:44:07 +00001960 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001961
Chris Lattner2acc6e32011-07-18 04:24:23 +00001962 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001963 llvm::Type *EltTy = ATy->getElementType();
Micah Villmow25a6a842012-10-08 16:25:52 +00001964 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
Chris Lattner021c3a32010-07-29 07:43:55 +00001965 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00001966 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1967 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00001968 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001969
Chris Lattner49382de2010-07-28 22:44:07 +00001970 // Okay, we don't have any better idea of what to pass, so we pass this in an
1971 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001972 unsigned TySizeInBytes =
1973 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00001974
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001975 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001976
Chris Lattner49382de2010-07-28 22:44:07 +00001977 // It is always safe to classify this as an integer type up to i64 that
1978 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001979 return llvm::IntegerType::get(getVMContext(),
1980 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00001981}
1982
Chris Lattner66e7b682010-09-01 00:50:20 +00001983
1984/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1985/// be used as elements of a two register pair to pass or return, return a
1986/// first class aggregate to represent them. For example, if the low part of
1987/// a by-value argument should be passed as i32* and the high part as float,
1988/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001989static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00001990GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Micah Villmow25a6a842012-10-08 16:25:52 +00001991 const llvm::DataLayout &TD) {
Chris Lattner66e7b682010-09-01 00:50:20 +00001992 // In order to correctly satisfy the ABI, we need to the high part to start
1993 // at offset 8. If the high and low parts we inferred are both 4-byte types
1994 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1995 // the second element at offset 8. Check for this:
1996 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1997 unsigned HiAlign = TD.getABITypeAlignment(Hi);
Micah Villmow25a6a842012-10-08 16:25:52 +00001998 unsigned HiStart = llvm::DataLayout::RoundUpAlignment(LoSize, HiAlign);
Chris Lattner66e7b682010-09-01 00:50:20 +00001999 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002000
Chris Lattner66e7b682010-09-01 00:50:20 +00002001 // To handle this, we have to increase the size of the low part so that the
2002 // second element will start at an 8 byte offset. We can't increase the size
2003 // of the second element because it might make us access off the end of the
2004 // struct.
2005 if (HiStart != 8) {
2006 // There are only two sorts of types the ABI generation code can produce for
2007 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
2008 // Promote these to a larger type.
2009 if (Lo->isFloatTy())
2010 Lo = llvm::Type::getDoubleTy(Lo->getContext());
2011 else {
2012 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
2013 Lo = llvm::Type::getInt64Ty(Lo->getContext());
2014 }
2015 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002016
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002017 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002018
2019
Chris Lattner66e7b682010-09-01 00:50:20 +00002020 // Verify that the second element is at an 8-byte offset.
2021 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
2022 "Invalid x86-64 argument pair!");
2023 return Result;
2024}
2025
Chris Lattner519f68c2010-07-28 23:06:14 +00002026ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00002027classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00002028 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
2029 // classification algorithm.
2030 X86_64ABIInfo::Class Lo, Hi;
2031 classify(RetTy, 0, Lo, Hi);
2032
2033 // Check some invariants.
2034 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002035 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2036
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002037 llvm::Type *ResType = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002038 switch (Lo) {
2039 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002040 if (Hi == NoClass)
2041 return ABIArgInfo::getIgnore();
2042 // If the low part is just padding, it takes no register, leave ResType
2043 // null.
2044 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2045 "Unknown missing lo part");
2046 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002047
2048 case SSEUp:
2049 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002050 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002051
2052 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
2053 // hidden argument.
2054 case Memory:
2055 return getIndirectReturnResult(RetTy);
2056
2057 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
2058 // available register of the sequence %rax, %rdx is used.
2059 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002060 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002061
Chris Lattnereb518b42010-07-29 21:42:50 +00002062 // If we have a sign or zero extended integer, make sure to return Extend
2063 // so that the parameter gets the right LLVM IR attributes.
2064 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2065 // Treat an enum type as its underlying type.
2066 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2067 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002068
Chris Lattnereb518b42010-07-29 21:42:50 +00002069 if (RetTy->isIntegralOrEnumerationType() &&
2070 RetTy->isPromotableIntegerType())
2071 return ABIArgInfo::getExtend();
2072 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002073 break;
2074
2075 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
2076 // available SSE register of the sequence %xmm0, %xmm1 is used.
2077 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002078 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00002079 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002080
2081 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
2082 // returned on the X87 stack in %st0 as 80-bit x87 number.
2083 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00002084 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00002085 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002086
2087 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
2088 // part of the value is returned in %st0 and the imaginary part in
2089 // %st1.
2090 case ComplexX87:
2091 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00002092 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00002093 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00002094 NULL);
2095 break;
2096 }
2097
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002098 llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002099 switch (Hi) {
2100 // Memory was handled previously and X87 should
2101 // never occur as a hi class.
2102 case Memory:
2103 case X87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002104 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002105
2106 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00002107 case NoClass:
2108 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002109
Chris Lattner3db4dde2010-09-01 00:20:33 +00002110 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002111 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002112 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2113 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002114 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00002115 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002116 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002117 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2118 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002119 break;
2120
2121 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002122 // is passed in the next available eightbyte chunk if the last used
2123 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00002124 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002125 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00002126 case SSEUp:
2127 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002128 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00002129 break;
2130
2131 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
2132 // returned together with the previous X87 value in %st0.
2133 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002134 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00002135 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002136 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00002137 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00002138 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002139 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002140 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2141 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00002142 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002143 break;
2144 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002145
Chris Lattner3db4dde2010-09-01 00:20:33 +00002146 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00002147 // known to pass in the high eightbyte of the result. We do this by forming a
2148 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00002149 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002150 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Chris Lattner519f68c2010-07-28 23:06:14 +00002151
Chris Lattnereb518b42010-07-29 21:42:50 +00002152 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00002153}
2154
Daniel Dunbaredfac032012-03-10 01:03:58 +00002155ABIArgInfo X86_64ABIInfo::classifyArgumentType(
2156 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE)
2157 const
2158{
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002159 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00002160 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002161
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002162 // Check some invariants.
2163 // FIXME: Enforce these by construction.
2164 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002165 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2166
2167 neededInt = 0;
2168 neededSSE = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002169 llvm::Type *ResType = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002170 switch (Lo) {
2171 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002172 if (Hi == NoClass)
2173 return ABIArgInfo::getIgnore();
2174 // If the low part is just padding, it takes no register, leave ResType
2175 // null.
2176 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2177 "Unknown missing lo part");
2178 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002179
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002180 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
2181 // on the stack.
2182 case Memory:
2183
2184 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
2185 // COMPLEX_X87, it is passed in memory.
2186 case X87:
2187 case ComplexX87:
Eli Friedmanded137f2011-06-29 07:04:55 +00002188 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2189 ++neededInt;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002190 return getIndirectResult(Ty, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002191
2192 case SSEUp:
2193 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002194 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002195
2196 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
2197 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
2198 // and %r9 is used.
2199 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00002200 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002201
Chris Lattner49382de2010-07-28 22:44:07 +00002202 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002203 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00002204
2205 // If we have a sign or zero extended integer, make sure to return Extend
2206 // so that the parameter gets the right LLVM IR attributes.
2207 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2208 // Treat an enum type as its underlying type.
2209 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2210 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002211
Chris Lattnereb518b42010-07-29 21:42:50 +00002212 if (Ty->isIntegralOrEnumerationType() &&
2213 Ty->isPromotableIntegerType())
2214 return ABIArgInfo::getExtend();
2215 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002216
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002217 break;
2218
2219 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
2220 // available SSE register is used, the registers are taken in the
2221 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00002222 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002223 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00002224 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00002225 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002226 break;
2227 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00002228 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002229
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002230 llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002231 switch (Hi) {
2232 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002233 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002234 // which is passed in memory.
2235 case Memory:
2236 case X87:
2237 case ComplexX87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002238 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002239
2240 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002241
Chris Lattner645406a2010-09-01 00:24:35 +00002242 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002243 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00002244 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002245 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002246
Chris Lattner645406a2010-09-01 00:24:35 +00002247 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2248 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002249 break;
2250
2251 // X87Up generally doesn't occur here (long double is passed in
2252 // memory), except in situations involving unions.
2253 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00002254 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002255 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002256
Chris Lattner645406a2010-09-01 00:24:35 +00002257 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2258 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00002259
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002260 ++neededSSE;
2261 break;
2262
2263 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
2264 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002265 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002266 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00002267 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002268 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002269 break;
2270 }
2271
Chris Lattner645406a2010-09-01 00:24:35 +00002272 // If a high part was specified, merge it together with the low part. It is
2273 // known to pass in the high eightbyte of the result. We do this by forming a
2274 // first class struct aggregate with the high and low part: {low, high}
2275 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002276 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002277
Chris Lattnereb518b42010-07-29 21:42:50 +00002278 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002279}
2280
Chris Lattneree5dcd02010-07-29 02:31:05 +00002281void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002282
Chris Lattnera3c109b2010-07-29 02:16:43 +00002283 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002284
2285 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00002286 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002287
2288 // If the return value is indirect, then the hidden argument is consuming one
2289 // integer register.
2290 if (FI.getReturnInfo().isIndirect())
2291 --freeIntRegs;
2292
2293 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2294 // get assigned (in left-to-right order) for passing as follows...
2295 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2296 it != ie; ++it) {
Bill Wendling99aaae82010-10-18 23:51:38 +00002297 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002298 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
2299 neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002300
2301 // AMD64-ABI 3.2.3p3: If there are no registers available for any
2302 // eightbyte of an argument, the whole argument is passed on the
2303 // stack. If registers have already been assigned for some
2304 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00002305 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002306 freeIntRegs -= neededInt;
2307 freeSSERegs -= neededSSE;
2308 } else {
Daniel Dunbaredfac032012-03-10 01:03:58 +00002309 it->info = getIndirectResult(it->type, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002310 }
2311 }
2312}
2313
2314static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2315 QualType Ty,
2316 CodeGenFunction &CGF) {
2317 llvm::Value *overflow_arg_area_p =
2318 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2319 llvm::Value *overflow_arg_area =
2320 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2321
2322 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2323 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedman8d2fe422011-11-18 02:44:19 +00002324 // It isn't stated explicitly in the standard, but in practice we use
2325 // alignment greater than 16 where necessary.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002326 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2327 if (Align > 8) {
Eli Friedman8d2fe422011-11-18 02:44:19 +00002328 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
Owen Anderson0032b272009-08-13 21:57:51 +00002329 llvm::Value *Offset =
Eli Friedman8d2fe422011-11-18 02:44:19 +00002330 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002331 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2332 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00002333 CGF.Int64Ty);
Eli Friedman8d2fe422011-11-18 02:44:19 +00002334 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002335 overflow_arg_area =
2336 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2337 overflow_arg_area->getType(),
2338 "overflow_arg_area.align");
2339 }
2340
2341 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002342 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002343 llvm::Value *Res =
2344 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002345 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002346
2347 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2348 // l->overflow_arg_area + sizeof(type).
2349 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2350 // an 8 byte boundary.
2351
2352 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00002353 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00002354 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002355 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2356 "overflow_arg_area.next");
2357 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2358
2359 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2360 return Res;
2361}
2362
2363llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2364 CodeGenFunction &CGF) const {
2365 // Assume that va_list type is correct; should be pointer to LLVM type:
2366 // struct {
2367 // i32 gp_offset;
2368 // i32 fp_offset;
2369 // i8* overflow_arg_area;
2370 // i8* reg_save_area;
2371 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00002372 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002373
Chris Lattnera14db752010-03-11 18:19:55 +00002374 Ty = CGF.getContext().getCanonicalType(Ty);
Daniel Dunbaredfac032012-03-10 01:03:58 +00002375 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002376
2377 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2378 // in the registers. If not go to step 7.
2379 if (!neededInt && !neededSSE)
2380 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2381
2382 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2383 // general purpose registers needed to pass type and num_fp to hold
2384 // the number of floating point registers needed.
2385
2386 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2387 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2388 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2389 //
2390 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2391 // register save space).
2392
2393 llvm::Value *InRegs = 0;
2394 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2395 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2396 if (neededInt) {
2397 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2398 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002399 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2400 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002401 }
2402
2403 if (neededSSE) {
2404 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2405 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2406 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00002407 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2408 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002409 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2410 }
2411
2412 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2413 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2414 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2415 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2416
2417 // Emit code to load the value if it was passed in registers.
2418
2419 CGF.EmitBlock(InRegBlock);
2420
2421 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2422 // an offset of l->gp_offset and/or l->fp_offset. This may require
2423 // copying to a temporary location in case the parameter is passed
2424 // in different register classes or requires an alignment greater
2425 // than 8 for general purpose registers and 16 for XMM registers.
2426 //
2427 // FIXME: This really results in shameful code when we end up needing to
2428 // collect arguments from different places; often what should result in a
2429 // simple assembling of a structure from scattered addresses has many more
2430 // loads than necessary. Can we clean this up?
Chris Lattner2acc6e32011-07-18 04:24:23 +00002431 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002432 llvm::Value *RegAddr =
2433 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2434 "reg_save_area");
2435 if (neededInt && neededSSE) {
2436 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002437 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002438 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002439 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2440 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002441 llvm::Type *TyLo = ST->getElementType(0);
2442 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002443 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002444 "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002445 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2446 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002447 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2448 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002449 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2450 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002451 llvm::Value *V =
2452 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2453 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2454 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2455 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2456
Owen Andersona1cf15f2009-07-14 23:10:40 +00002457 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002458 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002459 } else if (neededInt) {
2460 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2461 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002462 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002463 } else if (neededSSE == 1) {
2464 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2465 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2466 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002467 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002468 assert(neededSSE == 2 && "Invalid number of needed registers!");
2469 // SSE registers are spaced 16 bytes apart in the register save
2470 // area, we need to collect the two eightbytes together.
2471 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002472 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner8b418682012-02-07 00:39:47 +00002473 llvm::Type *DoubleTy = CGF.DoubleTy;
Chris Lattner2acc6e32011-07-18 04:24:23 +00002474 llvm::Type *DblPtrTy =
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002475 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002476 llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002477 DoubleTy, NULL);
2478 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2479 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2480 DblPtrTy));
2481 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2482 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2483 DblPtrTy));
2484 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2485 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2486 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002487 }
2488
2489 // AMD64-ABI 3.5.7p5: Step 5. Set:
2490 // l->gp_offset = l->gp_offset + num_gp * 8
2491 // l->fp_offset = l->fp_offset + num_fp * 16.
2492 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002493 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002494 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2495 gp_offset_p);
2496 }
2497 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002498 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002499 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2500 fp_offset_p);
2501 }
2502 CGF.EmitBranch(ContBlock);
2503
2504 // Emit code to load the value if it was passed in memory.
2505
2506 CGF.EmitBlock(InMemBlock);
2507 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2508
2509 // Return the appropriate result.
2510
2511 CGF.EmitBlock(ContBlock);
Jay Foadbbf3bac2011-03-30 11:28:58 +00002512 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002513 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002514 ResAddr->addIncoming(RegAddr, InRegBlock);
2515 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002516 return ResAddr;
2517}
2518
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002519ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2520
2521 if (Ty->isVoidType())
2522 return ABIArgInfo::getIgnore();
2523
2524 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2525 Ty = EnumTy->getDecl()->getIntegerType();
2526
2527 uint64_t Size = getContext().getTypeSize(Ty);
2528
2529 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002530 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2531 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002532 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2533
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002534 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2535 if (Size == 128 &&
Eli Friedman55fc7e22012-01-25 22:46:34 +00002536 getContext().getTargetInfo().getTriple().getOS()
2537 == llvm::Triple::MinGW32)
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002538 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2539 Size));
2540
2541 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2542 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2543 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002544 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002545 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2546 Size));
2547
2548 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2549 }
2550
2551 if (Ty->isPromotableIntegerType())
2552 return ABIArgInfo::getExtend();
2553
2554 return ABIArgInfo::getDirect();
2555}
2556
2557void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2558
2559 QualType RetTy = FI.getReturnType();
2560 FI.getReturnInfo() = classify(RetTy);
2561
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002562 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2563 it != ie; ++it)
2564 it->info = classify(it->type);
2565}
2566
Chris Lattnerf13721d2010-08-31 16:44:54 +00002567llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2568 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002569 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002570
Chris Lattnerf13721d2010-08-31 16:44:54 +00002571 CGBuilderTy &Builder = CGF.Builder;
2572 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2573 "ap");
2574 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2575 llvm::Type *PTy =
2576 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2577 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2578
2579 uint64_t Offset =
2580 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2581 llvm::Value *NextAddr =
2582 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2583 "ap.next");
2584 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2585
2586 return AddrTyped;
2587}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002588
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00002589namespace {
2590
Derek Schuff263366f2012-10-16 22:30:41 +00002591class NaClX86_64ABIInfo : public ABIInfo {
2592 public:
2593 NaClX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2594 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, HasAVX) {}
2595 virtual void computeInfo(CGFunctionInfo &FI) const;
2596 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2597 CodeGenFunction &CGF) const;
2598 private:
2599 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
2600 X86_64ABIInfo NInfo; // Used for everything else.
2601};
2602
2603class NaClX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2604 public:
2605 NaClX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2606 : TargetCodeGenInfo(new NaClX86_64ABIInfo(CGT, HasAVX)) {}
2607};
2608
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00002609}
2610
Derek Schuff263366f2012-10-16 22:30:41 +00002611void NaClX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2612 if (FI.getASTCallingConvention() == CC_PnaclCall)
2613 PInfo.computeInfo(FI);
2614 else
2615 NInfo.computeInfo(FI);
2616}
2617
2618llvm::Value *NaClX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2619 CodeGenFunction &CGF) const {
2620 // Always use the native convention; calling pnacl-style varargs functions
2621 // is unuspported.
2622 return NInfo.EmitVAArg(VAListAddr, Ty, CGF);
2623}
2624
2625
John McCallec853ba2010-03-11 00:10:12 +00002626// PowerPC-32
2627
2628namespace {
2629class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2630public:
Chris Lattnerea044322010-07-29 02:01:43 +00002631 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002632
John McCallec853ba2010-03-11 00:10:12 +00002633 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2634 // This is recovered from gcc output.
2635 return 1; // r1 is the dedicated stack pointer
2636 }
2637
2638 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002639 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002640};
2641
2642}
2643
2644bool
2645PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2646 llvm::Value *Address) const {
2647 // This is calculated from the LLVM and GCC tables and verified
2648 // against gcc output. AFAIK all ABIs use the same encoding.
2649
2650 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallec853ba2010-03-11 00:10:12 +00002651
Chris Lattner8b418682012-02-07 00:39:47 +00002652 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallec853ba2010-03-11 00:10:12 +00002653 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2654 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2655 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2656
2657 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002658 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002659
2660 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002661 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002662
2663 // 64-76 are various 4-byte special-purpose registers:
2664 // 64: mq
2665 // 65: lr
2666 // 66: ctr
2667 // 67: ap
2668 // 68-75 cr0-7
2669 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002670 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002671
2672 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002673 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002674
2675 // 109: vrsave
2676 // 110: vscr
2677 // 111: spe_acc
2678 // 112: spefscr
2679 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002680 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002681
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002682 return false;
John McCallec853ba2010-03-11 00:10:12 +00002683}
2684
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002685// PowerPC-64
2686
2687namespace {
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002688/// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
2689class PPC64_SVR4_ABIInfo : public DefaultABIInfo {
2690
2691public:
2692 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
2693
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002694 bool isPromotableTypeForABI(QualType Ty) const;
2695
2696 ABIArgInfo classifyReturnType(QualType RetTy) const;
2697 ABIArgInfo classifyArgumentType(QualType Ty) const;
2698
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00002699 // TODO: We can add more logic to computeInfo to improve performance.
2700 // Example: For aggregate arguments that fit in a register, we could
2701 // use getDirectInReg (as is done below for structs containing a single
2702 // floating-point value) to avoid pushing them to memory on function
2703 // entry. This would require changing the logic in PPCISelLowering
2704 // when lowering the parameters in the caller and args in the callee.
2705 virtual void computeInfo(CGFunctionInfo &FI) const {
2706 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2707 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2708 it != ie; ++it) {
2709 // We rely on the default argument classification for the most part.
2710 // One exception: An aggregate containing a single floating-point
2711 // item must be passed in a register if one is available.
2712 const Type *T = isSingleElementStruct(it->type, getContext());
2713 if (T) {
2714 const BuiltinType *BT = T->getAs<BuiltinType>();
2715 if (BT && BT->isFloatingPoint()) {
2716 QualType QT(T, 0);
2717 it->info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
2718 continue;
2719 }
2720 }
2721 it->info = classifyArgumentType(it->type);
2722 }
2723 }
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002724
2725 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr,
2726 QualType Ty,
2727 CodeGenFunction &CGF) const;
2728};
2729
2730class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
2731public:
2732 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT)
2733 : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT)) {}
2734
2735 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2736 // This is recovered from gcc output.
2737 return 1; // r1 is the dedicated stack pointer
2738 }
2739
2740 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2741 llvm::Value *Address) const;
2742};
2743
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002744class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2745public:
2746 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
2747
2748 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2749 // This is recovered from gcc output.
2750 return 1; // r1 is the dedicated stack pointer
2751 }
2752
2753 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2754 llvm::Value *Address) const;
2755};
2756
2757}
2758
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002759// Return true if the ABI requires Ty to be passed sign- or zero-
2760// extended to 64 bits.
2761bool
2762PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
2763 // Treat an enum type as its underlying type.
2764 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2765 Ty = EnumTy->getDecl()->getIntegerType();
2766
2767 // Promotable integer types are required to be promoted by the ABI.
2768 if (Ty->isPromotableIntegerType())
2769 return true;
2770
2771 // In addition to the usual promotable integer types, we also need to
2772 // extend all 32-bit types, since the ABI requires promotion to 64 bits.
2773 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2774 switch (BT->getKind()) {
2775 case BuiltinType::Int:
2776 case BuiltinType::UInt:
2777 return true;
2778 default:
2779 break;
2780 }
2781
2782 return false;
2783}
2784
2785ABIArgInfo
2786PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
Bill Schmidtc9715fc2012-11-27 02:46:43 +00002787 if (Ty->isAnyComplexType())
2788 return ABIArgInfo::getDirect();
2789
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002790 if (isAggregateTypeForABI(Ty)) {
2791 // Records with non trivial destructors/constructors should not be passed
2792 // by value.
2793 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2794 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2795
2796 return ABIArgInfo::getIndirect(0);
2797 }
2798
2799 return (isPromotableTypeForABI(Ty) ?
2800 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2801}
2802
2803ABIArgInfo
2804PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
2805 if (RetTy->isVoidType())
2806 return ABIArgInfo::getIgnore();
2807
Bill Schmidt9e6111a2012-12-17 04:20:17 +00002808 if (RetTy->isAnyComplexType())
2809 return ABIArgInfo::getDirect();
2810
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002811 if (isAggregateTypeForABI(RetTy))
2812 return ABIArgInfo::getIndirect(0);
2813
2814 return (isPromotableTypeForABI(RetTy) ?
2815 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2816}
2817
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002818// Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
2819llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr,
2820 QualType Ty,
2821 CodeGenFunction &CGF) const {
2822 llvm::Type *BP = CGF.Int8PtrTy;
2823 llvm::Type *BPP = CGF.Int8PtrPtrTy;
2824
2825 CGBuilderTy &Builder = CGF.Builder;
2826 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
2827 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2828
Bill Schmidt19f8e852013-01-14 17:45:36 +00002829 // Update the va_list pointer. The pointer should be bumped by the
2830 // size of the object. We can trust getTypeSize() except for a complex
2831 // type whose base type is smaller than a doubleword. For these, the
2832 // size of the object is 16 bytes; see below for further explanation.
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002833 unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8;
Bill Schmidt19f8e852013-01-14 17:45:36 +00002834 QualType BaseTy;
2835 unsigned CplxBaseSize = 0;
2836
2837 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
2838 BaseTy = CTy->getElementType();
2839 CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8;
2840 if (CplxBaseSize < 8)
2841 SizeInBytes = 16;
2842 }
2843
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002844 unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8);
2845 llvm::Value *NextAddr =
2846 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset),
2847 "ap.next");
2848 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2849
Bill Schmidt19f8e852013-01-14 17:45:36 +00002850 // If we have a complex type and the base type is smaller than 8 bytes,
2851 // the ABI calls for the real and imaginary parts to be right-adjusted
2852 // in separate doublewords. However, Clang expects us to produce a
2853 // pointer to a structure with the two parts packed tightly. So generate
2854 // loads of the real and imaginary parts relative to the va_list pointer,
2855 // and store them to a temporary structure.
2856 if (CplxBaseSize && CplxBaseSize < 8) {
2857 llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2858 llvm::Value *ImagAddr = RealAddr;
2859 RealAddr = Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize));
2860 ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize));
2861 llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy));
2862 RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy);
2863 ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy);
2864 llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal");
2865 llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag");
2866 llvm::Value *Ptr = CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty),
2867 "vacplx");
2868 llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, ".real");
2869 llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, ".imag");
2870 Builder.CreateStore(Real, RealPtr, false);
2871 Builder.CreateStore(Imag, ImagPtr, false);
2872 return Ptr;
2873 }
2874
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002875 // If the argument is smaller than 8 bytes, it is right-adjusted in
2876 // its doubleword slot. Adjust the pointer to pick it up from the
2877 // correct offset.
2878 if (SizeInBytes < 8) {
2879 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2880 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes));
2881 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2882 }
2883
2884 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2885 return Builder.CreateBitCast(Addr, PTy);
2886}
2887
2888static bool
2889PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2890 llvm::Value *Address) {
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002891 // This is calculated from the LLVM and GCC tables and verified
2892 // against gcc output. AFAIK all ABIs use the same encoding.
2893
2894 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2895
2896 llvm::IntegerType *i8 = CGF.Int8Ty;
2897 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2898 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2899 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2900
2901 // 0-31: r0-31, the 8-byte general-purpose registers
2902 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
2903
2904 // 32-63: fp0-31, the 8-byte floating-point registers
2905 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
2906
2907 // 64-76 are various 4-byte special-purpose registers:
2908 // 64: mq
2909 // 65: lr
2910 // 66: ctr
2911 // 67: ap
2912 // 68-75 cr0-7
2913 // 76: xer
2914 AssignToArrayRange(Builder, Address, Four8, 64, 76);
2915
2916 // 77-108: v0-31, the 16-byte vector registers
2917 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
2918
2919 // 109: vrsave
2920 // 110: vscr
2921 // 111: spe_acc
2922 // 112: spefscr
2923 // 113: sfp
2924 AssignToArrayRange(Builder, Address, Four8, 109, 113);
2925
2926 return false;
2927}
John McCallec853ba2010-03-11 00:10:12 +00002928
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002929bool
2930PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
2931 CodeGen::CodeGenFunction &CGF,
2932 llvm::Value *Address) const {
2933
2934 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
2935}
2936
2937bool
2938PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2939 llvm::Value *Address) const {
2940
2941 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
2942}
2943
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002944//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002945// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002946//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002947
2948namespace {
2949
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002950class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002951public:
2952 enum ABIKind {
2953 APCS = 0,
2954 AAPCS = 1,
2955 AAPCS_VFP
2956 };
2957
2958private:
2959 ABIKind Kind;
2960
2961public:
John McCallbd7370a2013-02-28 19:01:20 +00002962 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {
2963 setRuntimeCC();
2964 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002965
John McCall49e34be2011-08-30 01:42:09 +00002966 bool isEABI() const {
Eli Friedman55fc7e22012-01-25 22:46:34 +00002967 StringRef Env =
2968 getContext().getTargetInfo().getTriple().getEnvironmentName();
Logan Chien94a71422012-09-02 09:30:11 +00002969 return (Env == "gnueabi" || Env == "eabi" ||
2970 Env == "android" || Env == "androideabi");
John McCall49e34be2011-08-30 01:42:09 +00002971 }
2972
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002973private:
2974 ABIKind getABIKind() const { return Kind; }
2975
Chris Lattnera3c109b2010-07-29 02:16:43 +00002976 ABIArgInfo classifyReturnType(QualType RetTy) const;
Manman Ren710c5172012-10-31 19:02:26 +00002977 ABIArgInfo classifyArgumentType(QualType RetTy, int *VFPRegs,
2978 unsigned &AllocatedVFP,
Manman Renb3fa55f2012-10-30 23:21:41 +00002979 bool &IsHA) const;
Manman Ren97f81572012-10-16 19:18:39 +00002980 bool isIllegalVectorType(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002981
Chris Lattneree5dcd02010-07-29 02:31:05 +00002982 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002983
2984 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2985 CodeGenFunction &CGF) const;
John McCallbd7370a2013-02-28 19:01:20 +00002986
2987 llvm::CallingConv::ID getLLVMDefaultCC() const;
2988 llvm::CallingConv::ID getABIDefaultCC() const;
2989 void setRuntimeCC();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002990};
2991
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002992class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2993public:
Chris Lattnerea044322010-07-29 02:01:43 +00002994 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2995 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00002996
John McCall49e34be2011-08-30 01:42:09 +00002997 const ARMABIInfo &getABIInfo() const {
2998 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
2999 }
3000
John McCall6374c332010-03-06 00:35:14 +00003001 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3002 return 13;
3003 }
Roman Divacky09345d12011-05-18 19:36:54 +00003004
Chris Lattner5f9e2722011-07-23 10:55:15 +00003005 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCallf85e1932011-06-15 23:02:42 +00003006 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
3007 }
3008
Roman Divacky09345d12011-05-18 19:36:54 +00003009 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3010 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003011 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divacky09345d12011-05-18 19:36:54 +00003012
3013 // 0-15 are the 16 integer registers.
Chris Lattner8b418682012-02-07 00:39:47 +00003014 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divacky09345d12011-05-18 19:36:54 +00003015 return false;
3016 }
John McCall49e34be2011-08-30 01:42:09 +00003017
3018 unsigned getSizeOfUnwindException() const {
3019 if (getABIInfo().isEABI()) return 88;
3020 return TargetCodeGenInfo::getSizeOfUnwindException();
3021 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003022};
3023
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003024}
3025
Chris Lattneree5dcd02010-07-29 02:31:05 +00003026void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Manman Renb3fa55f2012-10-30 23:21:41 +00003027 // To correctly handle Homogeneous Aggregate, we need to keep track of the
Manman Ren710c5172012-10-31 19:02:26 +00003028 // VFP registers allocated so far.
Manman Renb3fa55f2012-10-30 23:21:41 +00003029 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
3030 // VFP registers of the appropriate type unallocated then the argument is
3031 // allocated to the lowest-numbered sequence of such registers.
3032 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
3033 // unallocated are marked as unavailable.
3034 unsigned AllocatedVFP = 0;
Manman Ren710c5172012-10-31 19:02:26 +00003035 int VFPRegs[16] = { 0 };
Chris Lattnera3c109b2010-07-29 02:16:43 +00003036 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003037 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Manman Renb3fa55f2012-10-30 23:21:41 +00003038 it != ie; ++it) {
3039 unsigned PreAllocation = AllocatedVFP;
3040 bool IsHA = false;
3041 // 6.1.2.3 There is one VFP co-processor register class using registers
3042 // s0-s15 (d0-d7) for passing arguments.
3043 const unsigned NumVFPs = 16;
Manman Ren710c5172012-10-31 19:02:26 +00003044 it->info = classifyArgumentType(it->type, VFPRegs, AllocatedVFP, IsHA);
Manman Renb3fa55f2012-10-30 23:21:41 +00003045 // If we do not have enough VFP registers for the HA, any VFP registers
3046 // that are unallocated are marked as unavailable. To achieve this, we add
3047 // padding of (NumVFPs - PreAllocation) floats.
3048 if (IsHA && AllocatedVFP > NumVFPs && PreAllocation < NumVFPs) {
3049 llvm::Type *PaddingTy = llvm::ArrayType::get(
3050 llvm::Type::getFloatTy(getVMContext()), NumVFPs - PreAllocation);
3051 it->info = ABIArgInfo::getExpandWithPadding(false, PaddingTy);
3052 }
3053 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003054
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003055 // Always honor user-specified calling convention.
3056 if (FI.getCallingConvention() != llvm::CallingConv::C)
3057 return;
3058
John McCallbd7370a2013-02-28 19:01:20 +00003059 llvm::CallingConv::ID cc = getRuntimeCC();
3060 if (cc != llvm::CallingConv::C)
3061 FI.setEffectiveCallingConvention(cc);
3062}
Rafael Espindola25117ab2010-06-16 16:13:39 +00003063
John McCallbd7370a2013-02-28 19:01:20 +00003064/// Return the default calling convention that LLVM will use.
3065llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
3066 // The default calling convention that LLVM will infer.
3067 if (getContext().getTargetInfo().getTriple().getEnvironmentName()=="gnueabihf")
3068 return llvm::CallingConv::ARM_AAPCS_VFP;
3069 else if (isEABI())
3070 return llvm::CallingConv::ARM_AAPCS;
3071 else
3072 return llvm::CallingConv::ARM_APCS;
3073}
3074
3075/// Return the calling convention that our ABI would like us to use
3076/// as the C calling convention.
3077llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003078 switch (getABIKind()) {
John McCallbd7370a2013-02-28 19:01:20 +00003079 case APCS: return llvm::CallingConv::ARM_APCS;
3080 case AAPCS: return llvm::CallingConv::ARM_AAPCS;
3081 case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003082 }
John McCallbd7370a2013-02-28 19:01:20 +00003083 llvm_unreachable("bad ABI kind");
3084}
3085
3086void ARMABIInfo::setRuntimeCC() {
3087 assert(getRuntimeCC() == llvm::CallingConv::C);
3088
3089 // Don't muddy up the IR with a ton of explicit annotations if
3090 // they'd just match what LLVM will infer from the triple.
3091 llvm::CallingConv::ID abiCC = getABIDefaultCC();
3092 if (abiCC != getLLVMDefaultCC())
3093 RuntimeCC = abiCC;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003094}
3095
Bob Wilson194f06a2011-08-03 05:58:22 +00003096/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
3097/// aggregate. If HAMembers is non-null, the number of base elements
3098/// contained in the type is returned through it; this is used for the
3099/// recursive calls that check aggregate component types.
3100static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
3101 ASTContext &Context,
3102 uint64_t *HAMembers = 0) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003103 uint64_t Members = 0;
Bob Wilson194f06a2011-08-03 05:58:22 +00003104 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
3105 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
3106 return false;
3107 Members *= AT->getSize().getZExtValue();
3108 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
3109 const RecordDecl *RD = RT->getDecl();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003110 if (RD->hasFlexibleArrayMember())
Bob Wilson194f06a2011-08-03 05:58:22 +00003111 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003112
Bob Wilson194f06a2011-08-03 05:58:22 +00003113 Members = 0;
3114 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3115 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00003116 const FieldDecl *FD = *i;
Bob Wilson194f06a2011-08-03 05:58:22 +00003117 uint64_t FldMembers;
3118 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
3119 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003120
3121 Members = (RD->isUnion() ?
3122 std::max(Members, FldMembers) : Members + FldMembers);
Bob Wilson194f06a2011-08-03 05:58:22 +00003123 }
3124 } else {
3125 Members = 1;
3126 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
3127 Members = 2;
3128 Ty = CT->getElementType();
3129 }
3130
3131 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
3132 // double, or 64-bit or 128-bit vectors.
3133 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3134 if (BT->getKind() != BuiltinType::Float &&
Tim Northoveradfa45f2012-07-20 22:29:29 +00003135 BT->getKind() != BuiltinType::Double &&
3136 BT->getKind() != BuiltinType::LongDouble)
Bob Wilson194f06a2011-08-03 05:58:22 +00003137 return false;
3138 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
3139 unsigned VecSize = Context.getTypeSize(VT);
3140 if (VecSize != 64 && VecSize != 128)
3141 return false;
3142 } else {
3143 return false;
3144 }
3145
3146 // The base type must be the same for all members. Vector types of the
3147 // same total size are treated as being equivalent here.
3148 const Type *TyPtr = Ty.getTypePtr();
3149 if (!Base)
3150 Base = TyPtr;
3151 if (Base != TyPtr &&
3152 (!Base->isVectorType() || !TyPtr->isVectorType() ||
3153 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
3154 return false;
3155 }
3156
3157 // Homogeneous Aggregates can have at most 4 members of the base type.
3158 if (HAMembers)
3159 *HAMembers = Members;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003160
3161 return (Members > 0 && Members <= 4);
Bob Wilson194f06a2011-08-03 05:58:22 +00003162}
3163
Manman Ren710c5172012-10-31 19:02:26 +00003164/// markAllocatedVFPs - update VFPRegs according to the alignment and
3165/// number of VFP registers (unit is S register) requested.
3166static void markAllocatedVFPs(int *VFPRegs, unsigned &AllocatedVFP,
3167 unsigned Alignment,
3168 unsigned NumRequired) {
3169 // Early Exit.
3170 if (AllocatedVFP >= 16)
3171 return;
3172 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
3173 // VFP registers of the appropriate type unallocated then the argument is
3174 // allocated to the lowest-numbered sequence of such registers.
3175 for (unsigned I = 0; I < 16; I += Alignment) {
3176 bool FoundSlot = true;
3177 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3178 if (J >= 16 || VFPRegs[J]) {
3179 FoundSlot = false;
3180 break;
3181 }
3182 if (FoundSlot) {
3183 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3184 VFPRegs[J] = 1;
3185 AllocatedVFP += NumRequired;
3186 return;
3187 }
3188 }
3189 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
3190 // unallocated are marked as unavailable.
3191 for (unsigned I = 0; I < 16; I++)
3192 VFPRegs[I] = 1;
3193 AllocatedVFP = 17; // We do not have enough VFP registers.
3194}
3195
3196ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, int *VFPRegs,
3197 unsigned &AllocatedVFP,
Manman Renb3fa55f2012-10-30 23:21:41 +00003198 bool &IsHA) const {
3199 // We update number of allocated VFPs according to
3200 // 6.1.2.1 The following argument types are VFP CPRCs:
3201 // A single-precision floating-point type (including promoted
3202 // half-precision types); A double-precision floating-point type;
3203 // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
3204 // with a Base Type of a single- or double-precision floating-point type,
3205 // 64-bit containerized vectors or 128-bit containerized vectors with one
3206 // to four Elements.
3207
Manman Ren97f81572012-10-16 19:18:39 +00003208 // Handle illegal vector types here.
3209 if (isIllegalVectorType(Ty)) {
3210 uint64_t Size = getContext().getTypeSize(Ty);
3211 if (Size <= 32) {
3212 llvm::Type *ResType =
3213 llvm::Type::getInt32Ty(getVMContext());
3214 return ABIArgInfo::getDirect(ResType);
3215 }
3216 if (Size == 64) {
3217 llvm::Type *ResType = llvm::VectorType::get(
3218 llvm::Type::getInt32Ty(getVMContext()), 2);
Manman Ren710c5172012-10-31 19:02:26 +00003219 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
Manman Ren97f81572012-10-16 19:18:39 +00003220 return ABIArgInfo::getDirect(ResType);
3221 }
3222 if (Size == 128) {
3223 llvm::Type *ResType = llvm::VectorType::get(
3224 llvm::Type::getInt32Ty(getVMContext()), 4);
Manman Ren710c5172012-10-31 19:02:26 +00003225 markAllocatedVFPs(VFPRegs, AllocatedVFP, 4, 4);
Manman Ren97f81572012-10-16 19:18:39 +00003226 return ABIArgInfo::getDirect(ResType);
3227 }
3228 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3229 }
Manman Ren710c5172012-10-31 19:02:26 +00003230 // Update VFPRegs for legal vector types.
Manman Renb3fa55f2012-10-30 23:21:41 +00003231 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3232 uint64_t Size = getContext().getTypeSize(VT);
3233 // Size of a legal vector should be power of 2 and above 64.
Manman Ren710c5172012-10-31 19:02:26 +00003234 markAllocatedVFPs(VFPRegs, AllocatedVFP, Size >= 128 ? 4 : 2, Size / 32);
Manman Renb3fa55f2012-10-30 23:21:41 +00003235 }
Manman Ren710c5172012-10-31 19:02:26 +00003236 // Update VFPRegs for floating point types.
Manman Renb3fa55f2012-10-30 23:21:41 +00003237 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3238 if (BT->getKind() == BuiltinType::Half ||
3239 BT->getKind() == BuiltinType::Float)
Manman Ren710c5172012-10-31 19:02:26 +00003240 markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, 1);
Manman Renb3fa55f2012-10-30 23:21:41 +00003241 if (BT->getKind() == BuiltinType::Double ||
Manman Ren710c5172012-10-31 19:02:26 +00003242 BT->getKind() == BuiltinType::LongDouble)
3243 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
Manman Renb3fa55f2012-10-30 23:21:41 +00003244 }
Manman Ren97f81572012-10-16 19:18:39 +00003245
John McCalld608cdb2010-08-22 10:59:02 +00003246 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003247 // Treat an enum type as its underlying type.
3248 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3249 Ty = EnumTy->getDecl()->getIntegerType();
3250
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003251 return (Ty->isPromotableIntegerType() ?
3252 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003253 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003254
Daniel Dunbar42025572009-09-14 21:54:03 +00003255 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003256 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00003257 return ABIArgInfo::getIgnore();
3258
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003259 // Structures with either a non-trivial destructor or a non-trivial
3260 // copy constructor are always indirect.
3261 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
3262 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3263
Bob Wilson194f06a2011-08-03 05:58:22 +00003264 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
Manman Renb3fa55f2012-10-30 23:21:41 +00003265 // Homogeneous Aggregates need to be expanded when we can fit the aggregate
3266 // into VFP registers.
Bob Wilson194f06a2011-08-03 05:58:22 +00003267 const Type *Base = 0;
Manman Renb3fa55f2012-10-30 23:21:41 +00003268 uint64_t Members = 0;
3269 if (isHomogeneousAggregate(Ty, Base, getContext(), &Members)) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003270 assert(Base && "Base class should be set for homogeneous aggregate");
Manman Renb3fa55f2012-10-30 23:21:41 +00003271 // Base can be a floating-point or a vector.
3272 if (Base->isVectorType()) {
3273 // ElementSize is in number of floats.
3274 unsigned ElementSize = getContext().getTypeSize(Base) == 64 ? 2 : 4;
Manman Rencb489dd2012-11-06 19:05:29 +00003275 markAllocatedVFPs(VFPRegs, AllocatedVFP, ElementSize,
3276 Members * ElementSize);
Manman Renb3fa55f2012-10-30 23:21:41 +00003277 } else if (Base->isSpecificBuiltinType(BuiltinType::Float))
Manman Ren710c5172012-10-31 19:02:26 +00003278 markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, Members);
Manman Renb3fa55f2012-10-30 23:21:41 +00003279 else {
3280 assert(Base->isSpecificBuiltinType(BuiltinType::Double) ||
3281 Base->isSpecificBuiltinType(BuiltinType::LongDouble));
Manman Ren710c5172012-10-31 19:02:26 +00003282 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, Members * 2);
Manman Renb3fa55f2012-10-30 23:21:41 +00003283 }
3284 IsHA = true;
Bob Wilson194f06a2011-08-03 05:58:22 +00003285 return ABIArgInfo::getExpand();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003286 }
Bob Wilson194f06a2011-08-03 05:58:22 +00003287 }
3288
Manman Ren634b3d22012-08-13 21:23:55 +00003289 // Support byval for ARM.
Manman Rencb489dd2012-11-06 19:05:29 +00003290 // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
3291 // most 8-byte. We realign the indirect argument if type alignment is bigger
3292 // than ABI alignment.
Manman Renfd1ba912012-11-05 22:42:46 +00003293 uint64_t ABIAlign = 4;
3294 uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8;
3295 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3296 getABIKind() == ARMABIInfo::AAPCS)
3297 ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
Manman Ren885ad692012-11-06 04:58:01 +00003298 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
3299 return ABIArgInfo::getIndirect(0, /*ByVal=*/true,
Manman Rencb489dd2012-11-06 19:05:29 +00003300 /*Realign=*/TyAlign > ABIAlign);
Eli Friedman79f30982012-08-09 00:31:40 +00003301 }
3302
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00003303 // Otherwise, pass by coercing to a structure of the appropriate size.
Chris Lattner2acc6e32011-07-18 04:24:23 +00003304 llvm::Type* ElemTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003305 unsigned SizeRegs;
Eli Friedman79f30982012-08-09 00:31:40 +00003306 // FIXME: Try to match the types of the arguments more accurately where
3307 // we can.
3308 if (getContext().getTypeAlign(Ty) <= 32) {
Bob Wilson53fc1a62011-08-01 23:39:04 +00003309 ElemTy = llvm::Type::getInt32Ty(getVMContext());
3310 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Manman Ren78eb76e2012-06-25 22:04:00 +00003311 } else {
Manman Ren78eb76e2012-06-25 22:04:00 +00003312 ElemTy = llvm::Type::getInt64Ty(getVMContext());
3313 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastings67d097e2011-04-27 17:24:02 +00003314 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003315
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003316 llvm::Type *STy =
Chris Lattner7650d952011-06-18 22:49:11 +00003317 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003318 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003319}
3320
Chris Lattnera3c109b2010-07-29 02:16:43 +00003321static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00003322 llvm::LLVMContext &VMContext) {
3323 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
3324 // is called integer-like if its size is less than or equal to one word, and
3325 // the offset of each of its addressable sub-fields is zero.
3326
3327 uint64_t Size = Context.getTypeSize(Ty);
3328
3329 // Check that the type fits in a word.
3330 if (Size > 32)
3331 return false;
3332
3333 // FIXME: Handle vector types!
3334 if (Ty->isVectorType())
3335 return false;
3336
Daniel Dunbarb0d58192009-09-14 02:20:34 +00003337 // Float types are never treated as "integer like".
3338 if (Ty->isRealFloatingType())
3339 return false;
3340
Daniel Dunbar98303b92009-09-13 08:03:58 +00003341 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00003342 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00003343 return true;
3344
Daniel Dunbar45815812010-02-01 23:31:26 +00003345 // Small complex integer types are "integer like".
3346 if (const ComplexType *CT = Ty->getAs<ComplexType>())
3347 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003348
3349 // Single element and zero sized arrays should be allowed, by the definition
3350 // above, but they are not.
3351
3352 // Otherwise, it must be a record type.
3353 const RecordType *RT = Ty->getAs<RecordType>();
3354 if (!RT) return false;
3355
3356 // Ignore records with flexible arrays.
3357 const RecordDecl *RD = RT->getDecl();
3358 if (RD->hasFlexibleArrayMember())
3359 return false;
3360
3361 // Check that all sub-fields are at offset 0, and are themselves "integer
3362 // like".
3363 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3364
3365 bool HadField = false;
3366 unsigned idx = 0;
3367 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3368 i != e; ++i, ++idx) {
David Blaikie581deb32012-06-06 20:45:41 +00003369 const FieldDecl *FD = *i;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003370
Daniel Dunbar679855a2010-01-29 03:22:29 +00003371 // Bit-fields are not addressable, we only need to verify they are "integer
3372 // like". We still have to disallow a subsequent non-bitfield, for example:
3373 // struct { int : 0; int x }
3374 // is non-integer like according to gcc.
3375 if (FD->isBitField()) {
3376 if (!RD->isUnion())
3377 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003378
Daniel Dunbar679855a2010-01-29 03:22:29 +00003379 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3380 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003381
Daniel Dunbar679855a2010-01-29 03:22:29 +00003382 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003383 }
3384
Daniel Dunbar679855a2010-01-29 03:22:29 +00003385 // Check if this field is at offset 0.
3386 if (Layout.getFieldOffset(idx) != 0)
3387 return false;
3388
Daniel Dunbar98303b92009-09-13 08:03:58 +00003389 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3390 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003391
Daniel Dunbar679855a2010-01-29 03:22:29 +00003392 // Only allow at most one field in a structure. This doesn't match the
3393 // wording above, but follows gcc in situations with a field following an
3394 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00003395 if (!RD->isUnion()) {
3396 if (HadField)
3397 return false;
3398
3399 HadField = true;
3400 }
3401 }
3402
3403 return true;
3404}
3405
Chris Lattnera3c109b2010-07-29 02:16:43 +00003406ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003407 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003408 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00003409
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00003410 // Large vector types should be returned via memory.
3411 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
3412 return ABIArgInfo::getIndirect(0);
3413
John McCalld608cdb2010-08-22 10:59:02 +00003414 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003415 // Treat an enum type as its underlying type.
3416 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3417 RetTy = EnumTy->getDecl()->getIntegerType();
3418
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003419 return (RetTy->isPromotableIntegerType() ?
3420 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003421 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003422
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003423 // Structures with either a non-trivial destructor or a non-trivial
3424 // copy constructor are always indirect.
3425 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
3426 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3427
Daniel Dunbar98303b92009-09-13 08:03:58 +00003428 // Are we following APCS?
3429 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00003430 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00003431 return ABIArgInfo::getIgnore();
3432
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003433 // Complex types are all returned as packed integers.
3434 //
3435 // FIXME: Consider using 2 x vector types if the back end handles them
3436 // correctly.
3437 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00003438 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00003439 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003440
Daniel Dunbar98303b92009-09-13 08:03:58 +00003441 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003442 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003443 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003444 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003445 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003446 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003447 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003448 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3449 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003450 }
3451
3452 // Otherwise return in memory.
3453 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003454 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003455
3456 // Otherwise this is an AAPCS variant.
3457
Chris Lattnera3c109b2010-07-29 02:16:43 +00003458 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00003459 return ABIArgInfo::getIgnore();
3460
Bob Wilson3b694fa2011-11-02 04:51:36 +00003461 // Check for homogeneous aggregates with AAPCS-VFP.
3462 if (getABIKind() == AAPCS_VFP) {
3463 const Type *Base = 0;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003464 if (isHomogeneousAggregate(RetTy, Base, getContext())) {
3465 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson3b694fa2011-11-02 04:51:36 +00003466 // Homogeneous Aggregates are returned directly.
3467 return ABIArgInfo::getDirect();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003468 }
Bob Wilson3b694fa2011-11-02 04:51:36 +00003469 }
3470
Daniel Dunbar98303b92009-09-13 08:03:58 +00003471 // Aggregates <= 4 bytes are returned in r0; other aggregates
3472 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003473 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00003474 if (Size <= 32) {
3475 // Return in the smallest viable integer type.
3476 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003477 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003478 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003479 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3480 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003481 }
3482
Daniel Dunbar98303b92009-09-13 08:03:58 +00003483 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003484}
3485
Manman Ren97f81572012-10-16 19:18:39 +00003486/// isIllegalVector - check whether Ty is an illegal vector type.
3487bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
3488 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3489 // Check whether VT is legal.
3490 unsigned NumElements = VT->getNumElements();
3491 uint64_t Size = getContext().getTypeSize(VT);
3492 // NumElements should be power of 2.
3493 if ((NumElements & (NumElements - 1)) != 0)
3494 return true;
3495 // Size should be greater than 32 bits.
3496 return Size <= 32;
3497 }
3498 return false;
3499}
3500
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003501llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00003502 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003503 llvm::Type *BP = CGF.Int8PtrTy;
3504 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003505
3506 CGBuilderTy &Builder = CGF.Builder;
Chris Lattner8b418682012-02-07 00:39:47 +00003507 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003508 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Manman Rend105e732012-10-16 19:01:37 +00003509
3510 uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8;
Rafael Espindolae164c182011-08-02 22:33:37 +00003511 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
Manman Ren97f81572012-10-16 19:18:39 +00003512 bool IsIndirect = false;
Manman Rend105e732012-10-16 19:01:37 +00003513
3514 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
3515 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
Manman Ren93371022012-10-16 19:51:48 +00003516 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3517 getABIKind() == ARMABIInfo::AAPCS)
3518 TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
3519 else
3520 TyAlign = 4;
Manman Ren97f81572012-10-16 19:18:39 +00003521 // Use indirect if size of the illegal vector is bigger than 16 bytes.
3522 if (isIllegalVectorType(Ty) && Size > 16) {
3523 IsIndirect = true;
3524 Size = 4;
3525 TyAlign = 4;
3526 }
Manman Rend105e732012-10-16 19:01:37 +00003527
3528 // Handle address alignment for ABI alignment > 4 bytes.
Rafael Espindolae164c182011-08-02 22:33:37 +00003529 if (TyAlign > 4) {
3530 assert((TyAlign & (TyAlign - 1)) == 0 &&
3531 "Alignment is not power of 2!");
3532 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3533 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
3534 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
Manman Rend105e732012-10-16 19:01:37 +00003535 Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align");
Rafael Espindolae164c182011-08-02 22:33:37 +00003536 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003537
3538 uint64_t Offset =
Manman Rend105e732012-10-16 19:01:37 +00003539 llvm::RoundUpToAlignment(Size, 4);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003540 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00003541 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003542 "ap.next");
3543 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3544
Manman Ren97f81572012-10-16 19:18:39 +00003545 if (IsIndirect)
3546 Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP));
Manman Ren93371022012-10-16 19:51:48 +00003547 else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) {
Manman Rend105e732012-10-16 19:01:37 +00003548 // We can't directly cast ap.cur to pointer to a vector type, since ap.cur
3549 // may not be correctly aligned for the vector type. We create an aligned
3550 // temporary space and copy the content over from ap.cur to the temporary
3551 // space. This is necessary if the natural alignment of the type is greater
3552 // than the ABI alignment.
3553 llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
3554 CharUnits CharSize = getContext().getTypeSizeInChars(Ty);
3555 llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty),
3556 "var.align");
3557 llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
3558 llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy);
3559 Builder.CreateMemCpy(Dst, Src,
3560 llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()),
3561 TyAlign, false);
3562 Addr = AlignedTemp; //The content is in aligned location.
3563 }
3564 llvm::Type *PTy =
3565 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3566 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3567
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003568 return AddrTyped;
3569}
3570
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00003571namespace {
3572
Derek Schuff263366f2012-10-16 22:30:41 +00003573class NaClARMABIInfo : public ABIInfo {
3574 public:
3575 NaClARMABIInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3576 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, Kind) {}
3577 virtual void computeInfo(CGFunctionInfo &FI) const;
3578 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3579 CodeGenFunction &CGF) const;
3580 private:
3581 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
3582 ARMABIInfo NInfo; // Used for everything else.
3583};
3584
3585class NaClARMTargetCodeGenInfo : public TargetCodeGenInfo {
3586 public:
3587 NaClARMTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3588 : TargetCodeGenInfo(new NaClARMABIInfo(CGT, Kind)) {}
3589};
3590
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00003591}
3592
Derek Schuff263366f2012-10-16 22:30:41 +00003593void NaClARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
3594 if (FI.getASTCallingConvention() == CC_PnaclCall)
3595 PInfo.computeInfo(FI);
3596 else
3597 static_cast<const ABIInfo&>(NInfo).computeInfo(FI);
3598}
3599
3600llvm::Value *NaClARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3601 CodeGenFunction &CGF) const {
3602 // Always use the native convention; calling pnacl-style varargs functions
3603 // is unsupported.
3604 return static_cast<const ABIInfo&>(NInfo).EmitVAArg(VAListAddr, Ty, CGF);
3605}
3606
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003607//===----------------------------------------------------------------------===//
Tim Northoverc264e162013-01-31 12:13:10 +00003608// AArch64 ABI Implementation
3609//===----------------------------------------------------------------------===//
3610
3611namespace {
3612
3613class AArch64ABIInfo : public ABIInfo {
3614public:
3615 AArch64ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3616
3617private:
3618 // The AArch64 PCS is explicit about return types and argument types being
3619 // handled identically, so we don't need to draw a distinction between
3620 // Argument and Return classification.
3621 ABIArgInfo classifyGenericType(QualType Ty, int &FreeIntRegs,
3622 int &FreeVFPRegs) const;
3623
3624 ABIArgInfo tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded, bool IsInt,
3625 llvm::Type *DirectTy = 0) const;
3626
3627 virtual void computeInfo(CGFunctionInfo &FI) const;
3628
3629 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3630 CodeGenFunction &CGF) const;
3631};
3632
3633class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
3634public:
3635 AArch64TargetCodeGenInfo(CodeGenTypes &CGT)
3636 :TargetCodeGenInfo(new AArch64ABIInfo(CGT)) {}
3637
3638 const AArch64ABIInfo &getABIInfo() const {
3639 return static_cast<const AArch64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
3640 }
3641
3642 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3643 return 31;
3644 }
3645
3646 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3647 llvm::Value *Address) const {
3648 // 0-31 are x0-x30 and sp: 8 bytes each
3649 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
3650 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 31);
3651
3652 // 64-95 are v0-v31: 16 bytes each
3653 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
3654 AssignToArrayRange(CGF.Builder, Address, Sixteen8, 64, 95);
3655
3656 return false;
3657 }
3658
3659};
3660
3661}
3662
3663void AArch64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
3664 int FreeIntRegs = 8, FreeVFPRegs = 8;
3665
3666 FI.getReturnInfo() = classifyGenericType(FI.getReturnType(),
3667 FreeIntRegs, FreeVFPRegs);
3668
3669 FreeIntRegs = FreeVFPRegs = 8;
3670 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3671 it != ie; ++it) {
3672 it->info = classifyGenericType(it->type, FreeIntRegs, FreeVFPRegs);
3673
3674 }
3675}
3676
3677ABIArgInfo
3678AArch64ABIInfo::tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded,
3679 bool IsInt, llvm::Type *DirectTy) const {
3680 if (FreeRegs >= RegsNeeded) {
3681 FreeRegs -= RegsNeeded;
3682 return ABIArgInfo::getDirect(DirectTy);
3683 }
3684
3685 llvm::Type *Padding = 0;
3686
3687 // We need padding so that later arguments don't get filled in anyway. That
3688 // wouldn't happen if only ByVal arguments followed in the same category, but
3689 // a large structure will simply seem to be a pointer as far as LLVM is
3690 // concerned.
3691 if (FreeRegs > 0) {
3692 if (IsInt)
3693 Padding = llvm::Type::getInt64Ty(getVMContext());
3694 else
3695 Padding = llvm::Type::getFloatTy(getVMContext());
3696
3697 // Either [N x i64] or [N x float].
3698 Padding = llvm::ArrayType::get(Padding, FreeRegs);
3699 FreeRegs = 0;
3700 }
3701
3702 return ABIArgInfo::getIndirect(getContext().getTypeAlign(Ty) / 8,
3703 /*IsByVal=*/ true, /*Realign=*/ false,
3704 Padding);
3705}
3706
3707
3708ABIArgInfo AArch64ABIInfo::classifyGenericType(QualType Ty,
3709 int &FreeIntRegs,
3710 int &FreeVFPRegs) const {
3711 // Can only occurs for return, but harmless otherwise.
3712 if (Ty->isVoidType())
3713 return ABIArgInfo::getIgnore();
3714
3715 // Large vector types should be returned via memory. There's no such concept
3716 // in the ABI, but they'd be over 16 bytes anyway so no matter how they're
3717 // classified they'd go into memory (see B.3).
3718 if (Ty->isVectorType() && getContext().getTypeSize(Ty) > 128) {
3719 if (FreeIntRegs > 0)
3720 --FreeIntRegs;
3721 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3722 }
3723
3724 // All non-aggregate LLVM types have a concrete ABI representation so they can
3725 // be passed directly. After this block we're guaranteed to be in a
3726 // complicated case.
3727 if (!isAggregateTypeForABI(Ty)) {
3728 // Treat an enum type as its underlying type.
3729 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3730 Ty = EnumTy->getDecl()->getIntegerType();
3731
3732 if (Ty->isFloatingType() || Ty->isVectorType())
3733 return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ false);
3734
3735 assert(getContext().getTypeSize(Ty) <= 128 &&
3736 "unexpectedly large scalar type");
3737
3738 int RegsNeeded = getContext().getTypeSize(Ty) > 64 ? 2 : 1;
3739
3740 // If the type may need padding registers to ensure "alignment", we must be
3741 // careful when this is accounted for. Increasing the effective size covers
3742 // all cases.
3743 if (getContext().getTypeAlign(Ty) == 128)
3744 RegsNeeded += FreeIntRegs % 2 != 0;
3745
3746 return tryUseRegs(Ty, FreeIntRegs, RegsNeeded, /*IsInt=*/ true);
3747 }
3748
3749 // Structures with either a non-trivial destructor or a non-trivial
3750 // copy constructor are always indirect.
3751 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) {
3752 if (FreeIntRegs > 0)
3753 --FreeIntRegs;
3754 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3755 }
3756
3757 if (isEmptyRecord(getContext(), Ty, true)) {
3758 if (!getContext().getLangOpts().CPlusPlus) {
3759 // Empty structs outside C++ mode are a GNU extension, so no ABI can
3760 // possibly tell us what to do. It turns out (I believe) that GCC ignores
3761 // the object for parameter-passsing purposes.
3762 return ABIArgInfo::getIgnore();
3763 }
3764
3765 // The combination of C++98 9p5 (sizeof(struct) != 0) and the pseudocode
3766 // description of va_arg in the PCS require that an empty struct does
3767 // actually occupy space for parameter-passing. I'm hoping for a
3768 // clarification giving an explicit paragraph to point to in future.
3769 return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ true,
3770 llvm::Type::getInt8Ty(getVMContext()));
3771 }
3772
3773 // Homogeneous vector aggregates get passed in registers or on the stack.
3774 const Type *Base = 0;
3775 uint64_t NumMembers = 0;
3776 if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)) {
3777 assert(Base && "Base class should be set for homogeneous aggregate");
3778 // Homogeneous aggregates are passed and returned directly.
3779 return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ NumMembers,
3780 /*IsInt=*/ false);
3781 }
3782
3783 uint64_t Size = getContext().getTypeSize(Ty);
3784 if (Size <= 128) {
3785 // Small structs can use the same direct type whether they're in registers
3786 // or on the stack.
3787 llvm::Type *BaseTy;
3788 unsigned NumBases;
3789 int SizeInRegs = (Size + 63) / 64;
3790
3791 if (getContext().getTypeAlign(Ty) == 128) {
3792 BaseTy = llvm::Type::getIntNTy(getVMContext(), 128);
3793 NumBases = 1;
3794
3795 // If the type may need padding registers to ensure "alignment", we must
3796 // be careful when this is accounted for. Increasing the effective size
3797 // covers all cases.
3798 SizeInRegs += FreeIntRegs % 2 != 0;
3799 } else {
3800 BaseTy = llvm::Type::getInt64Ty(getVMContext());
3801 NumBases = SizeInRegs;
3802 }
3803 llvm::Type *DirectTy = llvm::ArrayType::get(BaseTy, NumBases);
3804
3805 return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ SizeInRegs,
3806 /*IsInt=*/ true, DirectTy);
3807 }
3808
3809 // If the aggregate is > 16 bytes, it's passed and returned indirectly. In
3810 // LLVM terms the return uses an "sret" pointer, but that's handled elsewhere.
3811 --FreeIntRegs;
3812 return ABIArgInfo::getIndirect(0, /* byVal = */ false);
3813}
3814
3815llvm::Value *AArch64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3816 CodeGenFunction &CGF) const {
3817 // The AArch64 va_list type and handling is specified in the Procedure Call
3818 // Standard, section B.4:
3819 //
3820 // struct {
3821 // void *__stack;
3822 // void *__gr_top;
3823 // void *__vr_top;
3824 // int __gr_offs;
3825 // int __vr_offs;
3826 // };
3827
3828 assert(!CGF.CGM.getDataLayout().isBigEndian()
3829 && "va_arg not implemented for big-endian AArch64");
3830
3831 int FreeIntRegs = 8, FreeVFPRegs = 8;
3832 Ty = CGF.getContext().getCanonicalType(Ty);
3833 ABIArgInfo AI = classifyGenericType(Ty, FreeIntRegs, FreeVFPRegs);
3834
3835 llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
3836 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
3837 llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
3838 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
3839
3840 llvm::Value *reg_offs_p = 0, *reg_offs = 0;
3841 int reg_top_index;
3842 int RegSize;
3843 if (FreeIntRegs < 8) {
3844 assert(FreeVFPRegs == 8 && "Arguments never split between int & VFP regs");
3845 // 3 is the field number of __gr_offs
3846 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p");
3847 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
3848 reg_top_index = 1; // field number for __gr_top
3849 RegSize = 8 * (8 - FreeIntRegs);
3850 } else {
3851 assert(FreeVFPRegs < 8 && "Argument must go in VFP or int regs");
3852 // 4 is the field number of __vr_offs.
3853 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p");
3854 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
3855 reg_top_index = 2; // field number for __vr_top
3856 RegSize = 16 * (8 - FreeVFPRegs);
3857 }
3858
3859 //=======================================
3860 // Find out where argument was passed
3861 //=======================================
3862
3863 // If reg_offs >= 0 we're already using the stack for this type of
3864 // argument. We don't want to keep updating reg_offs (in case it overflows,
3865 // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
3866 // whatever they get).
3867 llvm::Value *UsingStack = 0;
3868 UsingStack = CGF.Builder.CreateICmpSGE(reg_offs,
3869 llvm::ConstantInt::get(CGF.Int32Ty, 0));
3870
3871 CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
3872
3873 // Otherwise, at least some kind of argument could go in these registers, the
3874 // quesiton is whether this particular type is too big.
3875 CGF.EmitBlock(MaybeRegBlock);
3876
3877 // Integer arguments may need to correct register alignment (for example a
3878 // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
3879 // align __gr_offs to calculate the potential address.
3880 if (FreeIntRegs < 8 && AI.isDirect() && getContext().getTypeAlign(Ty) > 64) {
3881 int Align = getContext().getTypeAlign(Ty) / 8;
3882
3883 reg_offs = CGF.Builder.CreateAdd(reg_offs,
3884 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
3885 "align_regoffs");
3886 reg_offs = CGF.Builder.CreateAnd(reg_offs,
3887 llvm::ConstantInt::get(CGF.Int32Ty, -Align),
3888 "aligned_regoffs");
3889 }
3890
3891 // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
3892 llvm::Value *NewOffset = 0;
3893 NewOffset = CGF.Builder.CreateAdd(reg_offs,
3894 llvm::ConstantInt::get(CGF.Int32Ty, RegSize),
3895 "new_reg_offs");
3896 CGF.Builder.CreateStore(NewOffset, reg_offs_p);
3897
3898 // Now we're in a position to decide whether this argument really was in
3899 // registers or not.
3900 llvm::Value *InRegs = 0;
3901 InRegs = CGF.Builder.CreateICmpSLE(NewOffset,
3902 llvm::ConstantInt::get(CGF.Int32Ty, 0),
3903 "inreg");
3904
3905 CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
3906
3907 //=======================================
3908 // Argument was in registers
3909 //=======================================
3910
3911 // Now we emit the code for if the argument was originally passed in
3912 // registers. First start the appropriate block:
3913 CGF.EmitBlock(InRegBlock);
3914
3915 llvm::Value *reg_top_p = 0, *reg_top = 0;
3916 reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p");
3917 reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
3918 llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs);
3919 llvm::Value *RegAddr = 0;
3920 llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
3921
3922 if (!AI.isDirect()) {
3923 // If it's been passed indirectly (actually a struct), whatever we find from
3924 // stored registers or on the stack will actually be a struct **.
3925 MemTy = llvm::PointerType::getUnqual(MemTy);
3926 }
3927
3928 const Type *Base = 0;
3929 uint64_t NumMembers;
3930 if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)
3931 && NumMembers > 1) {
3932 // Homogeneous aggregates passed in registers will have their elements split
3933 // and stored 16-bytes apart regardless of size (they're notionally in qN,
3934 // qN+1, ...). We reload and store into a temporary local variable
3935 // contiguously.
3936 assert(AI.isDirect() && "Homogeneous aggregates should be passed directly");
3937 llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
3938 llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
3939 llvm::Value *Tmp = CGF.CreateTempAlloca(HFATy);
3940
3941 for (unsigned i = 0; i < NumMembers; ++i) {
3942 llvm::Value *BaseOffset = llvm::ConstantInt::get(CGF.Int32Ty, 16 * i);
3943 llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset);
3944 LoadAddr = CGF.Builder.CreateBitCast(LoadAddr,
3945 llvm::PointerType::getUnqual(BaseTy));
3946 llvm::Value *StoreAddr = CGF.Builder.CreateStructGEP(Tmp, i);
3947
3948 llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
3949 CGF.Builder.CreateStore(Elem, StoreAddr);
3950 }
3951
3952 RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy);
3953 } else {
3954 // Otherwise the object is contiguous in memory
3955 RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy);
3956 }
3957
3958 CGF.EmitBranch(ContBlock);
3959
3960 //=======================================
3961 // Argument was on the stack
3962 //=======================================
3963 CGF.EmitBlock(OnStackBlock);
3964
3965 llvm::Value *stack_p = 0, *OnStackAddr = 0;
3966 stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p");
3967 OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack");
3968
3969 // Again, stack arguments may need realigmnent. In this case both integer and
3970 // floating-point ones might be affected.
3971 if (AI.isDirect() && getContext().getTypeAlign(Ty) > 64) {
3972 int Align = getContext().getTypeAlign(Ty) / 8;
3973
3974 OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty);
3975
3976 OnStackAddr = CGF.Builder.CreateAdd(OnStackAddr,
3977 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
3978 "align_stack");
3979 OnStackAddr = CGF.Builder.CreateAnd(OnStackAddr,
3980 llvm::ConstantInt::get(CGF.Int64Ty, -Align),
3981 "align_stack");
3982
3983 OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy);
3984 }
3985
3986 uint64_t StackSize;
3987 if (AI.isDirect())
3988 StackSize = getContext().getTypeSize(Ty) / 8;
3989 else
3990 StackSize = 8;
3991
3992 // All stack slots are 8 bytes
3993 StackSize = llvm::RoundUpToAlignment(StackSize, 8);
3994
3995 llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize);
3996 llvm::Value *NewStack = CGF.Builder.CreateGEP(OnStackAddr, StackSizeC,
3997 "new_stack");
3998
3999 // Write the new value of __stack for the next call to va_arg
4000 CGF.Builder.CreateStore(NewStack, stack_p);
4001
4002 OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy);
4003
4004 CGF.EmitBranch(ContBlock);
4005
4006 //=======================================
4007 // Tidy up
4008 //=======================================
4009 CGF.EmitBlock(ContBlock);
4010
4011 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr");
4012 ResAddr->addIncoming(RegAddr, InRegBlock);
4013 ResAddr->addIncoming(OnStackAddr, OnStackBlock);
4014
4015 if (AI.isDirect())
4016 return ResAddr;
4017
4018 return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr");
4019}
4020
4021//===----------------------------------------------------------------------===//
Justin Holewinski2c585b92012-05-24 17:43:12 +00004022// NVPTX ABI Implementation
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004023//===----------------------------------------------------------------------===//
4024
4025namespace {
4026
Justin Holewinski2c585b92012-05-24 17:43:12 +00004027class NVPTXABIInfo : public ABIInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004028public:
Justin Holewinskidca8f332013-03-30 14:38:24 +00004029 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004030
4031 ABIArgInfo classifyReturnType(QualType RetTy) const;
4032 ABIArgInfo classifyArgumentType(QualType Ty) const;
4033
4034 virtual void computeInfo(CGFunctionInfo &FI) const;
4035 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4036 CodeGenFunction &CFG) const;
4037};
4038
Justin Holewinski2c585b92012-05-24 17:43:12 +00004039class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004040public:
Justin Holewinski2c585b92012-05-24 17:43:12 +00004041 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
4042 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
Justin Holewinski818eafb2011-10-05 17:58:44 +00004043
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004044 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4045 CodeGen::CodeGenModule &M) const;
Justin Holewinskidca8f332013-03-30 14:38:24 +00004046private:
4047 static void addKernelMetadata(llvm::Function *F);
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004048};
4049
Justin Holewinski2c585b92012-05-24 17:43:12 +00004050ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004051 if (RetTy->isVoidType())
4052 return ABIArgInfo::getIgnore();
4053 if (isAggregateTypeForABI(RetTy))
4054 return ABIArgInfo::getIndirect(0);
4055 return ABIArgInfo::getDirect();
4056}
4057
Justin Holewinski2c585b92012-05-24 17:43:12 +00004058ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004059 if (isAggregateTypeForABI(Ty))
4060 return ABIArgInfo::getIndirect(0);
4061
4062 return ABIArgInfo::getDirect();
4063}
4064
Justin Holewinski2c585b92012-05-24 17:43:12 +00004065void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004066 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4067 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4068 it != ie; ++it)
4069 it->info = classifyArgumentType(it->type);
4070
4071 // Always honor user-specified calling convention.
4072 if (FI.getCallingConvention() != llvm::CallingConv::C)
4073 return;
4074
John McCallbd7370a2013-02-28 19:01:20 +00004075 FI.setEffectiveCallingConvention(getRuntimeCC());
4076}
4077
Justin Holewinski2c585b92012-05-24 17:43:12 +00004078llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4079 CodeGenFunction &CFG) const {
4080 llvm_unreachable("NVPTX does not support varargs");
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004081}
4082
Justin Holewinski2c585b92012-05-24 17:43:12 +00004083void NVPTXTargetCodeGenInfo::
4084SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4085 CodeGen::CodeGenModule &M) const{
Justin Holewinski818eafb2011-10-05 17:58:44 +00004086 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4087 if (!FD) return;
4088
4089 llvm::Function *F = cast<llvm::Function>(GV);
4090
4091 // Perform special handling in OpenCL mode
David Blaikie4e4d0842012-03-11 07:00:24 +00004092 if (M.getLangOpts().OpenCL) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00004093 // Use OpenCL function attributes to check for kernel functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00004094 // By default, all functions are device functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00004095 if (FD->hasAttr<OpenCLKernelAttr>()) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00004096 // OpenCL __kernel functions get kernel metadata
4097 addKernelMetadata(F);
Justin Holewinski818eafb2011-10-05 17:58:44 +00004098 // And kernel functions are not subject to inlining
Bill Wendling72390b32012-12-20 19:27:06 +00004099 F->addFnAttr(llvm::Attribute::NoInline);
Justin Holewinski818eafb2011-10-05 17:58:44 +00004100 }
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004101 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00004102
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004103 // Perform special handling in CUDA mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00004104 if (M.getLangOpts().CUDA) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00004105 // CUDA __global__ functions get a kernel metadata entry. Since
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004106 // __global__ functions cannot be called from the device, we do not
4107 // need to set the noinline attribute.
4108 if (FD->getAttr<CUDAGlobalAttr>())
Justin Holewinskidca8f332013-03-30 14:38:24 +00004109 addKernelMetadata(F);
Justin Holewinski818eafb2011-10-05 17:58:44 +00004110 }
4111}
4112
Justin Holewinskidca8f332013-03-30 14:38:24 +00004113void NVPTXTargetCodeGenInfo::addKernelMetadata(llvm::Function *F) {
4114 llvm::Module *M = F->getParent();
4115 llvm::LLVMContext &Ctx = M->getContext();
4116
4117 // Get "nvvm.annotations" metadata node
4118 llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
4119
4120 // Create !{<func-ref>, metadata !"kernel", i32 1} node
4121 llvm::SmallVector<llvm::Value *, 3> MDVals;
4122 MDVals.push_back(F);
4123 MDVals.push_back(llvm::MDString::get(Ctx, "kernel"));
4124 MDVals.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), 1));
4125
4126 // Append metadata to nvvm.annotations
4127 MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
4128}
4129
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004130}
4131
4132//===----------------------------------------------------------------------===//
Wesley Peck276fdf42010-12-19 19:57:51 +00004133// MBlaze ABI Implementation
4134//===----------------------------------------------------------------------===//
4135
4136namespace {
4137
4138class MBlazeABIInfo : public ABIInfo {
4139public:
4140 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
4141
4142 bool isPromotableIntegerType(QualType Ty) const;
4143
4144 ABIArgInfo classifyReturnType(QualType RetTy) const;
4145 ABIArgInfo classifyArgumentType(QualType RetTy) const;
4146
4147 virtual void computeInfo(CGFunctionInfo &FI) const {
4148 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4149 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4150 it != ie; ++it)
4151 it->info = classifyArgumentType(it->type);
4152 }
4153
4154 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4155 CodeGenFunction &CGF) const;
4156};
4157
4158class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
4159public:
4160 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
4161 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
4162 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4163 CodeGen::CodeGenModule &M) const;
4164};
4165
4166}
4167
4168bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
4169 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
4170 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4171 switch (BT->getKind()) {
4172 case BuiltinType::Bool:
4173 case BuiltinType::Char_S:
4174 case BuiltinType::Char_U:
4175 case BuiltinType::SChar:
4176 case BuiltinType::UChar:
4177 case BuiltinType::Short:
4178 case BuiltinType::UShort:
4179 return true;
4180 default:
4181 return false;
4182 }
4183 return false;
4184}
4185
4186llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4187 CodeGenFunction &CGF) const {
4188 // FIXME: Implement
4189 return 0;
4190}
4191
4192
4193ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
4194 if (RetTy->isVoidType())
4195 return ABIArgInfo::getIgnore();
4196 if (isAggregateTypeForABI(RetTy))
4197 return ABIArgInfo::getIndirect(0);
4198
4199 return (isPromotableIntegerType(RetTy) ?
4200 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4201}
4202
4203ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
4204 if (isAggregateTypeForABI(Ty))
4205 return ABIArgInfo::getIndirect(0);
4206
4207 return (isPromotableIntegerType(Ty) ?
4208 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4209}
4210
4211void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4212 llvm::GlobalValue *GV,
4213 CodeGen::CodeGenModule &M)
4214 const {
4215 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4216 if (!FD) return;
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00004217
Wesley Peck276fdf42010-12-19 19:57:51 +00004218 llvm::CallingConv::ID CC = llvm::CallingConv::C;
4219 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
4220 CC = llvm::CallingConv::MBLAZE_INTR;
4221 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
4222 CC = llvm::CallingConv::MBLAZE_SVOL;
4223
4224 if (CC != llvm::CallingConv::C) {
4225 // Handle 'interrupt_handler' attribute:
4226 llvm::Function *F = cast<llvm::Function>(GV);
4227
4228 // Step 1: Set ISR calling convention.
4229 F->setCallingConv(CC);
4230
4231 // Step 2: Add attributes goodness.
Bill Wendling72390b32012-12-20 19:27:06 +00004232 F->addFnAttr(llvm::Attribute::NoInline);
Wesley Peck276fdf42010-12-19 19:57:51 +00004233 }
4234
4235 // Step 3: Emit _interrupt_handler alias.
4236 if (CC == llvm::CallingConv::MBLAZE_INTR)
4237 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
4238 "_interrupt_handler", GV, &M.getModule());
4239}
4240
4241
4242//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004243// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004244//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004245
4246namespace {
4247
4248class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
4249public:
Chris Lattnerea044322010-07-29 02:01:43 +00004250 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
4251 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004252 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4253 CodeGen::CodeGenModule &M) const;
4254};
4255
4256}
4257
4258void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4259 llvm::GlobalValue *GV,
4260 CodeGen::CodeGenModule &M) const {
4261 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4262 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
4263 // Handle 'interrupt' attribute:
4264 llvm::Function *F = cast<llvm::Function>(GV);
4265
4266 // Step 1: Set ISR calling convention.
4267 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
4268
4269 // Step 2: Add attributes goodness.
Bill Wendling72390b32012-12-20 19:27:06 +00004270 F->addFnAttr(llvm::Attribute::NoInline);
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004271
4272 // Step 3: Emit ISR vector alias.
Anton Korobeynikovf419a852012-11-26 18:59:10 +00004273 unsigned Num = attr->getNumber() / 2;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004274 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Anton Korobeynikovf419a852012-11-26 18:59:10 +00004275 "__isr_" + Twine(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004276 GV, &M.getModule());
4277 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004278 }
4279}
4280
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004281//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00004282// MIPS ABI Implementation. This works for both little-endian and
4283// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004284//===----------------------------------------------------------------------===//
4285
John McCallaeeb7012010-05-27 06:19:26 +00004286namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00004287class MipsABIInfo : public ABIInfo {
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004288 bool IsO32;
Akira Hatanakac359f202012-07-03 19:24:06 +00004289 unsigned MinABIStackAlignInBytes, StackAlignInBytes;
4290 void CoerceToIntArgs(uint64_t TySize,
4291 SmallVector<llvm::Type*, 8> &ArgList) const;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004292 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004293 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004294 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004295public:
Akira Hatanakab551dd32011-11-03 00:05:50 +00004296 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
Akira Hatanakac359f202012-07-03 19:24:06 +00004297 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
4298 StackAlignInBytes(IsO32 ? 8 : 16) {}
Akira Hatanaka619e8872011-06-02 00:09:17 +00004299
4300 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004301 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004302 virtual void computeInfo(CGFunctionInfo &FI) const;
4303 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4304 CodeGenFunction &CGF) const;
4305};
4306
John McCallaeeb7012010-05-27 06:19:26 +00004307class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanakae624fa02011-09-20 18:23:28 +00004308 unsigned SizeOfUnwindException;
John McCallaeeb7012010-05-27 06:19:26 +00004309public:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004310 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
4311 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
4312 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCallaeeb7012010-05-27 06:19:26 +00004313
4314 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
4315 return 29;
4316 }
4317
Reed Kotler7dfd1822013-01-16 17:10:28 +00004318 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4319 CodeGen::CodeGenModule &CGM) const {
Reed Kotlerad4b8b42013-03-13 20:40:30 +00004320 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4321 if (!FD) return;
Rafael Espindolad8e6d6d2013-03-19 14:32:23 +00004322 llvm::Function *Fn = cast<llvm::Function>(GV);
Reed Kotlerad4b8b42013-03-13 20:40:30 +00004323 if (FD->hasAttr<Mips16Attr>()) {
4324 Fn->addFnAttr("mips16");
4325 }
4326 else if (FD->hasAttr<NoMips16Attr>()) {
4327 Fn->addFnAttr("nomips16");
4328 }
Reed Kotler7dfd1822013-01-16 17:10:28 +00004329 }
Reed Kotlerad4b8b42013-03-13 20:40:30 +00004330
John McCallaeeb7012010-05-27 06:19:26 +00004331 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00004332 llvm::Value *Address) const;
John McCall49e34be2011-08-30 01:42:09 +00004333
4334 unsigned getSizeOfUnwindException() const {
Akira Hatanakae624fa02011-09-20 18:23:28 +00004335 return SizeOfUnwindException;
John McCall49e34be2011-08-30 01:42:09 +00004336 }
John McCallaeeb7012010-05-27 06:19:26 +00004337};
4338}
4339
Akira Hatanakac359f202012-07-03 19:24:06 +00004340void MipsABIInfo::CoerceToIntArgs(uint64_t TySize,
4341 SmallVector<llvm::Type*, 8> &ArgList) const {
4342 llvm::IntegerType *IntTy =
4343 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004344
4345 // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
4346 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
4347 ArgList.push_back(IntTy);
4348
4349 // If necessary, add one more integer type to ArgList.
4350 unsigned R = TySize % (MinABIStackAlignInBytes * 8);
4351
4352 if (R)
4353 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004354}
4355
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004356// In N32/64, an aligned double precision floating point field is passed in
4357// a register.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004358llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
Akira Hatanakac359f202012-07-03 19:24:06 +00004359 SmallVector<llvm::Type*, 8> ArgList, IntArgList;
4360
4361 if (IsO32) {
4362 CoerceToIntArgs(TySize, ArgList);
4363 return llvm::StructType::get(getVMContext(), ArgList);
4364 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004365
Akira Hatanaka2afd23d2012-01-12 00:52:17 +00004366 if (Ty->isComplexType())
4367 return CGT.ConvertType(Ty);
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00004368
Akira Hatanakaa34e9212012-02-09 19:54:16 +00004369 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004370
Akira Hatanakac359f202012-07-03 19:24:06 +00004371 // Unions/vectors are passed in integer registers.
4372 if (!RT || !RT->isStructureOrClassType()) {
4373 CoerceToIntArgs(TySize, ArgList);
4374 return llvm::StructType::get(getVMContext(), ArgList);
4375 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004376
4377 const RecordDecl *RD = RT->getDecl();
4378 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004379 assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004380
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004381 uint64_t LastOffset = 0;
4382 unsigned idx = 0;
4383 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
4384
Akira Hatanakaa34e9212012-02-09 19:54:16 +00004385 // Iterate over fields in the struct/class and check if there are any aligned
4386 // double fields.
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004387 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
4388 i != e; ++i, ++idx) {
David Blaikie262bc182012-04-30 02:36:29 +00004389 const QualType Ty = i->getType();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004390 const BuiltinType *BT = Ty->getAs<BuiltinType>();
4391
4392 if (!BT || BT->getKind() != BuiltinType::Double)
4393 continue;
4394
4395 uint64_t Offset = Layout.getFieldOffset(idx);
4396 if (Offset % 64) // Ignore doubles that are not aligned.
4397 continue;
4398
4399 // Add ((Offset - LastOffset) / 64) args of type i64.
4400 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
4401 ArgList.push_back(I64);
4402
4403 // Add double type.
4404 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
4405 LastOffset = Offset + 64;
4406 }
4407
Akira Hatanakac359f202012-07-03 19:24:06 +00004408 CoerceToIntArgs(TySize - LastOffset, IntArgList);
4409 ArgList.append(IntArgList.begin(), IntArgList.end());
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004410
4411 return llvm::StructType::get(getVMContext(), ArgList);
4412}
4413
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004414llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004415 assert((Offset % MinABIStackAlignInBytes) == 0);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004416
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004417 if ((Align - 1) & Offset)
4418 return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
4419
4420 return 0;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004421}
Akira Hatanaka9659d592012-01-10 22:44:52 +00004422
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004423ABIArgInfo
4424MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004425 uint64_t OrigOffset = Offset;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004426 uint64_t TySize = getContext().getTypeSize(Ty);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004427 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004428
Akira Hatanakac359f202012-07-03 19:24:06 +00004429 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
4430 (uint64_t)StackAlignInBytes);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004431 Offset = llvm::RoundUpToAlignment(Offset, Align);
4432 Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004433
Akira Hatanakac359f202012-07-03 19:24:06 +00004434 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
Akira Hatanaka619e8872011-06-02 00:09:17 +00004435 // Ignore empty aggregates.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004436 if (TySize == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00004437 return ABIArgInfo::getIgnore();
4438
Akira Hatanaka511949b2011-08-01 18:09:58 +00004439 // Records with non trivial destructors/constructors should not be passed
4440 // by value.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004441 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004442 Offset = OrigOffset + MinABIStackAlignInBytes;
Akira Hatanaka511949b2011-08-01 18:09:58 +00004443 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004444 }
Akira Hatanaka511949b2011-08-01 18:09:58 +00004445
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004446 // If we have reached here, aggregates are passed directly by coercing to
4447 // another structure type. Padding is inserted if the offset of the
4448 // aggregate is unaligned.
4449 return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
4450 getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00004451 }
4452
4453 // Treat an enum type as its underlying type.
4454 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4455 Ty = EnumTy->getDecl()->getIntegerType();
4456
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004457 if (Ty->isPromotableIntegerType())
4458 return ABIArgInfo::getExtend();
4459
Akira Hatanaka4055cfc2013-01-24 21:47:33 +00004460 return ABIArgInfo::getDirect(0, 0,
4461 IsO32 ? 0 : getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00004462}
4463
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004464llvm::Type*
4465MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakada54ff32012-02-09 18:49:26 +00004466 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakac359f202012-07-03 19:24:06 +00004467 SmallVector<llvm::Type*, 8> RTList;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004468
Akira Hatanakada54ff32012-02-09 18:49:26 +00004469 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004470 const RecordDecl *RD = RT->getDecl();
Akira Hatanakada54ff32012-02-09 18:49:26 +00004471 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
4472 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004473
Akira Hatanakada54ff32012-02-09 18:49:26 +00004474 // N32/64 returns struct/classes in floating point registers if the
4475 // following conditions are met:
4476 // 1. The size of the struct/class is no larger than 128-bit.
4477 // 2. The struct/class has one or two fields all of which are floating
4478 // point types.
4479 // 3. The offset of the first field is zero (this follows what gcc does).
4480 //
4481 // Any other composite results are returned in integer registers.
4482 //
4483 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
4484 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
4485 for (; b != e; ++b) {
David Blaikie262bc182012-04-30 02:36:29 +00004486 const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004487
Akira Hatanakada54ff32012-02-09 18:49:26 +00004488 if (!BT || !BT->isFloatingPoint())
4489 break;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004490
David Blaikie262bc182012-04-30 02:36:29 +00004491 RTList.push_back(CGT.ConvertType(b->getType()));
Akira Hatanakada54ff32012-02-09 18:49:26 +00004492 }
4493
4494 if (b == e)
4495 return llvm::StructType::get(getVMContext(), RTList,
4496 RD->hasAttr<PackedAttr>());
4497
4498 RTList.clear();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004499 }
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004500 }
4501
Akira Hatanakac359f202012-07-03 19:24:06 +00004502 CoerceToIntArgs(Size, RTList);
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004503 return llvm::StructType::get(getVMContext(), RTList);
4504}
4505
Akira Hatanaka619e8872011-06-02 00:09:17 +00004506ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanakaa8536c02012-01-23 23:18:57 +00004507 uint64_t Size = getContext().getTypeSize(RetTy);
4508
4509 if (RetTy->isVoidType() || Size == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00004510 return ABIArgInfo::getIgnore();
4511
Akira Hatanaka8aeb1472012-05-11 21:01:17 +00004512 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004513 if (Size <= 128) {
4514 if (RetTy->isAnyComplexType())
4515 return ABIArgInfo::getDirect();
4516
Akira Hatanakac359f202012-07-03 19:24:06 +00004517 // O32 returns integer vectors in registers.
4518 if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())
4519 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
4520
Akira Hatanaka526cdfb2012-02-08 01:31:22 +00004521 if (!IsO32 && !isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004522 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
4523 }
Akira Hatanaka619e8872011-06-02 00:09:17 +00004524
4525 return ABIArgInfo::getIndirect(0);
4526 }
4527
4528 // Treat an enum type as its underlying type.
4529 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4530 RetTy = EnumTy->getDecl()->getIntegerType();
4531
4532 return (RetTy->isPromotableIntegerType() ?
4533 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4534}
4535
4536void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanakacc662542012-01-12 01:10:09 +00004537 ABIArgInfo &RetInfo = FI.getReturnInfo();
4538 RetInfo = classifyReturnType(FI.getReturnType());
4539
4540 // Check if a pointer to an aggregate is passed as a hidden argument.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004541 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
Akira Hatanakacc662542012-01-12 01:10:09 +00004542
Akira Hatanaka619e8872011-06-02 00:09:17 +00004543 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4544 it != ie; ++it)
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004545 it->info = classifyArgumentType(it->type, Offset);
Akira Hatanaka619e8872011-06-02 00:09:17 +00004546}
4547
4548llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4549 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00004550 llvm::Type *BP = CGF.Int8PtrTy;
4551 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004552
4553 CGBuilderTy &Builder = CGF.Builder;
4554 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
4555 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004556 int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004557 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
4558 llvm::Value *AddrTyped;
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004559 unsigned PtrWidth = getContext().getTargetInfo().getPointerWidth(0);
4560 llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004561
4562 if (TypeAlign > MinABIStackAlignInBytes) {
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004563 llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
4564 llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
4565 llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
4566 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004567 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
4568 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
4569 }
4570 else
4571 AddrTyped = Builder.CreateBitCast(Addr, PTy);
4572
4573 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004574 TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004575 uint64_t Offset =
4576 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
4577 llvm::Value *NextAddr =
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004578 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004579 "ap.next");
4580 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
4581
4582 return AddrTyped;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004583}
4584
John McCallaeeb7012010-05-27 06:19:26 +00004585bool
4586MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4587 llvm::Value *Address) const {
4588 // This information comes from gcc's implementation, which seems to
4589 // as canonical as it gets.
4590
John McCallaeeb7012010-05-27 06:19:26 +00004591 // Everything on MIPS is 4 bytes. Double-precision FP registers
4592 // are aliased to pairs of single-precision FP registers.
Chris Lattner8b418682012-02-07 00:39:47 +00004593 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCallaeeb7012010-05-27 06:19:26 +00004594
4595 // 0-31 are the general purpose registers, $0 - $31.
4596 // 32-63 are the floating-point registers, $f0 - $f31.
4597 // 64 and 65 are the multiply/divide registers, $hi and $lo.
4598 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattner8b418682012-02-07 00:39:47 +00004599 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCallaeeb7012010-05-27 06:19:26 +00004600
4601 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
4602 // They are one bit wide and ignored here.
4603
4604 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
4605 // (coprocessor 1 is the FP unit)
4606 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
4607 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
4608 // 176-181 are the DSP accumulator registers.
Chris Lattner8b418682012-02-07 00:39:47 +00004609 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCallaeeb7012010-05-27 06:19:26 +00004610 return false;
4611}
4612
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004613//===----------------------------------------------------------------------===//
4614// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
4615// Currently subclassed only to implement custom OpenCL C function attribute
4616// handling.
4617//===----------------------------------------------------------------------===//
4618
4619namespace {
4620
4621class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
4622public:
4623 TCETargetCodeGenInfo(CodeGenTypes &CGT)
4624 : DefaultTargetCodeGenInfo(CGT) {}
4625
4626 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4627 CodeGen::CodeGenModule &M) const;
4628};
4629
4630void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4631 llvm::GlobalValue *GV,
4632 CodeGen::CodeGenModule &M) const {
4633 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4634 if (!FD) return;
4635
4636 llvm::Function *F = cast<llvm::Function>(GV);
4637
David Blaikie4e4d0842012-03-11 07:00:24 +00004638 if (M.getLangOpts().OpenCL) {
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004639 if (FD->hasAttr<OpenCLKernelAttr>()) {
4640 // OpenCL C Kernel functions are not subject to inlining
Bill Wendling72390b32012-12-20 19:27:06 +00004641 F->addFnAttr(llvm::Attribute::NoInline);
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004642
4643 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
4644
4645 // Convert the reqd_work_group_size() attributes to metadata.
4646 llvm::LLVMContext &Context = F->getContext();
4647 llvm::NamedMDNode *OpenCLMetadata =
4648 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
4649
4650 SmallVector<llvm::Value*, 5> Operands;
4651 Operands.push_back(F);
4652
Chris Lattner8b418682012-02-07 00:39:47 +00004653 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4654 llvm::APInt(32,
4655 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
4656 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4657 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004658 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
Chris Lattner8b418682012-02-07 00:39:47 +00004659 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4660 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004661 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
4662
4663 // Add a boolean constant operand for "required" (true) or "hint" (false)
4664 // for implementing the work_group_size_hint attr later. Currently
4665 // always true as the hint is not yet implemented.
Chris Lattner8b418682012-02-07 00:39:47 +00004666 Operands.push_back(llvm::ConstantInt::getTrue(Context));
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004667 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
4668 }
4669 }
4670 }
4671}
4672
4673}
John McCallaeeb7012010-05-27 06:19:26 +00004674
Tony Linthicum96319392011-12-12 21:14:55 +00004675//===----------------------------------------------------------------------===//
4676// Hexagon ABI Implementation
4677//===----------------------------------------------------------------------===//
4678
4679namespace {
4680
4681class HexagonABIInfo : public ABIInfo {
4682
4683
4684public:
4685 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
4686
4687private:
4688
4689 ABIArgInfo classifyReturnType(QualType RetTy) const;
4690 ABIArgInfo classifyArgumentType(QualType RetTy) const;
4691
4692 virtual void computeInfo(CGFunctionInfo &FI) const;
4693
4694 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4695 CodeGenFunction &CGF) const;
4696};
4697
4698class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
4699public:
4700 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
4701 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
4702
4703 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
4704 return 29;
4705 }
4706};
4707
4708}
4709
4710void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
4711 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4712 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4713 it != ie; ++it)
4714 it->info = classifyArgumentType(it->type);
4715}
4716
4717ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
4718 if (!isAggregateTypeForABI(Ty)) {
4719 // Treat an enum type as its underlying type.
4720 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4721 Ty = EnumTy->getDecl()->getIntegerType();
4722
4723 return (Ty->isPromotableIntegerType() ?
4724 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4725 }
4726
4727 // Ignore empty records.
4728 if (isEmptyRecord(getContext(), Ty, true))
4729 return ABIArgInfo::getIgnore();
4730
4731 // Structures with either a non-trivial destructor or a non-trivial
4732 // copy constructor are always indirect.
4733 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
4734 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
4735
4736 uint64_t Size = getContext().getTypeSize(Ty);
4737 if (Size > 64)
4738 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
4739 // Pass in the smallest viable integer type.
4740 else if (Size > 32)
4741 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
4742 else if (Size > 16)
4743 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
4744 else if (Size > 8)
4745 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
4746 else
4747 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
4748}
4749
4750ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
4751 if (RetTy->isVoidType())
4752 return ABIArgInfo::getIgnore();
4753
4754 // Large vector types should be returned via memory.
4755 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
4756 return ABIArgInfo::getIndirect(0);
4757
4758 if (!isAggregateTypeForABI(RetTy)) {
4759 // Treat an enum type as its underlying type.
4760 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4761 RetTy = EnumTy->getDecl()->getIntegerType();
4762
4763 return (RetTy->isPromotableIntegerType() ?
4764 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4765 }
4766
4767 // Structures with either a non-trivial destructor or a non-trivial
4768 // copy constructor are always indirect.
4769 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
4770 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
4771
4772 if (isEmptyRecord(getContext(), RetTy, true))
4773 return ABIArgInfo::getIgnore();
4774
4775 // Aggregates <= 8 bytes are returned in r0; other aggregates
4776 // are returned indirectly.
4777 uint64_t Size = getContext().getTypeSize(RetTy);
4778 if (Size <= 64) {
4779 // Return in the smallest viable integer type.
4780 if (Size <= 8)
4781 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
4782 if (Size <= 16)
4783 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
4784 if (Size <= 32)
4785 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
4786 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
4787 }
4788
4789 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
4790}
4791
4792llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner8b418682012-02-07 00:39:47 +00004793 CodeGenFunction &CGF) const {
Tony Linthicum96319392011-12-12 21:14:55 +00004794 // FIXME: Need to handle alignment
Chris Lattner8b418682012-02-07 00:39:47 +00004795 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Tony Linthicum96319392011-12-12 21:14:55 +00004796
4797 CGBuilderTy &Builder = CGF.Builder;
4798 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
4799 "ap");
4800 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
4801 llvm::Type *PTy =
4802 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
4803 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
4804
4805 uint64_t Offset =
4806 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
4807 llvm::Value *NextAddr =
4808 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
4809 "ap.next");
4810 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
4811
4812 return AddrTyped;
4813}
4814
4815
Chris Lattnerea044322010-07-29 02:01:43 +00004816const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004817 if (TheTargetCodeGenInfo)
4818 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004819
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004820 const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00004821 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004822 default:
Chris Lattnerea044322010-07-29 02:01:43 +00004823 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004824
Derek Schuff9ed63f82012-09-06 17:37:28 +00004825 case llvm::Triple::le32:
4826 return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
John McCallaeeb7012010-05-27 06:19:26 +00004827 case llvm::Triple::mips:
4828 case llvm::Triple::mipsel:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004829 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCallaeeb7012010-05-27 06:19:26 +00004830
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00004831 case llvm::Triple::mips64:
4832 case llvm::Triple::mips64el:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004833 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00004834
Tim Northoverc264e162013-01-31 12:13:10 +00004835 case llvm::Triple::aarch64:
4836 return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types));
4837
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004838 case llvm::Triple::arm:
4839 case llvm::Triple::thumb:
Sandeep Patel34c1af82011-04-05 00:23:47 +00004840 {
4841 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004842 if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
Sandeep Patel34c1af82011-04-05 00:23:47 +00004843 Kind = ARMABIInfo::APCS;
David Tweedb16abb12012-10-25 13:33:01 +00004844 else if (CodeGenOpts.FloatABI == "hard" ||
4845 (CodeGenOpts.FloatABI != "soft" && Triple.getEnvironment()==llvm::Triple::GNUEABIHF))
Sandeep Patel34c1af82011-04-05 00:23:47 +00004846 Kind = ARMABIInfo::AAPCS_VFP;
4847
Derek Schuff263366f2012-10-16 22:30:41 +00004848 switch (Triple.getOS()) {
Eli Bendersky441d9f72012-12-04 18:38:10 +00004849 case llvm::Triple::NaCl:
Derek Schuff263366f2012-10-16 22:30:41 +00004850 return *(TheTargetCodeGenInfo =
4851 new NaClARMTargetCodeGenInfo(Types, Kind));
4852 default:
4853 return *(TheTargetCodeGenInfo =
4854 new ARMTargetCodeGenInfo(Types, Kind));
4855 }
Sandeep Patel34c1af82011-04-05 00:23:47 +00004856 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004857
John McCallec853ba2010-03-11 00:10:12 +00004858 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00004859 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
Roman Divacky0fbc4b92012-05-09 18:22:46 +00004860 case llvm::Triple::ppc64:
Bill Schmidt2fc107f2012-10-03 19:18:57 +00004861 if (Triple.isOSBinFormatELF())
4862 return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types));
4863 else
4864 return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00004865
Peter Collingbourneedb66f32012-05-20 23:28:41 +00004866 case llvm::Triple::nvptx:
4867 case llvm::Triple::nvptx64:
Justin Holewinski2c585b92012-05-24 17:43:12 +00004868 return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types));
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004869
Wesley Peck276fdf42010-12-19 19:57:51 +00004870 case llvm::Triple::mblaze:
4871 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
4872
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004873 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00004874 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004875
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004876 case llvm::Triple::tce:
4877 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
4878
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004879 case llvm::Triple::x86: {
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00004880 if (Triple.isOSDarwin())
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004881 return *(TheTargetCodeGenInfo =
Chad Rosier1f1df1f2013-03-25 21:00:27 +00004882 new X86_32TargetCodeGenInfo(Types, true, true, false,
Rafael Espindolab48280b2012-07-31 02:44:24 +00004883 CodeGenOpts.NumRegisterParameters));
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00004884
4885 switch (Triple.getOS()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004886 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004887 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00004888 case llvm::Triple::AuroraUX:
4889 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00004890 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004891 case llvm::Triple::OpenBSD:
Eli Friedman42f74f22012-08-08 23:57:20 +00004892 case llvm::Triple::Bitrig:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004893 return *(TheTargetCodeGenInfo =
Chad Rosier1f1df1f2013-03-25 21:00:27 +00004894 new X86_32TargetCodeGenInfo(Types, false, true, false,
Rafael Espindolab48280b2012-07-31 02:44:24 +00004895 CodeGenOpts.NumRegisterParameters));
Eli Friedman55fc7e22012-01-25 22:46:34 +00004896
4897 case llvm::Triple::Win32:
4898 return *(TheTargetCodeGenInfo =
Chad Rosier1f1df1f2013-03-25 21:00:27 +00004899 new X86_32TargetCodeGenInfo(Types, false, true, true,
Rafael Espindolab48280b2012-07-31 02:44:24 +00004900 CodeGenOpts.NumRegisterParameters));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004901
4902 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004903 return *(TheTargetCodeGenInfo =
Chad Rosier1f1df1f2013-03-25 21:00:27 +00004904 new X86_32TargetCodeGenInfo(Types, false, false, false,
Rafael Espindolab48280b2012-07-31 02:44:24 +00004905 CodeGenOpts.NumRegisterParameters));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004906 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004907 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004908
Eli Friedmanee1ad992011-12-02 00:11:43 +00004909 case llvm::Triple::x86_64: {
4910 bool HasAVX = strcmp(getContext().getTargetInfo().getABI(), "avx") == 0;
4911
Chris Lattnerf13721d2010-08-31 16:44:54 +00004912 switch (Triple.getOS()) {
4913 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00004914 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00004915 case llvm::Triple::Cygwin:
4916 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
Eli Bendersky441d9f72012-12-04 18:38:10 +00004917 case llvm::Triple::NaCl:
Derek Schuff263366f2012-10-16 22:30:41 +00004918 return *(TheTargetCodeGenInfo = new NaClX86_64TargetCodeGenInfo(Types, HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00004919 default:
Eli Friedmanee1ad992011-12-02 00:11:43 +00004920 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
4921 HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00004922 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004923 }
Tony Linthicum96319392011-12-12 21:14:55 +00004924 case llvm::Triple::hexagon:
4925 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
Eli Friedmanee1ad992011-12-02 00:11:43 +00004926 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004927}