blob: d0b4918c671a98107e5878c35254a0ca17f81d17 [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
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000401//
402// This is a simplified version of the x86_32 ABI. Arguments and return values
403// are always passed on the stack.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000404//===----------------------------------------------------------------------===//
405
406class PNaClABIInfo : public ABIInfo {
407 public:
408 PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
409
410 ABIArgInfo classifyReturnType(QualType RetTy) const;
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000411 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Derek Schuff9ed63f82012-09-06 17:37:28 +0000412
413 virtual void computeInfo(CGFunctionInfo &FI) const;
414 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
415 CodeGenFunction &CGF) const;
416};
417
418class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
419 public:
420 PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
421 : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {}
422};
423
424void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
425 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
426
Derek Schuff9ed63f82012-09-06 17:37:28 +0000427 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
428 it != ie; ++it)
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000429 it->info = classifyArgumentType(it->type);
Derek Schuff9ed63f82012-09-06 17:37:28 +0000430 }
431
432llvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
433 CodeGenFunction &CGF) const {
434 return 0;
435}
436
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000437/// \brief Classify argument of given type \p Ty.
438ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const {
Derek Schuff9ed63f82012-09-06 17:37:28 +0000439 if (isAggregateTypeForABI(Ty)) {
Eli Benderskye45dfd12013-04-04 22:49:35 +0000440 // In the PNaCl ABI we always pass records/structures on the stack. The
441 // byval attribute can be used if the record doesn't have non-trivial
442 // constructors/destructors.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000443 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
444 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Derek Schuff9ed63f82012-09-06 17:37:28 +0000445 return ABIArgInfo::getIndirect(0);
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000446 } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
447 // Treat an enum type as its underlying type.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000448 Ty = EnumTy->getDecl()->getIntegerType();
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000449 } else if (Ty->isFloatingType()) {
450 // Floating-point types don't go inreg.
451 return ABIArgInfo::getDirect();
Derek Schuff9ed63f82012-09-06 17:37:28 +0000452 }
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000453
454 return (Ty->isPromotableIntegerType() ?
455 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Derek Schuff9ed63f82012-09-06 17:37:28 +0000456}
457
458ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
459 if (RetTy->isVoidType())
460 return ABIArgInfo::getIgnore();
461
Eli Benderskye45dfd12013-04-04 22:49:35 +0000462 // In the PNaCl ABI we always return records/structures on the stack.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000463 if (isAggregateTypeForABI(RetTy))
464 return ABIArgInfo::getIndirect(0);
465
466 // Treat an enum type as its underlying type.
467 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
468 RetTy = EnumTy->getDecl()->getIntegerType();
469
470 return (RetTy->isPromotableIntegerType() ?
471 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
472}
473
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000474/// IsX86_MMXType - Return true if this is an MMX type.
475bool IsX86_MMXType(llvm::Type *IRType) {
476 // 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 +0000477 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
478 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
479 IRType->getScalarSizeInBits() != 64;
480}
481
Jay Foadef6de3d2011-07-11 09:56:20 +0000482static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000483 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000484 llvm::Type* Ty) {
Bill Wendling0507be62011-03-07 22:47:14 +0000485 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy())
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000486 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
487 return Ty;
488}
489
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000490//===----------------------------------------------------------------------===//
491// X86-32 ABI Implementation
492//===----------------------------------------------------------------------===//
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000493
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000494/// X86_32ABIInfo - The X86-32 ABI information.
495class X86_32ABIInfo : public ABIInfo {
Rafael Espindolab48280b2012-07-31 02:44:24 +0000496 enum Class {
497 Integer,
498 Float
499 };
500
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000501 static const unsigned MinABIStackAlignInBytes = 4;
502
David Chisnall1e4249c2009-08-17 23:08:21 +0000503 bool IsDarwinVectorABI;
504 bool IsSmallStructInRegABI;
Eli Friedman55fc7e22012-01-25 22:46:34 +0000505 bool IsWin32FloatStructABI;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000506 unsigned DefaultNumRegisterParameters;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000507
508 static bool isRegisterSize(unsigned Size) {
509 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
510 }
511
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000512 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context,
513 unsigned callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000514
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000515 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
516 /// such that the argument will be passed in memory.
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000517 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal,
518 unsigned &FreeRegs) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000519
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000520 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbare59d8582010-09-16 20:42:06 +0000521 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000522
Rafael Espindolab48280b2012-07-31 02:44:24 +0000523 Class classify(QualType Ty) const;
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000524 ABIArgInfo classifyReturnType(QualType RetTy,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000525 unsigned callingConvention) const;
Rafael Espindolab6932692012-10-24 01:58:58 +0000526 ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &FreeRegs,
527 bool IsFastCall) const;
528 bool shouldUseInReg(QualType Ty, unsigned &FreeRegs,
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000529 bool IsFastCall, bool &NeedsPadding) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000530
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000531public:
532
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000533 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000534 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
535 CodeGenFunction &CGF) const;
536
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000537 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool w,
Rafael Espindolab48280b2012-07-31 02:44:24 +0000538 unsigned r)
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000539 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000540 IsWin32FloatStructABI(w), DefaultNumRegisterParameters(r) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000541};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000542
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000543class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
544public:
Eli Friedman55fc7e22012-01-25 22:46:34 +0000545 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000546 bool d, bool p, bool w, unsigned r)
547 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, w, r)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000548
549 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
550 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000551
552 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
553 // Darwin uses different dwarf register numbers for EH.
554 if (CGM.isTargetDarwin()) return 5;
555
556 return 4;
557 }
558
559 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
560 llvm::Value *Address) const;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000561
Jay Foadef6de3d2011-07-11 09:56:20 +0000562 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000563 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000564 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000565 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
566 }
567
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000568};
569
570}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000571
572/// shouldReturnTypeInRegister - Determine if the given type should be
573/// passed in a register (for the Darwin ABI).
574bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000575 ASTContext &Context,
576 unsigned callingConvention) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000577 uint64_t Size = Context.getTypeSize(Ty);
578
579 // Type must be register sized.
580 if (!isRegisterSize(Size))
581 return false;
582
583 if (Ty->isVectorType()) {
584 // 64- and 128- bit vectors inside structures are not returned in
585 // registers.
586 if (Size == 64 || Size == 128)
587 return false;
588
589 return true;
590 }
591
Daniel Dunbar77115232010-05-15 00:00:30 +0000592 // If this is a builtin, pointer, enum, complex type, member pointer, or
593 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000594 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000595 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000596 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000597 return true;
598
599 // Arrays are treated like records.
600 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000601 return shouldReturnTypeInRegister(AT->getElementType(), Context,
602 callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000603
604 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000605 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000606 if (!RT) return false;
607
Anders Carlssona8874232010-01-27 03:25:19 +0000608 // FIXME: Traverse bases here too.
609
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000610 // For thiscall conventions, structures will never be returned in
611 // a register. This is for compatibility with the MSVC ABI
612 if (callingConvention == llvm::CallingConv::X86_ThisCall &&
613 RT->isStructureType()) {
614 return false;
615 }
616
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000617 // Structure types are passed in register if all fields would be
618 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000619 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
620 e = RT->getDecl()->field_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000621 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000622
623 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000624 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000625 continue;
626
627 // Check fields recursively.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000628 if (!shouldReturnTypeInRegister(FD->getType(), Context,
629 callingConvention))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000630 return false;
631 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000632 return true;
633}
634
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000635ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
636 unsigned callingConvention) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000637 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000638 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000639
Chris Lattnera3c109b2010-07-29 02:16:43 +0000640 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000641 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000642 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000643 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000644
645 // 128-bit vectors are a special case; they are returned in
646 // registers and we need to make sure to pick a type the LLVM
647 // backend will like.
648 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +0000649 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000650 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000651
652 // Always return in register if it fits in a general purpose
653 // register, or if it is 64 bits and has a single element.
654 if ((Size == 8 || Size == 16 || Size == 32) ||
655 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +0000656 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000657 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000658
659 return ABIArgInfo::getIndirect(0);
660 }
661
662 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000663 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000664
John McCalld608cdb2010-08-22 10:59:02 +0000665 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000666 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000667 // Structures with either a non-trivial destructor or a non-trivial
668 // copy constructor are always indirect.
669 if (hasNonTrivialDestructorOrCopyConstructor(RT))
670 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000671
Anders Carlsson40092972009-10-20 22:07:59 +0000672 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000673 if (RT->getDecl()->hasFlexibleArrayMember())
674 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000675 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000676
David Chisnall1e4249c2009-08-17 23:08:21 +0000677 // If specified, structs and unions are always indirect.
678 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000679 return ABIArgInfo::getIndirect(0);
680
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000681 // Small structures which are register sized are generally returned
682 // in a register.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000683 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(),
684 callingConvention)) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000685 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000686
687 // As a special-case, if the struct is a "single-element" struct, and
688 // the field is of type "float" or "double", return it in a
Eli Friedman55fc7e22012-01-25 22:46:34 +0000689 // floating-point register. (MSVC does not apply this special case.)
690 // We apply a similar transformation for pointer types to improve the
691 // quality of the generated IR.
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000692 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
Eli Friedman55fc7e22012-01-25 22:46:34 +0000693 if ((!IsWin32FloatStructABI && SeltTy->isRealFloatingType())
694 || SeltTy->hasPointerRepresentation())
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000695 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
696
697 // FIXME: We should be able to narrow this integer in cases with dead
698 // padding.
Chris Lattner800588f2010-07-29 06:26:06 +0000699 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000700 }
701
702 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000703 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000704
Chris Lattnera3c109b2010-07-29 02:16:43 +0000705 // Treat an enum type as its underlying type.
706 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
707 RetTy = EnumTy->getDecl()->getIntegerType();
708
709 return (RetTy->isPromotableIntegerType() ?
710 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000711}
712
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000713static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
714 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
715}
716
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000717static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
718 const RecordType *RT = Ty->getAs<RecordType>();
719 if (!RT)
720 return 0;
721 const RecordDecl *RD = RT->getDecl();
722
723 // If this is a C++ record, check the bases first.
724 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
725 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
726 e = CXXRD->bases_end(); i != e; ++i)
727 if (!isRecordWithSSEVectorType(Context, i->getType()))
728 return false;
729
730 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
731 i != e; ++i) {
732 QualType FT = i->getType();
733
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000734 if (isSSEVectorType(Context, FT))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000735 return true;
736
737 if (isRecordWithSSEVectorType(Context, FT))
738 return true;
739 }
740
741 return false;
742}
743
Daniel Dunbare59d8582010-09-16 20:42:06 +0000744unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
745 unsigned Align) const {
746 // Otherwise, if the alignment is less than or equal to the minimum ABI
747 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000748 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +0000749 return 0; // Use default alignment.
750
751 // On non-Darwin, the stack type alignment is always 4.
752 if (!IsDarwinVectorABI) {
753 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000754 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +0000755 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000756
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000757 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000758 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
759 isRecordWithSSEVectorType(getContext(), Ty)))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000760 return 16;
761
762 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000763}
764
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000765ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
766 unsigned &FreeRegs) const {
767 if (!ByVal) {
768 if (FreeRegs) {
769 --FreeRegs; // Non byval indirects just use one pointer.
770 return ABIArgInfo::getIndirectInReg(0, false);
771 }
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000772 return ABIArgInfo::getIndirect(0, false);
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000773 }
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000774
Daniel Dunbare59d8582010-09-16 20:42:06 +0000775 // Compute the byval alignment.
776 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
777 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
778 if (StackAlign == 0)
Chris Lattnerde92d732011-05-22 23:35:00 +0000779 return ABIArgInfo::getIndirect(4);
Daniel Dunbare59d8582010-09-16 20:42:06 +0000780
781 // If the stack alignment is less than the type alignment, realign the
782 // argument.
783 if (StackAlign < TypeAlign)
784 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
785 /*Realign=*/true);
786
787 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000788}
789
Rafael Espindolab48280b2012-07-31 02:44:24 +0000790X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
791 const Type *T = isSingleElementStruct(Ty, getContext());
792 if (!T)
793 T = Ty.getTypePtr();
794
795 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
796 BuiltinType::Kind K = BT->getKind();
797 if (K == BuiltinType::Float || K == BuiltinType::Double)
798 return Float;
799 }
800 return Integer;
801}
802
Rafael Espindolab6932692012-10-24 01:58:58 +0000803bool X86_32ABIInfo::shouldUseInReg(QualType Ty, unsigned &FreeRegs,
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000804 bool IsFastCall, bool &NeedsPadding) const {
805 NeedsPadding = false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000806 Class C = classify(Ty);
807 if (C == Float)
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000808 return false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000809
Rafael Espindolab6932692012-10-24 01:58:58 +0000810 unsigned Size = getContext().getTypeSize(Ty);
811 unsigned SizeInRegs = (Size + 31) / 32;
Rafael Espindola5f14fcb2012-10-23 02:04:01 +0000812
813 if (SizeInRegs == 0)
814 return false;
815
Rafael Espindolab48280b2012-07-31 02:44:24 +0000816 if (SizeInRegs > FreeRegs) {
817 FreeRegs = 0;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000818 return false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000819 }
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000820
Rafael Espindolab48280b2012-07-31 02:44:24 +0000821 FreeRegs -= SizeInRegs;
Rafael Espindolab6932692012-10-24 01:58:58 +0000822
823 if (IsFastCall) {
824 if (Size > 32)
825 return false;
826
827 if (Ty->isIntegralOrEnumerationType())
828 return true;
829
830 if (Ty->isPointerType())
831 return true;
832
833 if (Ty->isReferenceType())
834 return true;
835
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000836 if (FreeRegs)
837 NeedsPadding = true;
838
Rafael Espindolab6932692012-10-24 01:58:58 +0000839 return false;
840 }
841
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000842 return true;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000843}
844
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000845ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Rafael Espindolab6932692012-10-24 01:58:58 +0000846 unsigned &FreeRegs,
847 bool IsFastCall) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000848 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000849 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000850 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000851 if (const RecordType *RT = Ty->getAs<RecordType>()) {
852 // Structures with either a non-trivial destructor or a non-trivial
853 // copy constructor are always indirect.
854 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000855 return getIndirectResult(Ty, false, FreeRegs);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000856
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000857 if (RT->getDecl()->hasFlexibleArrayMember())
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000858 return getIndirectResult(Ty, true, FreeRegs);
Anders Carlssona8874232010-01-27 03:25:19 +0000859 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000860
Eli Friedman5a4d3522011-11-18 00:28:11 +0000861 // Ignore empty structs/unions.
Eli Friedman5a1ac892011-11-18 04:01:36 +0000862 if (isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000863 return ABIArgInfo::getIgnore();
864
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000865 llvm::LLVMContext &LLVMContext = getVMContext();
866 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
867 bool NeedsPadding;
868 if (shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding)) {
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000869 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000870 SmallVector<llvm::Type*, 3> Elements;
871 for (unsigned I = 0; I < SizeInRegs; ++I)
872 Elements.push_back(Int32);
873 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
874 return ABIArgInfo::getDirectInReg(Result);
875 }
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000876 llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : 0;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000877
Daniel Dunbar53012f42009-11-09 01:33:53 +0000878 // Expand small (<= 128-bit) record types when we know that the stack layout
879 // of those arguments will match the struct. This is important because the
880 // LLVM backend isn't smart enough to remove byval, which inhibits many
881 // optimizations.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000882 if (getContext().getTypeSize(Ty) <= 4*32 &&
883 canExpandIndirectArgument(Ty, getContext()))
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000884 return ABIArgInfo::getExpandWithPadding(IsFastCall, PaddingType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000885
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000886 return getIndirectResult(Ty, true, FreeRegs);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000887 }
888
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000889 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +0000890 // On Darwin, some vectors are passed in memory, we handle this by passing
891 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000892 if (IsDarwinVectorABI) {
893 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000894 if ((Size == 8 || Size == 16 || Size == 32) ||
895 (Size == 64 && VT->getNumElements() == 1))
896 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
897 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000898 }
Bill Wendlingbb465d72010-10-18 03:41:31 +0000899
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000900 if (IsX86_MMXType(CGT.ConvertType(Ty)))
901 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64));
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000902
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000903 return ABIArgInfo::getDirect();
904 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000905
906
Chris Lattnera3c109b2010-07-29 02:16:43 +0000907 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
908 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000909
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000910 bool NeedsPadding;
911 bool InReg = shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding);
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000912
913 if (Ty->isPromotableIntegerType()) {
914 if (InReg)
915 return ABIArgInfo::getExtendInReg();
916 return ABIArgInfo::getExtend();
917 }
918 if (InReg)
919 return ABIArgInfo::getDirectInReg();
920 return ABIArgInfo::getDirect();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000921}
922
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000923void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
924 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
925 FI.getCallingConvention());
Rafael Espindolab48280b2012-07-31 02:44:24 +0000926
Rafael Espindolab6932692012-10-24 01:58:58 +0000927 unsigned CC = FI.getCallingConvention();
928 bool IsFastCall = CC == llvm::CallingConv::X86_FastCall;
929 unsigned FreeRegs;
930 if (IsFastCall)
931 FreeRegs = 2;
932 else if (FI.getHasRegParm())
933 FreeRegs = FI.getRegParm();
934 else
935 FreeRegs = DefaultNumRegisterParameters;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000936
937 // If the return value is indirect, then the hidden argument is consuming one
938 // integer register.
939 if (FI.getReturnInfo().isIndirect() && FreeRegs) {
940 --FreeRegs;
941 ABIArgInfo &Old = FI.getReturnInfo();
942 Old = ABIArgInfo::getIndirectInReg(Old.getIndirectAlign(),
943 Old.getIndirectByVal(),
944 Old.getIndirectRealign());
945 }
946
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000947 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
948 it != ie; ++it)
Rafael Espindolab6932692012-10-24 01:58:58 +0000949 it->info = classifyArgumentType(it->type, FreeRegs, IsFastCall);
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000950}
951
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000952llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
953 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +0000954 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000955
956 CGBuilderTy &Builder = CGF.Builder;
957 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
958 "ap");
959 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Eli Friedman7b1fb812011-11-18 02:12:09 +0000960
961 // Compute if the address needs to be aligned
962 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
963 Align = getTypeStackAlignInBytes(Ty, Align);
964 Align = std::max(Align, 4U);
965 if (Align > 4) {
966 // addr = (addr + align - 1) & -align;
967 llvm::Value *Offset =
968 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
969 Addr = CGF.Builder.CreateGEP(Addr, Offset);
970 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
971 CGF.Int32Ty);
972 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
973 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
974 Addr->getType(),
975 "ap.cur.aligned");
976 }
977
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000978 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000979 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000980 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
981
982 uint64_t Offset =
Eli Friedman7b1fb812011-11-18 02:12:09 +0000983 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000984 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +0000985 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000986 "ap.next");
987 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
988
989 return AddrTyped;
990}
991
Charles Davis74f72932010-02-13 15:54:06 +0000992void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
993 llvm::GlobalValue *GV,
994 CodeGen::CodeGenModule &CGM) const {
995 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
996 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
997 // Get the LLVM function.
998 llvm::Function *Fn = cast<llvm::Function>(GV);
999
1000 // Now add the 'alignstack' attribute with a value of 16.
Bill Wendling0d583392012-10-15 20:36:26 +00001001 llvm::AttrBuilder B;
Bill Wendlinge91e9ec2012-10-14 03:28:14 +00001002 B.addStackAlignmentAttr(16);
Bill Wendling909b6de2013-01-23 00:21:06 +00001003 Fn->addAttributes(llvm::AttributeSet::FunctionIndex,
1004 llvm::AttributeSet::get(CGM.getLLVMContext(),
1005 llvm::AttributeSet::FunctionIndex,
1006 B));
Charles Davis74f72932010-02-13 15:54:06 +00001007 }
1008 }
1009}
1010
John McCall6374c332010-03-06 00:35:14 +00001011bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
1012 CodeGen::CodeGenFunction &CGF,
1013 llvm::Value *Address) const {
1014 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCall6374c332010-03-06 00:35:14 +00001015
Chris Lattner8b418682012-02-07 00:39:47 +00001016 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001017
John McCall6374c332010-03-06 00:35:14 +00001018 // 0-7 are the eight integer registers; the order is different
1019 // on Darwin (for EH), but the range is the same.
1020 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +00001021 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +00001022
1023 if (CGF.CGM.isTargetDarwin()) {
1024 // 12-16 are st(0..4). Not sure why we stop at 4.
1025 // These have size 16, which is sizeof(long double) on
1026 // platforms with 8-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001027 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCallaeeb7012010-05-27 06:19:26 +00001028 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001029
John McCall6374c332010-03-06 00:35:14 +00001030 } else {
1031 // 9 is %eflags, which doesn't get a size on Darwin for some
1032 // reason.
1033 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
1034
1035 // 11-16 are st(0..5). Not sure why we stop at 5.
1036 // These have size 12, which is sizeof(long double) on
1037 // platforms with 4-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001038 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCallaeeb7012010-05-27 06:19:26 +00001039 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1040 }
John McCall6374c332010-03-06 00:35:14 +00001041
1042 return false;
1043}
1044
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001045//===----------------------------------------------------------------------===//
1046// X86-64 ABI Implementation
1047//===----------------------------------------------------------------------===//
1048
1049
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001050namespace {
1051/// X86_64ABIInfo - The X86_64 ABI information.
1052class X86_64ABIInfo : public ABIInfo {
1053 enum Class {
1054 Integer = 0,
1055 SSE,
1056 SSEUp,
1057 X87,
1058 X87Up,
1059 ComplexX87,
1060 NoClass,
1061 Memory
1062 };
1063
1064 /// merge - Implement the X86_64 ABI merging algorithm.
1065 ///
1066 /// Merge an accumulating classification \arg Accum with a field
1067 /// classification \arg Field.
1068 ///
1069 /// \param Accum - The accumulating classification. This should
1070 /// always be either NoClass or the result of a previous merge
1071 /// call. In addition, this should never be Memory (the caller
1072 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001073 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001074
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001075 /// postMerge - Implement the X86_64 ABI post merging algorithm.
1076 ///
1077 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
1078 /// final MEMORY or SSE classes when necessary.
1079 ///
1080 /// \param AggregateSize - The size of the current aggregate in
1081 /// the classification process.
1082 ///
1083 /// \param Lo - The classification for the parts of the type
1084 /// residing in the low word of the containing object.
1085 ///
1086 /// \param Hi - The classification for the parts of the type
1087 /// residing in the higher words of the containing object.
1088 ///
1089 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
1090
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001091 /// classify - Determine the x86_64 register classes in which the
1092 /// given type T should be passed.
1093 ///
1094 /// \param Lo - The classification for the parts of the type
1095 /// residing in the low word of the containing object.
1096 ///
1097 /// \param Hi - The classification for the parts of the type
1098 /// residing in the high word of the containing object.
1099 ///
1100 /// \param OffsetBase - The bit offset of this type in the
1101 /// containing object. Some parameters are classified different
1102 /// depending on whether they straddle an eightbyte boundary.
1103 ///
1104 /// If a word is unused its result will be NoClass; if a type should
1105 /// be passed in Memory then at least the classification of \arg Lo
1106 /// will be Memory.
1107 ///
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001108 /// The \arg Lo class will be NoClass iff the argument is ignored.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001109 ///
1110 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
1111 /// also be ComplexX87.
Chris Lattner9c254f02010-06-29 06:01:59 +00001112 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001113
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001114 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001115 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
1116 unsigned IROffset, QualType SourceTy,
1117 unsigned SourceOffset) const;
1118 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
1119 unsigned IROffset, QualType SourceTy,
1120 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001121
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001122 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001123 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +00001124 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001125
1126 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001127 /// such that the argument will be passed in memory.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001128 ///
1129 /// \param freeIntRegs - The number of free integer registers remaining
1130 /// available.
1131 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001132
Chris Lattnera3c109b2010-07-29 02:16:43 +00001133 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001134
Bill Wendlingbb465d72010-10-18 03:41:31 +00001135 ABIArgInfo classifyArgumentType(QualType Ty,
Daniel Dunbaredfac032012-03-10 01:03:58 +00001136 unsigned freeIntRegs,
Bill Wendlingbb465d72010-10-18 03:41:31 +00001137 unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +00001138 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001139
Eli Friedmanee1ad992011-12-02 00:11:43 +00001140 bool IsIllegalVectorType(QualType Ty) const;
1141
John McCall67a57732011-04-21 01:20:55 +00001142 /// The 0.98 ABI revision clarified a lot of ambiguities,
1143 /// unfortunately in ways that were not always consistent with
1144 /// certain previous compilers. In particular, platforms which
1145 /// required strict binary compatibility with older versions of GCC
1146 /// may need to exempt themselves.
1147 bool honorsRevision0_98() const {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001148 return !getContext().getTargetInfo().getTriple().isOSDarwin();
John McCall67a57732011-04-21 01:20:55 +00001149 }
1150
Eli Friedmanee1ad992011-12-02 00:11:43 +00001151 bool HasAVX;
Derek Schuffbabaf312012-10-11 15:52:22 +00001152 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
1153 // 64-bit hardware.
1154 bool Has64BitPointers;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001155
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001156public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001157 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
Derek Schuffbabaf312012-10-11 15:52:22 +00001158 ABIInfo(CGT), HasAVX(hasavx),
Derek Schuff90da80c2012-10-11 18:21:13 +00001159 Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001160 }
Chris Lattner9c254f02010-06-29 06:01:59 +00001161
John McCallde5d3c72012-02-17 03:33:10 +00001162 bool isPassedUsingAVXType(QualType type) const {
1163 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00001164 // The freeIntRegs argument doesn't matter here.
1165 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE);
John McCallde5d3c72012-02-17 03:33:10 +00001166 if (info.isDirect()) {
1167 llvm::Type *ty = info.getCoerceToType();
1168 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
1169 return (vectorTy->getBitWidth() > 128);
1170 }
1171 return false;
1172 }
1173
Chris Lattneree5dcd02010-07-29 02:31:05 +00001174 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001175
1176 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1177 CodeGenFunction &CGF) const;
1178};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001179
Chris Lattnerf13721d2010-08-31 16:44:54 +00001180/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001181class WinX86_64ABIInfo : public ABIInfo {
1182
1183 ABIArgInfo classify(QualType Ty) const;
1184
Chris Lattnerf13721d2010-08-31 16:44:54 +00001185public:
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001186 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
1187
1188 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattnerf13721d2010-08-31 16:44:54 +00001189
1190 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1191 CodeGenFunction &CGF) const;
1192};
1193
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001194class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1195public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001196 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
Derek Schuffbabaf312012-10-11 15:52:22 +00001197 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
John McCall6374c332010-03-06 00:35:14 +00001198
John McCallde5d3c72012-02-17 03:33:10 +00001199 const X86_64ABIInfo &getABIInfo() const {
1200 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
1201 }
1202
John McCall6374c332010-03-06 00:35:14 +00001203 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1204 return 7;
1205 }
1206
1207 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1208 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001209 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001210
John McCallaeeb7012010-05-27 06:19:26 +00001211 // 0-15 are the 16 integer registers.
1212 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001213 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +00001214 return false;
1215 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001216
Jay Foadef6de3d2011-07-11 09:56:20 +00001217 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001218 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +00001219 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001220 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1221 }
1222
John McCallde5d3c72012-02-17 03:33:10 +00001223 bool isNoProtoCallVariadic(const CallArgList &args,
1224 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +00001225 // The default CC on x86-64 sets %al to the number of SSA
1226 // registers used, and GCC sets this when calling an unprototyped
Eli Friedman3ed79032011-12-01 04:53:19 +00001227 // function, so we override the default behavior. However, don't do
Eli Friedman68805fe2011-12-06 03:08:26 +00001228 // that when AVX types are involved: the ABI explicitly states it is
1229 // undefined, and it doesn't work in practice because of how the ABI
1230 // defines varargs anyway.
John McCallde5d3c72012-02-17 03:33:10 +00001231 if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) {
Eli Friedman3ed79032011-12-01 04:53:19 +00001232 bool HasAVXType = false;
John McCallde5d3c72012-02-17 03:33:10 +00001233 for (CallArgList::const_iterator
1234 it = args.begin(), ie = args.end(); it != ie; ++it) {
1235 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1236 HasAVXType = true;
1237 break;
Eli Friedman3ed79032011-12-01 04:53:19 +00001238 }
1239 }
John McCallde5d3c72012-02-17 03:33:10 +00001240
Eli Friedman3ed79032011-12-01 04:53:19 +00001241 if (!HasAVXType)
1242 return true;
1243 }
John McCall01f151e2011-09-21 08:08:30 +00001244
John McCallde5d3c72012-02-17 03:33:10 +00001245 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
John McCall01f151e2011-09-21 08:08:30 +00001246 }
1247
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001248};
1249
Chris Lattnerf13721d2010-08-31 16:44:54 +00001250class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1251public:
1252 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1253 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1254
1255 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1256 return 7;
1257 }
1258
1259 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1260 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001261 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001262
Chris Lattnerf13721d2010-08-31 16:44:54 +00001263 // 0-15 are the 16 integer registers.
1264 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001265 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattnerf13721d2010-08-31 16:44:54 +00001266 return false;
1267 }
1268};
1269
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001270}
1271
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001272void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1273 Class &Hi) const {
1274 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1275 //
1276 // (a) If one of the classes is Memory, the whole argument is passed in
1277 // memory.
1278 //
1279 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1280 // memory.
1281 //
1282 // (c) If the size of the aggregate exceeds two eightbytes and the first
1283 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1284 // argument is passed in memory. NOTE: This is necessary to keep the
1285 // ABI working for processors that don't support the __m256 type.
1286 //
1287 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1288 //
1289 // Some of these are enforced by the merging logic. Others can arise
1290 // only with unions; for example:
1291 // union { _Complex double; unsigned; }
1292 //
1293 // Note that clauses (b) and (c) were added in 0.98.
1294 //
1295 if (Hi == Memory)
1296 Lo = Memory;
1297 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1298 Lo = Memory;
1299 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1300 Lo = Memory;
1301 if (Hi == SSEUp && Lo != SSE)
1302 Hi = SSE;
1303}
1304
Chris Lattner1090a9b2010-06-28 21:43:59 +00001305X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001306 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1307 // classified recursively so that always two fields are
1308 // considered. The resulting class is calculated according to
1309 // the classes of the fields in the eightbyte:
1310 //
1311 // (a) If both classes are equal, this is the resulting class.
1312 //
1313 // (b) If one of the classes is NO_CLASS, the resulting class is
1314 // the other class.
1315 //
1316 // (c) If one of the classes is MEMORY, the result is the MEMORY
1317 // class.
1318 //
1319 // (d) If one of the classes is INTEGER, the result is the
1320 // INTEGER.
1321 //
1322 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1323 // MEMORY is used as class.
1324 //
1325 // (f) Otherwise class SSE is used.
1326
1327 // Accum should never be memory (we should have returned) or
1328 // ComplexX87 (because this cannot be passed in a structure).
1329 assert((Accum != Memory && Accum != ComplexX87) &&
1330 "Invalid accumulated classification during merge.");
1331 if (Accum == Field || Field == NoClass)
1332 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001333 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001334 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001335 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001336 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001337 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001338 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001339 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1340 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001341 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001342 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001343}
1344
Chris Lattnerbcaedae2010-06-30 19:14:05 +00001345void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001346 Class &Lo, Class &Hi) const {
1347 // FIXME: This code can be simplified by introducing a simple value class for
1348 // Class pairs with appropriate constructor methods for the various
1349 // situations.
1350
1351 // FIXME: Some of the split computations are wrong; unaligned vectors
1352 // shouldn't be passed in registers for example, so there is no chance they
1353 // can straddle an eightbyte. Verify & simplify.
1354
1355 Lo = Hi = NoClass;
1356
1357 Class &Current = OffsetBase < 64 ? Lo : Hi;
1358 Current = Memory;
1359
John McCall183700f2009-09-21 23:43:11 +00001360 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001361 BuiltinType::Kind k = BT->getKind();
1362
1363 if (k == BuiltinType::Void) {
1364 Current = NoClass;
1365 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1366 Lo = Integer;
1367 Hi = Integer;
1368 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1369 Current = Integer;
Derek Schuff7da46f92012-10-11 16:55:58 +00001370 } else if ((k == BuiltinType::Float || k == BuiltinType::Double) ||
1371 (k == BuiltinType::LongDouble &&
1372 getContext().getTargetInfo().getTriple().getOS() ==
Eli Bendersky441d9f72012-12-04 18:38:10 +00001373 llvm::Triple::NaCl)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001374 Current = SSE;
1375 } else if (k == BuiltinType::LongDouble) {
1376 Lo = X87;
1377 Hi = X87Up;
1378 }
1379 // FIXME: _Decimal32 and _Decimal64 are SSE.
1380 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001381 return;
1382 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001383
Chris Lattner1090a9b2010-06-28 21:43:59 +00001384 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001385 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +00001386 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001387 return;
1388 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001389
Chris Lattner1090a9b2010-06-28 21:43:59 +00001390 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001391 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001392 return;
1393 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001394
Chris Lattner1090a9b2010-06-28 21:43:59 +00001395 if (Ty->isMemberPointerType()) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001396 if (Ty->isMemberFunctionPointerType() && Has64BitPointers)
Daniel Dunbar67d438d2010-05-15 00:00:37 +00001397 Lo = Hi = Integer;
1398 else
1399 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001400 return;
1401 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001402
Chris Lattner1090a9b2010-06-28 21:43:59 +00001403 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001404 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001405 if (Size == 32) {
1406 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1407 // float> as integer.
1408 Current = Integer;
1409
1410 // If this type crosses an eightbyte boundary, it should be
1411 // split.
1412 uint64_t EB_Real = (OffsetBase) / 64;
1413 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1414 if (EB_Real != EB_Imag)
1415 Hi = Lo;
1416 } else if (Size == 64) {
1417 // gcc passes <1 x double> in memory. :(
1418 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1419 return;
1420
1421 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001422 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001423 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1424 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1425 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001426 Current = Integer;
1427 else
1428 Current = SSE;
1429
1430 // If this type crosses an eightbyte boundary, it should be
1431 // split.
1432 if (OffsetBase && OffsetBase != 64)
1433 Hi = Lo;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001434 } else if (Size == 128 || (HasAVX && Size == 256)) {
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001435 // Arguments of 256-bits are split into four eightbyte chunks. The
1436 // least significant one belongs to class SSE and all the others to class
1437 // SSEUP. The original Lo and Hi design considers that types can't be
1438 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1439 // This design isn't correct for 256-bits, but since there're no cases
1440 // where the upper parts would need to be inspected, avoid adding
1441 // complexity and just consider Hi to match the 64-256 part.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001442 Lo = SSE;
1443 Hi = SSEUp;
1444 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001445 return;
1446 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001447
Chris Lattner1090a9b2010-06-28 21:43:59 +00001448 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001449 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001450
Chris Lattnerea044322010-07-29 02:01:43 +00001451 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001452 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001453 if (Size <= 64)
1454 Current = Integer;
1455 else if (Size <= 128)
1456 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001457 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001458 Current = SSE;
Derek Schuff7da46f92012-10-11 16:55:58 +00001459 else if (ET == getContext().DoubleTy ||
1460 (ET == getContext().LongDoubleTy &&
1461 getContext().getTargetInfo().getTriple().getOS() ==
Eli Bendersky441d9f72012-12-04 18:38:10 +00001462 llvm::Triple::NaCl))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001463 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001464 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001465 Current = ComplexX87;
1466
1467 // If this complex type crosses an eightbyte boundary then it
1468 // should be split.
1469 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001470 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001471 if (Hi == NoClass && EB_Real != EB_Imag)
1472 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001473
Chris Lattner1090a9b2010-06-28 21:43:59 +00001474 return;
1475 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001476
Chris Lattnerea044322010-07-29 02:01:43 +00001477 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001478 // Arrays are treated like structures.
1479
Chris Lattnerea044322010-07-29 02:01:43 +00001480 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001481
1482 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001483 // than four eightbytes, ..., it has class MEMORY.
1484 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001485 return;
1486
1487 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1488 // fields, it has class MEMORY.
1489 //
1490 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001491 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001492 return;
1493
1494 // Otherwise implement simplified merge. We could be smarter about
1495 // this, but it isn't worth it and would be harder to verify.
1496 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001497 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001498 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes089d8922011-07-12 01:27:38 +00001499
1500 // The only case a 256-bit wide vector could be used is when the array
1501 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1502 // to work for sizes wider than 128, early check and fallback to memory.
1503 if (Size > 128 && EltSize != 256)
1504 return;
1505
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001506 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1507 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001508 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001509 Lo = merge(Lo, FieldLo);
1510 Hi = merge(Hi, FieldHi);
1511 if (Lo == Memory || Hi == Memory)
1512 break;
1513 }
1514
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001515 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001516 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001517 return;
1518 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001519
Chris Lattner1090a9b2010-06-28 21:43:59 +00001520 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001521 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001522
1523 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001524 // than four eightbytes, ..., it has class MEMORY.
1525 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001526 return;
1527
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001528 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1529 // copy constructor or a non-trivial destructor, it is passed by invisible
1530 // reference.
1531 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1532 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001533
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001534 const RecordDecl *RD = RT->getDecl();
1535
1536 // Assume variable sized types are passed in memory.
1537 if (RD->hasFlexibleArrayMember())
1538 return;
1539
Chris Lattnerea044322010-07-29 02:01:43 +00001540 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001541
1542 // Reset Lo class, this will be recomputed.
1543 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001544
1545 // If this is a C++ record, classify the bases first.
1546 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1547 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1548 e = CXXRD->bases_end(); i != e; ++i) {
1549 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1550 "Unexpected base class!");
1551 const CXXRecordDecl *Base =
1552 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1553
1554 // Classify this field.
1555 //
1556 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1557 // single eightbyte, each is classified separately. Each eightbyte gets
1558 // initialized to class NO_CLASS.
1559 Class FieldLo, FieldHi;
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001560 uint64_t Offset =
1561 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
Chris Lattner9c254f02010-06-29 06:01:59 +00001562 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001563 Lo = merge(Lo, FieldLo);
1564 Hi = merge(Hi, FieldHi);
1565 if (Lo == Memory || Hi == Memory)
1566 break;
1567 }
1568 }
1569
1570 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001571 unsigned idx = 0;
Bruno Cardoso Lopes548e4782011-07-12 22:30:58 +00001572 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001573 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001574 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1575 bool BitField = i->isBitField();
1576
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001577 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1578 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001579 //
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001580 // The only case a 256-bit wide vector could be used is when the struct
1581 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1582 // to work for sizes wider than 128, early check and fallback to memory.
1583 //
1584 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1585 Lo = Memory;
1586 return;
1587 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001588 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001589 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001590 Lo = Memory;
1591 return;
1592 }
1593
1594 // Classify this field.
1595 //
1596 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1597 // exceeds a single eightbyte, each is classified
1598 // separately. Each eightbyte gets initialized to class
1599 // NO_CLASS.
1600 Class FieldLo, FieldHi;
1601
1602 // Bit-fields require special handling, they do not force the
1603 // structure to be passed in memory even if unaligned, and
1604 // therefore they can straddle an eightbyte.
1605 if (BitField) {
1606 // Ignore padding bit-fields.
1607 if (i->isUnnamedBitfield())
1608 continue;
1609
1610 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001611 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001612
1613 uint64_t EB_Lo = Offset / 64;
1614 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1615 FieldLo = FieldHi = NoClass;
1616 if (EB_Lo) {
1617 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1618 FieldLo = NoClass;
1619 FieldHi = Integer;
1620 } else {
1621 FieldLo = Integer;
1622 FieldHi = EB_Hi ? Integer : NoClass;
1623 }
1624 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001625 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001626 Lo = merge(Lo, FieldLo);
1627 Hi = merge(Hi, FieldHi);
1628 if (Lo == Memory || Hi == Memory)
1629 break;
1630 }
1631
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001632 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001633 }
1634}
1635
Chris Lattner9c254f02010-06-29 06:01:59 +00001636ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001637 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1638 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001639 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001640 // Treat an enum type as its underlying type.
1641 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1642 Ty = EnumTy->getDecl()->getIntegerType();
1643
1644 return (Ty->isPromotableIntegerType() ?
1645 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1646 }
1647
1648 return ABIArgInfo::getIndirect(0);
1649}
1650
Eli Friedmanee1ad992011-12-02 00:11:43 +00001651bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1652 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1653 uint64_t Size = getContext().getTypeSize(VecTy);
1654 unsigned LargestVector = HasAVX ? 256 : 128;
1655 if (Size <= 64 || Size > LargestVector)
1656 return true;
1657 }
1658
1659 return false;
1660}
1661
Daniel Dunbaredfac032012-03-10 01:03:58 +00001662ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1663 unsigned freeIntRegs) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001664 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1665 // place naturally.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001666 //
1667 // This assumption is optimistic, as there could be free registers available
1668 // when we need to pass this argument in memory, and LLVM could try to pass
1669 // the argument in the free register. This does not seem to happen currently,
1670 // but this code would be much safer if we could mark the argument with
1671 // 'onstack'. See PR12193.
Eli Friedmanee1ad992011-12-02 00:11:43 +00001672 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001673 // Treat an enum type as its underlying type.
1674 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1675 Ty = EnumTy->getDecl()->getIntegerType();
1676
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001677 return (Ty->isPromotableIntegerType() ?
1678 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001679 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001680
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001681 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1682 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001683
Chris Lattner855d2272011-05-22 23:21:23 +00001684 // Compute the byval alignment. We specify the alignment of the byval in all
1685 // cases so that the mid-level optimizer knows the alignment of the byval.
1686 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
Daniel Dunbaredfac032012-03-10 01:03:58 +00001687
1688 // Attempt to avoid passing indirect results using byval when possible. This
1689 // is important for good codegen.
1690 //
1691 // We do this by coercing the value into a scalar type which the backend can
1692 // handle naturally (i.e., without using byval).
1693 //
1694 // For simplicity, we currently only do this when we have exhausted all of the
1695 // free integer registers. Doing this when there are free integer registers
1696 // would require more care, as we would have to ensure that the coerced value
1697 // did not claim the unused register. That would require either reording the
1698 // arguments to the function (so that any subsequent inreg values came first),
1699 // or only doing this optimization when there were no following arguments that
1700 // might be inreg.
1701 //
1702 // We currently expect it to be rare (particularly in well written code) for
1703 // arguments to be passed on the stack when there are still free integer
1704 // registers available (this would typically imply large structs being passed
1705 // by value), so this seems like a fair tradeoff for now.
1706 //
1707 // We can revisit this if the backend grows support for 'onstack' parameter
1708 // attributes. See PR12193.
1709 if (freeIntRegs == 0) {
1710 uint64_t Size = getContext().getTypeSize(Ty);
1711
1712 // If this type fits in an eightbyte, coerce it into the matching integral
1713 // type, which will end up on the stack (with alignment 8).
1714 if (Align == 8 && Size <= 64)
1715 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1716 Size));
1717 }
1718
Chris Lattner855d2272011-05-22 23:21:23 +00001719 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001720}
1721
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001722/// GetByteVectorType - The ABI specifies that a value should be passed in an
1723/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner0f408f52010-07-29 04:56:46 +00001724/// vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001725llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001726 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001727
Chris Lattner15842bd2010-07-29 05:02:29 +00001728 // Wrapper structs that just contain vectors are passed just like vectors,
1729 // strip them off if present.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001730 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner15842bd2010-07-29 05:02:29 +00001731 while (STy && STy->getNumElements() == 1) {
1732 IRType = STy->getElementType(0);
1733 STy = dyn_cast<llvm::StructType>(IRType);
1734 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001735
Bruno Cardoso Lopes528a8c72011-07-08 22:57:35 +00001736 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001737 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1738 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001739 unsigned BitWidth = VT->getBitWidth();
Tanya Lattnerce275672011-11-28 23:18:11 +00001740 if ((BitWidth >= 128 && BitWidth <= 256) &&
Chris Lattner0f408f52010-07-29 04:56:46 +00001741 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1742 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1743 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1744 EltTy->isIntegerTy(128)))
1745 return VT;
1746 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001747
Chris Lattner0f408f52010-07-29 04:56:46 +00001748 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1749}
1750
Chris Lattnere2962be2010-07-29 07:30:00 +00001751/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1752/// is known to either be off the end of the specified type or being in
1753/// alignment padding. The user type specified is known to be at most 128 bits
1754/// in size, and have passed through X86_64ABIInfo::classify with a successful
1755/// classification that put one of the two halves in the INTEGER class.
1756///
1757/// It is conservatively correct to return false.
1758static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1759 unsigned EndBit, ASTContext &Context) {
1760 // If the bytes being queried are off the end of the type, there is no user
1761 // data hiding here. This handles analysis of builtins, vectors and other
1762 // types that don't contain interesting padding.
1763 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1764 if (TySize <= StartBit)
1765 return true;
1766
Chris Lattner021c3a32010-07-29 07:43:55 +00001767 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1768 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1769 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1770
1771 // Check each element to see if the element overlaps with the queried range.
1772 for (unsigned i = 0; i != NumElts; ++i) {
1773 // If the element is after the span we care about, then we're done..
1774 unsigned EltOffset = i*EltSize;
1775 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001776
Chris Lattner021c3a32010-07-29 07:43:55 +00001777 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1778 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1779 EndBit-EltOffset, Context))
1780 return false;
1781 }
1782 // If it overlaps no elements, then it is safe to process as padding.
1783 return true;
1784 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001785
Chris Lattnere2962be2010-07-29 07:30:00 +00001786 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1787 const RecordDecl *RD = RT->getDecl();
1788 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001789
Chris Lattnere2962be2010-07-29 07:30:00 +00001790 // If this is a C++ record, check the bases first.
1791 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1792 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1793 e = CXXRD->bases_end(); i != e; ++i) {
1794 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1795 "Unexpected base class!");
1796 const CXXRecordDecl *Base =
1797 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001798
Chris Lattnere2962be2010-07-29 07:30:00 +00001799 // If the base is after the span we care about, ignore it.
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001800 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
Chris Lattnere2962be2010-07-29 07:30:00 +00001801 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001802
Chris Lattnere2962be2010-07-29 07:30:00 +00001803 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1804 if (!BitsContainNoUserData(i->getType(), BaseStart,
1805 EndBit-BaseOffset, Context))
1806 return false;
1807 }
1808 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001809
Chris Lattnere2962be2010-07-29 07:30:00 +00001810 // Verify that no field has data that overlaps the region of interest. Yes
1811 // this could be sped up a lot by being smarter about queried fields,
1812 // however we're only looking at structs up to 16 bytes, so we don't care
1813 // much.
1814 unsigned idx = 0;
1815 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1816 i != e; ++i, ++idx) {
1817 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001818
Chris Lattnere2962be2010-07-29 07:30:00 +00001819 // If we found a field after the region we care about, then we're done.
1820 if (FieldOffset >= EndBit) break;
1821
1822 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1823 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1824 Context))
1825 return false;
1826 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001827
Chris Lattnere2962be2010-07-29 07:30:00 +00001828 // If nothing in this record overlapped the area of interest, then we're
1829 // clean.
1830 return true;
1831 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001832
Chris Lattnere2962be2010-07-29 07:30:00 +00001833 return false;
1834}
1835
Chris Lattner0b362002010-07-29 18:39:32 +00001836/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1837/// float member at the specified offset. For example, {int,{float}} has a
1838/// float at offset 4. It is conservatively correct for this routine to return
1839/// false.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001840static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Micah Villmow25a6a842012-10-08 16:25:52 +00001841 const llvm::DataLayout &TD) {
Chris Lattner0b362002010-07-29 18:39:32 +00001842 // Base case if we find a float.
1843 if (IROffset == 0 && IRType->isFloatTy())
1844 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001845
Chris Lattner0b362002010-07-29 18:39:32 +00001846 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001847 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner0b362002010-07-29 18:39:32 +00001848 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1849 unsigned Elt = SL->getElementContainingOffset(IROffset);
1850 IROffset -= SL->getElementOffset(Elt);
1851 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1852 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001853
Chris Lattner0b362002010-07-29 18:39:32 +00001854 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001855 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1856 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner0b362002010-07-29 18:39:32 +00001857 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1858 IROffset -= IROffset/EltSize*EltSize;
1859 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1860 }
1861
1862 return false;
1863}
1864
Chris Lattnerf47c9442010-07-29 18:13:09 +00001865
1866/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1867/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001868llvm::Type *X86_64ABIInfo::
1869GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00001870 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001871 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001872 // pass as float if the last 4 bytes is just padding. This happens for
1873 // structs that contain 3 floats.
1874 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1875 SourceOffset*8+64, getContext()))
1876 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001877
Chris Lattner0b362002010-07-29 18:39:32 +00001878 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1879 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1880 // case.
Micah Villmow25a6a842012-10-08 16:25:52 +00001881 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
1882 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001883 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001884
Chris Lattnerf47c9442010-07-29 18:13:09 +00001885 return llvm::Type::getDoubleTy(getVMContext());
1886}
1887
1888
Chris Lattner0d2656d2010-07-29 17:40:35 +00001889/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1890/// an 8-byte GPR. This means that we either have a scalar or we are talking
1891/// about the high or low part of an up-to-16-byte struct. This routine picks
1892/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001893/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1894/// etc).
1895///
1896/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1897/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1898/// the 8-byte value references. PrefType may be null.
1899///
1900/// SourceTy is the source level type for the entire argument. SourceOffset is
1901/// an offset into this that we're processing (which is always either 0 or 8).
1902///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001903llvm::Type *X86_64ABIInfo::
1904GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00001905 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001906 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1907 // returning an 8-byte unit starting with it. See if we can safely use it.
1908 if (IROffset == 0) {
1909 // Pointers and int64's always fill the 8-byte unit.
Derek Schuffbabaf312012-10-11 15:52:22 +00001910 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
1911 IRType->isIntegerTy(64))
Chris Lattnere2962be2010-07-29 07:30:00 +00001912 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001913
Chris Lattnere2962be2010-07-29 07:30:00 +00001914 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1915 // goodness in the source type is just tail padding. This is allowed to
1916 // kick in for struct {double,int} on the int, but not on
1917 // struct{double,int,int} because we wouldn't return the second int. We
1918 // have to do this analysis on the source type because we can't depend on
1919 // unions being lowered a specific way etc.
1920 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
Derek Schuffbabaf312012-10-11 15:52:22 +00001921 IRType->isIntegerTy(32) ||
1922 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
1923 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
1924 cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001925
Chris Lattnere2962be2010-07-29 07:30:00 +00001926 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1927 SourceOffset*8+64, getContext()))
1928 return IRType;
1929 }
1930 }
Chris Lattner49382de2010-07-28 22:44:07 +00001931
Chris Lattner2acc6e32011-07-18 04:24:23 +00001932 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001933 // If this is a struct, recurse into the field at the specified offset.
Micah Villmow25a6a842012-10-08 16:25:52 +00001934 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001935 if (IROffset < SL->getSizeInBytes()) {
1936 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1937 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001938
Chris Lattner0d2656d2010-07-29 17:40:35 +00001939 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1940 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001941 }
Chris Lattner49382de2010-07-28 22:44:07 +00001942 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001943
Chris Lattner2acc6e32011-07-18 04:24:23 +00001944 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001945 llvm::Type *EltTy = ATy->getElementType();
Micah Villmow25a6a842012-10-08 16:25:52 +00001946 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
Chris Lattner021c3a32010-07-29 07:43:55 +00001947 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00001948 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1949 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00001950 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001951
Chris Lattner49382de2010-07-28 22:44:07 +00001952 // Okay, we don't have any better idea of what to pass, so we pass this in an
1953 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001954 unsigned TySizeInBytes =
1955 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00001956
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001957 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001958
Chris Lattner49382de2010-07-28 22:44:07 +00001959 // It is always safe to classify this as an integer type up to i64 that
1960 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001961 return llvm::IntegerType::get(getVMContext(),
1962 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00001963}
1964
Chris Lattner66e7b682010-09-01 00:50:20 +00001965
1966/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1967/// be used as elements of a two register pair to pass or return, return a
1968/// first class aggregate to represent them. For example, if the low part of
1969/// a by-value argument should be passed as i32* and the high part as float,
1970/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001971static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00001972GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Micah Villmow25a6a842012-10-08 16:25:52 +00001973 const llvm::DataLayout &TD) {
Chris Lattner66e7b682010-09-01 00:50:20 +00001974 // In order to correctly satisfy the ABI, we need to the high part to start
1975 // at offset 8. If the high and low parts we inferred are both 4-byte types
1976 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1977 // the second element at offset 8. Check for this:
1978 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1979 unsigned HiAlign = TD.getABITypeAlignment(Hi);
Micah Villmow25a6a842012-10-08 16:25:52 +00001980 unsigned HiStart = llvm::DataLayout::RoundUpAlignment(LoSize, HiAlign);
Chris Lattner66e7b682010-09-01 00:50:20 +00001981 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001982
Chris Lattner66e7b682010-09-01 00:50:20 +00001983 // To handle this, we have to increase the size of the low part so that the
1984 // second element will start at an 8 byte offset. We can't increase the size
1985 // of the second element because it might make us access off the end of the
1986 // struct.
1987 if (HiStart != 8) {
1988 // There are only two sorts of types the ABI generation code can produce for
1989 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1990 // Promote these to a larger type.
1991 if (Lo->isFloatTy())
1992 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1993 else {
1994 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1995 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1996 }
1997 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001998
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001999 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002000
2001
Chris Lattner66e7b682010-09-01 00:50:20 +00002002 // Verify that the second element is at an 8-byte offset.
2003 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
2004 "Invalid x86-64 argument pair!");
2005 return Result;
2006}
2007
Chris Lattner519f68c2010-07-28 23:06:14 +00002008ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00002009classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00002010 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
2011 // classification algorithm.
2012 X86_64ABIInfo::Class Lo, Hi;
2013 classify(RetTy, 0, Lo, Hi);
2014
2015 // Check some invariants.
2016 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002017 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2018
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002019 llvm::Type *ResType = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002020 switch (Lo) {
2021 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002022 if (Hi == NoClass)
2023 return ABIArgInfo::getIgnore();
2024 // If the low part is just padding, it takes no register, leave ResType
2025 // null.
2026 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2027 "Unknown missing lo part");
2028 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002029
2030 case SSEUp:
2031 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002032 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002033
2034 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
2035 // hidden argument.
2036 case Memory:
2037 return getIndirectReturnResult(RetTy);
2038
2039 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
2040 // available register of the sequence %rax, %rdx is used.
2041 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002042 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002043
Chris Lattnereb518b42010-07-29 21:42:50 +00002044 // If we have a sign or zero extended integer, make sure to return Extend
2045 // so that the parameter gets the right LLVM IR attributes.
2046 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2047 // Treat an enum type as its underlying type.
2048 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2049 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002050
Chris Lattnereb518b42010-07-29 21:42:50 +00002051 if (RetTy->isIntegralOrEnumerationType() &&
2052 RetTy->isPromotableIntegerType())
2053 return ABIArgInfo::getExtend();
2054 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002055 break;
2056
2057 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
2058 // available SSE register of the sequence %xmm0, %xmm1 is used.
2059 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002060 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00002061 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002062
2063 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
2064 // returned on the X87 stack in %st0 as 80-bit x87 number.
2065 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00002066 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00002067 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002068
2069 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
2070 // part of the value is returned in %st0 and the imaginary part in
2071 // %st1.
2072 case ComplexX87:
2073 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00002074 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00002075 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00002076 NULL);
2077 break;
2078 }
2079
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002080 llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002081 switch (Hi) {
2082 // Memory was handled previously and X87 should
2083 // never occur as a hi class.
2084 case Memory:
2085 case X87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002086 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002087
2088 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00002089 case NoClass:
2090 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002091
Chris Lattner3db4dde2010-09-01 00:20:33 +00002092 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002093 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002094 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2095 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002096 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00002097 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002098 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002099 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2100 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002101 break;
2102
2103 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002104 // is passed in the next available eightbyte chunk if the last used
2105 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00002106 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002107 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00002108 case SSEUp:
2109 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002110 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00002111 break;
2112
2113 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
2114 // returned together with the previous X87 value in %st0.
2115 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002116 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00002117 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002118 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00002119 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00002120 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002121 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002122 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2123 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00002124 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002125 break;
2126 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002127
Chris Lattner3db4dde2010-09-01 00:20:33 +00002128 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00002129 // known to pass in the high eightbyte of the result. We do this by forming a
2130 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00002131 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002132 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Chris Lattner519f68c2010-07-28 23:06:14 +00002133
Chris Lattnereb518b42010-07-29 21:42:50 +00002134 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00002135}
2136
Daniel Dunbaredfac032012-03-10 01:03:58 +00002137ABIArgInfo X86_64ABIInfo::classifyArgumentType(
2138 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE)
2139 const
2140{
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002141 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00002142 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002143
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002144 // Check some invariants.
2145 // FIXME: Enforce these by construction.
2146 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002147 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2148
2149 neededInt = 0;
2150 neededSSE = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002151 llvm::Type *ResType = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002152 switch (Lo) {
2153 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002154 if (Hi == NoClass)
2155 return ABIArgInfo::getIgnore();
2156 // If the low part is just padding, it takes no register, leave ResType
2157 // null.
2158 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2159 "Unknown missing lo part");
2160 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002161
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002162 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
2163 // on the stack.
2164 case Memory:
2165
2166 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
2167 // COMPLEX_X87, it is passed in memory.
2168 case X87:
2169 case ComplexX87:
Eli Friedmanded137f2011-06-29 07:04:55 +00002170 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2171 ++neededInt;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002172 return getIndirectResult(Ty, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002173
2174 case SSEUp:
2175 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002176 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002177
2178 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
2179 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
2180 // and %r9 is used.
2181 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00002182 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002183
Chris Lattner49382de2010-07-28 22:44:07 +00002184 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002185 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00002186
2187 // If we have a sign or zero extended integer, make sure to return Extend
2188 // so that the parameter gets the right LLVM IR attributes.
2189 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2190 // Treat an enum type as its underlying type.
2191 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2192 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002193
Chris Lattnereb518b42010-07-29 21:42:50 +00002194 if (Ty->isIntegralOrEnumerationType() &&
2195 Ty->isPromotableIntegerType())
2196 return ABIArgInfo::getExtend();
2197 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002198
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002199 break;
2200
2201 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
2202 // available SSE register is used, the registers are taken in the
2203 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00002204 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002205 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00002206 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00002207 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002208 break;
2209 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00002210 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002211
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002212 llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002213 switch (Hi) {
2214 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002215 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002216 // which is passed in memory.
2217 case Memory:
2218 case X87:
2219 case ComplexX87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002220 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002221
2222 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002223
Chris Lattner645406a2010-09-01 00:24:35 +00002224 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002225 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00002226 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002227 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002228
Chris Lattner645406a2010-09-01 00:24:35 +00002229 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2230 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002231 break;
2232
2233 // X87Up generally doesn't occur here (long double is passed in
2234 // memory), except in situations involving unions.
2235 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00002236 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002237 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002238
Chris Lattner645406a2010-09-01 00:24:35 +00002239 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2240 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00002241
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002242 ++neededSSE;
2243 break;
2244
2245 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
2246 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002247 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002248 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00002249 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002250 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002251 break;
2252 }
2253
Chris Lattner645406a2010-09-01 00:24:35 +00002254 // If a high part was specified, merge it together with the low part. It is
2255 // known to pass in the high eightbyte of the result. We do this by forming a
2256 // first class struct aggregate with the high and low part: {low, high}
2257 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002258 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002259
Chris Lattnereb518b42010-07-29 21:42:50 +00002260 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002261}
2262
Chris Lattneree5dcd02010-07-29 02:31:05 +00002263void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002264
Chris Lattnera3c109b2010-07-29 02:16:43 +00002265 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002266
2267 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00002268 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002269
2270 // If the return value is indirect, then the hidden argument is consuming one
2271 // integer register.
2272 if (FI.getReturnInfo().isIndirect())
2273 --freeIntRegs;
2274
2275 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2276 // get assigned (in left-to-right order) for passing as follows...
2277 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2278 it != ie; ++it) {
Bill Wendling99aaae82010-10-18 23:51:38 +00002279 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002280 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
2281 neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002282
2283 // AMD64-ABI 3.2.3p3: If there are no registers available for any
2284 // eightbyte of an argument, the whole argument is passed on the
2285 // stack. If registers have already been assigned for some
2286 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00002287 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002288 freeIntRegs -= neededInt;
2289 freeSSERegs -= neededSSE;
2290 } else {
Daniel Dunbaredfac032012-03-10 01:03:58 +00002291 it->info = getIndirectResult(it->type, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002292 }
2293 }
2294}
2295
2296static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2297 QualType Ty,
2298 CodeGenFunction &CGF) {
2299 llvm::Value *overflow_arg_area_p =
2300 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2301 llvm::Value *overflow_arg_area =
2302 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2303
2304 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2305 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedman8d2fe422011-11-18 02:44:19 +00002306 // It isn't stated explicitly in the standard, but in practice we use
2307 // alignment greater than 16 where necessary.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002308 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2309 if (Align > 8) {
Eli Friedman8d2fe422011-11-18 02:44:19 +00002310 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
Owen Anderson0032b272009-08-13 21:57:51 +00002311 llvm::Value *Offset =
Eli Friedman8d2fe422011-11-18 02:44:19 +00002312 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002313 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2314 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00002315 CGF.Int64Ty);
Eli Friedman8d2fe422011-11-18 02:44:19 +00002316 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002317 overflow_arg_area =
2318 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2319 overflow_arg_area->getType(),
2320 "overflow_arg_area.align");
2321 }
2322
2323 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002324 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002325 llvm::Value *Res =
2326 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002327 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002328
2329 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2330 // l->overflow_arg_area + sizeof(type).
2331 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2332 // an 8 byte boundary.
2333
2334 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00002335 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00002336 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002337 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2338 "overflow_arg_area.next");
2339 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2340
2341 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2342 return Res;
2343}
2344
2345llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2346 CodeGenFunction &CGF) const {
2347 // Assume that va_list type is correct; should be pointer to LLVM type:
2348 // struct {
2349 // i32 gp_offset;
2350 // i32 fp_offset;
2351 // i8* overflow_arg_area;
2352 // i8* reg_save_area;
2353 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00002354 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002355
Chris Lattnera14db752010-03-11 18:19:55 +00002356 Ty = CGF.getContext().getCanonicalType(Ty);
Daniel Dunbaredfac032012-03-10 01:03:58 +00002357 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002358
2359 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2360 // in the registers. If not go to step 7.
2361 if (!neededInt && !neededSSE)
2362 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2363
2364 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2365 // general purpose registers needed to pass type and num_fp to hold
2366 // the number of floating point registers needed.
2367
2368 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2369 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2370 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2371 //
2372 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2373 // register save space).
2374
2375 llvm::Value *InRegs = 0;
2376 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2377 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2378 if (neededInt) {
2379 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2380 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002381 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2382 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002383 }
2384
2385 if (neededSSE) {
2386 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2387 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2388 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00002389 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2390 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002391 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2392 }
2393
2394 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2395 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2396 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2397 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2398
2399 // Emit code to load the value if it was passed in registers.
2400
2401 CGF.EmitBlock(InRegBlock);
2402
2403 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2404 // an offset of l->gp_offset and/or l->fp_offset. This may require
2405 // copying to a temporary location in case the parameter is passed
2406 // in different register classes or requires an alignment greater
2407 // than 8 for general purpose registers and 16 for XMM registers.
2408 //
2409 // FIXME: This really results in shameful code when we end up needing to
2410 // collect arguments from different places; often what should result in a
2411 // simple assembling of a structure from scattered addresses has many more
2412 // loads than necessary. Can we clean this up?
Chris Lattner2acc6e32011-07-18 04:24:23 +00002413 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002414 llvm::Value *RegAddr =
2415 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2416 "reg_save_area");
2417 if (neededInt && neededSSE) {
2418 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002419 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002420 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002421 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2422 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002423 llvm::Type *TyLo = ST->getElementType(0);
2424 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002425 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002426 "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002427 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2428 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002429 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2430 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002431 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2432 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002433 llvm::Value *V =
2434 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2435 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2436 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2437 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2438
Owen Andersona1cf15f2009-07-14 23:10:40 +00002439 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002440 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002441 } else if (neededInt) {
2442 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2443 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002444 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002445 } else if (neededSSE == 1) {
2446 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2447 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2448 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002449 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002450 assert(neededSSE == 2 && "Invalid number of needed registers!");
2451 // SSE registers are spaced 16 bytes apart in the register save
2452 // area, we need to collect the two eightbytes together.
2453 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002454 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner8b418682012-02-07 00:39:47 +00002455 llvm::Type *DoubleTy = CGF.DoubleTy;
Chris Lattner2acc6e32011-07-18 04:24:23 +00002456 llvm::Type *DblPtrTy =
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002457 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002458 llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002459 DoubleTy, NULL);
2460 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2461 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2462 DblPtrTy));
2463 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2464 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2465 DblPtrTy));
2466 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2467 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2468 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002469 }
2470
2471 // AMD64-ABI 3.5.7p5: Step 5. Set:
2472 // l->gp_offset = l->gp_offset + num_gp * 8
2473 // l->fp_offset = l->fp_offset + num_fp * 16.
2474 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002475 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002476 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2477 gp_offset_p);
2478 }
2479 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002480 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002481 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2482 fp_offset_p);
2483 }
2484 CGF.EmitBranch(ContBlock);
2485
2486 // Emit code to load the value if it was passed in memory.
2487
2488 CGF.EmitBlock(InMemBlock);
2489 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2490
2491 // Return the appropriate result.
2492
2493 CGF.EmitBlock(ContBlock);
Jay Foadbbf3bac2011-03-30 11:28:58 +00002494 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002495 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002496 ResAddr->addIncoming(RegAddr, InRegBlock);
2497 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002498 return ResAddr;
2499}
2500
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002501ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2502
2503 if (Ty->isVoidType())
2504 return ABIArgInfo::getIgnore();
2505
2506 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2507 Ty = EnumTy->getDecl()->getIntegerType();
2508
2509 uint64_t Size = getContext().getTypeSize(Ty);
2510
2511 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002512 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2513 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002514 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2515
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002516 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2517 if (Size == 128 &&
Eli Friedman55fc7e22012-01-25 22:46:34 +00002518 getContext().getTargetInfo().getTriple().getOS()
2519 == llvm::Triple::MinGW32)
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002520 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2521 Size));
2522
2523 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2524 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2525 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002526 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002527 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2528 Size));
2529
2530 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2531 }
2532
2533 if (Ty->isPromotableIntegerType())
2534 return ABIArgInfo::getExtend();
2535
2536 return ABIArgInfo::getDirect();
2537}
2538
2539void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2540
2541 QualType RetTy = FI.getReturnType();
2542 FI.getReturnInfo() = classify(RetTy);
2543
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002544 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2545 it != ie; ++it)
2546 it->info = classify(it->type);
2547}
2548
Chris Lattnerf13721d2010-08-31 16:44:54 +00002549llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2550 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002551 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002552
Chris Lattnerf13721d2010-08-31 16:44:54 +00002553 CGBuilderTy &Builder = CGF.Builder;
2554 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2555 "ap");
2556 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2557 llvm::Type *PTy =
2558 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2559 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2560
2561 uint64_t Offset =
2562 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2563 llvm::Value *NextAddr =
2564 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2565 "ap.next");
2566 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2567
2568 return AddrTyped;
2569}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002570
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00002571namespace {
2572
Derek Schuff263366f2012-10-16 22:30:41 +00002573class NaClX86_64ABIInfo : public ABIInfo {
2574 public:
2575 NaClX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2576 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, HasAVX) {}
2577 virtual void computeInfo(CGFunctionInfo &FI) const;
2578 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2579 CodeGenFunction &CGF) const;
2580 private:
2581 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
2582 X86_64ABIInfo NInfo; // Used for everything else.
2583};
2584
2585class NaClX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2586 public:
2587 NaClX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2588 : TargetCodeGenInfo(new NaClX86_64ABIInfo(CGT, HasAVX)) {}
2589};
2590
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00002591}
2592
Derek Schuff263366f2012-10-16 22:30:41 +00002593void NaClX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2594 if (FI.getASTCallingConvention() == CC_PnaclCall)
2595 PInfo.computeInfo(FI);
2596 else
2597 NInfo.computeInfo(FI);
2598}
2599
2600llvm::Value *NaClX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2601 CodeGenFunction &CGF) const {
2602 // Always use the native convention; calling pnacl-style varargs functions
2603 // is unuspported.
2604 return NInfo.EmitVAArg(VAListAddr, Ty, CGF);
2605}
2606
2607
John McCallec853ba2010-03-11 00:10:12 +00002608// PowerPC-32
2609
2610namespace {
2611class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2612public:
Chris Lattnerea044322010-07-29 02:01:43 +00002613 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002614
John McCallec853ba2010-03-11 00:10:12 +00002615 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2616 // This is recovered from gcc output.
2617 return 1; // r1 is the dedicated stack pointer
2618 }
2619
2620 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002621 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002622};
2623
2624}
2625
2626bool
2627PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2628 llvm::Value *Address) const {
2629 // This is calculated from the LLVM and GCC tables and verified
2630 // against gcc output. AFAIK all ABIs use the same encoding.
2631
2632 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallec853ba2010-03-11 00:10:12 +00002633
Chris Lattner8b418682012-02-07 00:39:47 +00002634 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallec853ba2010-03-11 00:10:12 +00002635 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2636 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2637 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2638
2639 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002640 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002641
2642 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002643 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002644
2645 // 64-76 are various 4-byte special-purpose registers:
2646 // 64: mq
2647 // 65: lr
2648 // 66: ctr
2649 // 67: ap
2650 // 68-75 cr0-7
2651 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002652 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002653
2654 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002655 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002656
2657 // 109: vrsave
2658 // 110: vscr
2659 // 111: spe_acc
2660 // 112: spefscr
2661 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002662 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002663
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002664 return false;
John McCallec853ba2010-03-11 00:10:12 +00002665}
2666
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002667// PowerPC-64
2668
2669namespace {
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002670/// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
2671class PPC64_SVR4_ABIInfo : public DefaultABIInfo {
2672
2673public:
2674 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
2675
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002676 bool isPromotableTypeForABI(QualType Ty) const;
2677
2678 ABIArgInfo classifyReturnType(QualType RetTy) const;
2679 ABIArgInfo classifyArgumentType(QualType Ty) const;
2680
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00002681 // TODO: We can add more logic to computeInfo to improve performance.
2682 // Example: For aggregate arguments that fit in a register, we could
2683 // use getDirectInReg (as is done below for structs containing a single
2684 // floating-point value) to avoid pushing them to memory on function
2685 // entry. This would require changing the logic in PPCISelLowering
2686 // when lowering the parameters in the caller and args in the callee.
2687 virtual void computeInfo(CGFunctionInfo &FI) const {
2688 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2689 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2690 it != ie; ++it) {
2691 // We rely on the default argument classification for the most part.
2692 // One exception: An aggregate containing a single floating-point
2693 // item must be passed in a register if one is available.
2694 const Type *T = isSingleElementStruct(it->type, getContext());
2695 if (T) {
2696 const BuiltinType *BT = T->getAs<BuiltinType>();
2697 if (BT && BT->isFloatingPoint()) {
2698 QualType QT(T, 0);
2699 it->info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
2700 continue;
2701 }
2702 }
2703 it->info = classifyArgumentType(it->type);
2704 }
2705 }
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002706
2707 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr,
2708 QualType Ty,
2709 CodeGenFunction &CGF) const;
2710};
2711
2712class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
2713public:
2714 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT)
2715 : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT)) {}
2716
2717 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2718 // This is recovered from gcc output.
2719 return 1; // r1 is the dedicated stack pointer
2720 }
2721
2722 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2723 llvm::Value *Address) const;
2724};
2725
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002726class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2727public:
2728 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
2729
2730 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2731 // This is recovered from gcc output.
2732 return 1; // r1 is the dedicated stack pointer
2733 }
2734
2735 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2736 llvm::Value *Address) const;
2737};
2738
2739}
2740
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002741// Return true if the ABI requires Ty to be passed sign- or zero-
2742// extended to 64 bits.
2743bool
2744PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
2745 // Treat an enum type as its underlying type.
2746 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2747 Ty = EnumTy->getDecl()->getIntegerType();
2748
2749 // Promotable integer types are required to be promoted by the ABI.
2750 if (Ty->isPromotableIntegerType())
2751 return true;
2752
2753 // In addition to the usual promotable integer types, we also need to
2754 // extend all 32-bit types, since the ABI requires promotion to 64 bits.
2755 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2756 switch (BT->getKind()) {
2757 case BuiltinType::Int:
2758 case BuiltinType::UInt:
2759 return true;
2760 default:
2761 break;
2762 }
2763
2764 return false;
2765}
2766
2767ABIArgInfo
2768PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
Bill Schmidtc9715fc2012-11-27 02:46:43 +00002769 if (Ty->isAnyComplexType())
2770 return ABIArgInfo::getDirect();
2771
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002772 if (isAggregateTypeForABI(Ty)) {
2773 // Records with non trivial destructors/constructors should not be passed
2774 // by value.
2775 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2776 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2777
2778 return ABIArgInfo::getIndirect(0);
2779 }
2780
2781 return (isPromotableTypeForABI(Ty) ?
2782 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2783}
2784
2785ABIArgInfo
2786PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
2787 if (RetTy->isVoidType())
2788 return ABIArgInfo::getIgnore();
2789
Bill Schmidt9e6111a2012-12-17 04:20:17 +00002790 if (RetTy->isAnyComplexType())
2791 return ABIArgInfo::getDirect();
2792
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002793 if (isAggregateTypeForABI(RetTy))
2794 return ABIArgInfo::getIndirect(0);
2795
2796 return (isPromotableTypeForABI(RetTy) ?
2797 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2798}
2799
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002800// Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
2801llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr,
2802 QualType Ty,
2803 CodeGenFunction &CGF) const {
2804 llvm::Type *BP = CGF.Int8PtrTy;
2805 llvm::Type *BPP = CGF.Int8PtrPtrTy;
2806
2807 CGBuilderTy &Builder = CGF.Builder;
2808 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
2809 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2810
Bill Schmidt19f8e852013-01-14 17:45:36 +00002811 // Update the va_list pointer. The pointer should be bumped by the
2812 // size of the object. We can trust getTypeSize() except for a complex
2813 // type whose base type is smaller than a doubleword. For these, the
2814 // size of the object is 16 bytes; see below for further explanation.
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002815 unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8;
Bill Schmidt19f8e852013-01-14 17:45:36 +00002816 QualType BaseTy;
2817 unsigned CplxBaseSize = 0;
2818
2819 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
2820 BaseTy = CTy->getElementType();
2821 CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8;
2822 if (CplxBaseSize < 8)
2823 SizeInBytes = 16;
2824 }
2825
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002826 unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8);
2827 llvm::Value *NextAddr =
2828 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset),
2829 "ap.next");
2830 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2831
Bill Schmidt19f8e852013-01-14 17:45:36 +00002832 // If we have a complex type and the base type is smaller than 8 bytes,
2833 // the ABI calls for the real and imaginary parts to be right-adjusted
2834 // in separate doublewords. However, Clang expects us to produce a
2835 // pointer to a structure with the two parts packed tightly. So generate
2836 // loads of the real and imaginary parts relative to the va_list pointer,
2837 // and store them to a temporary structure.
2838 if (CplxBaseSize && CplxBaseSize < 8) {
2839 llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2840 llvm::Value *ImagAddr = RealAddr;
2841 RealAddr = Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize));
2842 ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize));
2843 llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy));
2844 RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy);
2845 ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy);
2846 llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal");
2847 llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag");
2848 llvm::Value *Ptr = CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty),
2849 "vacplx");
2850 llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, ".real");
2851 llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, ".imag");
2852 Builder.CreateStore(Real, RealPtr, false);
2853 Builder.CreateStore(Imag, ImagPtr, false);
2854 return Ptr;
2855 }
2856
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002857 // If the argument is smaller than 8 bytes, it is right-adjusted in
2858 // its doubleword slot. Adjust the pointer to pick it up from the
2859 // correct offset.
2860 if (SizeInBytes < 8) {
2861 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2862 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes));
2863 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2864 }
2865
2866 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2867 return Builder.CreateBitCast(Addr, PTy);
2868}
2869
2870static bool
2871PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2872 llvm::Value *Address) {
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002873 // This is calculated from the LLVM and GCC tables and verified
2874 // against gcc output. AFAIK all ABIs use the same encoding.
2875
2876 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2877
2878 llvm::IntegerType *i8 = CGF.Int8Ty;
2879 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2880 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2881 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2882
2883 // 0-31: r0-31, the 8-byte general-purpose registers
2884 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
2885
2886 // 32-63: fp0-31, the 8-byte floating-point registers
2887 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
2888
2889 // 64-76 are various 4-byte special-purpose registers:
2890 // 64: mq
2891 // 65: lr
2892 // 66: ctr
2893 // 67: ap
2894 // 68-75 cr0-7
2895 // 76: xer
2896 AssignToArrayRange(Builder, Address, Four8, 64, 76);
2897
2898 // 77-108: v0-31, the 16-byte vector registers
2899 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
2900
2901 // 109: vrsave
2902 // 110: vscr
2903 // 111: spe_acc
2904 // 112: spefscr
2905 // 113: sfp
2906 AssignToArrayRange(Builder, Address, Four8, 109, 113);
2907
2908 return false;
2909}
John McCallec853ba2010-03-11 00:10:12 +00002910
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002911bool
2912PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
2913 CodeGen::CodeGenFunction &CGF,
2914 llvm::Value *Address) const {
2915
2916 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
2917}
2918
2919bool
2920PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2921 llvm::Value *Address) const {
2922
2923 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
2924}
2925
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002926//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002927// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002928//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002929
2930namespace {
2931
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002932class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002933public:
2934 enum ABIKind {
2935 APCS = 0,
2936 AAPCS = 1,
2937 AAPCS_VFP
2938 };
2939
2940private:
2941 ABIKind Kind;
2942
2943public:
John McCallbd7370a2013-02-28 19:01:20 +00002944 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {
2945 setRuntimeCC();
2946 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002947
John McCall49e34be2011-08-30 01:42:09 +00002948 bool isEABI() const {
Eli Friedman55fc7e22012-01-25 22:46:34 +00002949 StringRef Env =
2950 getContext().getTargetInfo().getTriple().getEnvironmentName();
Logan Chien94a71422012-09-02 09:30:11 +00002951 return (Env == "gnueabi" || Env == "eabi" ||
2952 Env == "android" || Env == "androideabi");
John McCall49e34be2011-08-30 01:42:09 +00002953 }
2954
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002955private:
2956 ABIKind getABIKind() const { return Kind; }
2957
Chris Lattnera3c109b2010-07-29 02:16:43 +00002958 ABIArgInfo classifyReturnType(QualType RetTy) const;
Manman Ren710c5172012-10-31 19:02:26 +00002959 ABIArgInfo classifyArgumentType(QualType RetTy, int *VFPRegs,
2960 unsigned &AllocatedVFP,
Manman Renb3fa55f2012-10-30 23:21:41 +00002961 bool &IsHA) const;
Manman Ren97f81572012-10-16 19:18:39 +00002962 bool isIllegalVectorType(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002963
Chris Lattneree5dcd02010-07-29 02:31:05 +00002964 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002965
2966 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2967 CodeGenFunction &CGF) const;
John McCallbd7370a2013-02-28 19:01:20 +00002968
2969 llvm::CallingConv::ID getLLVMDefaultCC() const;
2970 llvm::CallingConv::ID getABIDefaultCC() const;
2971 void setRuntimeCC();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002972};
2973
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002974class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2975public:
Chris Lattnerea044322010-07-29 02:01:43 +00002976 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2977 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00002978
John McCall49e34be2011-08-30 01:42:09 +00002979 const ARMABIInfo &getABIInfo() const {
2980 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
2981 }
2982
John McCall6374c332010-03-06 00:35:14 +00002983 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2984 return 13;
2985 }
Roman Divacky09345d12011-05-18 19:36:54 +00002986
Chris Lattner5f9e2722011-07-23 10:55:15 +00002987 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCallf85e1932011-06-15 23:02:42 +00002988 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2989 }
2990
Roman Divacky09345d12011-05-18 19:36:54 +00002991 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2992 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002993 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divacky09345d12011-05-18 19:36:54 +00002994
2995 // 0-15 are the 16 integer registers.
Chris Lattner8b418682012-02-07 00:39:47 +00002996 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divacky09345d12011-05-18 19:36:54 +00002997 return false;
2998 }
John McCall49e34be2011-08-30 01:42:09 +00002999
3000 unsigned getSizeOfUnwindException() const {
3001 if (getABIInfo().isEABI()) return 88;
3002 return TargetCodeGenInfo::getSizeOfUnwindException();
3003 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003004};
3005
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003006}
3007
Chris Lattneree5dcd02010-07-29 02:31:05 +00003008void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Manman Renb3fa55f2012-10-30 23:21:41 +00003009 // To correctly handle Homogeneous Aggregate, we need to keep track of the
Manman Ren710c5172012-10-31 19:02:26 +00003010 // VFP registers allocated so far.
Manman Renb3fa55f2012-10-30 23:21:41 +00003011 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
3012 // VFP registers of the appropriate type unallocated then the argument is
3013 // allocated to the lowest-numbered sequence of such registers.
3014 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
3015 // unallocated are marked as unavailable.
3016 unsigned AllocatedVFP = 0;
Manman Ren710c5172012-10-31 19:02:26 +00003017 int VFPRegs[16] = { 0 };
Chris Lattnera3c109b2010-07-29 02:16:43 +00003018 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003019 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Manman Renb3fa55f2012-10-30 23:21:41 +00003020 it != ie; ++it) {
3021 unsigned PreAllocation = AllocatedVFP;
3022 bool IsHA = false;
3023 // 6.1.2.3 There is one VFP co-processor register class using registers
3024 // s0-s15 (d0-d7) for passing arguments.
3025 const unsigned NumVFPs = 16;
Manman Ren710c5172012-10-31 19:02:26 +00003026 it->info = classifyArgumentType(it->type, VFPRegs, AllocatedVFP, IsHA);
Manman Renb3fa55f2012-10-30 23:21:41 +00003027 // If we do not have enough VFP registers for the HA, any VFP registers
3028 // that are unallocated are marked as unavailable. To achieve this, we add
3029 // padding of (NumVFPs - PreAllocation) floats.
3030 if (IsHA && AllocatedVFP > NumVFPs && PreAllocation < NumVFPs) {
3031 llvm::Type *PaddingTy = llvm::ArrayType::get(
3032 llvm::Type::getFloatTy(getVMContext()), NumVFPs - PreAllocation);
3033 it->info = ABIArgInfo::getExpandWithPadding(false, PaddingTy);
3034 }
3035 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003036
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003037 // Always honor user-specified calling convention.
3038 if (FI.getCallingConvention() != llvm::CallingConv::C)
3039 return;
3040
John McCallbd7370a2013-02-28 19:01:20 +00003041 llvm::CallingConv::ID cc = getRuntimeCC();
3042 if (cc != llvm::CallingConv::C)
3043 FI.setEffectiveCallingConvention(cc);
3044}
Rafael Espindola25117ab2010-06-16 16:13:39 +00003045
John McCallbd7370a2013-02-28 19:01:20 +00003046/// Return the default calling convention that LLVM will use.
3047llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
3048 // The default calling convention that LLVM will infer.
3049 if (getContext().getTargetInfo().getTriple().getEnvironmentName()=="gnueabihf")
3050 return llvm::CallingConv::ARM_AAPCS_VFP;
3051 else if (isEABI())
3052 return llvm::CallingConv::ARM_AAPCS;
3053 else
3054 return llvm::CallingConv::ARM_APCS;
3055}
3056
3057/// Return the calling convention that our ABI would like us to use
3058/// as the C calling convention.
3059llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003060 switch (getABIKind()) {
John McCallbd7370a2013-02-28 19:01:20 +00003061 case APCS: return llvm::CallingConv::ARM_APCS;
3062 case AAPCS: return llvm::CallingConv::ARM_AAPCS;
3063 case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003064 }
John McCallbd7370a2013-02-28 19:01:20 +00003065 llvm_unreachable("bad ABI kind");
3066}
3067
3068void ARMABIInfo::setRuntimeCC() {
3069 assert(getRuntimeCC() == llvm::CallingConv::C);
3070
3071 // Don't muddy up the IR with a ton of explicit annotations if
3072 // they'd just match what LLVM will infer from the triple.
3073 llvm::CallingConv::ID abiCC = getABIDefaultCC();
3074 if (abiCC != getLLVMDefaultCC())
3075 RuntimeCC = abiCC;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003076}
3077
Bob Wilson194f06a2011-08-03 05:58:22 +00003078/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
3079/// aggregate. If HAMembers is non-null, the number of base elements
3080/// contained in the type is returned through it; this is used for the
3081/// recursive calls that check aggregate component types.
3082static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
3083 ASTContext &Context,
3084 uint64_t *HAMembers = 0) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003085 uint64_t Members = 0;
Bob Wilson194f06a2011-08-03 05:58:22 +00003086 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
3087 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
3088 return false;
3089 Members *= AT->getSize().getZExtValue();
3090 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
3091 const RecordDecl *RD = RT->getDecl();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003092 if (RD->hasFlexibleArrayMember())
Bob Wilson194f06a2011-08-03 05:58:22 +00003093 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003094
Bob Wilson194f06a2011-08-03 05:58:22 +00003095 Members = 0;
3096 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3097 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00003098 const FieldDecl *FD = *i;
Bob Wilson194f06a2011-08-03 05:58:22 +00003099 uint64_t FldMembers;
3100 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
3101 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003102
3103 Members = (RD->isUnion() ?
3104 std::max(Members, FldMembers) : Members + FldMembers);
Bob Wilson194f06a2011-08-03 05:58:22 +00003105 }
3106 } else {
3107 Members = 1;
3108 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
3109 Members = 2;
3110 Ty = CT->getElementType();
3111 }
3112
3113 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
3114 // double, or 64-bit or 128-bit vectors.
3115 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3116 if (BT->getKind() != BuiltinType::Float &&
Tim Northoveradfa45f2012-07-20 22:29:29 +00003117 BT->getKind() != BuiltinType::Double &&
3118 BT->getKind() != BuiltinType::LongDouble)
Bob Wilson194f06a2011-08-03 05:58:22 +00003119 return false;
3120 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
3121 unsigned VecSize = Context.getTypeSize(VT);
3122 if (VecSize != 64 && VecSize != 128)
3123 return false;
3124 } else {
3125 return false;
3126 }
3127
3128 // The base type must be the same for all members. Vector types of the
3129 // same total size are treated as being equivalent here.
3130 const Type *TyPtr = Ty.getTypePtr();
3131 if (!Base)
3132 Base = TyPtr;
3133 if (Base != TyPtr &&
3134 (!Base->isVectorType() || !TyPtr->isVectorType() ||
3135 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
3136 return false;
3137 }
3138
3139 // Homogeneous Aggregates can have at most 4 members of the base type.
3140 if (HAMembers)
3141 *HAMembers = Members;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003142
3143 return (Members > 0 && Members <= 4);
Bob Wilson194f06a2011-08-03 05:58:22 +00003144}
3145
Manman Ren710c5172012-10-31 19:02:26 +00003146/// markAllocatedVFPs - update VFPRegs according to the alignment and
3147/// number of VFP registers (unit is S register) requested.
3148static void markAllocatedVFPs(int *VFPRegs, unsigned &AllocatedVFP,
3149 unsigned Alignment,
3150 unsigned NumRequired) {
3151 // Early Exit.
3152 if (AllocatedVFP >= 16)
3153 return;
3154 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
3155 // VFP registers of the appropriate type unallocated then the argument is
3156 // allocated to the lowest-numbered sequence of such registers.
3157 for (unsigned I = 0; I < 16; I += Alignment) {
3158 bool FoundSlot = true;
3159 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3160 if (J >= 16 || VFPRegs[J]) {
3161 FoundSlot = false;
3162 break;
3163 }
3164 if (FoundSlot) {
3165 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3166 VFPRegs[J] = 1;
3167 AllocatedVFP += NumRequired;
3168 return;
3169 }
3170 }
3171 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
3172 // unallocated are marked as unavailable.
3173 for (unsigned I = 0; I < 16; I++)
3174 VFPRegs[I] = 1;
3175 AllocatedVFP = 17; // We do not have enough VFP registers.
3176}
3177
3178ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, int *VFPRegs,
3179 unsigned &AllocatedVFP,
Manman Renb3fa55f2012-10-30 23:21:41 +00003180 bool &IsHA) const {
3181 // We update number of allocated VFPs according to
3182 // 6.1.2.1 The following argument types are VFP CPRCs:
3183 // A single-precision floating-point type (including promoted
3184 // half-precision types); A double-precision floating-point type;
3185 // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
3186 // with a Base Type of a single- or double-precision floating-point type,
3187 // 64-bit containerized vectors or 128-bit containerized vectors with one
3188 // to four Elements.
3189
Manman Ren97f81572012-10-16 19:18:39 +00003190 // Handle illegal vector types here.
3191 if (isIllegalVectorType(Ty)) {
3192 uint64_t Size = getContext().getTypeSize(Ty);
3193 if (Size <= 32) {
3194 llvm::Type *ResType =
3195 llvm::Type::getInt32Ty(getVMContext());
3196 return ABIArgInfo::getDirect(ResType);
3197 }
3198 if (Size == 64) {
3199 llvm::Type *ResType = llvm::VectorType::get(
3200 llvm::Type::getInt32Ty(getVMContext()), 2);
Manman Ren710c5172012-10-31 19:02:26 +00003201 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
Manman Ren97f81572012-10-16 19:18:39 +00003202 return ABIArgInfo::getDirect(ResType);
3203 }
3204 if (Size == 128) {
3205 llvm::Type *ResType = llvm::VectorType::get(
3206 llvm::Type::getInt32Ty(getVMContext()), 4);
Manman Ren710c5172012-10-31 19:02:26 +00003207 markAllocatedVFPs(VFPRegs, AllocatedVFP, 4, 4);
Manman Ren97f81572012-10-16 19:18:39 +00003208 return ABIArgInfo::getDirect(ResType);
3209 }
3210 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3211 }
Manman Ren710c5172012-10-31 19:02:26 +00003212 // Update VFPRegs for legal vector types.
Manman Renb3fa55f2012-10-30 23:21:41 +00003213 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3214 uint64_t Size = getContext().getTypeSize(VT);
3215 // Size of a legal vector should be power of 2 and above 64.
Manman Ren710c5172012-10-31 19:02:26 +00003216 markAllocatedVFPs(VFPRegs, AllocatedVFP, Size >= 128 ? 4 : 2, Size / 32);
Manman Renb3fa55f2012-10-30 23:21:41 +00003217 }
Manman Ren710c5172012-10-31 19:02:26 +00003218 // Update VFPRegs for floating point types.
Manman Renb3fa55f2012-10-30 23:21:41 +00003219 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3220 if (BT->getKind() == BuiltinType::Half ||
3221 BT->getKind() == BuiltinType::Float)
Manman Ren710c5172012-10-31 19:02:26 +00003222 markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, 1);
Manman Renb3fa55f2012-10-30 23:21:41 +00003223 if (BT->getKind() == BuiltinType::Double ||
Manman Ren710c5172012-10-31 19:02:26 +00003224 BT->getKind() == BuiltinType::LongDouble)
3225 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
Manman Renb3fa55f2012-10-30 23:21:41 +00003226 }
Manman Ren97f81572012-10-16 19:18:39 +00003227
John McCalld608cdb2010-08-22 10:59:02 +00003228 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003229 // Treat an enum type as its underlying type.
3230 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3231 Ty = EnumTy->getDecl()->getIntegerType();
3232
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003233 return (Ty->isPromotableIntegerType() ?
3234 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003235 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003236
Daniel Dunbar42025572009-09-14 21:54:03 +00003237 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003238 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00003239 return ABIArgInfo::getIgnore();
3240
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003241 // Structures with either a non-trivial destructor or a non-trivial
3242 // copy constructor are always indirect.
3243 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
3244 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3245
Bob Wilson194f06a2011-08-03 05:58:22 +00003246 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
Manman Renb3fa55f2012-10-30 23:21:41 +00003247 // Homogeneous Aggregates need to be expanded when we can fit the aggregate
3248 // into VFP registers.
Bob Wilson194f06a2011-08-03 05:58:22 +00003249 const Type *Base = 0;
Manman Renb3fa55f2012-10-30 23:21:41 +00003250 uint64_t Members = 0;
3251 if (isHomogeneousAggregate(Ty, Base, getContext(), &Members)) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003252 assert(Base && "Base class should be set for homogeneous aggregate");
Manman Renb3fa55f2012-10-30 23:21:41 +00003253 // Base can be a floating-point or a vector.
3254 if (Base->isVectorType()) {
3255 // ElementSize is in number of floats.
3256 unsigned ElementSize = getContext().getTypeSize(Base) == 64 ? 2 : 4;
Manman Rencb489dd2012-11-06 19:05:29 +00003257 markAllocatedVFPs(VFPRegs, AllocatedVFP, ElementSize,
3258 Members * ElementSize);
Manman Renb3fa55f2012-10-30 23:21:41 +00003259 } else if (Base->isSpecificBuiltinType(BuiltinType::Float))
Manman Ren710c5172012-10-31 19:02:26 +00003260 markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, Members);
Manman Renb3fa55f2012-10-30 23:21:41 +00003261 else {
3262 assert(Base->isSpecificBuiltinType(BuiltinType::Double) ||
3263 Base->isSpecificBuiltinType(BuiltinType::LongDouble));
Manman Ren710c5172012-10-31 19:02:26 +00003264 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, Members * 2);
Manman Renb3fa55f2012-10-30 23:21:41 +00003265 }
3266 IsHA = true;
Bob Wilson194f06a2011-08-03 05:58:22 +00003267 return ABIArgInfo::getExpand();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003268 }
Bob Wilson194f06a2011-08-03 05:58:22 +00003269 }
3270
Manman Ren634b3d22012-08-13 21:23:55 +00003271 // Support byval for ARM.
Manman Rencb489dd2012-11-06 19:05:29 +00003272 // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
3273 // most 8-byte. We realign the indirect argument if type alignment is bigger
3274 // than ABI alignment.
Manman Renfd1ba912012-11-05 22:42:46 +00003275 uint64_t ABIAlign = 4;
3276 uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8;
3277 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3278 getABIKind() == ARMABIInfo::AAPCS)
3279 ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
Manman Ren885ad692012-11-06 04:58:01 +00003280 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
3281 return ABIArgInfo::getIndirect(0, /*ByVal=*/true,
Manman Rencb489dd2012-11-06 19:05:29 +00003282 /*Realign=*/TyAlign > ABIAlign);
Eli Friedman79f30982012-08-09 00:31:40 +00003283 }
3284
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00003285 // Otherwise, pass by coercing to a structure of the appropriate size.
Chris Lattner2acc6e32011-07-18 04:24:23 +00003286 llvm::Type* ElemTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003287 unsigned SizeRegs;
Eli Friedman79f30982012-08-09 00:31:40 +00003288 // FIXME: Try to match the types of the arguments more accurately where
3289 // we can.
3290 if (getContext().getTypeAlign(Ty) <= 32) {
Bob Wilson53fc1a62011-08-01 23:39:04 +00003291 ElemTy = llvm::Type::getInt32Ty(getVMContext());
3292 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Manman Ren78eb76e2012-06-25 22:04:00 +00003293 } else {
Manman Ren78eb76e2012-06-25 22:04:00 +00003294 ElemTy = llvm::Type::getInt64Ty(getVMContext());
3295 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastings67d097e2011-04-27 17:24:02 +00003296 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003297
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003298 llvm::Type *STy =
Chris Lattner7650d952011-06-18 22:49:11 +00003299 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003300 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003301}
3302
Chris Lattnera3c109b2010-07-29 02:16:43 +00003303static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00003304 llvm::LLVMContext &VMContext) {
3305 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
3306 // is called integer-like if its size is less than or equal to one word, and
3307 // the offset of each of its addressable sub-fields is zero.
3308
3309 uint64_t Size = Context.getTypeSize(Ty);
3310
3311 // Check that the type fits in a word.
3312 if (Size > 32)
3313 return false;
3314
3315 // FIXME: Handle vector types!
3316 if (Ty->isVectorType())
3317 return false;
3318
Daniel Dunbarb0d58192009-09-14 02:20:34 +00003319 // Float types are never treated as "integer like".
3320 if (Ty->isRealFloatingType())
3321 return false;
3322
Daniel Dunbar98303b92009-09-13 08:03:58 +00003323 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00003324 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00003325 return true;
3326
Daniel Dunbar45815812010-02-01 23:31:26 +00003327 // Small complex integer types are "integer like".
3328 if (const ComplexType *CT = Ty->getAs<ComplexType>())
3329 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003330
3331 // Single element and zero sized arrays should be allowed, by the definition
3332 // above, but they are not.
3333
3334 // Otherwise, it must be a record type.
3335 const RecordType *RT = Ty->getAs<RecordType>();
3336 if (!RT) return false;
3337
3338 // Ignore records with flexible arrays.
3339 const RecordDecl *RD = RT->getDecl();
3340 if (RD->hasFlexibleArrayMember())
3341 return false;
3342
3343 // Check that all sub-fields are at offset 0, and are themselves "integer
3344 // like".
3345 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3346
3347 bool HadField = false;
3348 unsigned idx = 0;
3349 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3350 i != e; ++i, ++idx) {
David Blaikie581deb32012-06-06 20:45:41 +00003351 const FieldDecl *FD = *i;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003352
Daniel Dunbar679855a2010-01-29 03:22:29 +00003353 // Bit-fields are not addressable, we only need to verify they are "integer
3354 // like". We still have to disallow a subsequent non-bitfield, for example:
3355 // struct { int : 0; int x }
3356 // is non-integer like according to gcc.
3357 if (FD->isBitField()) {
3358 if (!RD->isUnion())
3359 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003360
Daniel Dunbar679855a2010-01-29 03:22:29 +00003361 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3362 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003363
Daniel Dunbar679855a2010-01-29 03:22:29 +00003364 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003365 }
3366
Daniel Dunbar679855a2010-01-29 03:22:29 +00003367 // Check if this field is at offset 0.
3368 if (Layout.getFieldOffset(idx) != 0)
3369 return false;
3370
Daniel Dunbar98303b92009-09-13 08:03:58 +00003371 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3372 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003373
Daniel Dunbar679855a2010-01-29 03:22:29 +00003374 // Only allow at most one field in a structure. This doesn't match the
3375 // wording above, but follows gcc in situations with a field following an
3376 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00003377 if (!RD->isUnion()) {
3378 if (HadField)
3379 return false;
3380
3381 HadField = true;
3382 }
3383 }
3384
3385 return true;
3386}
3387
Chris Lattnera3c109b2010-07-29 02:16:43 +00003388ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003389 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003390 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00003391
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00003392 // Large vector types should be returned via memory.
3393 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
3394 return ABIArgInfo::getIndirect(0);
3395
John McCalld608cdb2010-08-22 10:59:02 +00003396 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003397 // Treat an enum type as its underlying type.
3398 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3399 RetTy = EnumTy->getDecl()->getIntegerType();
3400
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003401 return (RetTy->isPromotableIntegerType() ?
3402 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003403 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003404
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003405 // Structures with either a non-trivial destructor or a non-trivial
3406 // copy constructor are always indirect.
3407 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
3408 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3409
Daniel Dunbar98303b92009-09-13 08:03:58 +00003410 // Are we following APCS?
3411 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00003412 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00003413 return ABIArgInfo::getIgnore();
3414
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003415 // Complex types are all returned as packed integers.
3416 //
3417 // FIXME: Consider using 2 x vector types if the back end handles them
3418 // correctly.
3419 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00003420 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00003421 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003422
Daniel Dunbar98303b92009-09-13 08:03:58 +00003423 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003424 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003425 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003426 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003427 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003428 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003429 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003430 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3431 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003432 }
3433
3434 // Otherwise return in memory.
3435 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003436 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003437
3438 // Otherwise this is an AAPCS variant.
3439
Chris Lattnera3c109b2010-07-29 02:16:43 +00003440 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00003441 return ABIArgInfo::getIgnore();
3442
Bob Wilson3b694fa2011-11-02 04:51:36 +00003443 // Check for homogeneous aggregates with AAPCS-VFP.
3444 if (getABIKind() == AAPCS_VFP) {
3445 const Type *Base = 0;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003446 if (isHomogeneousAggregate(RetTy, Base, getContext())) {
3447 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson3b694fa2011-11-02 04:51:36 +00003448 // Homogeneous Aggregates are returned directly.
3449 return ABIArgInfo::getDirect();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003450 }
Bob Wilson3b694fa2011-11-02 04:51:36 +00003451 }
3452
Daniel Dunbar98303b92009-09-13 08:03:58 +00003453 // Aggregates <= 4 bytes are returned in r0; other aggregates
3454 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003455 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00003456 if (Size <= 32) {
3457 // Return in the smallest viable integer type.
3458 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003459 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003460 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003461 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3462 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003463 }
3464
Daniel Dunbar98303b92009-09-13 08:03:58 +00003465 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003466}
3467
Manman Ren97f81572012-10-16 19:18:39 +00003468/// isIllegalVector - check whether Ty is an illegal vector type.
3469bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
3470 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3471 // Check whether VT is legal.
3472 unsigned NumElements = VT->getNumElements();
3473 uint64_t Size = getContext().getTypeSize(VT);
3474 // NumElements should be power of 2.
3475 if ((NumElements & (NumElements - 1)) != 0)
3476 return true;
3477 // Size should be greater than 32 bits.
3478 return Size <= 32;
3479 }
3480 return false;
3481}
3482
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003483llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00003484 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003485 llvm::Type *BP = CGF.Int8PtrTy;
3486 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003487
3488 CGBuilderTy &Builder = CGF.Builder;
Chris Lattner8b418682012-02-07 00:39:47 +00003489 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003490 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Manman Rend105e732012-10-16 19:01:37 +00003491
3492 uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8;
Rafael Espindolae164c182011-08-02 22:33:37 +00003493 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
Manman Ren97f81572012-10-16 19:18:39 +00003494 bool IsIndirect = false;
Manman Rend105e732012-10-16 19:01:37 +00003495
3496 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
3497 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
Manman Ren93371022012-10-16 19:51:48 +00003498 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3499 getABIKind() == ARMABIInfo::AAPCS)
3500 TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
3501 else
3502 TyAlign = 4;
Manman Ren97f81572012-10-16 19:18:39 +00003503 // Use indirect if size of the illegal vector is bigger than 16 bytes.
3504 if (isIllegalVectorType(Ty) && Size > 16) {
3505 IsIndirect = true;
3506 Size = 4;
3507 TyAlign = 4;
3508 }
Manman Rend105e732012-10-16 19:01:37 +00003509
3510 // Handle address alignment for ABI alignment > 4 bytes.
Rafael Espindolae164c182011-08-02 22:33:37 +00003511 if (TyAlign > 4) {
3512 assert((TyAlign & (TyAlign - 1)) == 0 &&
3513 "Alignment is not power of 2!");
3514 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3515 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
3516 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
Manman Rend105e732012-10-16 19:01:37 +00003517 Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align");
Rafael Espindolae164c182011-08-02 22:33:37 +00003518 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003519
3520 uint64_t Offset =
Manman Rend105e732012-10-16 19:01:37 +00003521 llvm::RoundUpToAlignment(Size, 4);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003522 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00003523 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003524 "ap.next");
3525 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3526
Manman Ren97f81572012-10-16 19:18:39 +00003527 if (IsIndirect)
3528 Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP));
Manman Ren93371022012-10-16 19:51:48 +00003529 else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) {
Manman Rend105e732012-10-16 19:01:37 +00003530 // We can't directly cast ap.cur to pointer to a vector type, since ap.cur
3531 // may not be correctly aligned for the vector type. We create an aligned
3532 // temporary space and copy the content over from ap.cur to the temporary
3533 // space. This is necessary if the natural alignment of the type is greater
3534 // than the ABI alignment.
3535 llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
3536 CharUnits CharSize = getContext().getTypeSizeInChars(Ty);
3537 llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty),
3538 "var.align");
3539 llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
3540 llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy);
3541 Builder.CreateMemCpy(Dst, Src,
3542 llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()),
3543 TyAlign, false);
3544 Addr = AlignedTemp; //The content is in aligned location.
3545 }
3546 llvm::Type *PTy =
3547 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3548 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3549
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003550 return AddrTyped;
3551}
3552
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00003553namespace {
3554
Derek Schuff263366f2012-10-16 22:30:41 +00003555class NaClARMABIInfo : public ABIInfo {
3556 public:
3557 NaClARMABIInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3558 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, Kind) {}
3559 virtual void computeInfo(CGFunctionInfo &FI) const;
3560 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3561 CodeGenFunction &CGF) const;
3562 private:
3563 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
3564 ARMABIInfo NInfo; // Used for everything else.
3565};
3566
3567class NaClARMTargetCodeGenInfo : public TargetCodeGenInfo {
3568 public:
3569 NaClARMTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3570 : TargetCodeGenInfo(new NaClARMABIInfo(CGT, Kind)) {}
3571};
3572
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00003573}
3574
Derek Schuff263366f2012-10-16 22:30:41 +00003575void NaClARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
3576 if (FI.getASTCallingConvention() == CC_PnaclCall)
3577 PInfo.computeInfo(FI);
3578 else
3579 static_cast<const ABIInfo&>(NInfo).computeInfo(FI);
3580}
3581
3582llvm::Value *NaClARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3583 CodeGenFunction &CGF) const {
3584 // Always use the native convention; calling pnacl-style varargs functions
3585 // is unsupported.
3586 return static_cast<const ABIInfo&>(NInfo).EmitVAArg(VAListAddr, Ty, CGF);
3587}
3588
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003589//===----------------------------------------------------------------------===//
Tim Northoverc264e162013-01-31 12:13:10 +00003590// AArch64 ABI Implementation
3591//===----------------------------------------------------------------------===//
3592
3593namespace {
3594
3595class AArch64ABIInfo : public ABIInfo {
3596public:
3597 AArch64ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3598
3599private:
3600 // The AArch64 PCS is explicit about return types and argument types being
3601 // handled identically, so we don't need to draw a distinction between
3602 // Argument and Return classification.
3603 ABIArgInfo classifyGenericType(QualType Ty, int &FreeIntRegs,
3604 int &FreeVFPRegs) const;
3605
3606 ABIArgInfo tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded, bool IsInt,
3607 llvm::Type *DirectTy = 0) const;
3608
3609 virtual void computeInfo(CGFunctionInfo &FI) const;
3610
3611 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3612 CodeGenFunction &CGF) const;
3613};
3614
3615class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
3616public:
3617 AArch64TargetCodeGenInfo(CodeGenTypes &CGT)
3618 :TargetCodeGenInfo(new AArch64ABIInfo(CGT)) {}
3619
3620 const AArch64ABIInfo &getABIInfo() const {
3621 return static_cast<const AArch64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
3622 }
3623
3624 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3625 return 31;
3626 }
3627
3628 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3629 llvm::Value *Address) const {
3630 // 0-31 are x0-x30 and sp: 8 bytes each
3631 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
3632 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 31);
3633
3634 // 64-95 are v0-v31: 16 bytes each
3635 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
3636 AssignToArrayRange(CGF.Builder, Address, Sixteen8, 64, 95);
3637
3638 return false;
3639 }
3640
3641};
3642
3643}
3644
3645void AArch64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
3646 int FreeIntRegs = 8, FreeVFPRegs = 8;
3647
3648 FI.getReturnInfo() = classifyGenericType(FI.getReturnType(),
3649 FreeIntRegs, FreeVFPRegs);
3650
3651 FreeIntRegs = FreeVFPRegs = 8;
3652 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3653 it != ie; ++it) {
3654 it->info = classifyGenericType(it->type, FreeIntRegs, FreeVFPRegs);
3655
3656 }
3657}
3658
3659ABIArgInfo
3660AArch64ABIInfo::tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded,
3661 bool IsInt, llvm::Type *DirectTy) const {
3662 if (FreeRegs >= RegsNeeded) {
3663 FreeRegs -= RegsNeeded;
3664 return ABIArgInfo::getDirect(DirectTy);
3665 }
3666
3667 llvm::Type *Padding = 0;
3668
3669 // We need padding so that later arguments don't get filled in anyway. That
3670 // wouldn't happen if only ByVal arguments followed in the same category, but
3671 // a large structure will simply seem to be a pointer as far as LLVM is
3672 // concerned.
3673 if (FreeRegs > 0) {
3674 if (IsInt)
3675 Padding = llvm::Type::getInt64Ty(getVMContext());
3676 else
3677 Padding = llvm::Type::getFloatTy(getVMContext());
3678
3679 // Either [N x i64] or [N x float].
3680 Padding = llvm::ArrayType::get(Padding, FreeRegs);
3681 FreeRegs = 0;
3682 }
3683
3684 return ABIArgInfo::getIndirect(getContext().getTypeAlign(Ty) / 8,
3685 /*IsByVal=*/ true, /*Realign=*/ false,
3686 Padding);
3687}
3688
3689
3690ABIArgInfo AArch64ABIInfo::classifyGenericType(QualType Ty,
3691 int &FreeIntRegs,
3692 int &FreeVFPRegs) const {
3693 // Can only occurs for return, but harmless otherwise.
3694 if (Ty->isVoidType())
3695 return ABIArgInfo::getIgnore();
3696
3697 // Large vector types should be returned via memory. There's no such concept
3698 // in the ABI, but they'd be over 16 bytes anyway so no matter how they're
3699 // classified they'd go into memory (see B.3).
3700 if (Ty->isVectorType() && getContext().getTypeSize(Ty) > 128) {
3701 if (FreeIntRegs > 0)
3702 --FreeIntRegs;
3703 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3704 }
3705
3706 // All non-aggregate LLVM types have a concrete ABI representation so they can
3707 // be passed directly. After this block we're guaranteed to be in a
3708 // complicated case.
3709 if (!isAggregateTypeForABI(Ty)) {
3710 // Treat an enum type as its underlying type.
3711 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3712 Ty = EnumTy->getDecl()->getIntegerType();
3713
3714 if (Ty->isFloatingType() || Ty->isVectorType())
3715 return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ false);
3716
3717 assert(getContext().getTypeSize(Ty) <= 128 &&
3718 "unexpectedly large scalar type");
3719
3720 int RegsNeeded = getContext().getTypeSize(Ty) > 64 ? 2 : 1;
3721
3722 // If the type may need padding registers to ensure "alignment", we must be
3723 // careful when this is accounted for. Increasing the effective size covers
3724 // all cases.
3725 if (getContext().getTypeAlign(Ty) == 128)
3726 RegsNeeded += FreeIntRegs % 2 != 0;
3727
3728 return tryUseRegs(Ty, FreeIntRegs, RegsNeeded, /*IsInt=*/ true);
3729 }
3730
3731 // Structures with either a non-trivial destructor or a non-trivial
3732 // copy constructor are always indirect.
3733 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) {
3734 if (FreeIntRegs > 0)
3735 --FreeIntRegs;
3736 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3737 }
3738
3739 if (isEmptyRecord(getContext(), Ty, true)) {
3740 if (!getContext().getLangOpts().CPlusPlus) {
3741 // Empty structs outside C++ mode are a GNU extension, so no ABI can
3742 // possibly tell us what to do. It turns out (I believe) that GCC ignores
3743 // the object for parameter-passsing purposes.
3744 return ABIArgInfo::getIgnore();
3745 }
3746
3747 // The combination of C++98 9p5 (sizeof(struct) != 0) and the pseudocode
3748 // description of va_arg in the PCS require that an empty struct does
3749 // actually occupy space for parameter-passing. I'm hoping for a
3750 // clarification giving an explicit paragraph to point to in future.
3751 return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ true,
3752 llvm::Type::getInt8Ty(getVMContext()));
3753 }
3754
3755 // Homogeneous vector aggregates get passed in registers or on the stack.
3756 const Type *Base = 0;
3757 uint64_t NumMembers = 0;
3758 if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)) {
3759 assert(Base && "Base class should be set for homogeneous aggregate");
3760 // Homogeneous aggregates are passed and returned directly.
3761 return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ NumMembers,
3762 /*IsInt=*/ false);
3763 }
3764
3765 uint64_t Size = getContext().getTypeSize(Ty);
3766 if (Size <= 128) {
3767 // Small structs can use the same direct type whether they're in registers
3768 // or on the stack.
3769 llvm::Type *BaseTy;
3770 unsigned NumBases;
3771 int SizeInRegs = (Size + 63) / 64;
3772
3773 if (getContext().getTypeAlign(Ty) == 128) {
3774 BaseTy = llvm::Type::getIntNTy(getVMContext(), 128);
3775 NumBases = 1;
3776
3777 // If the type may need padding registers to ensure "alignment", we must
3778 // be careful when this is accounted for. Increasing the effective size
3779 // covers all cases.
3780 SizeInRegs += FreeIntRegs % 2 != 0;
3781 } else {
3782 BaseTy = llvm::Type::getInt64Ty(getVMContext());
3783 NumBases = SizeInRegs;
3784 }
3785 llvm::Type *DirectTy = llvm::ArrayType::get(BaseTy, NumBases);
3786
3787 return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ SizeInRegs,
3788 /*IsInt=*/ true, DirectTy);
3789 }
3790
3791 // If the aggregate is > 16 bytes, it's passed and returned indirectly. In
3792 // LLVM terms the return uses an "sret" pointer, but that's handled elsewhere.
3793 --FreeIntRegs;
3794 return ABIArgInfo::getIndirect(0, /* byVal = */ false);
3795}
3796
3797llvm::Value *AArch64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3798 CodeGenFunction &CGF) const {
3799 // The AArch64 va_list type and handling is specified in the Procedure Call
3800 // Standard, section B.4:
3801 //
3802 // struct {
3803 // void *__stack;
3804 // void *__gr_top;
3805 // void *__vr_top;
3806 // int __gr_offs;
3807 // int __vr_offs;
3808 // };
3809
3810 assert(!CGF.CGM.getDataLayout().isBigEndian()
3811 && "va_arg not implemented for big-endian AArch64");
3812
3813 int FreeIntRegs = 8, FreeVFPRegs = 8;
3814 Ty = CGF.getContext().getCanonicalType(Ty);
3815 ABIArgInfo AI = classifyGenericType(Ty, FreeIntRegs, FreeVFPRegs);
3816
3817 llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
3818 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
3819 llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
3820 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
3821
3822 llvm::Value *reg_offs_p = 0, *reg_offs = 0;
3823 int reg_top_index;
3824 int RegSize;
3825 if (FreeIntRegs < 8) {
3826 assert(FreeVFPRegs == 8 && "Arguments never split between int & VFP regs");
3827 // 3 is the field number of __gr_offs
3828 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p");
3829 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
3830 reg_top_index = 1; // field number for __gr_top
3831 RegSize = 8 * (8 - FreeIntRegs);
3832 } else {
3833 assert(FreeVFPRegs < 8 && "Argument must go in VFP or int regs");
3834 // 4 is the field number of __vr_offs.
3835 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p");
3836 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
3837 reg_top_index = 2; // field number for __vr_top
3838 RegSize = 16 * (8 - FreeVFPRegs);
3839 }
3840
3841 //=======================================
3842 // Find out where argument was passed
3843 //=======================================
3844
3845 // If reg_offs >= 0 we're already using the stack for this type of
3846 // argument. We don't want to keep updating reg_offs (in case it overflows,
3847 // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
3848 // whatever they get).
3849 llvm::Value *UsingStack = 0;
3850 UsingStack = CGF.Builder.CreateICmpSGE(reg_offs,
3851 llvm::ConstantInt::get(CGF.Int32Ty, 0));
3852
3853 CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
3854
3855 // Otherwise, at least some kind of argument could go in these registers, the
3856 // quesiton is whether this particular type is too big.
3857 CGF.EmitBlock(MaybeRegBlock);
3858
3859 // Integer arguments may need to correct register alignment (for example a
3860 // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
3861 // align __gr_offs to calculate the potential address.
3862 if (FreeIntRegs < 8 && AI.isDirect() && getContext().getTypeAlign(Ty) > 64) {
3863 int Align = getContext().getTypeAlign(Ty) / 8;
3864
3865 reg_offs = CGF.Builder.CreateAdd(reg_offs,
3866 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
3867 "align_regoffs");
3868 reg_offs = CGF.Builder.CreateAnd(reg_offs,
3869 llvm::ConstantInt::get(CGF.Int32Ty, -Align),
3870 "aligned_regoffs");
3871 }
3872
3873 // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
3874 llvm::Value *NewOffset = 0;
3875 NewOffset = CGF.Builder.CreateAdd(reg_offs,
3876 llvm::ConstantInt::get(CGF.Int32Ty, RegSize),
3877 "new_reg_offs");
3878 CGF.Builder.CreateStore(NewOffset, reg_offs_p);
3879
3880 // Now we're in a position to decide whether this argument really was in
3881 // registers or not.
3882 llvm::Value *InRegs = 0;
3883 InRegs = CGF.Builder.CreateICmpSLE(NewOffset,
3884 llvm::ConstantInt::get(CGF.Int32Ty, 0),
3885 "inreg");
3886
3887 CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
3888
3889 //=======================================
3890 // Argument was in registers
3891 //=======================================
3892
3893 // Now we emit the code for if the argument was originally passed in
3894 // registers. First start the appropriate block:
3895 CGF.EmitBlock(InRegBlock);
3896
3897 llvm::Value *reg_top_p = 0, *reg_top = 0;
3898 reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p");
3899 reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
3900 llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs);
3901 llvm::Value *RegAddr = 0;
3902 llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
3903
3904 if (!AI.isDirect()) {
3905 // If it's been passed indirectly (actually a struct), whatever we find from
3906 // stored registers or on the stack will actually be a struct **.
3907 MemTy = llvm::PointerType::getUnqual(MemTy);
3908 }
3909
3910 const Type *Base = 0;
3911 uint64_t NumMembers;
3912 if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)
3913 && NumMembers > 1) {
3914 // Homogeneous aggregates passed in registers will have their elements split
3915 // and stored 16-bytes apart regardless of size (they're notionally in qN,
3916 // qN+1, ...). We reload and store into a temporary local variable
3917 // contiguously.
3918 assert(AI.isDirect() && "Homogeneous aggregates should be passed directly");
3919 llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
3920 llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
3921 llvm::Value *Tmp = CGF.CreateTempAlloca(HFATy);
3922
3923 for (unsigned i = 0; i < NumMembers; ++i) {
3924 llvm::Value *BaseOffset = llvm::ConstantInt::get(CGF.Int32Ty, 16 * i);
3925 llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset);
3926 LoadAddr = CGF.Builder.CreateBitCast(LoadAddr,
3927 llvm::PointerType::getUnqual(BaseTy));
3928 llvm::Value *StoreAddr = CGF.Builder.CreateStructGEP(Tmp, i);
3929
3930 llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
3931 CGF.Builder.CreateStore(Elem, StoreAddr);
3932 }
3933
3934 RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy);
3935 } else {
3936 // Otherwise the object is contiguous in memory
3937 RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy);
3938 }
3939
3940 CGF.EmitBranch(ContBlock);
3941
3942 //=======================================
3943 // Argument was on the stack
3944 //=======================================
3945 CGF.EmitBlock(OnStackBlock);
3946
3947 llvm::Value *stack_p = 0, *OnStackAddr = 0;
3948 stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p");
3949 OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack");
3950
3951 // Again, stack arguments may need realigmnent. In this case both integer and
3952 // floating-point ones might be affected.
3953 if (AI.isDirect() && getContext().getTypeAlign(Ty) > 64) {
3954 int Align = getContext().getTypeAlign(Ty) / 8;
3955
3956 OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty);
3957
3958 OnStackAddr = CGF.Builder.CreateAdd(OnStackAddr,
3959 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
3960 "align_stack");
3961 OnStackAddr = CGF.Builder.CreateAnd(OnStackAddr,
3962 llvm::ConstantInt::get(CGF.Int64Ty, -Align),
3963 "align_stack");
3964
3965 OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy);
3966 }
3967
3968 uint64_t StackSize;
3969 if (AI.isDirect())
3970 StackSize = getContext().getTypeSize(Ty) / 8;
3971 else
3972 StackSize = 8;
3973
3974 // All stack slots are 8 bytes
3975 StackSize = llvm::RoundUpToAlignment(StackSize, 8);
3976
3977 llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize);
3978 llvm::Value *NewStack = CGF.Builder.CreateGEP(OnStackAddr, StackSizeC,
3979 "new_stack");
3980
3981 // Write the new value of __stack for the next call to va_arg
3982 CGF.Builder.CreateStore(NewStack, stack_p);
3983
3984 OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy);
3985
3986 CGF.EmitBranch(ContBlock);
3987
3988 //=======================================
3989 // Tidy up
3990 //=======================================
3991 CGF.EmitBlock(ContBlock);
3992
3993 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr");
3994 ResAddr->addIncoming(RegAddr, InRegBlock);
3995 ResAddr->addIncoming(OnStackAddr, OnStackBlock);
3996
3997 if (AI.isDirect())
3998 return ResAddr;
3999
4000 return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr");
4001}
4002
4003//===----------------------------------------------------------------------===//
Justin Holewinski2c585b92012-05-24 17:43:12 +00004004// NVPTX ABI Implementation
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004005//===----------------------------------------------------------------------===//
4006
4007namespace {
4008
Justin Holewinski2c585b92012-05-24 17:43:12 +00004009class NVPTXABIInfo : public ABIInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004010public:
Justin Holewinskidca8f332013-03-30 14:38:24 +00004011 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004012
4013 ABIArgInfo classifyReturnType(QualType RetTy) const;
4014 ABIArgInfo classifyArgumentType(QualType Ty) const;
4015
4016 virtual void computeInfo(CGFunctionInfo &FI) const;
4017 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4018 CodeGenFunction &CFG) const;
4019};
4020
Justin Holewinski2c585b92012-05-24 17:43:12 +00004021class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004022public:
Justin Holewinski2c585b92012-05-24 17:43:12 +00004023 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
4024 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
Justin Holewinski818eafb2011-10-05 17:58:44 +00004025
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004026 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4027 CodeGen::CodeGenModule &M) const;
Justin Holewinskidca8f332013-03-30 14:38:24 +00004028private:
4029 static void addKernelMetadata(llvm::Function *F);
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004030};
4031
Justin Holewinski2c585b92012-05-24 17:43:12 +00004032ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004033 if (RetTy->isVoidType())
4034 return ABIArgInfo::getIgnore();
4035 if (isAggregateTypeForABI(RetTy))
4036 return ABIArgInfo::getIndirect(0);
4037 return ABIArgInfo::getDirect();
4038}
4039
Justin Holewinski2c585b92012-05-24 17:43:12 +00004040ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004041 if (isAggregateTypeForABI(Ty))
4042 return ABIArgInfo::getIndirect(0);
4043
4044 return ABIArgInfo::getDirect();
4045}
4046
Justin Holewinski2c585b92012-05-24 17:43:12 +00004047void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004048 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4049 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4050 it != ie; ++it)
4051 it->info = classifyArgumentType(it->type);
4052
4053 // Always honor user-specified calling convention.
4054 if (FI.getCallingConvention() != llvm::CallingConv::C)
4055 return;
4056
John McCallbd7370a2013-02-28 19:01:20 +00004057 FI.setEffectiveCallingConvention(getRuntimeCC());
4058}
4059
Justin Holewinski2c585b92012-05-24 17:43:12 +00004060llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4061 CodeGenFunction &CFG) const {
4062 llvm_unreachable("NVPTX does not support varargs");
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004063}
4064
Justin Holewinski2c585b92012-05-24 17:43:12 +00004065void NVPTXTargetCodeGenInfo::
4066SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4067 CodeGen::CodeGenModule &M) const{
Justin Holewinski818eafb2011-10-05 17:58:44 +00004068 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4069 if (!FD) return;
4070
4071 llvm::Function *F = cast<llvm::Function>(GV);
4072
4073 // Perform special handling in OpenCL mode
David Blaikie4e4d0842012-03-11 07:00:24 +00004074 if (M.getLangOpts().OpenCL) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00004075 // Use OpenCL function attributes to check for kernel functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00004076 // By default, all functions are device functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00004077 if (FD->hasAttr<OpenCLKernelAttr>()) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00004078 // OpenCL __kernel functions get kernel metadata
4079 addKernelMetadata(F);
Justin Holewinski818eafb2011-10-05 17:58:44 +00004080 // And kernel functions are not subject to inlining
Bill Wendling72390b32012-12-20 19:27:06 +00004081 F->addFnAttr(llvm::Attribute::NoInline);
Justin Holewinski818eafb2011-10-05 17:58:44 +00004082 }
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004083 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00004084
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004085 // Perform special handling in CUDA mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00004086 if (M.getLangOpts().CUDA) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00004087 // CUDA __global__ functions get a kernel metadata entry. Since
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004088 // __global__ functions cannot be called from the device, we do not
4089 // need to set the noinline attribute.
4090 if (FD->getAttr<CUDAGlobalAttr>())
Justin Holewinskidca8f332013-03-30 14:38:24 +00004091 addKernelMetadata(F);
Justin Holewinski818eafb2011-10-05 17:58:44 +00004092 }
4093}
4094
Justin Holewinskidca8f332013-03-30 14:38:24 +00004095void NVPTXTargetCodeGenInfo::addKernelMetadata(llvm::Function *F) {
4096 llvm::Module *M = F->getParent();
4097 llvm::LLVMContext &Ctx = M->getContext();
4098
4099 // Get "nvvm.annotations" metadata node
4100 llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
4101
4102 // Create !{<func-ref>, metadata !"kernel", i32 1} node
4103 llvm::SmallVector<llvm::Value *, 3> MDVals;
4104 MDVals.push_back(F);
4105 MDVals.push_back(llvm::MDString::get(Ctx, "kernel"));
4106 MDVals.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), 1));
4107
4108 // Append metadata to nvvm.annotations
4109 MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
4110}
4111
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004112}
4113
4114//===----------------------------------------------------------------------===//
Wesley Peck276fdf42010-12-19 19:57:51 +00004115// MBlaze ABI Implementation
4116//===----------------------------------------------------------------------===//
4117
4118namespace {
4119
4120class MBlazeABIInfo : public ABIInfo {
4121public:
4122 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
4123
4124 bool isPromotableIntegerType(QualType Ty) const;
4125
4126 ABIArgInfo classifyReturnType(QualType RetTy) const;
4127 ABIArgInfo classifyArgumentType(QualType RetTy) const;
4128
4129 virtual void computeInfo(CGFunctionInfo &FI) const {
4130 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4131 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4132 it != ie; ++it)
4133 it->info = classifyArgumentType(it->type);
4134 }
4135
4136 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4137 CodeGenFunction &CGF) const;
4138};
4139
4140class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
4141public:
4142 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
4143 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
4144 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4145 CodeGen::CodeGenModule &M) const;
4146};
4147
4148}
4149
4150bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
4151 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
4152 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4153 switch (BT->getKind()) {
4154 case BuiltinType::Bool:
4155 case BuiltinType::Char_S:
4156 case BuiltinType::Char_U:
4157 case BuiltinType::SChar:
4158 case BuiltinType::UChar:
4159 case BuiltinType::Short:
4160 case BuiltinType::UShort:
4161 return true;
4162 default:
4163 return false;
4164 }
4165 return false;
4166}
4167
4168llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4169 CodeGenFunction &CGF) const {
4170 // FIXME: Implement
4171 return 0;
4172}
4173
4174
4175ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
4176 if (RetTy->isVoidType())
4177 return ABIArgInfo::getIgnore();
4178 if (isAggregateTypeForABI(RetTy))
4179 return ABIArgInfo::getIndirect(0);
4180
4181 return (isPromotableIntegerType(RetTy) ?
4182 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4183}
4184
4185ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
4186 if (isAggregateTypeForABI(Ty))
4187 return ABIArgInfo::getIndirect(0);
4188
4189 return (isPromotableIntegerType(Ty) ?
4190 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4191}
4192
4193void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4194 llvm::GlobalValue *GV,
4195 CodeGen::CodeGenModule &M)
4196 const {
4197 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4198 if (!FD) return;
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00004199
Wesley Peck276fdf42010-12-19 19:57:51 +00004200 llvm::CallingConv::ID CC = llvm::CallingConv::C;
4201 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
4202 CC = llvm::CallingConv::MBLAZE_INTR;
4203 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
4204 CC = llvm::CallingConv::MBLAZE_SVOL;
4205
4206 if (CC != llvm::CallingConv::C) {
4207 // Handle 'interrupt_handler' attribute:
4208 llvm::Function *F = cast<llvm::Function>(GV);
4209
4210 // Step 1: Set ISR calling convention.
4211 F->setCallingConv(CC);
4212
4213 // Step 2: Add attributes goodness.
Bill Wendling72390b32012-12-20 19:27:06 +00004214 F->addFnAttr(llvm::Attribute::NoInline);
Wesley Peck276fdf42010-12-19 19:57:51 +00004215 }
4216
4217 // Step 3: Emit _interrupt_handler alias.
4218 if (CC == llvm::CallingConv::MBLAZE_INTR)
4219 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
4220 "_interrupt_handler", GV, &M.getModule());
4221}
4222
4223
4224//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004225// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004226//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004227
4228namespace {
4229
4230class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
4231public:
Chris Lattnerea044322010-07-29 02:01:43 +00004232 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
4233 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004234 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4235 CodeGen::CodeGenModule &M) const;
4236};
4237
4238}
4239
4240void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4241 llvm::GlobalValue *GV,
4242 CodeGen::CodeGenModule &M) const {
4243 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4244 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
4245 // Handle 'interrupt' attribute:
4246 llvm::Function *F = cast<llvm::Function>(GV);
4247
4248 // Step 1: Set ISR calling convention.
4249 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
4250
4251 // Step 2: Add attributes goodness.
Bill Wendling72390b32012-12-20 19:27:06 +00004252 F->addFnAttr(llvm::Attribute::NoInline);
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004253
4254 // Step 3: Emit ISR vector alias.
Anton Korobeynikovf419a852012-11-26 18:59:10 +00004255 unsigned Num = attr->getNumber() / 2;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004256 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Anton Korobeynikovf419a852012-11-26 18:59:10 +00004257 "__isr_" + Twine(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004258 GV, &M.getModule());
4259 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004260 }
4261}
4262
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004263//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00004264// MIPS ABI Implementation. This works for both little-endian and
4265// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004266//===----------------------------------------------------------------------===//
4267
John McCallaeeb7012010-05-27 06:19:26 +00004268namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00004269class MipsABIInfo : public ABIInfo {
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004270 bool IsO32;
Akira Hatanakac359f202012-07-03 19:24:06 +00004271 unsigned MinABIStackAlignInBytes, StackAlignInBytes;
4272 void CoerceToIntArgs(uint64_t TySize,
4273 SmallVector<llvm::Type*, 8> &ArgList) const;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004274 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004275 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004276 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004277public:
Akira Hatanakab551dd32011-11-03 00:05:50 +00004278 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
Akira Hatanakac359f202012-07-03 19:24:06 +00004279 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
4280 StackAlignInBytes(IsO32 ? 8 : 16) {}
Akira Hatanaka619e8872011-06-02 00:09:17 +00004281
4282 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004283 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004284 virtual void computeInfo(CGFunctionInfo &FI) const;
4285 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4286 CodeGenFunction &CGF) const;
4287};
4288
John McCallaeeb7012010-05-27 06:19:26 +00004289class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanakae624fa02011-09-20 18:23:28 +00004290 unsigned SizeOfUnwindException;
John McCallaeeb7012010-05-27 06:19:26 +00004291public:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004292 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
4293 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
4294 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCallaeeb7012010-05-27 06:19:26 +00004295
4296 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
4297 return 29;
4298 }
4299
Reed Kotler7dfd1822013-01-16 17:10:28 +00004300 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4301 CodeGen::CodeGenModule &CGM) const {
Reed Kotlerad4b8b42013-03-13 20:40:30 +00004302 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4303 if (!FD) return;
Rafael Espindolad8e6d6d2013-03-19 14:32:23 +00004304 llvm::Function *Fn = cast<llvm::Function>(GV);
Reed Kotlerad4b8b42013-03-13 20:40:30 +00004305 if (FD->hasAttr<Mips16Attr>()) {
4306 Fn->addFnAttr("mips16");
4307 }
4308 else if (FD->hasAttr<NoMips16Attr>()) {
4309 Fn->addFnAttr("nomips16");
4310 }
Reed Kotler7dfd1822013-01-16 17:10:28 +00004311 }
Reed Kotlerad4b8b42013-03-13 20:40:30 +00004312
John McCallaeeb7012010-05-27 06:19:26 +00004313 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00004314 llvm::Value *Address) const;
John McCall49e34be2011-08-30 01:42:09 +00004315
4316 unsigned getSizeOfUnwindException() const {
Akira Hatanakae624fa02011-09-20 18:23:28 +00004317 return SizeOfUnwindException;
John McCall49e34be2011-08-30 01:42:09 +00004318 }
John McCallaeeb7012010-05-27 06:19:26 +00004319};
4320}
4321
Akira Hatanakac359f202012-07-03 19:24:06 +00004322void MipsABIInfo::CoerceToIntArgs(uint64_t TySize,
4323 SmallVector<llvm::Type*, 8> &ArgList) const {
4324 llvm::IntegerType *IntTy =
4325 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004326
4327 // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
4328 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
4329 ArgList.push_back(IntTy);
4330
4331 // If necessary, add one more integer type to ArgList.
4332 unsigned R = TySize % (MinABIStackAlignInBytes * 8);
4333
4334 if (R)
4335 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004336}
4337
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004338// In N32/64, an aligned double precision floating point field is passed in
4339// a register.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004340llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
Akira Hatanakac359f202012-07-03 19:24:06 +00004341 SmallVector<llvm::Type*, 8> ArgList, IntArgList;
4342
4343 if (IsO32) {
4344 CoerceToIntArgs(TySize, ArgList);
4345 return llvm::StructType::get(getVMContext(), ArgList);
4346 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004347
Akira Hatanaka2afd23d2012-01-12 00:52:17 +00004348 if (Ty->isComplexType())
4349 return CGT.ConvertType(Ty);
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00004350
Akira Hatanakaa34e9212012-02-09 19:54:16 +00004351 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004352
Akira Hatanakac359f202012-07-03 19:24:06 +00004353 // Unions/vectors are passed in integer registers.
4354 if (!RT || !RT->isStructureOrClassType()) {
4355 CoerceToIntArgs(TySize, ArgList);
4356 return llvm::StructType::get(getVMContext(), ArgList);
4357 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004358
4359 const RecordDecl *RD = RT->getDecl();
4360 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004361 assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004362
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004363 uint64_t LastOffset = 0;
4364 unsigned idx = 0;
4365 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
4366
Akira Hatanakaa34e9212012-02-09 19:54:16 +00004367 // Iterate over fields in the struct/class and check if there are any aligned
4368 // double fields.
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004369 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
4370 i != e; ++i, ++idx) {
David Blaikie262bc182012-04-30 02:36:29 +00004371 const QualType Ty = i->getType();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004372 const BuiltinType *BT = Ty->getAs<BuiltinType>();
4373
4374 if (!BT || BT->getKind() != BuiltinType::Double)
4375 continue;
4376
4377 uint64_t Offset = Layout.getFieldOffset(idx);
4378 if (Offset % 64) // Ignore doubles that are not aligned.
4379 continue;
4380
4381 // Add ((Offset - LastOffset) / 64) args of type i64.
4382 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
4383 ArgList.push_back(I64);
4384
4385 // Add double type.
4386 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
4387 LastOffset = Offset + 64;
4388 }
4389
Akira Hatanakac359f202012-07-03 19:24:06 +00004390 CoerceToIntArgs(TySize - LastOffset, IntArgList);
4391 ArgList.append(IntArgList.begin(), IntArgList.end());
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004392
4393 return llvm::StructType::get(getVMContext(), ArgList);
4394}
4395
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004396llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004397 assert((Offset % MinABIStackAlignInBytes) == 0);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004398
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004399 if ((Align - 1) & Offset)
4400 return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
4401
4402 return 0;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004403}
Akira Hatanaka9659d592012-01-10 22:44:52 +00004404
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004405ABIArgInfo
4406MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004407 uint64_t OrigOffset = Offset;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004408 uint64_t TySize = getContext().getTypeSize(Ty);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004409 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004410
Akira Hatanakac359f202012-07-03 19:24:06 +00004411 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
4412 (uint64_t)StackAlignInBytes);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004413 Offset = llvm::RoundUpToAlignment(Offset, Align);
4414 Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004415
Akira Hatanakac359f202012-07-03 19:24:06 +00004416 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
Akira Hatanaka619e8872011-06-02 00:09:17 +00004417 // Ignore empty aggregates.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004418 if (TySize == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00004419 return ABIArgInfo::getIgnore();
4420
Akira Hatanaka511949b2011-08-01 18:09:58 +00004421 // Records with non trivial destructors/constructors should not be passed
4422 // by value.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004423 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004424 Offset = OrigOffset + MinABIStackAlignInBytes;
Akira Hatanaka511949b2011-08-01 18:09:58 +00004425 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004426 }
Akira Hatanaka511949b2011-08-01 18:09:58 +00004427
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004428 // If we have reached here, aggregates are passed directly by coercing to
4429 // another structure type. Padding is inserted if the offset of the
4430 // aggregate is unaligned.
4431 return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
4432 getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00004433 }
4434
4435 // Treat an enum type as its underlying type.
4436 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4437 Ty = EnumTy->getDecl()->getIntegerType();
4438
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004439 if (Ty->isPromotableIntegerType())
4440 return ABIArgInfo::getExtend();
4441
Akira Hatanaka4055cfc2013-01-24 21:47:33 +00004442 return ABIArgInfo::getDirect(0, 0,
4443 IsO32 ? 0 : getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00004444}
4445
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004446llvm::Type*
4447MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakada54ff32012-02-09 18:49:26 +00004448 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakac359f202012-07-03 19:24:06 +00004449 SmallVector<llvm::Type*, 8> RTList;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004450
Akira Hatanakada54ff32012-02-09 18:49:26 +00004451 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004452 const RecordDecl *RD = RT->getDecl();
Akira Hatanakada54ff32012-02-09 18:49:26 +00004453 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
4454 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004455
Akira Hatanakada54ff32012-02-09 18:49:26 +00004456 // N32/64 returns struct/classes in floating point registers if the
4457 // following conditions are met:
4458 // 1. The size of the struct/class is no larger than 128-bit.
4459 // 2. The struct/class has one or two fields all of which are floating
4460 // point types.
4461 // 3. The offset of the first field is zero (this follows what gcc does).
4462 //
4463 // Any other composite results are returned in integer registers.
4464 //
4465 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
4466 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
4467 for (; b != e; ++b) {
David Blaikie262bc182012-04-30 02:36:29 +00004468 const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004469
Akira Hatanakada54ff32012-02-09 18:49:26 +00004470 if (!BT || !BT->isFloatingPoint())
4471 break;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004472
David Blaikie262bc182012-04-30 02:36:29 +00004473 RTList.push_back(CGT.ConvertType(b->getType()));
Akira Hatanakada54ff32012-02-09 18:49:26 +00004474 }
4475
4476 if (b == e)
4477 return llvm::StructType::get(getVMContext(), RTList,
4478 RD->hasAttr<PackedAttr>());
4479
4480 RTList.clear();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004481 }
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004482 }
4483
Akira Hatanakac359f202012-07-03 19:24:06 +00004484 CoerceToIntArgs(Size, RTList);
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004485 return llvm::StructType::get(getVMContext(), RTList);
4486}
4487
Akira Hatanaka619e8872011-06-02 00:09:17 +00004488ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanakaa8536c02012-01-23 23:18:57 +00004489 uint64_t Size = getContext().getTypeSize(RetTy);
4490
4491 if (RetTy->isVoidType() || Size == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00004492 return ABIArgInfo::getIgnore();
4493
Akira Hatanaka8aeb1472012-05-11 21:01:17 +00004494 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004495 if (Size <= 128) {
4496 if (RetTy->isAnyComplexType())
4497 return ABIArgInfo::getDirect();
4498
Akira Hatanakac359f202012-07-03 19:24:06 +00004499 // O32 returns integer vectors in registers.
4500 if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())
4501 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
4502
Akira Hatanaka526cdfb2012-02-08 01:31:22 +00004503 if (!IsO32 && !isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004504 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
4505 }
Akira Hatanaka619e8872011-06-02 00:09:17 +00004506
4507 return ABIArgInfo::getIndirect(0);
4508 }
4509
4510 // Treat an enum type as its underlying type.
4511 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4512 RetTy = EnumTy->getDecl()->getIntegerType();
4513
4514 return (RetTy->isPromotableIntegerType() ?
4515 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4516}
4517
4518void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanakacc662542012-01-12 01:10:09 +00004519 ABIArgInfo &RetInfo = FI.getReturnInfo();
4520 RetInfo = classifyReturnType(FI.getReturnType());
4521
4522 // Check if a pointer to an aggregate is passed as a hidden argument.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004523 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
Akira Hatanakacc662542012-01-12 01:10:09 +00004524
Akira Hatanaka619e8872011-06-02 00:09:17 +00004525 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4526 it != ie; ++it)
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004527 it->info = classifyArgumentType(it->type, Offset);
Akira Hatanaka619e8872011-06-02 00:09:17 +00004528}
4529
4530llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4531 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00004532 llvm::Type *BP = CGF.Int8PtrTy;
4533 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004534
4535 CGBuilderTy &Builder = CGF.Builder;
4536 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
4537 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004538 int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004539 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
4540 llvm::Value *AddrTyped;
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004541 unsigned PtrWidth = getContext().getTargetInfo().getPointerWidth(0);
4542 llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004543
4544 if (TypeAlign > MinABIStackAlignInBytes) {
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004545 llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
4546 llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
4547 llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
4548 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004549 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
4550 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
4551 }
4552 else
4553 AddrTyped = Builder.CreateBitCast(Addr, PTy);
4554
4555 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004556 TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004557 uint64_t Offset =
4558 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
4559 llvm::Value *NextAddr =
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004560 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004561 "ap.next");
4562 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
4563
4564 return AddrTyped;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004565}
4566
John McCallaeeb7012010-05-27 06:19:26 +00004567bool
4568MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4569 llvm::Value *Address) const {
4570 // This information comes from gcc's implementation, which seems to
4571 // as canonical as it gets.
4572
John McCallaeeb7012010-05-27 06:19:26 +00004573 // Everything on MIPS is 4 bytes. Double-precision FP registers
4574 // are aliased to pairs of single-precision FP registers.
Chris Lattner8b418682012-02-07 00:39:47 +00004575 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCallaeeb7012010-05-27 06:19:26 +00004576
4577 // 0-31 are the general purpose registers, $0 - $31.
4578 // 32-63 are the floating-point registers, $f0 - $f31.
4579 // 64 and 65 are the multiply/divide registers, $hi and $lo.
4580 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattner8b418682012-02-07 00:39:47 +00004581 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCallaeeb7012010-05-27 06:19:26 +00004582
4583 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
4584 // They are one bit wide and ignored here.
4585
4586 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
4587 // (coprocessor 1 is the FP unit)
4588 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
4589 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
4590 // 176-181 are the DSP accumulator registers.
Chris Lattner8b418682012-02-07 00:39:47 +00004591 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCallaeeb7012010-05-27 06:19:26 +00004592 return false;
4593}
4594
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004595//===----------------------------------------------------------------------===//
4596// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
4597// Currently subclassed only to implement custom OpenCL C function attribute
4598// handling.
4599//===----------------------------------------------------------------------===//
4600
4601namespace {
4602
4603class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
4604public:
4605 TCETargetCodeGenInfo(CodeGenTypes &CGT)
4606 : DefaultTargetCodeGenInfo(CGT) {}
4607
4608 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4609 CodeGen::CodeGenModule &M) const;
4610};
4611
4612void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4613 llvm::GlobalValue *GV,
4614 CodeGen::CodeGenModule &M) const {
4615 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4616 if (!FD) return;
4617
4618 llvm::Function *F = cast<llvm::Function>(GV);
4619
David Blaikie4e4d0842012-03-11 07:00:24 +00004620 if (M.getLangOpts().OpenCL) {
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004621 if (FD->hasAttr<OpenCLKernelAttr>()) {
4622 // OpenCL C Kernel functions are not subject to inlining
Bill Wendling72390b32012-12-20 19:27:06 +00004623 F->addFnAttr(llvm::Attribute::NoInline);
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004624
4625 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
4626
4627 // Convert the reqd_work_group_size() attributes to metadata.
4628 llvm::LLVMContext &Context = F->getContext();
4629 llvm::NamedMDNode *OpenCLMetadata =
4630 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
4631
4632 SmallVector<llvm::Value*, 5> Operands;
4633 Operands.push_back(F);
4634
Chris Lattner8b418682012-02-07 00:39:47 +00004635 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4636 llvm::APInt(32,
4637 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
4638 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4639 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004640 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
Chris Lattner8b418682012-02-07 00:39:47 +00004641 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4642 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004643 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
4644
4645 // Add a boolean constant operand for "required" (true) or "hint" (false)
4646 // for implementing the work_group_size_hint attr later. Currently
4647 // always true as the hint is not yet implemented.
Chris Lattner8b418682012-02-07 00:39:47 +00004648 Operands.push_back(llvm::ConstantInt::getTrue(Context));
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004649 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
4650 }
4651 }
4652 }
4653}
4654
4655}
John McCallaeeb7012010-05-27 06:19:26 +00004656
Tony Linthicum96319392011-12-12 21:14:55 +00004657//===----------------------------------------------------------------------===//
4658// Hexagon ABI Implementation
4659//===----------------------------------------------------------------------===//
4660
4661namespace {
4662
4663class HexagonABIInfo : public ABIInfo {
4664
4665
4666public:
4667 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
4668
4669private:
4670
4671 ABIArgInfo classifyReturnType(QualType RetTy) const;
4672 ABIArgInfo classifyArgumentType(QualType RetTy) const;
4673
4674 virtual void computeInfo(CGFunctionInfo &FI) const;
4675
4676 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4677 CodeGenFunction &CGF) const;
4678};
4679
4680class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
4681public:
4682 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
4683 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
4684
4685 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
4686 return 29;
4687 }
4688};
4689
4690}
4691
4692void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
4693 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4694 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4695 it != ie; ++it)
4696 it->info = classifyArgumentType(it->type);
4697}
4698
4699ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
4700 if (!isAggregateTypeForABI(Ty)) {
4701 // Treat an enum type as its underlying type.
4702 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4703 Ty = EnumTy->getDecl()->getIntegerType();
4704
4705 return (Ty->isPromotableIntegerType() ?
4706 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4707 }
4708
4709 // Ignore empty records.
4710 if (isEmptyRecord(getContext(), Ty, true))
4711 return ABIArgInfo::getIgnore();
4712
4713 // Structures with either a non-trivial destructor or a non-trivial
4714 // copy constructor are always indirect.
4715 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
4716 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
4717
4718 uint64_t Size = getContext().getTypeSize(Ty);
4719 if (Size > 64)
4720 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
4721 // Pass in the smallest viable integer type.
4722 else if (Size > 32)
4723 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
4724 else if (Size > 16)
4725 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
4726 else if (Size > 8)
4727 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
4728 else
4729 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
4730}
4731
4732ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
4733 if (RetTy->isVoidType())
4734 return ABIArgInfo::getIgnore();
4735
4736 // Large vector types should be returned via memory.
4737 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
4738 return ABIArgInfo::getIndirect(0);
4739
4740 if (!isAggregateTypeForABI(RetTy)) {
4741 // Treat an enum type as its underlying type.
4742 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4743 RetTy = EnumTy->getDecl()->getIntegerType();
4744
4745 return (RetTy->isPromotableIntegerType() ?
4746 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4747 }
4748
4749 // Structures with either a non-trivial destructor or a non-trivial
4750 // copy constructor are always indirect.
4751 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
4752 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
4753
4754 if (isEmptyRecord(getContext(), RetTy, true))
4755 return ABIArgInfo::getIgnore();
4756
4757 // Aggregates <= 8 bytes are returned in r0; other aggregates
4758 // are returned indirectly.
4759 uint64_t Size = getContext().getTypeSize(RetTy);
4760 if (Size <= 64) {
4761 // Return in the smallest viable integer type.
4762 if (Size <= 8)
4763 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
4764 if (Size <= 16)
4765 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
4766 if (Size <= 32)
4767 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
4768 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
4769 }
4770
4771 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
4772}
4773
4774llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner8b418682012-02-07 00:39:47 +00004775 CodeGenFunction &CGF) const {
Tony Linthicum96319392011-12-12 21:14:55 +00004776 // FIXME: Need to handle alignment
Chris Lattner8b418682012-02-07 00:39:47 +00004777 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Tony Linthicum96319392011-12-12 21:14:55 +00004778
4779 CGBuilderTy &Builder = CGF.Builder;
4780 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
4781 "ap");
4782 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
4783 llvm::Type *PTy =
4784 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
4785 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
4786
4787 uint64_t Offset =
4788 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
4789 llvm::Value *NextAddr =
4790 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
4791 "ap.next");
4792 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
4793
4794 return AddrTyped;
4795}
4796
4797
Chris Lattnerea044322010-07-29 02:01:43 +00004798const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004799 if (TheTargetCodeGenInfo)
4800 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004801
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004802 const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00004803 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004804 default:
Chris Lattnerea044322010-07-29 02:01:43 +00004805 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004806
Derek Schuff9ed63f82012-09-06 17:37:28 +00004807 case llvm::Triple::le32:
4808 return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
John McCallaeeb7012010-05-27 06:19:26 +00004809 case llvm::Triple::mips:
4810 case llvm::Triple::mipsel:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004811 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCallaeeb7012010-05-27 06:19:26 +00004812
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00004813 case llvm::Triple::mips64:
4814 case llvm::Triple::mips64el:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004815 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00004816
Tim Northoverc264e162013-01-31 12:13:10 +00004817 case llvm::Triple::aarch64:
4818 return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types));
4819
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004820 case llvm::Triple::arm:
4821 case llvm::Triple::thumb:
Sandeep Patel34c1af82011-04-05 00:23:47 +00004822 {
4823 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004824 if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
Sandeep Patel34c1af82011-04-05 00:23:47 +00004825 Kind = ARMABIInfo::APCS;
David Tweedb16abb12012-10-25 13:33:01 +00004826 else if (CodeGenOpts.FloatABI == "hard" ||
4827 (CodeGenOpts.FloatABI != "soft" && Triple.getEnvironment()==llvm::Triple::GNUEABIHF))
Sandeep Patel34c1af82011-04-05 00:23:47 +00004828 Kind = ARMABIInfo::AAPCS_VFP;
4829
Derek Schuff263366f2012-10-16 22:30:41 +00004830 switch (Triple.getOS()) {
Eli Bendersky441d9f72012-12-04 18:38:10 +00004831 case llvm::Triple::NaCl:
Derek Schuff263366f2012-10-16 22:30:41 +00004832 return *(TheTargetCodeGenInfo =
4833 new NaClARMTargetCodeGenInfo(Types, Kind));
4834 default:
4835 return *(TheTargetCodeGenInfo =
4836 new ARMTargetCodeGenInfo(Types, Kind));
4837 }
Sandeep Patel34c1af82011-04-05 00:23:47 +00004838 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004839
John McCallec853ba2010-03-11 00:10:12 +00004840 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00004841 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
Roman Divacky0fbc4b92012-05-09 18:22:46 +00004842 case llvm::Triple::ppc64:
Bill Schmidt2fc107f2012-10-03 19:18:57 +00004843 if (Triple.isOSBinFormatELF())
4844 return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types));
4845 else
4846 return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00004847
Peter Collingbourneedb66f32012-05-20 23:28:41 +00004848 case llvm::Triple::nvptx:
4849 case llvm::Triple::nvptx64:
Justin Holewinski2c585b92012-05-24 17:43:12 +00004850 return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types));
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004851
Wesley Peck276fdf42010-12-19 19:57:51 +00004852 case llvm::Triple::mblaze:
4853 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
4854
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004855 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00004856 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004857
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004858 case llvm::Triple::tce:
4859 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
4860
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004861 case llvm::Triple::x86: {
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00004862 if (Triple.isOSDarwin())
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004863 return *(TheTargetCodeGenInfo =
Chad Rosier1f1df1f2013-03-25 21:00:27 +00004864 new X86_32TargetCodeGenInfo(Types, true, true, false,
Rafael Espindolab48280b2012-07-31 02:44:24 +00004865 CodeGenOpts.NumRegisterParameters));
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00004866
4867 switch (Triple.getOS()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004868 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004869 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00004870 case llvm::Triple::AuroraUX:
4871 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00004872 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004873 case llvm::Triple::OpenBSD:
Eli Friedman42f74f22012-08-08 23:57:20 +00004874 case llvm::Triple::Bitrig:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004875 return *(TheTargetCodeGenInfo =
Chad Rosier1f1df1f2013-03-25 21:00:27 +00004876 new X86_32TargetCodeGenInfo(Types, false, true, false,
Rafael Espindolab48280b2012-07-31 02:44:24 +00004877 CodeGenOpts.NumRegisterParameters));
Eli Friedman55fc7e22012-01-25 22:46:34 +00004878
4879 case llvm::Triple::Win32:
4880 return *(TheTargetCodeGenInfo =
Chad Rosier1f1df1f2013-03-25 21:00:27 +00004881 new X86_32TargetCodeGenInfo(Types, false, true, true,
Rafael Espindolab48280b2012-07-31 02:44:24 +00004882 CodeGenOpts.NumRegisterParameters));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004883
4884 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004885 return *(TheTargetCodeGenInfo =
Chad Rosier1f1df1f2013-03-25 21:00:27 +00004886 new X86_32TargetCodeGenInfo(Types, false, false, false,
Rafael Espindolab48280b2012-07-31 02:44:24 +00004887 CodeGenOpts.NumRegisterParameters));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004888 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004889 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004890
Eli Friedmanee1ad992011-12-02 00:11:43 +00004891 case llvm::Triple::x86_64: {
4892 bool HasAVX = strcmp(getContext().getTargetInfo().getABI(), "avx") == 0;
4893
Chris Lattnerf13721d2010-08-31 16:44:54 +00004894 switch (Triple.getOS()) {
4895 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00004896 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00004897 case llvm::Triple::Cygwin:
4898 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
Eli Bendersky441d9f72012-12-04 18:38:10 +00004899 case llvm::Triple::NaCl:
Derek Schuff263366f2012-10-16 22:30:41 +00004900 return *(TheTargetCodeGenInfo = new NaClX86_64TargetCodeGenInfo(Types, HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00004901 default:
Eli Friedmanee1ad992011-12-02 00:11:43 +00004902 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
4903 HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00004904 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004905 }
Tony Linthicum96319392011-12-12 21:14:55 +00004906 case llvm::Triple::hexagon:
4907 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
Eli Friedmanee1ad992011-12-02 00:11:43 +00004908 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004909}