blob: 556999f61b3ea100d9e9ca493177d65951e13b08 [file] [log] [blame]
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000016#include "ABIInfo.h"
17#include "CodeGenFunction.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000018#include "clang/AST/RecordLayout.h"
Sandeep Patel34c1af82011-04-05 00:23:47 +000019#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbar2c0843f2009-08-24 08:52:16 +000020#include "llvm/ADT/Triple.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000021#include "llvm/IR/DataLayout.h"
22#include "llvm/IR/Type.h"
Daniel Dunbar28df7a52009-12-03 09:13:49 +000023#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000024using namespace clang;
25using namespace CodeGen;
26
John McCallaeeb7012010-05-27 06:19:26 +000027static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
28 llvm::Value *Array,
29 llvm::Value *Value,
30 unsigned FirstIndex,
31 unsigned LastIndex) {
32 // Alternatively, we could emit this as a loop in the source.
33 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
34 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
35 Builder.CreateStore(Value, Cell);
36 }
37}
38
John McCalld608cdb2010-08-22 10:59:02 +000039static bool isAggregateTypeForABI(QualType T) {
John McCall9d232c82013-03-07 21:37:08 +000040 return !CodeGenFunction::hasScalarEvaluationKind(T) ||
John McCalld608cdb2010-08-22 10:59:02 +000041 T->isMemberFunctionPointerType();
42}
43
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000044ABIInfo::~ABIInfo() {}
45
Chris Lattnerea044322010-07-29 02:01:43 +000046ASTContext &ABIInfo::getContext() const {
47 return CGT.getContext();
48}
49
50llvm::LLVMContext &ABIInfo::getVMContext() const {
51 return CGT.getLLVMContext();
52}
53
Micah Villmow25a6a842012-10-08 16:25:52 +000054const llvm::DataLayout &ABIInfo::getDataLayout() const {
55 return CGT.getDataLayout();
Chris Lattnerea044322010-07-29 02:01:43 +000056}
57
58
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000059void ABIArgInfo::dump() const {
Chris Lattner5f9e2722011-07-23 10:55:15 +000060 raw_ostream &OS = llvm::errs();
Daniel Dunbar28df7a52009-12-03 09:13:49 +000061 OS << "(ABIArgInfo Kind=";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000062 switch (TheKind) {
63 case Direct:
Chris Lattner800588f2010-07-29 06:26:06 +000064 OS << "Direct Type=";
Chris Lattner2acc6e32011-07-18 04:24:23 +000065 if (llvm::Type *Ty = getCoerceToType())
Chris Lattner800588f2010-07-29 06:26:06 +000066 Ty->print(OS);
67 else
68 OS << "null";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000069 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000070 case Extend:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000071 OS << "Extend";
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000072 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000073 case Ignore:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000074 OS << "Ignore";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000075 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000076 case Indirect:
Daniel Dunbardc6d5742010-04-21 19:10:51 +000077 OS << "Indirect Align=" << getIndirectAlign()
Joerg Sonnenbergere9b5d772011-07-15 18:23:44 +000078 << " ByVal=" << getIndirectByVal()
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +000079 << " Realign=" << getIndirectRealign();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000080 break;
81 case Expand:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000082 OS << "Expand";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000083 break;
84 }
Daniel Dunbar28df7a52009-12-03 09:13:49 +000085 OS << ")\n";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000086}
87
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000088TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
89
John McCall49e34be2011-08-30 01:42:09 +000090// If someone can figure out a general rule for this, that would be great.
91// It's probably just doomed to be platform-dependent, though.
92unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
93 // Verified for:
94 // x86-64 FreeBSD, Linux, Darwin
95 // x86-32 FreeBSD, Linux, Darwin
96 // PowerPC Linux, Darwin
97 // ARM Darwin (*not* EABI)
Tim Northoverc264e162013-01-31 12:13:10 +000098 // AArch64 Linux
John McCall49e34be2011-08-30 01:42:09 +000099 return 32;
100}
101
John McCallde5d3c72012-02-17 03:33:10 +0000102bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
103 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +0000104 // The following conventions are known to require this to be false:
105 // x86_stdcall
106 // MIPS
107 // For everything else, we just prefer false unless we opt out.
108 return false;
109}
110
Daniel Dunbar98303b92009-09-13 08:03:58 +0000111static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000112
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000113/// isEmptyField - Return true iff a the field is "empty", that is it
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000114/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +0000115static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
116 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000117 if (FD->isUnnamedBitfield())
118 return true;
119
120 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000121
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000122 // Constant arrays of empty records count as empty, strip them off.
123 // Constant arrays of zero length always count as empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000124 if (AllowArrays)
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000125 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
126 if (AT->getSize() == 0)
127 return true;
Daniel Dunbar98303b92009-09-13 08:03:58 +0000128 FT = AT->getElementType();
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000129 }
Daniel Dunbar98303b92009-09-13 08:03:58 +0000130
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000131 const RecordType *RT = FT->getAs<RecordType>();
132 if (!RT)
133 return false;
134
135 // C++ record fields are never empty, at least in the Itanium ABI.
136 //
137 // FIXME: We should use a predicate for whether this behavior is true in the
138 // current ABI.
139 if (isa<CXXRecordDecl>(RT->getDecl()))
140 return false;
141
Daniel Dunbar98303b92009-09-13 08:03:58 +0000142 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000143}
144
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000145/// isEmptyRecord - Return true iff a structure contains only empty
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000146/// fields. Note that a structure with a flexible array member is not
147/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000148static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000149 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000150 if (!RT)
151 return 0;
152 const RecordDecl *RD = RT->getDecl();
153 if (RD->hasFlexibleArrayMember())
154 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000155
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000156 // If this is a C++ record, check the bases first.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000157 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000158 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
159 e = CXXRD->bases_end(); i != e; ++i)
160 if (!isEmptyRecord(Context, i->getType(), true))
161 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000162
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000163 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
164 i != e; ++i)
David Blaikie581deb32012-06-06 20:45:41 +0000165 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000166 return false;
167 return true;
168}
169
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000170/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
171/// a non-trivial destructor or a non-trivial copy constructor.
172static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
173 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
174 if (!RD)
175 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000176
Richard Smith426391c2012-11-16 00:53:38 +0000177 return !RD->hasTrivialDestructor() || RD->hasNonTrivialCopyConstructor();
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000178}
179
180/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
181/// a record type with either a non-trivial destructor or a non-trivial copy
182/// constructor.
183static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
184 const RecordType *RT = T->getAs<RecordType>();
185 if (!RT)
186 return false;
187
188 return hasNonTrivialDestructorOrCopyConstructor(RT);
189}
190
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000191/// isSingleElementStruct - Determine if a structure is a "single
192/// element struct", i.e. it has exactly one non-empty field or
193/// exactly one field which is itself a single element
194/// struct. Structures with flexible array members are never
195/// considered single element structs.
196///
197/// \return The field declaration for the single non-empty field, if
198/// it exists.
199static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
200 const RecordType *RT = T->getAsStructureType();
201 if (!RT)
202 return 0;
203
204 const RecordDecl *RD = RT->getDecl();
205 if (RD->hasFlexibleArrayMember())
206 return 0;
207
208 const Type *Found = 0;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000209
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000210 // If this is a C++ record, check the bases first.
211 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
212 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
213 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000214 // Ignore empty records.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000215 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000216 continue;
217
218 // If we already found an element then this isn't a single-element struct.
219 if (Found)
220 return 0;
221
222 // If this is non-empty and not a single element struct, the composite
223 // cannot be a single element struct.
224 Found = isSingleElementStruct(i->getType(), Context);
225 if (!Found)
226 return 0;
227 }
228 }
229
230 // Check for single element.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000231 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
232 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000233 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000234 QualType FT = FD->getType();
235
236 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000237 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000238 continue;
239
240 // If we already found an element then this isn't a single-element
241 // struct.
242 if (Found)
243 return 0;
244
245 // Treat single element arrays as the element.
246 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
247 if (AT->getSize().getZExtValue() != 1)
248 break;
249 FT = AT->getElementType();
250 }
251
John McCalld608cdb2010-08-22 10:59:02 +0000252 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000253 Found = FT.getTypePtr();
254 } else {
255 Found = isSingleElementStruct(FT, Context);
256 if (!Found)
257 return 0;
258 }
259 }
260
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000261 // We don't consider a struct a single-element struct if it has
262 // padding beyond the element type.
263 if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
264 return 0;
265
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000266 return Found;
267}
268
269static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Eli Friedmandb748a32012-11-29 23:21:04 +0000270 // Treat complex types as the element type.
271 if (const ComplexType *CTy = Ty->getAs<ComplexType>())
272 Ty = CTy->getElementType();
273
274 // Check for a type which we know has a simple scalar argument-passing
275 // convention without any padding. (We're specifically looking for 32
276 // and 64-bit integer and integer-equivalents, float, and double.)
Daniel Dunbara1842d32010-05-14 03:40:53 +0000277 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Eli Friedmandb748a32012-11-29 23:21:04 +0000278 !Ty->isEnumeralType() && !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000279 return false;
280
281 uint64_t Size = Context.getTypeSize(Ty);
282 return Size == 32 || Size == 64;
283}
284
Daniel Dunbar53012f42009-11-09 01:33:53 +0000285/// canExpandIndirectArgument - Test whether an argument type which is to be
286/// passed indirectly (on the stack) would have the equivalent layout if it was
287/// expanded into separate arguments. If so, we prefer to do the latter to avoid
288/// inhibiting optimizations.
289///
290// FIXME: This predicate is missing many cases, currently it just follows
291// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
292// should probably make this smarter, or better yet make the LLVM backend
293// capable of handling it.
294static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
295 // We can only expand structure types.
296 const RecordType *RT = Ty->getAs<RecordType>();
297 if (!RT)
298 return false;
299
300 // We can only expand (C) structures.
301 //
302 // FIXME: This needs to be generalized to handle classes as well.
303 const RecordDecl *RD = RT->getDecl();
304 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
305 return false;
306
Eli Friedman506d4e32011-11-18 01:32:26 +0000307 uint64_t Size = 0;
308
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000309 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
310 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000311 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000312
313 if (!is32Or64BitBasicType(FD->getType(), Context))
314 return false;
315
316 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
317 // how to expand them yet, and the predicate for telling if a bitfield still
318 // counts as "basic" is more complicated than what we were doing previously.
319 if (FD->isBitField())
320 return false;
Eli Friedman506d4e32011-11-18 01:32:26 +0000321
322 Size += Context.getTypeSize(FD->getType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000323 }
324
Eli Friedman506d4e32011-11-18 01:32:26 +0000325 // Make sure there are not any holes in the struct.
326 if (Size != Context.getTypeSize(Ty))
327 return false;
328
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000329 return true;
330}
331
332namespace {
333/// DefaultABIInfo - The default implementation for ABI specific
334/// details. This implementation provides information which results in
335/// self-consistent and sensible LLVM IR generation, but does not
336/// conform to any particular ABI.
337class DefaultABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +0000338public:
339 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000340
Chris Lattnera3c109b2010-07-29 02:16:43 +0000341 ABIArgInfo classifyReturnType(QualType RetTy) const;
342 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000343
Chris Lattneree5dcd02010-07-29 02:31:05 +0000344 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000345 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000346 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
347 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000348 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000349 }
350
351 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
352 CodeGenFunction &CGF) const;
353};
354
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000355class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
356public:
Chris Lattnerea044322010-07-29 02:01:43 +0000357 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
358 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000359};
360
361llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
362 CodeGenFunction &CGF) const {
363 return 0;
364}
365
Chris Lattnera3c109b2010-07-29 02:16:43 +0000366ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
Jan Wen Voung90306932011-11-03 00:59:44 +0000367 if (isAggregateTypeForABI(Ty)) {
368 // Records with non trivial destructors/constructors should not be passed
369 // by value.
370 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
371 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
372
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000373 return ABIArgInfo::getIndirect(0);
Jan Wen Voung90306932011-11-03 00:59:44 +0000374 }
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000375
Chris Lattnera14db752010-03-11 18:19:55 +0000376 // Treat an enum type as its underlying type.
377 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
378 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000379
Chris Lattnera14db752010-03-11 18:19:55 +0000380 return (Ty->isPromotableIntegerType() ?
381 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000382}
383
Bob Wilson0024f942011-01-10 23:54:17 +0000384ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
385 if (RetTy->isVoidType())
386 return ABIArgInfo::getIgnore();
387
388 if (isAggregateTypeForABI(RetTy))
389 return ABIArgInfo::getIndirect(0);
390
391 // Treat an enum type as its underlying type.
392 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
393 RetTy = EnumTy->getDecl()->getIntegerType();
394
395 return (RetTy->isPromotableIntegerType() ?
396 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
397}
398
Derek Schuff9ed63f82012-09-06 17:37:28 +0000399//===----------------------------------------------------------------------===//
400// le32/PNaCl bitcode ABI Implementation
401//===----------------------------------------------------------------------===//
402
403class PNaClABIInfo : public ABIInfo {
404 public:
405 PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
406
407 ABIArgInfo classifyReturnType(QualType RetTy) const;
408 ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &FreeRegs) const;
409
410 virtual void computeInfo(CGFunctionInfo &FI) const;
411 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
412 CodeGenFunction &CGF) const;
413};
414
415class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
416 public:
417 PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
418 : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {}
419};
420
421void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
422 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
423
424 unsigned FreeRegs = FI.getHasRegParm() ? FI.getRegParm() : 0;
425
426 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
427 it != ie; ++it)
428 it->info = classifyArgumentType(it->type, FreeRegs);
429 }
430
431llvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
432 CodeGenFunction &CGF) const {
433 return 0;
434}
435
436ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty,
437 unsigned &FreeRegs) const {
438 if (isAggregateTypeForABI(Ty)) {
439 // Records with non trivial destructors/constructors should not be passed
440 // by value.
441 FreeRegs = 0;
442 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
443 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
444
445 return ABIArgInfo::getIndirect(0);
446 }
447
448 // Treat an enum type as its underlying type.
449 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
450 Ty = EnumTy->getDecl()->getIntegerType();
451
452 ABIArgInfo BaseInfo = (Ty->isPromotableIntegerType() ?
453 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
454
455 // Regparm regs hold 32 bits.
456 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
457 if (SizeInRegs == 0) return BaseInfo;
458 if (SizeInRegs > FreeRegs) {
459 FreeRegs = 0;
460 return BaseInfo;
461 }
462 FreeRegs -= SizeInRegs;
463 return BaseInfo.isDirect() ?
464 ABIArgInfo::getDirectInReg(BaseInfo.getCoerceToType()) :
465 ABIArgInfo::getExtendInReg(BaseInfo.getCoerceToType());
466}
467
468ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
469 if (RetTy->isVoidType())
470 return ABIArgInfo::getIgnore();
471
472 if (isAggregateTypeForABI(RetTy))
473 return ABIArgInfo::getIndirect(0);
474
475 // Treat an enum type as its underlying type.
476 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
477 RetTy = EnumTy->getDecl()->getIntegerType();
478
479 return (RetTy->isPromotableIntegerType() ?
480 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
481}
482
Eli Friedman55fc7e22012-01-25 22:46:34 +0000483/// UseX86_MMXType - Return true if this is an MMX type that should use the
484/// special x86_mmx type.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000485bool UseX86_MMXType(llvm::Type *IRType) {
Bill Wendlingbb465d72010-10-18 03:41:31 +0000486 // If the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>, use the
487 // special x86_mmx type.
488 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
489 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
490 IRType->getScalarSizeInBits() != 64;
491}
492
Jay Foadef6de3d2011-07-11 09:56:20 +0000493static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000494 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000495 llvm::Type* Ty) {
Bill Wendling0507be62011-03-07 22:47:14 +0000496 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy())
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000497 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
498 return Ty;
499}
500
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000501//===----------------------------------------------------------------------===//
502// X86-32 ABI Implementation
503//===----------------------------------------------------------------------===//
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000504
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000505/// X86_32ABIInfo - The X86-32 ABI information.
506class X86_32ABIInfo : public ABIInfo {
Rafael Espindolab48280b2012-07-31 02:44:24 +0000507 enum Class {
508 Integer,
509 Float
510 };
511
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000512 static const unsigned MinABIStackAlignInBytes = 4;
513
David Chisnall1e4249c2009-08-17 23:08:21 +0000514 bool IsDarwinVectorABI;
515 bool IsSmallStructInRegABI;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000516 bool IsMMXDisabled;
Eli Friedman55fc7e22012-01-25 22:46:34 +0000517 bool IsWin32FloatStructABI;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000518 unsigned DefaultNumRegisterParameters;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000519
520 static bool isRegisterSize(unsigned Size) {
521 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
522 }
523
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000524 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context,
525 unsigned callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000526
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000527 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
528 /// such that the argument will be passed in memory.
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000529 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal,
530 unsigned &FreeRegs) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000531
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000532 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbare59d8582010-09-16 20:42:06 +0000533 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000534
Rafael Espindolab48280b2012-07-31 02:44:24 +0000535 Class classify(QualType Ty) const;
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000536 ABIArgInfo classifyReturnType(QualType RetTy,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000537 unsigned callingConvention) const;
Rafael Espindolab6932692012-10-24 01:58:58 +0000538 ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &FreeRegs,
539 bool IsFastCall) const;
540 bool shouldUseInReg(QualType Ty, unsigned &FreeRegs,
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000541 bool IsFastCall, bool &NeedsPadding) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000542
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000543public:
544
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000545 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000546 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
547 CodeGenFunction &CGF) const;
548
Rafael Espindolab48280b2012-07-31 02:44:24 +0000549 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m, bool w,
550 unsigned r)
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000551 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
Rafael Espindolab48280b2012-07-31 02:44:24 +0000552 IsMMXDisabled(m), IsWin32FloatStructABI(w),
553 DefaultNumRegisterParameters(r) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000554};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000555
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000556class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
557public:
Eli Friedman55fc7e22012-01-25 22:46:34 +0000558 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
Rafael Espindolab48280b2012-07-31 02:44:24 +0000559 bool d, bool p, bool m, bool w, unsigned r)
560 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, m, w, r)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000561
562 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
563 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000564
565 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
566 // Darwin uses different dwarf register numbers for EH.
567 if (CGM.isTargetDarwin()) return 5;
568
569 return 4;
570 }
571
572 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
573 llvm::Value *Address) const;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000574
Jay Foadef6de3d2011-07-11 09:56:20 +0000575 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000576 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000577 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000578 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
579 }
580
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000581};
582
583}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000584
585/// shouldReturnTypeInRegister - Determine if the given type should be
586/// passed in a register (for the Darwin ABI).
587bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000588 ASTContext &Context,
589 unsigned callingConvention) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000590 uint64_t Size = Context.getTypeSize(Ty);
591
592 // Type must be register sized.
593 if (!isRegisterSize(Size))
594 return false;
595
596 if (Ty->isVectorType()) {
597 // 64- and 128- bit vectors inside structures are not returned in
598 // registers.
599 if (Size == 64 || Size == 128)
600 return false;
601
602 return true;
603 }
604
Daniel Dunbar77115232010-05-15 00:00:30 +0000605 // If this is a builtin, pointer, enum, complex type, member pointer, or
606 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000607 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000608 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000609 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000610 return true;
611
612 // Arrays are treated like records.
613 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000614 return shouldReturnTypeInRegister(AT->getElementType(), Context,
615 callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000616
617 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000618 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000619 if (!RT) return false;
620
Anders Carlssona8874232010-01-27 03:25:19 +0000621 // FIXME: Traverse bases here too.
622
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000623 // For thiscall conventions, structures will never be returned in
624 // a register. This is for compatibility with the MSVC ABI
625 if (callingConvention == llvm::CallingConv::X86_ThisCall &&
626 RT->isStructureType()) {
627 return false;
628 }
629
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000630 // Structure types are passed in register if all fields would be
631 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000632 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
633 e = RT->getDecl()->field_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000634 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000635
636 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000637 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000638 continue;
639
640 // Check fields recursively.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000641 if (!shouldReturnTypeInRegister(FD->getType(), Context,
642 callingConvention))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000643 return false;
644 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000645 return true;
646}
647
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000648ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
649 unsigned callingConvention) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000650 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000651 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000652
Chris Lattnera3c109b2010-07-29 02:16:43 +0000653 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000654 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000655 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000656 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000657
658 // 128-bit vectors are a special case; they are returned in
659 // registers and we need to make sure to pick a type the LLVM
660 // backend will like.
661 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +0000662 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000663 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000664
665 // Always return in register if it fits in a general purpose
666 // register, or if it is 64 bits and has a single element.
667 if ((Size == 8 || Size == 16 || Size == 32) ||
668 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +0000669 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000670 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000671
672 return ABIArgInfo::getIndirect(0);
673 }
674
675 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000676 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000677
John McCalld608cdb2010-08-22 10:59:02 +0000678 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000679 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000680 // Structures with either a non-trivial destructor or a non-trivial
681 // copy constructor are always indirect.
682 if (hasNonTrivialDestructorOrCopyConstructor(RT))
683 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000684
Anders Carlsson40092972009-10-20 22:07:59 +0000685 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000686 if (RT->getDecl()->hasFlexibleArrayMember())
687 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000688 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000689
David Chisnall1e4249c2009-08-17 23:08:21 +0000690 // If specified, structs and unions are always indirect.
691 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000692 return ABIArgInfo::getIndirect(0);
693
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000694 // Small structures which are register sized are generally returned
695 // in a register.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000696 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(),
697 callingConvention)) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000698 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000699
700 // As a special-case, if the struct is a "single-element" struct, and
701 // the field is of type "float" or "double", return it in a
Eli Friedman55fc7e22012-01-25 22:46:34 +0000702 // floating-point register. (MSVC does not apply this special case.)
703 // We apply a similar transformation for pointer types to improve the
704 // quality of the generated IR.
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000705 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
Eli Friedman55fc7e22012-01-25 22:46:34 +0000706 if ((!IsWin32FloatStructABI && SeltTy->isRealFloatingType())
707 || SeltTy->hasPointerRepresentation())
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000708 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
709
710 // FIXME: We should be able to narrow this integer in cases with dead
711 // padding.
Chris Lattner800588f2010-07-29 06:26:06 +0000712 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000713 }
714
715 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000716 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000717
Chris Lattnera3c109b2010-07-29 02:16:43 +0000718 // Treat an enum type as its underlying type.
719 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
720 RetTy = EnumTy->getDecl()->getIntegerType();
721
722 return (RetTy->isPromotableIntegerType() ?
723 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000724}
725
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000726static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
727 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
728}
729
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000730static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
731 const RecordType *RT = Ty->getAs<RecordType>();
732 if (!RT)
733 return 0;
734 const RecordDecl *RD = RT->getDecl();
735
736 // If this is a C++ record, check the bases first.
737 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
738 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
739 e = CXXRD->bases_end(); i != e; ++i)
740 if (!isRecordWithSSEVectorType(Context, i->getType()))
741 return false;
742
743 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
744 i != e; ++i) {
745 QualType FT = i->getType();
746
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000747 if (isSSEVectorType(Context, FT))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000748 return true;
749
750 if (isRecordWithSSEVectorType(Context, FT))
751 return true;
752 }
753
754 return false;
755}
756
Daniel Dunbare59d8582010-09-16 20:42:06 +0000757unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
758 unsigned Align) const {
759 // Otherwise, if the alignment is less than or equal to the minimum ABI
760 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000761 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +0000762 return 0; // Use default alignment.
763
764 // On non-Darwin, the stack type alignment is always 4.
765 if (!IsDarwinVectorABI) {
766 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000767 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +0000768 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000769
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000770 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000771 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
772 isRecordWithSSEVectorType(getContext(), Ty)))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000773 return 16;
774
775 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000776}
777
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000778ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
779 unsigned &FreeRegs) const {
780 if (!ByVal) {
781 if (FreeRegs) {
782 --FreeRegs; // Non byval indirects just use one pointer.
783 return ABIArgInfo::getIndirectInReg(0, false);
784 }
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000785 return ABIArgInfo::getIndirect(0, false);
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000786 }
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000787
Daniel Dunbare59d8582010-09-16 20:42:06 +0000788 // Compute the byval alignment.
789 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
790 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
791 if (StackAlign == 0)
Chris Lattnerde92d732011-05-22 23:35:00 +0000792 return ABIArgInfo::getIndirect(4);
Daniel Dunbare59d8582010-09-16 20:42:06 +0000793
794 // If the stack alignment is less than the type alignment, realign the
795 // argument.
796 if (StackAlign < TypeAlign)
797 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
798 /*Realign=*/true);
799
800 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000801}
802
Rafael Espindolab48280b2012-07-31 02:44:24 +0000803X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
804 const Type *T = isSingleElementStruct(Ty, getContext());
805 if (!T)
806 T = Ty.getTypePtr();
807
808 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
809 BuiltinType::Kind K = BT->getKind();
810 if (K == BuiltinType::Float || K == BuiltinType::Double)
811 return Float;
812 }
813 return Integer;
814}
815
Rafael Espindolab6932692012-10-24 01:58:58 +0000816bool X86_32ABIInfo::shouldUseInReg(QualType Ty, unsigned &FreeRegs,
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000817 bool IsFastCall, bool &NeedsPadding) const {
818 NeedsPadding = false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000819 Class C = classify(Ty);
820 if (C == Float)
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000821 return false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000822
Rafael Espindolab6932692012-10-24 01:58:58 +0000823 unsigned Size = getContext().getTypeSize(Ty);
824 unsigned SizeInRegs = (Size + 31) / 32;
Rafael Espindola5f14fcb2012-10-23 02:04:01 +0000825
826 if (SizeInRegs == 0)
827 return false;
828
Rafael Espindolab48280b2012-07-31 02:44:24 +0000829 if (SizeInRegs > FreeRegs) {
830 FreeRegs = 0;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000831 return false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000832 }
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000833
Rafael Espindolab48280b2012-07-31 02:44:24 +0000834 FreeRegs -= SizeInRegs;
Rafael Espindolab6932692012-10-24 01:58:58 +0000835
836 if (IsFastCall) {
837 if (Size > 32)
838 return false;
839
840 if (Ty->isIntegralOrEnumerationType())
841 return true;
842
843 if (Ty->isPointerType())
844 return true;
845
846 if (Ty->isReferenceType())
847 return true;
848
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000849 if (FreeRegs)
850 NeedsPadding = true;
851
Rafael Espindolab6932692012-10-24 01:58:58 +0000852 return false;
853 }
854
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000855 return true;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000856}
857
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000858ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Rafael Espindolab6932692012-10-24 01:58:58 +0000859 unsigned &FreeRegs,
860 bool IsFastCall) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000861 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000862 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000863 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000864 if (const RecordType *RT = Ty->getAs<RecordType>()) {
865 // Structures with either a non-trivial destructor or a non-trivial
866 // copy constructor are always indirect.
867 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000868 return getIndirectResult(Ty, false, FreeRegs);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000869
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000870 if (RT->getDecl()->hasFlexibleArrayMember())
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000871 return getIndirectResult(Ty, true, FreeRegs);
Anders Carlssona8874232010-01-27 03:25:19 +0000872 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000873
Eli Friedman5a4d3522011-11-18 00:28:11 +0000874 // Ignore empty structs/unions.
Eli Friedman5a1ac892011-11-18 04:01:36 +0000875 if (isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000876 return ABIArgInfo::getIgnore();
877
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000878 llvm::LLVMContext &LLVMContext = getVMContext();
879 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
880 bool NeedsPadding;
881 if (shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding)) {
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000882 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000883 SmallVector<llvm::Type*, 3> Elements;
884 for (unsigned I = 0; I < SizeInRegs; ++I)
885 Elements.push_back(Int32);
886 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
887 return ABIArgInfo::getDirectInReg(Result);
888 }
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000889 llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : 0;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000890
Daniel Dunbar53012f42009-11-09 01:33:53 +0000891 // Expand small (<= 128-bit) record types when we know that the stack layout
892 // of those arguments will match the struct. This is important because the
893 // LLVM backend isn't smart enough to remove byval, which inhibits many
894 // optimizations.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000895 if (getContext().getTypeSize(Ty) <= 4*32 &&
896 canExpandIndirectArgument(Ty, getContext()))
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000897 return ABIArgInfo::getExpandWithPadding(IsFastCall, PaddingType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000898
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000899 return getIndirectResult(Ty, true, FreeRegs);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000900 }
901
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000902 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +0000903 // On Darwin, some vectors are passed in memory, we handle this by passing
904 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000905 if (IsDarwinVectorABI) {
906 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000907 if ((Size == 8 || Size == 16 || Size == 32) ||
908 (Size == 64 && VT->getNumElements() == 1))
909 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
910 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000911 }
Bill Wendlingbb465d72010-10-18 03:41:31 +0000912
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000913 llvm::Type *IRType = CGT.ConvertType(Ty);
Bill Wendlingbb465d72010-10-18 03:41:31 +0000914 if (UseX86_MMXType(IRType)) {
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000915 if (IsMMXDisabled)
916 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
917 64));
Bill Wendlingbb465d72010-10-18 03:41:31 +0000918 ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
919 AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
920 return AAI;
921 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000922
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000923 return ABIArgInfo::getDirect();
924 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000925
926
Chris Lattnera3c109b2010-07-29 02:16:43 +0000927 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
928 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000929
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000930 bool NeedsPadding;
931 bool InReg = shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding);
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000932
933 if (Ty->isPromotableIntegerType()) {
934 if (InReg)
935 return ABIArgInfo::getExtendInReg();
936 return ABIArgInfo::getExtend();
937 }
938 if (InReg)
939 return ABIArgInfo::getDirectInReg();
940 return ABIArgInfo::getDirect();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000941}
942
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000943void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
944 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
945 FI.getCallingConvention());
Rafael Espindolab48280b2012-07-31 02:44:24 +0000946
Rafael Espindolab6932692012-10-24 01:58:58 +0000947 unsigned CC = FI.getCallingConvention();
948 bool IsFastCall = CC == llvm::CallingConv::X86_FastCall;
949 unsigned FreeRegs;
950 if (IsFastCall)
951 FreeRegs = 2;
952 else if (FI.getHasRegParm())
953 FreeRegs = FI.getRegParm();
954 else
955 FreeRegs = DefaultNumRegisterParameters;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000956
957 // If the return value is indirect, then the hidden argument is consuming one
958 // integer register.
959 if (FI.getReturnInfo().isIndirect() && FreeRegs) {
960 --FreeRegs;
961 ABIArgInfo &Old = FI.getReturnInfo();
962 Old = ABIArgInfo::getIndirectInReg(Old.getIndirectAlign(),
963 Old.getIndirectByVal(),
964 Old.getIndirectRealign());
965 }
966
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000967 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
968 it != ie; ++it)
Rafael Espindolab6932692012-10-24 01:58:58 +0000969 it->info = classifyArgumentType(it->type, FreeRegs, IsFastCall);
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000970}
971
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000972llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
973 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +0000974 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000975
976 CGBuilderTy &Builder = CGF.Builder;
977 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
978 "ap");
979 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Eli Friedman7b1fb812011-11-18 02:12:09 +0000980
981 // Compute if the address needs to be aligned
982 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
983 Align = getTypeStackAlignInBytes(Ty, Align);
984 Align = std::max(Align, 4U);
985 if (Align > 4) {
986 // addr = (addr + align - 1) & -align;
987 llvm::Value *Offset =
988 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
989 Addr = CGF.Builder.CreateGEP(Addr, Offset);
990 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
991 CGF.Int32Ty);
992 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
993 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
994 Addr->getType(),
995 "ap.cur.aligned");
996 }
997
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000998 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000999 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001000 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1001
1002 uint64_t Offset =
Eli Friedman7b1fb812011-11-18 02:12:09 +00001003 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001004 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00001005 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001006 "ap.next");
1007 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1008
1009 return AddrTyped;
1010}
1011
Charles Davis74f72932010-02-13 15:54:06 +00001012void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
1013 llvm::GlobalValue *GV,
1014 CodeGen::CodeGenModule &CGM) const {
1015 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1016 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
1017 // Get the LLVM function.
1018 llvm::Function *Fn = cast<llvm::Function>(GV);
1019
1020 // Now add the 'alignstack' attribute with a value of 16.
Bill Wendling0d583392012-10-15 20:36:26 +00001021 llvm::AttrBuilder B;
Bill Wendlinge91e9ec2012-10-14 03:28:14 +00001022 B.addStackAlignmentAttr(16);
Bill Wendling909b6de2013-01-23 00:21:06 +00001023 Fn->addAttributes(llvm::AttributeSet::FunctionIndex,
1024 llvm::AttributeSet::get(CGM.getLLVMContext(),
1025 llvm::AttributeSet::FunctionIndex,
1026 B));
Charles Davis74f72932010-02-13 15:54:06 +00001027 }
1028 }
1029}
1030
John McCall6374c332010-03-06 00:35:14 +00001031bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
1032 CodeGen::CodeGenFunction &CGF,
1033 llvm::Value *Address) const {
1034 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCall6374c332010-03-06 00:35:14 +00001035
Chris Lattner8b418682012-02-07 00:39:47 +00001036 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001037
John McCall6374c332010-03-06 00:35:14 +00001038 // 0-7 are the eight integer registers; the order is different
1039 // on Darwin (for EH), but the range is the same.
1040 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +00001041 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +00001042
1043 if (CGF.CGM.isTargetDarwin()) {
1044 // 12-16 are st(0..4). Not sure why we stop at 4.
1045 // These have size 16, which is sizeof(long double) on
1046 // platforms with 8-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001047 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCallaeeb7012010-05-27 06:19:26 +00001048 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001049
John McCall6374c332010-03-06 00:35:14 +00001050 } else {
1051 // 9 is %eflags, which doesn't get a size on Darwin for some
1052 // reason.
1053 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
1054
1055 // 11-16 are st(0..5). Not sure why we stop at 5.
1056 // These have size 12, which is sizeof(long double) on
1057 // platforms with 4-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001058 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCallaeeb7012010-05-27 06:19:26 +00001059 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1060 }
John McCall6374c332010-03-06 00:35:14 +00001061
1062 return false;
1063}
1064
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001065//===----------------------------------------------------------------------===//
1066// X86-64 ABI Implementation
1067//===----------------------------------------------------------------------===//
1068
1069
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001070namespace {
1071/// X86_64ABIInfo - The X86_64 ABI information.
1072class X86_64ABIInfo : public ABIInfo {
1073 enum Class {
1074 Integer = 0,
1075 SSE,
1076 SSEUp,
1077 X87,
1078 X87Up,
1079 ComplexX87,
1080 NoClass,
1081 Memory
1082 };
1083
1084 /// merge - Implement the X86_64 ABI merging algorithm.
1085 ///
1086 /// Merge an accumulating classification \arg Accum with a field
1087 /// classification \arg Field.
1088 ///
1089 /// \param Accum - The accumulating classification. This should
1090 /// always be either NoClass or the result of a previous merge
1091 /// call. In addition, this should never be Memory (the caller
1092 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001093 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001094
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001095 /// postMerge - Implement the X86_64 ABI post merging algorithm.
1096 ///
1097 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
1098 /// final MEMORY or SSE classes when necessary.
1099 ///
1100 /// \param AggregateSize - The size of the current aggregate in
1101 /// the classification process.
1102 ///
1103 /// \param Lo - The classification for the parts of the type
1104 /// residing in the low word of the containing object.
1105 ///
1106 /// \param Hi - The classification for the parts of the type
1107 /// residing in the higher words of the containing object.
1108 ///
1109 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
1110
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001111 /// classify - Determine the x86_64 register classes in which the
1112 /// given type T should be passed.
1113 ///
1114 /// \param Lo - The classification for the parts of the type
1115 /// residing in the low word of the containing object.
1116 ///
1117 /// \param Hi - The classification for the parts of the type
1118 /// residing in the high word of the containing object.
1119 ///
1120 /// \param OffsetBase - The bit offset of this type in the
1121 /// containing object. Some parameters are classified different
1122 /// depending on whether they straddle an eightbyte boundary.
1123 ///
1124 /// If a word is unused its result will be NoClass; if a type should
1125 /// be passed in Memory then at least the classification of \arg Lo
1126 /// will be Memory.
1127 ///
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001128 /// The \arg Lo class will be NoClass iff the argument is ignored.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001129 ///
1130 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
1131 /// also be ComplexX87.
Chris Lattner9c254f02010-06-29 06:01:59 +00001132 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001133
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001134 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001135 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
1136 unsigned IROffset, QualType SourceTy,
1137 unsigned SourceOffset) const;
1138 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
1139 unsigned IROffset, QualType SourceTy,
1140 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001141
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001142 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001143 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +00001144 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001145
1146 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001147 /// such that the argument will be passed in memory.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001148 ///
1149 /// \param freeIntRegs - The number of free integer registers remaining
1150 /// available.
1151 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001152
Chris Lattnera3c109b2010-07-29 02:16:43 +00001153 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001154
Bill Wendlingbb465d72010-10-18 03:41:31 +00001155 ABIArgInfo classifyArgumentType(QualType Ty,
Daniel Dunbaredfac032012-03-10 01:03:58 +00001156 unsigned freeIntRegs,
Bill Wendlingbb465d72010-10-18 03:41:31 +00001157 unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +00001158 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001159
Eli Friedmanee1ad992011-12-02 00:11:43 +00001160 bool IsIllegalVectorType(QualType Ty) const;
1161
John McCall67a57732011-04-21 01:20:55 +00001162 /// The 0.98 ABI revision clarified a lot of ambiguities,
1163 /// unfortunately in ways that were not always consistent with
1164 /// certain previous compilers. In particular, platforms which
1165 /// required strict binary compatibility with older versions of GCC
1166 /// may need to exempt themselves.
1167 bool honorsRevision0_98() const {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001168 return !getContext().getTargetInfo().getTriple().isOSDarwin();
John McCall67a57732011-04-21 01:20:55 +00001169 }
1170
Eli Friedmanee1ad992011-12-02 00:11:43 +00001171 bool HasAVX;
Derek Schuffbabaf312012-10-11 15:52:22 +00001172 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
1173 // 64-bit hardware.
1174 bool Has64BitPointers;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001175
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001176public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001177 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
Derek Schuffbabaf312012-10-11 15:52:22 +00001178 ABIInfo(CGT), HasAVX(hasavx),
Derek Schuff90da80c2012-10-11 18:21:13 +00001179 Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001180 }
Chris Lattner9c254f02010-06-29 06:01:59 +00001181
John McCallde5d3c72012-02-17 03:33:10 +00001182 bool isPassedUsingAVXType(QualType type) const {
1183 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00001184 // The freeIntRegs argument doesn't matter here.
1185 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE);
John McCallde5d3c72012-02-17 03:33:10 +00001186 if (info.isDirect()) {
1187 llvm::Type *ty = info.getCoerceToType();
1188 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
1189 return (vectorTy->getBitWidth() > 128);
1190 }
1191 return false;
1192 }
1193
Chris Lattneree5dcd02010-07-29 02:31:05 +00001194 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001195
1196 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1197 CodeGenFunction &CGF) const;
1198};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001199
Chris Lattnerf13721d2010-08-31 16:44:54 +00001200/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001201class WinX86_64ABIInfo : public ABIInfo {
1202
1203 ABIArgInfo classify(QualType Ty) const;
1204
Chris Lattnerf13721d2010-08-31 16:44:54 +00001205public:
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001206 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
1207
1208 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattnerf13721d2010-08-31 16:44:54 +00001209
1210 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1211 CodeGenFunction &CGF) const;
1212};
1213
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001214class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1215public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001216 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
Derek Schuffbabaf312012-10-11 15:52:22 +00001217 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
John McCall6374c332010-03-06 00:35:14 +00001218
John McCallde5d3c72012-02-17 03:33:10 +00001219 const X86_64ABIInfo &getABIInfo() const {
1220 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
1221 }
1222
John McCall6374c332010-03-06 00:35:14 +00001223 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1224 return 7;
1225 }
1226
1227 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1228 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001229 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001230
John McCallaeeb7012010-05-27 06:19:26 +00001231 // 0-15 are the 16 integer registers.
1232 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001233 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +00001234 return false;
1235 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001236
Jay Foadef6de3d2011-07-11 09:56:20 +00001237 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001238 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +00001239 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001240 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1241 }
1242
John McCallde5d3c72012-02-17 03:33:10 +00001243 bool isNoProtoCallVariadic(const CallArgList &args,
1244 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +00001245 // The default CC on x86-64 sets %al to the number of SSA
1246 // registers used, and GCC sets this when calling an unprototyped
Eli Friedman3ed79032011-12-01 04:53:19 +00001247 // function, so we override the default behavior. However, don't do
Eli Friedman68805fe2011-12-06 03:08:26 +00001248 // that when AVX types are involved: the ABI explicitly states it is
1249 // undefined, and it doesn't work in practice because of how the ABI
1250 // defines varargs anyway.
John McCallde5d3c72012-02-17 03:33:10 +00001251 if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) {
Eli Friedman3ed79032011-12-01 04:53:19 +00001252 bool HasAVXType = false;
John McCallde5d3c72012-02-17 03:33:10 +00001253 for (CallArgList::const_iterator
1254 it = args.begin(), ie = args.end(); it != ie; ++it) {
1255 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1256 HasAVXType = true;
1257 break;
Eli Friedman3ed79032011-12-01 04:53:19 +00001258 }
1259 }
John McCallde5d3c72012-02-17 03:33:10 +00001260
Eli Friedman3ed79032011-12-01 04:53:19 +00001261 if (!HasAVXType)
1262 return true;
1263 }
John McCall01f151e2011-09-21 08:08:30 +00001264
John McCallde5d3c72012-02-17 03:33:10 +00001265 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
John McCall01f151e2011-09-21 08:08:30 +00001266 }
1267
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001268};
1269
Chris Lattnerf13721d2010-08-31 16:44:54 +00001270class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1271public:
1272 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1273 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1274
1275 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1276 return 7;
1277 }
1278
1279 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1280 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001281 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001282
Chris Lattnerf13721d2010-08-31 16:44:54 +00001283 // 0-15 are the 16 integer registers.
1284 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001285 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattnerf13721d2010-08-31 16:44:54 +00001286 return false;
1287 }
1288};
1289
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001290}
1291
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001292void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1293 Class &Hi) const {
1294 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1295 //
1296 // (a) If one of the classes is Memory, the whole argument is passed in
1297 // memory.
1298 //
1299 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1300 // memory.
1301 //
1302 // (c) If the size of the aggregate exceeds two eightbytes and the first
1303 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1304 // argument is passed in memory. NOTE: This is necessary to keep the
1305 // ABI working for processors that don't support the __m256 type.
1306 //
1307 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1308 //
1309 // Some of these are enforced by the merging logic. Others can arise
1310 // only with unions; for example:
1311 // union { _Complex double; unsigned; }
1312 //
1313 // Note that clauses (b) and (c) were added in 0.98.
1314 //
1315 if (Hi == Memory)
1316 Lo = Memory;
1317 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1318 Lo = Memory;
1319 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1320 Lo = Memory;
1321 if (Hi == SSEUp && Lo != SSE)
1322 Hi = SSE;
1323}
1324
Chris Lattner1090a9b2010-06-28 21:43:59 +00001325X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001326 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1327 // classified recursively so that always two fields are
1328 // considered. The resulting class is calculated according to
1329 // the classes of the fields in the eightbyte:
1330 //
1331 // (a) If both classes are equal, this is the resulting class.
1332 //
1333 // (b) If one of the classes is NO_CLASS, the resulting class is
1334 // the other class.
1335 //
1336 // (c) If one of the classes is MEMORY, the result is the MEMORY
1337 // class.
1338 //
1339 // (d) If one of the classes is INTEGER, the result is the
1340 // INTEGER.
1341 //
1342 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1343 // MEMORY is used as class.
1344 //
1345 // (f) Otherwise class SSE is used.
1346
1347 // Accum should never be memory (we should have returned) or
1348 // ComplexX87 (because this cannot be passed in a structure).
1349 assert((Accum != Memory && Accum != ComplexX87) &&
1350 "Invalid accumulated classification during merge.");
1351 if (Accum == Field || Field == NoClass)
1352 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001353 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001354 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001355 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001356 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001357 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001358 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001359 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1360 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001361 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001362 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001363}
1364
Chris Lattnerbcaedae2010-06-30 19:14:05 +00001365void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001366 Class &Lo, Class &Hi) const {
1367 // FIXME: This code can be simplified by introducing a simple value class for
1368 // Class pairs with appropriate constructor methods for the various
1369 // situations.
1370
1371 // FIXME: Some of the split computations are wrong; unaligned vectors
1372 // shouldn't be passed in registers for example, so there is no chance they
1373 // can straddle an eightbyte. Verify & simplify.
1374
1375 Lo = Hi = NoClass;
1376
1377 Class &Current = OffsetBase < 64 ? Lo : Hi;
1378 Current = Memory;
1379
John McCall183700f2009-09-21 23:43:11 +00001380 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001381 BuiltinType::Kind k = BT->getKind();
1382
1383 if (k == BuiltinType::Void) {
1384 Current = NoClass;
1385 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1386 Lo = Integer;
1387 Hi = Integer;
1388 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1389 Current = Integer;
Derek Schuff7da46f92012-10-11 16:55:58 +00001390 } else if ((k == BuiltinType::Float || k == BuiltinType::Double) ||
1391 (k == BuiltinType::LongDouble &&
1392 getContext().getTargetInfo().getTriple().getOS() ==
Eli Bendersky441d9f72012-12-04 18:38:10 +00001393 llvm::Triple::NaCl)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001394 Current = SSE;
1395 } else if (k == BuiltinType::LongDouble) {
1396 Lo = X87;
1397 Hi = X87Up;
1398 }
1399 // FIXME: _Decimal32 and _Decimal64 are SSE.
1400 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001401 return;
1402 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001403
Chris Lattner1090a9b2010-06-28 21:43:59 +00001404 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001405 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +00001406 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001407 return;
1408 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001409
Chris Lattner1090a9b2010-06-28 21:43:59 +00001410 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001411 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001412 return;
1413 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001414
Chris Lattner1090a9b2010-06-28 21:43:59 +00001415 if (Ty->isMemberPointerType()) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001416 if (Ty->isMemberFunctionPointerType() && Has64BitPointers)
Daniel Dunbar67d438d2010-05-15 00:00:37 +00001417 Lo = Hi = Integer;
1418 else
1419 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001420 return;
1421 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001422
Chris Lattner1090a9b2010-06-28 21:43:59 +00001423 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001424 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001425 if (Size == 32) {
1426 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1427 // float> as integer.
1428 Current = Integer;
1429
1430 // If this type crosses an eightbyte boundary, it should be
1431 // split.
1432 uint64_t EB_Real = (OffsetBase) / 64;
1433 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1434 if (EB_Real != EB_Imag)
1435 Hi = Lo;
1436 } else if (Size == 64) {
1437 // gcc passes <1 x double> in memory. :(
1438 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1439 return;
1440
1441 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001442 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001443 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1444 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1445 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001446 Current = Integer;
1447 else
1448 Current = SSE;
1449
1450 // If this type crosses an eightbyte boundary, it should be
1451 // split.
1452 if (OffsetBase && OffsetBase != 64)
1453 Hi = Lo;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001454 } else if (Size == 128 || (HasAVX && Size == 256)) {
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001455 // Arguments of 256-bits are split into four eightbyte chunks. The
1456 // least significant one belongs to class SSE and all the others to class
1457 // SSEUP. The original Lo and Hi design considers that types can't be
1458 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1459 // This design isn't correct for 256-bits, but since there're no cases
1460 // where the upper parts would need to be inspected, avoid adding
1461 // complexity and just consider Hi to match the 64-256 part.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001462 Lo = SSE;
1463 Hi = SSEUp;
1464 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001465 return;
1466 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001467
Chris Lattner1090a9b2010-06-28 21:43:59 +00001468 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001469 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001470
Chris Lattnerea044322010-07-29 02:01:43 +00001471 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001472 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001473 if (Size <= 64)
1474 Current = Integer;
1475 else if (Size <= 128)
1476 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001477 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001478 Current = SSE;
Derek Schuff7da46f92012-10-11 16:55:58 +00001479 else if (ET == getContext().DoubleTy ||
1480 (ET == getContext().LongDoubleTy &&
1481 getContext().getTargetInfo().getTriple().getOS() ==
Eli Bendersky441d9f72012-12-04 18:38:10 +00001482 llvm::Triple::NaCl))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001483 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001484 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001485 Current = ComplexX87;
1486
1487 // If this complex type crosses an eightbyte boundary then it
1488 // should be split.
1489 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001490 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001491 if (Hi == NoClass && EB_Real != EB_Imag)
1492 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001493
Chris Lattner1090a9b2010-06-28 21:43:59 +00001494 return;
1495 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001496
Chris Lattnerea044322010-07-29 02:01:43 +00001497 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001498 // Arrays are treated like structures.
1499
Chris Lattnerea044322010-07-29 02:01:43 +00001500 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001501
1502 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001503 // than four eightbytes, ..., it has class MEMORY.
1504 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001505 return;
1506
1507 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1508 // fields, it has class MEMORY.
1509 //
1510 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001511 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001512 return;
1513
1514 // Otherwise implement simplified merge. We could be smarter about
1515 // this, but it isn't worth it and would be harder to verify.
1516 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001517 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001518 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes089d8922011-07-12 01:27:38 +00001519
1520 // The only case a 256-bit wide vector could be used is when the array
1521 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1522 // to work for sizes wider than 128, early check and fallback to memory.
1523 if (Size > 128 && EltSize != 256)
1524 return;
1525
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001526 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1527 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001528 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001529 Lo = merge(Lo, FieldLo);
1530 Hi = merge(Hi, FieldHi);
1531 if (Lo == Memory || Hi == Memory)
1532 break;
1533 }
1534
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001535 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001536 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001537 return;
1538 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001539
Chris Lattner1090a9b2010-06-28 21:43:59 +00001540 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001541 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001542
1543 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001544 // than four eightbytes, ..., it has class MEMORY.
1545 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001546 return;
1547
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001548 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1549 // copy constructor or a non-trivial destructor, it is passed by invisible
1550 // reference.
1551 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1552 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001553
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001554 const RecordDecl *RD = RT->getDecl();
1555
1556 // Assume variable sized types are passed in memory.
1557 if (RD->hasFlexibleArrayMember())
1558 return;
1559
Chris Lattnerea044322010-07-29 02:01:43 +00001560 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001561
1562 // Reset Lo class, this will be recomputed.
1563 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001564
1565 // If this is a C++ record, classify the bases first.
1566 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1567 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1568 e = CXXRD->bases_end(); i != e; ++i) {
1569 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1570 "Unexpected base class!");
1571 const CXXRecordDecl *Base =
1572 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1573
1574 // Classify this field.
1575 //
1576 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1577 // single eightbyte, each is classified separately. Each eightbyte gets
1578 // initialized to class NO_CLASS.
1579 Class FieldLo, FieldHi;
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001580 uint64_t Offset =
1581 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
Chris Lattner9c254f02010-06-29 06:01:59 +00001582 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001583 Lo = merge(Lo, FieldLo);
1584 Hi = merge(Hi, FieldHi);
1585 if (Lo == Memory || Hi == Memory)
1586 break;
1587 }
1588 }
1589
1590 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001591 unsigned idx = 0;
Bruno Cardoso Lopes548e4782011-07-12 22:30:58 +00001592 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001593 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001594 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1595 bool BitField = i->isBitField();
1596
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001597 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1598 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001599 //
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001600 // The only case a 256-bit wide vector could be used is when the struct
1601 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1602 // to work for sizes wider than 128, early check and fallback to memory.
1603 //
1604 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1605 Lo = Memory;
1606 return;
1607 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001608 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001609 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001610 Lo = Memory;
1611 return;
1612 }
1613
1614 // Classify this field.
1615 //
1616 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1617 // exceeds a single eightbyte, each is classified
1618 // separately. Each eightbyte gets initialized to class
1619 // NO_CLASS.
1620 Class FieldLo, FieldHi;
1621
1622 // Bit-fields require special handling, they do not force the
1623 // structure to be passed in memory even if unaligned, and
1624 // therefore they can straddle an eightbyte.
1625 if (BitField) {
1626 // Ignore padding bit-fields.
1627 if (i->isUnnamedBitfield())
1628 continue;
1629
1630 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001631 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001632
1633 uint64_t EB_Lo = Offset / 64;
1634 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1635 FieldLo = FieldHi = NoClass;
1636 if (EB_Lo) {
1637 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1638 FieldLo = NoClass;
1639 FieldHi = Integer;
1640 } else {
1641 FieldLo = Integer;
1642 FieldHi = EB_Hi ? Integer : NoClass;
1643 }
1644 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001645 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001646 Lo = merge(Lo, FieldLo);
1647 Hi = merge(Hi, FieldHi);
1648 if (Lo == Memory || Hi == Memory)
1649 break;
1650 }
1651
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001652 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001653 }
1654}
1655
Chris Lattner9c254f02010-06-29 06:01:59 +00001656ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001657 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1658 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001659 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001660 // Treat an enum type as its underlying type.
1661 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1662 Ty = EnumTy->getDecl()->getIntegerType();
1663
1664 return (Ty->isPromotableIntegerType() ?
1665 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1666 }
1667
1668 return ABIArgInfo::getIndirect(0);
1669}
1670
Eli Friedmanee1ad992011-12-02 00:11:43 +00001671bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1672 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1673 uint64_t Size = getContext().getTypeSize(VecTy);
1674 unsigned LargestVector = HasAVX ? 256 : 128;
1675 if (Size <= 64 || Size > LargestVector)
1676 return true;
1677 }
1678
1679 return false;
1680}
1681
Daniel Dunbaredfac032012-03-10 01:03:58 +00001682ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1683 unsigned freeIntRegs) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001684 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1685 // place naturally.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001686 //
1687 // This assumption is optimistic, as there could be free registers available
1688 // when we need to pass this argument in memory, and LLVM could try to pass
1689 // the argument in the free register. This does not seem to happen currently,
1690 // but this code would be much safer if we could mark the argument with
1691 // 'onstack'. See PR12193.
Eli Friedmanee1ad992011-12-02 00:11:43 +00001692 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001693 // Treat an enum type as its underlying type.
1694 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1695 Ty = EnumTy->getDecl()->getIntegerType();
1696
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001697 return (Ty->isPromotableIntegerType() ?
1698 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001699 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001700
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001701 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1702 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001703
Chris Lattner855d2272011-05-22 23:21:23 +00001704 // Compute the byval alignment. We specify the alignment of the byval in all
1705 // cases so that the mid-level optimizer knows the alignment of the byval.
1706 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
Daniel Dunbaredfac032012-03-10 01:03:58 +00001707
1708 // Attempt to avoid passing indirect results using byval when possible. This
1709 // is important for good codegen.
1710 //
1711 // We do this by coercing the value into a scalar type which the backend can
1712 // handle naturally (i.e., without using byval).
1713 //
1714 // For simplicity, we currently only do this when we have exhausted all of the
1715 // free integer registers. Doing this when there are free integer registers
1716 // would require more care, as we would have to ensure that the coerced value
1717 // did not claim the unused register. That would require either reording the
1718 // arguments to the function (so that any subsequent inreg values came first),
1719 // or only doing this optimization when there were no following arguments that
1720 // might be inreg.
1721 //
1722 // We currently expect it to be rare (particularly in well written code) for
1723 // arguments to be passed on the stack when there are still free integer
1724 // registers available (this would typically imply large structs being passed
1725 // by value), so this seems like a fair tradeoff for now.
1726 //
1727 // We can revisit this if the backend grows support for 'onstack' parameter
1728 // attributes. See PR12193.
1729 if (freeIntRegs == 0) {
1730 uint64_t Size = getContext().getTypeSize(Ty);
1731
1732 // If this type fits in an eightbyte, coerce it into the matching integral
1733 // type, which will end up on the stack (with alignment 8).
1734 if (Align == 8 && Size <= 64)
1735 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1736 Size));
1737 }
1738
Chris Lattner855d2272011-05-22 23:21:23 +00001739 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001740}
1741
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001742/// GetByteVectorType - The ABI specifies that a value should be passed in an
1743/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner0f408f52010-07-29 04:56:46 +00001744/// vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001745llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001746 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001747
Chris Lattner15842bd2010-07-29 05:02:29 +00001748 // Wrapper structs that just contain vectors are passed just like vectors,
1749 // strip them off if present.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001750 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner15842bd2010-07-29 05:02:29 +00001751 while (STy && STy->getNumElements() == 1) {
1752 IRType = STy->getElementType(0);
1753 STy = dyn_cast<llvm::StructType>(IRType);
1754 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001755
Bruno Cardoso Lopes528a8c72011-07-08 22:57:35 +00001756 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001757 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1758 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001759 unsigned BitWidth = VT->getBitWidth();
Tanya Lattnerce275672011-11-28 23:18:11 +00001760 if ((BitWidth >= 128 && BitWidth <= 256) &&
Chris Lattner0f408f52010-07-29 04:56:46 +00001761 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1762 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1763 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1764 EltTy->isIntegerTy(128)))
1765 return VT;
1766 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001767
Chris Lattner0f408f52010-07-29 04:56:46 +00001768 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1769}
1770
Chris Lattnere2962be2010-07-29 07:30:00 +00001771/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1772/// is known to either be off the end of the specified type or being in
1773/// alignment padding. The user type specified is known to be at most 128 bits
1774/// in size, and have passed through X86_64ABIInfo::classify with a successful
1775/// classification that put one of the two halves in the INTEGER class.
1776///
1777/// It is conservatively correct to return false.
1778static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1779 unsigned EndBit, ASTContext &Context) {
1780 // If the bytes being queried are off the end of the type, there is no user
1781 // data hiding here. This handles analysis of builtins, vectors and other
1782 // types that don't contain interesting padding.
1783 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1784 if (TySize <= StartBit)
1785 return true;
1786
Chris Lattner021c3a32010-07-29 07:43:55 +00001787 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1788 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1789 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1790
1791 // Check each element to see if the element overlaps with the queried range.
1792 for (unsigned i = 0; i != NumElts; ++i) {
1793 // If the element is after the span we care about, then we're done..
1794 unsigned EltOffset = i*EltSize;
1795 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001796
Chris Lattner021c3a32010-07-29 07:43:55 +00001797 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1798 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1799 EndBit-EltOffset, Context))
1800 return false;
1801 }
1802 // If it overlaps no elements, then it is safe to process as padding.
1803 return true;
1804 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001805
Chris Lattnere2962be2010-07-29 07:30:00 +00001806 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1807 const RecordDecl *RD = RT->getDecl();
1808 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001809
Chris Lattnere2962be2010-07-29 07:30:00 +00001810 // If this is a C++ record, check the bases first.
1811 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1812 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1813 e = CXXRD->bases_end(); i != e; ++i) {
1814 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1815 "Unexpected base class!");
1816 const CXXRecordDecl *Base =
1817 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001818
Chris Lattnere2962be2010-07-29 07:30:00 +00001819 // If the base is after the span we care about, ignore it.
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001820 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
Chris Lattnere2962be2010-07-29 07:30:00 +00001821 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001822
Chris Lattnere2962be2010-07-29 07:30:00 +00001823 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1824 if (!BitsContainNoUserData(i->getType(), BaseStart,
1825 EndBit-BaseOffset, Context))
1826 return false;
1827 }
1828 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001829
Chris Lattnere2962be2010-07-29 07:30:00 +00001830 // Verify that no field has data that overlaps the region of interest. Yes
1831 // this could be sped up a lot by being smarter about queried fields,
1832 // however we're only looking at structs up to 16 bytes, so we don't care
1833 // much.
1834 unsigned idx = 0;
1835 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1836 i != e; ++i, ++idx) {
1837 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001838
Chris Lattnere2962be2010-07-29 07:30:00 +00001839 // If we found a field after the region we care about, then we're done.
1840 if (FieldOffset >= EndBit) break;
1841
1842 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1843 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1844 Context))
1845 return false;
1846 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001847
Chris Lattnere2962be2010-07-29 07:30:00 +00001848 // If nothing in this record overlapped the area of interest, then we're
1849 // clean.
1850 return true;
1851 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001852
Chris Lattnere2962be2010-07-29 07:30:00 +00001853 return false;
1854}
1855
Chris Lattner0b362002010-07-29 18:39:32 +00001856/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1857/// float member at the specified offset. For example, {int,{float}} has a
1858/// float at offset 4. It is conservatively correct for this routine to return
1859/// false.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001860static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Micah Villmow25a6a842012-10-08 16:25:52 +00001861 const llvm::DataLayout &TD) {
Chris Lattner0b362002010-07-29 18:39:32 +00001862 // Base case if we find a float.
1863 if (IROffset == 0 && IRType->isFloatTy())
1864 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001865
Chris Lattner0b362002010-07-29 18:39:32 +00001866 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001867 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner0b362002010-07-29 18:39:32 +00001868 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1869 unsigned Elt = SL->getElementContainingOffset(IROffset);
1870 IROffset -= SL->getElementOffset(Elt);
1871 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1872 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001873
Chris Lattner0b362002010-07-29 18:39:32 +00001874 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001875 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1876 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner0b362002010-07-29 18:39:32 +00001877 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1878 IROffset -= IROffset/EltSize*EltSize;
1879 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1880 }
1881
1882 return false;
1883}
1884
Chris Lattnerf47c9442010-07-29 18:13:09 +00001885
1886/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1887/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001888llvm::Type *X86_64ABIInfo::
1889GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00001890 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001891 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001892 // pass as float if the last 4 bytes is just padding. This happens for
1893 // structs that contain 3 floats.
1894 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1895 SourceOffset*8+64, getContext()))
1896 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001897
Chris Lattner0b362002010-07-29 18:39:32 +00001898 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1899 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1900 // case.
Micah Villmow25a6a842012-10-08 16:25:52 +00001901 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
1902 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001903 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001904
Chris Lattnerf47c9442010-07-29 18:13:09 +00001905 return llvm::Type::getDoubleTy(getVMContext());
1906}
1907
1908
Chris Lattner0d2656d2010-07-29 17:40:35 +00001909/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1910/// an 8-byte GPR. This means that we either have a scalar or we are talking
1911/// about the high or low part of an up-to-16-byte struct. This routine picks
1912/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001913/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1914/// etc).
1915///
1916/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1917/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1918/// the 8-byte value references. PrefType may be null.
1919///
1920/// SourceTy is the source level type for the entire argument. SourceOffset is
1921/// an offset into this that we're processing (which is always either 0 or 8).
1922///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001923llvm::Type *X86_64ABIInfo::
1924GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00001925 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001926 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1927 // returning an 8-byte unit starting with it. See if we can safely use it.
1928 if (IROffset == 0) {
1929 // Pointers and int64's always fill the 8-byte unit.
Derek Schuffbabaf312012-10-11 15:52:22 +00001930 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
1931 IRType->isIntegerTy(64))
Chris Lattnere2962be2010-07-29 07:30:00 +00001932 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001933
Chris Lattnere2962be2010-07-29 07:30:00 +00001934 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1935 // goodness in the source type is just tail padding. This is allowed to
1936 // kick in for struct {double,int} on the int, but not on
1937 // struct{double,int,int} because we wouldn't return the second int. We
1938 // have to do this analysis on the source type because we can't depend on
1939 // unions being lowered a specific way etc.
1940 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
Derek Schuffbabaf312012-10-11 15:52:22 +00001941 IRType->isIntegerTy(32) ||
1942 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
1943 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
1944 cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001945
Chris Lattnere2962be2010-07-29 07:30:00 +00001946 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1947 SourceOffset*8+64, getContext()))
1948 return IRType;
1949 }
1950 }
Chris Lattner49382de2010-07-28 22:44:07 +00001951
Chris Lattner2acc6e32011-07-18 04:24:23 +00001952 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001953 // If this is a struct, recurse into the field at the specified offset.
Micah Villmow25a6a842012-10-08 16:25:52 +00001954 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001955 if (IROffset < SL->getSizeInBytes()) {
1956 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1957 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001958
Chris Lattner0d2656d2010-07-29 17:40:35 +00001959 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1960 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001961 }
Chris Lattner49382de2010-07-28 22:44:07 +00001962 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001963
Chris Lattner2acc6e32011-07-18 04:24:23 +00001964 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001965 llvm::Type *EltTy = ATy->getElementType();
Micah Villmow25a6a842012-10-08 16:25:52 +00001966 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
Chris Lattner021c3a32010-07-29 07:43:55 +00001967 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00001968 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1969 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00001970 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001971
Chris Lattner49382de2010-07-28 22:44:07 +00001972 // Okay, we don't have any better idea of what to pass, so we pass this in an
1973 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001974 unsigned TySizeInBytes =
1975 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00001976
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001977 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001978
Chris Lattner49382de2010-07-28 22:44:07 +00001979 // It is always safe to classify this as an integer type up to i64 that
1980 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001981 return llvm::IntegerType::get(getVMContext(),
1982 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00001983}
1984
Chris Lattner66e7b682010-09-01 00:50:20 +00001985
1986/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1987/// be used as elements of a two register pair to pass or return, return a
1988/// first class aggregate to represent them. For example, if the low part of
1989/// a by-value argument should be passed as i32* and the high part as float,
1990/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001991static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00001992GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Micah Villmow25a6a842012-10-08 16:25:52 +00001993 const llvm::DataLayout &TD) {
Chris Lattner66e7b682010-09-01 00:50:20 +00001994 // In order to correctly satisfy the ABI, we need to the high part to start
1995 // at offset 8. If the high and low parts we inferred are both 4-byte types
1996 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1997 // the second element at offset 8. Check for this:
1998 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1999 unsigned HiAlign = TD.getABITypeAlignment(Hi);
Micah Villmow25a6a842012-10-08 16:25:52 +00002000 unsigned HiStart = llvm::DataLayout::RoundUpAlignment(LoSize, HiAlign);
Chris Lattner66e7b682010-09-01 00:50:20 +00002001 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002002
Chris Lattner66e7b682010-09-01 00:50:20 +00002003 // To handle this, we have to increase the size of the low part so that the
2004 // second element will start at an 8 byte offset. We can't increase the size
2005 // of the second element because it might make us access off the end of the
2006 // struct.
2007 if (HiStart != 8) {
2008 // There are only two sorts of types the ABI generation code can produce for
2009 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
2010 // Promote these to a larger type.
2011 if (Lo->isFloatTy())
2012 Lo = llvm::Type::getDoubleTy(Lo->getContext());
2013 else {
2014 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
2015 Lo = llvm::Type::getInt64Ty(Lo->getContext());
2016 }
2017 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002018
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002019 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002020
2021
Chris Lattner66e7b682010-09-01 00:50:20 +00002022 // Verify that the second element is at an 8-byte offset.
2023 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
2024 "Invalid x86-64 argument pair!");
2025 return Result;
2026}
2027
Chris Lattner519f68c2010-07-28 23:06:14 +00002028ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00002029classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00002030 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
2031 // classification algorithm.
2032 X86_64ABIInfo::Class Lo, Hi;
2033 classify(RetTy, 0, Lo, Hi);
2034
2035 // Check some invariants.
2036 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002037 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2038
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002039 llvm::Type *ResType = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002040 switch (Lo) {
2041 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002042 if (Hi == NoClass)
2043 return ABIArgInfo::getIgnore();
2044 // If the low part is just padding, it takes no register, leave ResType
2045 // null.
2046 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2047 "Unknown missing lo part");
2048 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002049
2050 case SSEUp:
2051 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002052 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002053
2054 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
2055 // hidden argument.
2056 case Memory:
2057 return getIndirectReturnResult(RetTy);
2058
2059 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
2060 // available register of the sequence %rax, %rdx is used.
2061 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002062 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002063
Chris Lattnereb518b42010-07-29 21:42:50 +00002064 // If we have a sign or zero extended integer, make sure to return Extend
2065 // so that the parameter gets the right LLVM IR attributes.
2066 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2067 // Treat an enum type as its underlying type.
2068 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2069 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002070
Chris Lattnereb518b42010-07-29 21:42:50 +00002071 if (RetTy->isIntegralOrEnumerationType() &&
2072 RetTy->isPromotableIntegerType())
2073 return ABIArgInfo::getExtend();
2074 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002075 break;
2076
2077 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
2078 // available SSE register of the sequence %xmm0, %xmm1 is used.
2079 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002080 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00002081 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002082
2083 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
2084 // returned on the X87 stack in %st0 as 80-bit x87 number.
2085 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00002086 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00002087 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002088
2089 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
2090 // part of the value is returned in %st0 and the imaginary part in
2091 // %st1.
2092 case ComplexX87:
2093 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00002094 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00002095 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00002096 NULL);
2097 break;
2098 }
2099
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002100 llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002101 switch (Hi) {
2102 // Memory was handled previously and X87 should
2103 // never occur as a hi class.
2104 case Memory:
2105 case X87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002106 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002107
2108 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00002109 case NoClass:
2110 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002111
Chris Lattner3db4dde2010-09-01 00:20:33 +00002112 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002113 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002114 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2115 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002116 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00002117 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002118 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002119 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2120 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002121 break;
2122
2123 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002124 // is passed in the next available eightbyte chunk if the last used
2125 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00002126 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002127 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00002128 case SSEUp:
2129 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002130 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00002131 break;
2132
2133 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
2134 // returned together with the previous X87 value in %st0.
2135 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002136 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00002137 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002138 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00002139 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00002140 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002141 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002142 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2143 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00002144 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002145 break;
2146 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002147
Chris Lattner3db4dde2010-09-01 00:20:33 +00002148 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00002149 // known to pass in the high eightbyte of the result. We do this by forming a
2150 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00002151 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002152 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Chris Lattner519f68c2010-07-28 23:06:14 +00002153
Chris Lattnereb518b42010-07-29 21:42:50 +00002154 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00002155}
2156
Daniel Dunbaredfac032012-03-10 01:03:58 +00002157ABIArgInfo X86_64ABIInfo::classifyArgumentType(
2158 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE)
2159 const
2160{
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002161 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00002162 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002163
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002164 // Check some invariants.
2165 // FIXME: Enforce these by construction.
2166 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002167 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2168
2169 neededInt = 0;
2170 neededSSE = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002171 llvm::Type *ResType = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002172 switch (Lo) {
2173 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002174 if (Hi == NoClass)
2175 return ABIArgInfo::getIgnore();
2176 // If the low part is just padding, it takes no register, leave ResType
2177 // null.
2178 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2179 "Unknown missing lo part");
2180 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002181
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002182 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
2183 // on the stack.
2184 case Memory:
2185
2186 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
2187 // COMPLEX_X87, it is passed in memory.
2188 case X87:
2189 case ComplexX87:
Eli Friedmanded137f2011-06-29 07:04:55 +00002190 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2191 ++neededInt;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002192 return getIndirectResult(Ty, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002193
2194 case SSEUp:
2195 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002196 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002197
2198 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
2199 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
2200 // and %r9 is used.
2201 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00002202 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002203
Chris Lattner49382de2010-07-28 22:44:07 +00002204 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002205 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00002206
2207 // If we have a sign or zero extended integer, make sure to return Extend
2208 // so that the parameter gets the right LLVM IR attributes.
2209 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2210 // Treat an enum type as its underlying type.
2211 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2212 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002213
Chris Lattnereb518b42010-07-29 21:42:50 +00002214 if (Ty->isIntegralOrEnumerationType() &&
2215 Ty->isPromotableIntegerType())
2216 return ABIArgInfo::getExtend();
2217 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002218
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002219 break;
2220
2221 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
2222 // available SSE register is used, the registers are taken in the
2223 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00002224 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002225 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00002226 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00002227 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002228 break;
2229 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00002230 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002231
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002232 llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002233 switch (Hi) {
2234 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002235 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002236 // which is passed in memory.
2237 case Memory:
2238 case X87:
2239 case ComplexX87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002240 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002241
2242 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002243
Chris Lattner645406a2010-09-01 00:24:35 +00002244 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002245 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00002246 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002247 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002248
Chris Lattner645406a2010-09-01 00:24:35 +00002249 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2250 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002251 break;
2252
2253 // X87Up generally doesn't occur here (long double is passed in
2254 // memory), except in situations involving unions.
2255 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00002256 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002257 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002258
Chris Lattner645406a2010-09-01 00:24:35 +00002259 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2260 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00002261
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002262 ++neededSSE;
2263 break;
2264
2265 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
2266 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002267 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002268 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00002269 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002270 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002271 break;
2272 }
2273
Chris Lattner645406a2010-09-01 00:24:35 +00002274 // If a high part was specified, merge it together with the low part. It is
2275 // known to pass in the high eightbyte of the result. We do this by forming a
2276 // first class struct aggregate with the high and low part: {low, high}
2277 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002278 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002279
Chris Lattnereb518b42010-07-29 21:42:50 +00002280 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002281}
2282
Chris Lattneree5dcd02010-07-29 02:31:05 +00002283void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002284
Chris Lattnera3c109b2010-07-29 02:16:43 +00002285 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002286
2287 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00002288 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002289
2290 // If the return value is indirect, then the hidden argument is consuming one
2291 // integer register.
2292 if (FI.getReturnInfo().isIndirect())
2293 --freeIntRegs;
2294
2295 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2296 // get assigned (in left-to-right order) for passing as follows...
2297 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2298 it != ie; ++it) {
Bill Wendling99aaae82010-10-18 23:51:38 +00002299 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002300 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
2301 neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002302
2303 // AMD64-ABI 3.2.3p3: If there are no registers available for any
2304 // eightbyte of an argument, the whole argument is passed on the
2305 // stack. If registers have already been assigned for some
2306 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00002307 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002308 freeIntRegs -= neededInt;
2309 freeSSERegs -= neededSSE;
2310 } else {
Daniel Dunbaredfac032012-03-10 01:03:58 +00002311 it->info = getIndirectResult(it->type, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002312 }
2313 }
2314}
2315
2316static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2317 QualType Ty,
2318 CodeGenFunction &CGF) {
2319 llvm::Value *overflow_arg_area_p =
2320 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2321 llvm::Value *overflow_arg_area =
2322 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2323
2324 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2325 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedman8d2fe422011-11-18 02:44:19 +00002326 // It isn't stated explicitly in the standard, but in practice we use
2327 // alignment greater than 16 where necessary.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002328 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2329 if (Align > 8) {
Eli Friedman8d2fe422011-11-18 02:44:19 +00002330 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
Owen Anderson0032b272009-08-13 21:57:51 +00002331 llvm::Value *Offset =
Eli Friedman8d2fe422011-11-18 02:44:19 +00002332 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002333 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2334 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00002335 CGF.Int64Ty);
Eli Friedman8d2fe422011-11-18 02:44:19 +00002336 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002337 overflow_arg_area =
2338 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2339 overflow_arg_area->getType(),
2340 "overflow_arg_area.align");
2341 }
2342
2343 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002344 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002345 llvm::Value *Res =
2346 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002347 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002348
2349 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2350 // l->overflow_arg_area + sizeof(type).
2351 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2352 // an 8 byte boundary.
2353
2354 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00002355 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00002356 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002357 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2358 "overflow_arg_area.next");
2359 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2360
2361 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2362 return Res;
2363}
2364
2365llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2366 CodeGenFunction &CGF) const {
2367 // Assume that va_list type is correct; should be pointer to LLVM type:
2368 // struct {
2369 // i32 gp_offset;
2370 // i32 fp_offset;
2371 // i8* overflow_arg_area;
2372 // i8* reg_save_area;
2373 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00002374 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002375
Chris Lattnera14db752010-03-11 18:19:55 +00002376 Ty = CGF.getContext().getCanonicalType(Ty);
Daniel Dunbaredfac032012-03-10 01:03:58 +00002377 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002378
2379 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2380 // in the registers. If not go to step 7.
2381 if (!neededInt && !neededSSE)
2382 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2383
2384 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2385 // general purpose registers needed to pass type and num_fp to hold
2386 // the number of floating point registers needed.
2387
2388 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2389 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2390 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2391 //
2392 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2393 // register save space).
2394
2395 llvm::Value *InRegs = 0;
2396 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2397 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2398 if (neededInt) {
2399 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2400 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002401 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2402 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002403 }
2404
2405 if (neededSSE) {
2406 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2407 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2408 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00002409 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2410 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002411 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2412 }
2413
2414 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2415 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2416 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2417 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2418
2419 // Emit code to load the value if it was passed in registers.
2420
2421 CGF.EmitBlock(InRegBlock);
2422
2423 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2424 // an offset of l->gp_offset and/or l->fp_offset. This may require
2425 // copying to a temporary location in case the parameter is passed
2426 // in different register classes or requires an alignment greater
2427 // than 8 for general purpose registers and 16 for XMM registers.
2428 //
2429 // FIXME: This really results in shameful code when we end up needing to
2430 // collect arguments from different places; often what should result in a
2431 // simple assembling of a structure from scattered addresses has many more
2432 // loads than necessary. Can we clean this up?
Chris Lattner2acc6e32011-07-18 04:24:23 +00002433 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002434 llvm::Value *RegAddr =
2435 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2436 "reg_save_area");
2437 if (neededInt && neededSSE) {
2438 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002439 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002440 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002441 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2442 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002443 llvm::Type *TyLo = ST->getElementType(0);
2444 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002445 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002446 "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002447 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2448 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002449 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2450 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002451 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2452 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002453 llvm::Value *V =
2454 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2455 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2456 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2457 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2458
Owen Andersona1cf15f2009-07-14 23:10:40 +00002459 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002460 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002461 } else if (neededInt) {
2462 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2463 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002464 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002465 } else if (neededSSE == 1) {
2466 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2467 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2468 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002469 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002470 assert(neededSSE == 2 && "Invalid number of needed registers!");
2471 // SSE registers are spaced 16 bytes apart in the register save
2472 // area, we need to collect the two eightbytes together.
2473 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002474 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner8b418682012-02-07 00:39:47 +00002475 llvm::Type *DoubleTy = CGF.DoubleTy;
Chris Lattner2acc6e32011-07-18 04:24:23 +00002476 llvm::Type *DblPtrTy =
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002477 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002478 llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002479 DoubleTy, NULL);
2480 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2481 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2482 DblPtrTy));
2483 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2484 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2485 DblPtrTy));
2486 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2487 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2488 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002489 }
2490
2491 // AMD64-ABI 3.5.7p5: Step 5. Set:
2492 // l->gp_offset = l->gp_offset + num_gp * 8
2493 // l->fp_offset = l->fp_offset + num_fp * 16.
2494 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002495 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002496 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2497 gp_offset_p);
2498 }
2499 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002500 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002501 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2502 fp_offset_p);
2503 }
2504 CGF.EmitBranch(ContBlock);
2505
2506 // Emit code to load the value if it was passed in memory.
2507
2508 CGF.EmitBlock(InMemBlock);
2509 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2510
2511 // Return the appropriate result.
2512
2513 CGF.EmitBlock(ContBlock);
Jay Foadbbf3bac2011-03-30 11:28:58 +00002514 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002515 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002516 ResAddr->addIncoming(RegAddr, InRegBlock);
2517 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002518 return ResAddr;
2519}
2520
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002521ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2522
2523 if (Ty->isVoidType())
2524 return ABIArgInfo::getIgnore();
2525
2526 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2527 Ty = EnumTy->getDecl()->getIntegerType();
2528
2529 uint64_t Size = getContext().getTypeSize(Ty);
2530
2531 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002532 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2533 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002534 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2535
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002536 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2537 if (Size == 128 &&
Eli Friedman55fc7e22012-01-25 22:46:34 +00002538 getContext().getTargetInfo().getTriple().getOS()
2539 == llvm::Triple::MinGW32)
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002540 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2541 Size));
2542
2543 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2544 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2545 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002546 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002547 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2548 Size));
2549
2550 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2551 }
2552
2553 if (Ty->isPromotableIntegerType())
2554 return ABIArgInfo::getExtend();
2555
2556 return ABIArgInfo::getDirect();
2557}
2558
2559void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2560
2561 QualType RetTy = FI.getReturnType();
2562 FI.getReturnInfo() = classify(RetTy);
2563
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002564 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2565 it != ie; ++it)
2566 it->info = classify(it->type);
2567}
2568
Chris Lattnerf13721d2010-08-31 16:44:54 +00002569llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2570 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002571 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002572
Chris Lattnerf13721d2010-08-31 16:44:54 +00002573 CGBuilderTy &Builder = CGF.Builder;
2574 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2575 "ap");
2576 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2577 llvm::Type *PTy =
2578 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2579 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2580
2581 uint64_t Offset =
2582 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2583 llvm::Value *NextAddr =
2584 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2585 "ap.next");
2586 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2587
2588 return AddrTyped;
2589}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002590
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00002591namespace {
2592
Derek Schuff263366f2012-10-16 22:30:41 +00002593class NaClX86_64ABIInfo : public ABIInfo {
2594 public:
2595 NaClX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2596 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, HasAVX) {}
2597 virtual void computeInfo(CGFunctionInfo &FI) const;
2598 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2599 CodeGenFunction &CGF) const;
2600 private:
2601 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
2602 X86_64ABIInfo NInfo; // Used for everything else.
2603};
2604
2605class NaClX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2606 public:
2607 NaClX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2608 : TargetCodeGenInfo(new NaClX86_64ABIInfo(CGT, HasAVX)) {}
2609};
2610
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00002611}
2612
Derek Schuff263366f2012-10-16 22:30:41 +00002613void NaClX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2614 if (FI.getASTCallingConvention() == CC_PnaclCall)
2615 PInfo.computeInfo(FI);
2616 else
2617 NInfo.computeInfo(FI);
2618}
2619
2620llvm::Value *NaClX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2621 CodeGenFunction &CGF) const {
2622 // Always use the native convention; calling pnacl-style varargs functions
2623 // is unuspported.
2624 return NInfo.EmitVAArg(VAListAddr, Ty, CGF);
2625}
2626
2627
John McCallec853ba2010-03-11 00:10:12 +00002628// PowerPC-32
2629
2630namespace {
2631class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2632public:
Chris Lattnerea044322010-07-29 02:01:43 +00002633 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002634
John McCallec853ba2010-03-11 00:10:12 +00002635 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2636 // This is recovered from gcc output.
2637 return 1; // r1 is the dedicated stack pointer
2638 }
2639
2640 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002641 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002642};
2643
2644}
2645
2646bool
2647PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2648 llvm::Value *Address) const {
2649 // This is calculated from the LLVM and GCC tables and verified
2650 // against gcc output. AFAIK all ABIs use the same encoding.
2651
2652 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallec853ba2010-03-11 00:10:12 +00002653
Chris Lattner8b418682012-02-07 00:39:47 +00002654 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallec853ba2010-03-11 00:10:12 +00002655 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2656 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2657 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2658
2659 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002660 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002661
2662 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002663 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002664
2665 // 64-76 are various 4-byte special-purpose registers:
2666 // 64: mq
2667 // 65: lr
2668 // 66: ctr
2669 // 67: ap
2670 // 68-75 cr0-7
2671 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002672 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002673
2674 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002675 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002676
2677 // 109: vrsave
2678 // 110: vscr
2679 // 111: spe_acc
2680 // 112: spefscr
2681 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002682 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002683
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002684 return false;
John McCallec853ba2010-03-11 00:10:12 +00002685}
2686
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002687// PowerPC-64
2688
2689namespace {
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002690/// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
2691class PPC64_SVR4_ABIInfo : public DefaultABIInfo {
2692
2693public:
2694 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
2695
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002696 bool isPromotableTypeForABI(QualType Ty) const;
2697
2698 ABIArgInfo classifyReturnType(QualType RetTy) const;
2699 ABIArgInfo classifyArgumentType(QualType Ty) const;
2700
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00002701 // TODO: We can add more logic to computeInfo to improve performance.
2702 // Example: For aggregate arguments that fit in a register, we could
2703 // use getDirectInReg (as is done below for structs containing a single
2704 // floating-point value) to avoid pushing them to memory on function
2705 // entry. This would require changing the logic in PPCISelLowering
2706 // when lowering the parameters in the caller and args in the callee.
2707 virtual void computeInfo(CGFunctionInfo &FI) const {
2708 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2709 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2710 it != ie; ++it) {
2711 // We rely on the default argument classification for the most part.
2712 // One exception: An aggregate containing a single floating-point
2713 // item must be passed in a register if one is available.
2714 const Type *T = isSingleElementStruct(it->type, getContext());
2715 if (T) {
2716 const BuiltinType *BT = T->getAs<BuiltinType>();
2717 if (BT && BT->isFloatingPoint()) {
2718 QualType QT(T, 0);
2719 it->info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
2720 continue;
2721 }
2722 }
2723 it->info = classifyArgumentType(it->type);
2724 }
2725 }
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002726
2727 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr,
2728 QualType Ty,
2729 CodeGenFunction &CGF) const;
2730};
2731
2732class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
2733public:
2734 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT)
2735 : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT)) {}
2736
2737 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2738 // This is recovered from gcc output.
2739 return 1; // r1 is the dedicated stack pointer
2740 }
2741
2742 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2743 llvm::Value *Address) const;
2744};
2745
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002746class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2747public:
2748 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
2749
2750 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2751 // This is recovered from gcc output.
2752 return 1; // r1 is the dedicated stack pointer
2753 }
2754
2755 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2756 llvm::Value *Address) const;
2757};
2758
2759}
2760
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002761// Return true if the ABI requires Ty to be passed sign- or zero-
2762// extended to 64 bits.
2763bool
2764PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
2765 // Treat an enum type as its underlying type.
2766 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2767 Ty = EnumTy->getDecl()->getIntegerType();
2768
2769 // Promotable integer types are required to be promoted by the ABI.
2770 if (Ty->isPromotableIntegerType())
2771 return true;
2772
2773 // In addition to the usual promotable integer types, we also need to
2774 // extend all 32-bit types, since the ABI requires promotion to 64 bits.
2775 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2776 switch (BT->getKind()) {
2777 case BuiltinType::Int:
2778 case BuiltinType::UInt:
2779 return true;
2780 default:
2781 break;
2782 }
2783
2784 return false;
2785}
2786
2787ABIArgInfo
2788PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
Bill Schmidtc9715fc2012-11-27 02:46:43 +00002789 if (Ty->isAnyComplexType())
2790 return ABIArgInfo::getDirect();
2791
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002792 if (isAggregateTypeForABI(Ty)) {
2793 // Records with non trivial destructors/constructors should not be passed
2794 // by value.
2795 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2796 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2797
2798 return ABIArgInfo::getIndirect(0);
2799 }
2800
2801 return (isPromotableTypeForABI(Ty) ?
2802 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2803}
2804
2805ABIArgInfo
2806PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
2807 if (RetTy->isVoidType())
2808 return ABIArgInfo::getIgnore();
2809
Bill Schmidt9e6111a2012-12-17 04:20:17 +00002810 if (RetTy->isAnyComplexType())
2811 return ABIArgInfo::getDirect();
2812
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002813 if (isAggregateTypeForABI(RetTy))
2814 return ABIArgInfo::getIndirect(0);
2815
2816 return (isPromotableTypeForABI(RetTy) ?
2817 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2818}
2819
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002820// Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
2821llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr,
2822 QualType Ty,
2823 CodeGenFunction &CGF) const {
2824 llvm::Type *BP = CGF.Int8PtrTy;
2825 llvm::Type *BPP = CGF.Int8PtrPtrTy;
2826
2827 CGBuilderTy &Builder = CGF.Builder;
2828 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
2829 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2830
Bill Schmidt19f8e852013-01-14 17:45:36 +00002831 // Update the va_list pointer. The pointer should be bumped by the
2832 // size of the object. We can trust getTypeSize() except for a complex
2833 // type whose base type is smaller than a doubleword. For these, the
2834 // size of the object is 16 bytes; see below for further explanation.
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002835 unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8;
Bill Schmidt19f8e852013-01-14 17:45:36 +00002836 QualType BaseTy;
2837 unsigned CplxBaseSize = 0;
2838
2839 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
2840 BaseTy = CTy->getElementType();
2841 CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8;
2842 if (CplxBaseSize < 8)
2843 SizeInBytes = 16;
2844 }
2845
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002846 unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8);
2847 llvm::Value *NextAddr =
2848 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset),
2849 "ap.next");
2850 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2851
Bill Schmidt19f8e852013-01-14 17:45:36 +00002852 // If we have a complex type and the base type is smaller than 8 bytes,
2853 // the ABI calls for the real and imaginary parts to be right-adjusted
2854 // in separate doublewords. However, Clang expects us to produce a
2855 // pointer to a structure with the two parts packed tightly. So generate
2856 // loads of the real and imaginary parts relative to the va_list pointer,
2857 // and store them to a temporary structure.
2858 if (CplxBaseSize && CplxBaseSize < 8) {
2859 llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2860 llvm::Value *ImagAddr = RealAddr;
2861 RealAddr = Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize));
2862 ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize));
2863 llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy));
2864 RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy);
2865 ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy);
2866 llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal");
2867 llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag");
2868 llvm::Value *Ptr = CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty),
2869 "vacplx");
2870 llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, ".real");
2871 llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, ".imag");
2872 Builder.CreateStore(Real, RealPtr, false);
2873 Builder.CreateStore(Imag, ImagPtr, false);
2874 return Ptr;
2875 }
2876
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002877 // If the argument is smaller than 8 bytes, it is right-adjusted in
2878 // its doubleword slot. Adjust the pointer to pick it up from the
2879 // correct offset.
2880 if (SizeInBytes < 8) {
2881 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2882 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes));
2883 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2884 }
2885
2886 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2887 return Builder.CreateBitCast(Addr, PTy);
2888}
2889
2890static bool
2891PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2892 llvm::Value *Address) {
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002893 // This is calculated from the LLVM and GCC tables and verified
2894 // against gcc output. AFAIK all ABIs use the same encoding.
2895
2896 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2897
2898 llvm::IntegerType *i8 = CGF.Int8Ty;
2899 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2900 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2901 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2902
2903 // 0-31: r0-31, the 8-byte general-purpose registers
2904 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
2905
2906 // 32-63: fp0-31, the 8-byte floating-point registers
2907 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
2908
2909 // 64-76 are various 4-byte special-purpose registers:
2910 // 64: mq
2911 // 65: lr
2912 // 66: ctr
2913 // 67: ap
2914 // 68-75 cr0-7
2915 // 76: xer
2916 AssignToArrayRange(Builder, Address, Four8, 64, 76);
2917
2918 // 77-108: v0-31, the 16-byte vector registers
2919 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
2920
2921 // 109: vrsave
2922 // 110: vscr
2923 // 111: spe_acc
2924 // 112: spefscr
2925 // 113: sfp
2926 AssignToArrayRange(Builder, Address, Four8, 109, 113);
2927
2928 return false;
2929}
John McCallec853ba2010-03-11 00:10:12 +00002930
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002931bool
2932PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
2933 CodeGen::CodeGenFunction &CGF,
2934 llvm::Value *Address) const {
2935
2936 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
2937}
2938
2939bool
2940PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2941 llvm::Value *Address) const {
2942
2943 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
2944}
2945
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002946//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002947// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002948//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002949
2950namespace {
2951
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002952class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002953public:
2954 enum ABIKind {
2955 APCS = 0,
2956 AAPCS = 1,
2957 AAPCS_VFP
2958 };
2959
2960private:
2961 ABIKind Kind;
2962
2963public:
John McCallbd7370a2013-02-28 19:01:20 +00002964 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {
2965 setRuntimeCC();
2966 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002967
John McCall49e34be2011-08-30 01:42:09 +00002968 bool isEABI() const {
Eli Friedman55fc7e22012-01-25 22:46:34 +00002969 StringRef Env =
2970 getContext().getTargetInfo().getTriple().getEnvironmentName();
Logan Chien94a71422012-09-02 09:30:11 +00002971 return (Env == "gnueabi" || Env == "eabi" ||
2972 Env == "android" || Env == "androideabi");
John McCall49e34be2011-08-30 01:42:09 +00002973 }
2974
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002975private:
2976 ABIKind getABIKind() const { return Kind; }
2977
Chris Lattnera3c109b2010-07-29 02:16:43 +00002978 ABIArgInfo classifyReturnType(QualType RetTy) const;
Manman Ren710c5172012-10-31 19:02:26 +00002979 ABIArgInfo classifyArgumentType(QualType RetTy, int *VFPRegs,
2980 unsigned &AllocatedVFP,
Manman Renb3fa55f2012-10-30 23:21:41 +00002981 bool &IsHA) const;
Manman Ren97f81572012-10-16 19:18:39 +00002982 bool isIllegalVectorType(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002983
Chris Lattneree5dcd02010-07-29 02:31:05 +00002984 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002985
2986 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2987 CodeGenFunction &CGF) const;
John McCallbd7370a2013-02-28 19:01:20 +00002988
2989 llvm::CallingConv::ID getLLVMDefaultCC() const;
2990 llvm::CallingConv::ID getABIDefaultCC() const;
2991 void setRuntimeCC();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002992};
2993
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002994class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2995public:
Chris Lattnerea044322010-07-29 02:01:43 +00002996 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2997 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00002998
John McCall49e34be2011-08-30 01:42:09 +00002999 const ARMABIInfo &getABIInfo() const {
3000 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
3001 }
3002
John McCall6374c332010-03-06 00:35:14 +00003003 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3004 return 13;
3005 }
Roman Divacky09345d12011-05-18 19:36:54 +00003006
Chris Lattner5f9e2722011-07-23 10:55:15 +00003007 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCallf85e1932011-06-15 23:02:42 +00003008 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
3009 }
3010
Roman Divacky09345d12011-05-18 19:36:54 +00003011 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3012 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003013 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divacky09345d12011-05-18 19:36:54 +00003014
3015 // 0-15 are the 16 integer registers.
Chris Lattner8b418682012-02-07 00:39:47 +00003016 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divacky09345d12011-05-18 19:36:54 +00003017 return false;
3018 }
John McCall49e34be2011-08-30 01:42:09 +00003019
3020 unsigned getSizeOfUnwindException() const {
3021 if (getABIInfo().isEABI()) return 88;
3022 return TargetCodeGenInfo::getSizeOfUnwindException();
3023 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003024};
3025
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003026}
3027
Chris Lattneree5dcd02010-07-29 02:31:05 +00003028void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Manman Renb3fa55f2012-10-30 23:21:41 +00003029 // To correctly handle Homogeneous Aggregate, we need to keep track of the
Manman Ren710c5172012-10-31 19:02:26 +00003030 // VFP registers allocated so far.
Manman Renb3fa55f2012-10-30 23:21:41 +00003031 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
3032 // VFP registers of the appropriate type unallocated then the argument is
3033 // allocated to the lowest-numbered sequence of such registers.
3034 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
3035 // unallocated are marked as unavailable.
3036 unsigned AllocatedVFP = 0;
Manman Ren710c5172012-10-31 19:02:26 +00003037 int VFPRegs[16] = { 0 };
Chris Lattnera3c109b2010-07-29 02:16:43 +00003038 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003039 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Manman Renb3fa55f2012-10-30 23:21:41 +00003040 it != ie; ++it) {
3041 unsigned PreAllocation = AllocatedVFP;
3042 bool IsHA = false;
3043 // 6.1.2.3 There is one VFP co-processor register class using registers
3044 // s0-s15 (d0-d7) for passing arguments.
3045 const unsigned NumVFPs = 16;
Manman Ren710c5172012-10-31 19:02:26 +00003046 it->info = classifyArgumentType(it->type, VFPRegs, AllocatedVFP, IsHA);
Manman Renb3fa55f2012-10-30 23:21:41 +00003047 // If we do not have enough VFP registers for the HA, any VFP registers
3048 // that are unallocated are marked as unavailable. To achieve this, we add
3049 // padding of (NumVFPs - PreAllocation) floats.
3050 if (IsHA && AllocatedVFP > NumVFPs && PreAllocation < NumVFPs) {
3051 llvm::Type *PaddingTy = llvm::ArrayType::get(
3052 llvm::Type::getFloatTy(getVMContext()), NumVFPs - PreAllocation);
3053 it->info = ABIArgInfo::getExpandWithPadding(false, PaddingTy);
3054 }
3055 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003056
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003057 // Always honor user-specified calling convention.
3058 if (FI.getCallingConvention() != llvm::CallingConv::C)
3059 return;
3060
John McCallbd7370a2013-02-28 19:01:20 +00003061 llvm::CallingConv::ID cc = getRuntimeCC();
3062 if (cc != llvm::CallingConv::C)
3063 FI.setEffectiveCallingConvention(cc);
3064}
Rafael Espindola25117ab2010-06-16 16:13:39 +00003065
John McCallbd7370a2013-02-28 19:01:20 +00003066/// Return the default calling convention that LLVM will use.
3067llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
3068 // The default calling convention that LLVM will infer.
3069 if (getContext().getTargetInfo().getTriple().getEnvironmentName()=="gnueabihf")
3070 return llvm::CallingConv::ARM_AAPCS_VFP;
3071 else if (isEABI())
3072 return llvm::CallingConv::ARM_AAPCS;
3073 else
3074 return llvm::CallingConv::ARM_APCS;
3075}
3076
3077/// Return the calling convention that our ABI would like us to use
3078/// as the C calling convention.
3079llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003080 switch (getABIKind()) {
John McCallbd7370a2013-02-28 19:01:20 +00003081 case APCS: return llvm::CallingConv::ARM_APCS;
3082 case AAPCS: return llvm::CallingConv::ARM_AAPCS;
3083 case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003084 }
John McCallbd7370a2013-02-28 19:01:20 +00003085 llvm_unreachable("bad ABI kind");
3086}
3087
3088void ARMABIInfo::setRuntimeCC() {
3089 assert(getRuntimeCC() == llvm::CallingConv::C);
3090
3091 // Don't muddy up the IR with a ton of explicit annotations if
3092 // they'd just match what LLVM will infer from the triple.
3093 llvm::CallingConv::ID abiCC = getABIDefaultCC();
3094 if (abiCC != getLLVMDefaultCC())
3095 RuntimeCC = abiCC;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003096}
3097
Bob Wilson194f06a2011-08-03 05:58:22 +00003098/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
3099/// aggregate. If HAMembers is non-null, the number of base elements
3100/// contained in the type is returned through it; this is used for the
3101/// recursive calls that check aggregate component types.
3102static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
3103 ASTContext &Context,
3104 uint64_t *HAMembers = 0) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003105 uint64_t Members = 0;
Bob Wilson194f06a2011-08-03 05:58:22 +00003106 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
3107 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
3108 return false;
3109 Members *= AT->getSize().getZExtValue();
3110 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
3111 const RecordDecl *RD = RT->getDecl();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003112 if (RD->hasFlexibleArrayMember())
Bob Wilson194f06a2011-08-03 05:58:22 +00003113 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003114
Bob Wilson194f06a2011-08-03 05:58:22 +00003115 Members = 0;
3116 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3117 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00003118 const FieldDecl *FD = *i;
Bob Wilson194f06a2011-08-03 05:58:22 +00003119 uint64_t FldMembers;
3120 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
3121 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003122
3123 Members = (RD->isUnion() ?
3124 std::max(Members, FldMembers) : Members + FldMembers);
Bob Wilson194f06a2011-08-03 05:58:22 +00003125 }
3126 } else {
3127 Members = 1;
3128 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
3129 Members = 2;
3130 Ty = CT->getElementType();
3131 }
3132
3133 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
3134 // double, or 64-bit or 128-bit vectors.
3135 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3136 if (BT->getKind() != BuiltinType::Float &&
Tim Northoveradfa45f2012-07-20 22:29:29 +00003137 BT->getKind() != BuiltinType::Double &&
3138 BT->getKind() != BuiltinType::LongDouble)
Bob Wilson194f06a2011-08-03 05:58:22 +00003139 return false;
3140 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
3141 unsigned VecSize = Context.getTypeSize(VT);
3142 if (VecSize != 64 && VecSize != 128)
3143 return false;
3144 } else {
3145 return false;
3146 }
3147
3148 // The base type must be the same for all members. Vector types of the
3149 // same total size are treated as being equivalent here.
3150 const Type *TyPtr = Ty.getTypePtr();
3151 if (!Base)
3152 Base = TyPtr;
3153 if (Base != TyPtr &&
3154 (!Base->isVectorType() || !TyPtr->isVectorType() ||
3155 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
3156 return false;
3157 }
3158
3159 // Homogeneous Aggregates can have at most 4 members of the base type.
3160 if (HAMembers)
3161 *HAMembers = Members;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003162
3163 return (Members > 0 && Members <= 4);
Bob Wilson194f06a2011-08-03 05:58:22 +00003164}
3165
Manman Ren710c5172012-10-31 19:02:26 +00003166/// markAllocatedVFPs - update VFPRegs according to the alignment and
3167/// number of VFP registers (unit is S register) requested.
3168static void markAllocatedVFPs(int *VFPRegs, unsigned &AllocatedVFP,
3169 unsigned Alignment,
3170 unsigned NumRequired) {
3171 // Early Exit.
3172 if (AllocatedVFP >= 16)
3173 return;
3174 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
3175 // VFP registers of the appropriate type unallocated then the argument is
3176 // allocated to the lowest-numbered sequence of such registers.
3177 for (unsigned I = 0; I < 16; I += Alignment) {
3178 bool FoundSlot = true;
3179 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3180 if (J >= 16 || VFPRegs[J]) {
3181 FoundSlot = false;
3182 break;
3183 }
3184 if (FoundSlot) {
3185 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3186 VFPRegs[J] = 1;
3187 AllocatedVFP += NumRequired;
3188 return;
3189 }
3190 }
3191 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
3192 // unallocated are marked as unavailable.
3193 for (unsigned I = 0; I < 16; I++)
3194 VFPRegs[I] = 1;
3195 AllocatedVFP = 17; // We do not have enough VFP registers.
3196}
3197
3198ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, int *VFPRegs,
3199 unsigned &AllocatedVFP,
Manman Renb3fa55f2012-10-30 23:21:41 +00003200 bool &IsHA) const {
3201 // We update number of allocated VFPs according to
3202 // 6.1.2.1 The following argument types are VFP CPRCs:
3203 // A single-precision floating-point type (including promoted
3204 // half-precision types); A double-precision floating-point type;
3205 // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
3206 // with a Base Type of a single- or double-precision floating-point type,
3207 // 64-bit containerized vectors or 128-bit containerized vectors with one
3208 // to four Elements.
3209
Manman Ren97f81572012-10-16 19:18:39 +00003210 // Handle illegal vector types here.
3211 if (isIllegalVectorType(Ty)) {
3212 uint64_t Size = getContext().getTypeSize(Ty);
3213 if (Size <= 32) {
3214 llvm::Type *ResType =
3215 llvm::Type::getInt32Ty(getVMContext());
3216 return ABIArgInfo::getDirect(ResType);
3217 }
3218 if (Size == 64) {
3219 llvm::Type *ResType = llvm::VectorType::get(
3220 llvm::Type::getInt32Ty(getVMContext()), 2);
Manman Ren710c5172012-10-31 19:02:26 +00003221 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
Manman Ren97f81572012-10-16 19:18:39 +00003222 return ABIArgInfo::getDirect(ResType);
3223 }
3224 if (Size == 128) {
3225 llvm::Type *ResType = llvm::VectorType::get(
3226 llvm::Type::getInt32Ty(getVMContext()), 4);
Manman Ren710c5172012-10-31 19:02:26 +00003227 markAllocatedVFPs(VFPRegs, AllocatedVFP, 4, 4);
Manman Ren97f81572012-10-16 19:18:39 +00003228 return ABIArgInfo::getDirect(ResType);
3229 }
3230 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3231 }
Manman Ren710c5172012-10-31 19:02:26 +00003232 // Update VFPRegs for legal vector types.
Manman Renb3fa55f2012-10-30 23:21:41 +00003233 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3234 uint64_t Size = getContext().getTypeSize(VT);
3235 // Size of a legal vector should be power of 2 and above 64.
Manman Ren710c5172012-10-31 19:02:26 +00003236 markAllocatedVFPs(VFPRegs, AllocatedVFP, Size >= 128 ? 4 : 2, Size / 32);
Manman Renb3fa55f2012-10-30 23:21:41 +00003237 }
Manman Ren710c5172012-10-31 19:02:26 +00003238 // Update VFPRegs for floating point types.
Manman Renb3fa55f2012-10-30 23:21:41 +00003239 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3240 if (BT->getKind() == BuiltinType::Half ||
3241 BT->getKind() == BuiltinType::Float)
Manman Ren710c5172012-10-31 19:02:26 +00003242 markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, 1);
Manman Renb3fa55f2012-10-30 23:21:41 +00003243 if (BT->getKind() == BuiltinType::Double ||
Manman Ren710c5172012-10-31 19:02:26 +00003244 BT->getKind() == BuiltinType::LongDouble)
3245 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
Manman Renb3fa55f2012-10-30 23:21:41 +00003246 }
Manman Ren97f81572012-10-16 19:18:39 +00003247
John McCalld608cdb2010-08-22 10:59:02 +00003248 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003249 // Treat an enum type as its underlying type.
3250 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3251 Ty = EnumTy->getDecl()->getIntegerType();
3252
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003253 return (Ty->isPromotableIntegerType() ?
3254 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003255 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003256
Daniel Dunbar42025572009-09-14 21:54:03 +00003257 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003258 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00003259 return ABIArgInfo::getIgnore();
3260
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003261 // Structures with either a non-trivial destructor or a non-trivial
3262 // copy constructor are always indirect.
3263 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
3264 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3265
Bob Wilson194f06a2011-08-03 05:58:22 +00003266 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
Manman Renb3fa55f2012-10-30 23:21:41 +00003267 // Homogeneous Aggregates need to be expanded when we can fit the aggregate
3268 // into VFP registers.
Bob Wilson194f06a2011-08-03 05:58:22 +00003269 const Type *Base = 0;
Manman Renb3fa55f2012-10-30 23:21:41 +00003270 uint64_t Members = 0;
3271 if (isHomogeneousAggregate(Ty, Base, getContext(), &Members)) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003272 assert(Base && "Base class should be set for homogeneous aggregate");
Manman Renb3fa55f2012-10-30 23:21:41 +00003273 // Base can be a floating-point or a vector.
3274 if (Base->isVectorType()) {
3275 // ElementSize is in number of floats.
3276 unsigned ElementSize = getContext().getTypeSize(Base) == 64 ? 2 : 4;
Manman Rencb489dd2012-11-06 19:05:29 +00003277 markAllocatedVFPs(VFPRegs, AllocatedVFP, ElementSize,
3278 Members * ElementSize);
Manman Renb3fa55f2012-10-30 23:21:41 +00003279 } else if (Base->isSpecificBuiltinType(BuiltinType::Float))
Manman Ren710c5172012-10-31 19:02:26 +00003280 markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, Members);
Manman Renb3fa55f2012-10-30 23:21:41 +00003281 else {
3282 assert(Base->isSpecificBuiltinType(BuiltinType::Double) ||
3283 Base->isSpecificBuiltinType(BuiltinType::LongDouble));
Manman Ren710c5172012-10-31 19:02:26 +00003284 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, Members * 2);
Manman Renb3fa55f2012-10-30 23:21:41 +00003285 }
3286 IsHA = true;
Bob Wilson194f06a2011-08-03 05:58:22 +00003287 return ABIArgInfo::getExpand();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003288 }
Bob Wilson194f06a2011-08-03 05:58:22 +00003289 }
3290
Manman Ren634b3d22012-08-13 21:23:55 +00003291 // Support byval for ARM.
Manman Rencb489dd2012-11-06 19:05:29 +00003292 // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
3293 // most 8-byte. We realign the indirect argument if type alignment is bigger
3294 // than ABI alignment.
Manman Renfd1ba912012-11-05 22:42:46 +00003295 uint64_t ABIAlign = 4;
3296 uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8;
3297 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3298 getABIKind() == ARMABIInfo::AAPCS)
3299 ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
Manman Ren885ad692012-11-06 04:58:01 +00003300 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
3301 return ABIArgInfo::getIndirect(0, /*ByVal=*/true,
Manman Rencb489dd2012-11-06 19:05:29 +00003302 /*Realign=*/TyAlign > ABIAlign);
Eli Friedman79f30982012-08-09 00:31:40 +00003303 }
3304
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00003305 // Otherwise, pass by coercing to a structure of the appropriate size.
Chris Lattner2acc6e32011-07-18 04:24:23 +00003306 llvm::Type* ElemTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003307 unsigned SizeRegs;
Eli Friedman79f30982012-08-09 00:31:40 +00003308 // FIXME: Try to match the types of the arguments more accurately where
3309 // we can.
3310 if (getContext().getTypeAlign(Ty) <= 32) {
Bob Wilson53fc1a62011-08-01 23:39:04 +00003311 ElemTy = llvm::Type::getInt32Ty(getVMContext());
3312 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Manman Ren78eb76e2012-06-25 22:04:00 +00003313 } else {
Manman Ren78eb76e2012-06-25 22:04:00 +00003314 ElemTy = llvm::Type::getInt64Ty(getVMContext());
3315 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastings67d097e2011-04-27 17:24:02 +00003316 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003317
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003318 llvm::Type *STy =
Chris Lattner7650d952011-06-18 22:49:11 +00003319 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003320 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003321}
3322
Chris Lattnera3c109b2010-07-29 02:16:43 +00003323static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00003324 llvm::LLVMContext &VMContext) {
3325 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
3326 // is called integer-like if its size is less than or equal to one word, and
3327 // the offset of each of its addressable sub-fields is zero.
3328
3329 uint64_t Size = Context.getTypeSize(Ty);
3330
3331 // Check that the type fits in a word.
3332 if (Size > 32)
3333 return false;
3334
3335 // FIXME: Handle vector types!
3336 if (Ty->isVectorType())
3337 return false;
3338
Daniel Dunbarb0d58192009-09-14 02:20:34 +00003339 // Float types are never treated as "integer like".
3340 if (Ty->isRealFloatingType())
3341 return false;
3342
Daniel Dunbar98303b92009-09-13 08:03:58 +00003343 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00003344 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00003345 return true;
3346
Daniel Dunbar45815812010-02-01 23:31:26 +00003347 // Small complex integer types are "integer like".
3348 if (const ComplexType *CT = Ty->getAs<ComplexType>())
3349 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003350
3351 // Single element and zero sized arrays should be allowed, by the definition
3352 // above, but they are not.
3353
3354 // Otherwise, it must be a record type.
3355 const RecordType *RT = Ty->getAs<RecordType>();
3356 if (!RT) return false;
3357
3358 // Ignore records with flexible arrays.
3359 const RecordDecl *RD = RT->getDecl();
3360 if (RD->hasFlexibleArrayMember())
3361 return false;
3362
3363 // Check that all sub-fields are at offset 0, and are themselves "integer
3364 // like".
3365 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3366
3367 bool HadField = false;
3368 unsigned idx = 0;
3369 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3370 i != e; ++i, ++idx) {
David Blaikie581deb32012-06-06 20:45:41 +00003371 const FieldDecl *FD = *i;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003372
Daniel Dunbar679855a2010-01-29 03:22:29 +00003373 // Bit-fields are not addressable, we only need to verify they are "integer
3374 // like". We still have to disallow a subsequent non-bitfield, for example:
3375 // struct { int : 0; int x }
3376 // is non-integer like according to gcc.
3377 if (FD->isBitField()) {
3378 if (!RD->isUnion())
3379 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003380
Daniel Dunbar679855a2010-01-29 03:22:29 +00003381 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3382 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003383
Daniel Dunbar679855a2010-01-29 03:22:29 +00003384 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003385 }
3386
Daniel Dunbar679855a2010-01-29 03:22:29 +00003387 // Check if this field is at offset 0.
3388 if (Layout.getFieldOffset(idx) != 0)
3389 return false;
3390
Daniel Dunbar98303b92009-09-13 08:03:58 +00003391 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3392 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003393
Daniel Dunbar679855a2010-01-29 03:22:29 +00003394 // Only allow at most one field in a structure. This doesn't match the
3395 // wording above, but follows gcc in situations with a field following an
3396 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00003397 if (!RD->isUnion()) {
3398 if (HadField)
3399 return false;
3400
3401 HadField = true;
3402 }
3403 }
3404
3405 return true;
3406}
3407
Chris Lattnera3c109b2010-07-29 02:16:43 +00003408ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003409 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003410 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00003411
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00003412 // Large vector types should be returned via memory.
3413 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
3414 return ABIArgInfo::getIndirect(0);
3415
John McCalld608cdb2010-08-22 10:59:02 +00003416 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003417 // Treat an enum type as its underlying type.
3418 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3419 RetTy = EnumTy->getDecl()->getIntegerType();
3420
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003421 return (RetTy->isPromotableIntegerType() ?
3422 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003423 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003424
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003425 // Structures with either a non-trivial destructor or a non-trivial
3426 // copy constructor are always indirect.
3427 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
3428 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3429
Daniel Dunbar98303b92009-09-13 08:03:58 +00003430 // Are we following APCS?
3431 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00003432 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00003433 return ABIArgInfo::getIgnore();
3434
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003435 // Complex types are all returned as packed integers.
3436 //
3437 // FIXME: Consider using 2 x vector types if the back end handles them
3438 // correctly.
3439 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00003440 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00003441 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003442
Daniel Dunbar98303b92009-09-13 08:03:58 +00003443 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003444 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003445 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003446 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003447 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003448 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003449 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003450 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3451 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003452 }
3453
3454 // Otherwise return in memory.
3455 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003456 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003457
3458 // Otherwise this is an AAPCS variant.
3459
Chris Lattnera3c109b2010-07-29 02:16:43 +00003460 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00003461 return ABIArgInfo::getIgnore();
3462
Bob Wilson3b694fa2011-11-02 04:51:36 +00003463 // Check for homogeneous aggregates with AAPCS-VFP.
3464 if (getABIKind() == AAPCS_VFP) {
3465 const Type *Base = 0;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003466 if (isHomogeneousAggregate(RetTy, Base, getContext())) {
3467 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson3b694fa2011-11-02 04:51:36 +00003468 // Homogeneous Aggregates are returned directly.
3469 return ABIArgInfo::getDirect();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003470 }
Bob Wilson3b694fa2011-11-02 04:51:36 +00003471 }
3472
Daniel Dunbar98303b92009-09-13 08:03:58 +00003473 // Aggregates <= 4 bytes are returned in r0; other aggregates
3474 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003475 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00003476 if (Size <= 32) {
3477 // Return in the smallest viable integer type.
3478 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003479 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003480 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003481 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3482 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003483 }
3484
Daniel Dunbar98303b92009-09-13 08:03:58 +00003485 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003486}
3487
Manman Ren97f81572012-10-16 19:18:39 +00003488/// isIllegalVector - check whether Ty is an illegal vector type.
3489bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
3490 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3491 // Check whether VT is legal.
3492 unsigned NumElements = VT->getNumElements();
3493 uint64_t Size = getContext().getTypeSize(VT);
3494 // NumElements should be power of 2.
3495 if ((NumElements & (NumElements - 1)) != 0)
3496 return true;
3497 // Size should be greater than 32 bits.
3498 return Size <= 32;
3499 }
3500 return false;
3501}
3502
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003503llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00003504 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003505 llvm::Type *BP = CGF.Int8PtrTy;
3506 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003507
3508 CGBuilderTy &Builder = CGF.Builder;
Chris Lattner8b418682012-02-07 00:39:47 +00003509 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003510 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Manman Rend105e732012-10-16 19:01:37 +00003511
3512 uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8;
Rafael Espindolae164c182011-08-02 22:33:37 +00003513 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
Manman Ren97f81572012-10-16 19:18:39 +00003514 bool IsIndirect = false;
Manman Rend105e732012-10-16 19:01:37 +00003515
3516 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
3517 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
Manman Ren93371022012-10-16 19:51:48 +00003518 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3519 getABIKind() == ARMABIInfo::AAPCS)
3520 TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
3521 else
3522 TyAlign = 4;
Manman Ren97f81572012-10-16 19:18:39 +00003523 // Use indirect if size of the illegal vector is bigger than 16 bytes.
3524 if (isIllegalVectorType(Ty) && Size > 16) {
3525 IsIndirect = true;
3526 Size = 4;
3527 TyAlign = 4;
3528 }
Manman Rend105e732012-10-16 19:01:37 +00003529
3530 // Handle address alignment for ABI alignment > 4 bytes.
Rafael Espindolae164c182011-08-02 22:33:37 +00003531 if (TyAlign > 4) {
3532 assert((TyAlign & (TyAlign - 1)) == 0 &&
3533 "Alignment is not power of 2!");
3534 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3535 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
3536 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
Manman Rend105e732012-10-16 19:01:37 +00003537 Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align");
Rafael Espindolae164c182011-08-02 22:33:37 +00003538 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003539
3540 uint64_t Offset =
Manman Rend105e732012-10-16 19:01:37 +00003541 llvm::RoundUpToAlignment(Size, 4);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003542 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00003543 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003544 "ap.next");
3545 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3546
Manman Ren97f81572012-10-16 19:18:39 +00003547 if (IsIndirect)
3548 Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP));
Manman Ren93371022012-10-16 19:51:48 +00003549 else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) {
Manman Rend105e732012-10-16 19:01:37 +00003550 // We can't directly cast ap.cur to pointer to a vector type, since ap.cur
3551 // may not be correctly aligned for the vector type. We create an aligned
3552 // temporary space and copy the content over from ap.cur to the temporary
3553 // space. This is necessary if the natural alignment of the type is greater
3554 // than the ABI alignment.
3555 llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
3556 CharUnits CharSize = getContext().getTypeSizeInChars(Ty);
3557 llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty),
3558 "var.align");
3559 llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
3560 llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy);
3561 Builder.CreateMemCpy(Dst, Src,
3562 llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()),
3563 TyAlign, false);
3564 Addr = AlignedTemp; //The content is in aligned location.
3565 }
3566 llvm::Type *PTy =
3567 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3568 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3569
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003570 return AddrTyped;
3571}
3572
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00003573namespace {
3574
Derek Schuff263366f2012-10-16 22:30:41 +00003575class NaClARMABIInfo : public ABIInfo {
3576 public:
3577 NaClARMABIInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3578 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, Kind) {}
3579 virtual void computeInfo(CGFunctionInfo &FI) const;
3580 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3581 CodeGenFunction &CGF) const;
3582 private:
3583 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
3584 ARMABIInfo NInfo; // Used for everything else.
3585};
3586
3587class NaClARMTargetCodeGenInfo : public TargetCodeGenInfo {
3588 public:
3589 NaClARMTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3590 : TargetCodeGenInfo(new NaClARMABIInfo(CGT, Kind)) {}
3591};
3592
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00003593}
3594
Derek Schuff263366f2012-10-16 22:30:41 +00003595void NaClARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
3596 if (FI.getASTCallingConvention() == CC_PnaclCall)
3597 PInfo.computeInfo(FI);
3598 else
3599 static_cast<const ABIInfo&>(NInfo).computeInfo(FI);
3600}
3601
3602llvm::Value *NaClARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3603 CodeGenFunction &CGF) const {
3604 // Always use the native convention; calling pnacl-style varargs functions
3605 // is unsupported.
3606 return static_cast<const ABIInfo&>(NInfo).EmitVAArg(VAListAddr, Ty, CGF);
3607}
3608
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003609//===----------------------------------------------------------------------===//
Tim Northoverc264e162013-01-31 12:13:10 +00003610// AArch64 ABI Implementation
3611//===----------------------------------------------------------------------===//
3612
3613namespace {
3614
3615class AArch64ABIInfo : public ABIInfo {
3616public:
3617 AArch64ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3618
3619private:
3620 // The AArch64 PCS is explicit about return types and argument types being
3621 // handled identically, so we don't need to draw a distinction between
3622 // Argument and Return classification.
3623 ABIArgInfo classifyGenericType(QualType Ty, int &FreeIntRegs,
3624 int &FreeVFPRegs) const;
3625
3626 ABIArgInfo tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded, bool IsInt,
3627 llvm::Type *DirectTy = 0) const;
3628
3629 virtual void computeInfo(CGFunctionInfo &FI) const;
3630
3631 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3632 CodeGenFunction &CGF) const;
3633};
3634
3635class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
3636public:
3637 AArch64TargetCodeGenInfo(CodeGenTypes &CGT)
3638 :TargetCodeGenInfo(new AArch64ABIInfo(CGT)) {}
3639
3640 const AArch64ABIInfo &getABIInfo() const {
3641 return static_cast<const AArch64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
3642 }
3643
3644 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3645 return 31;
3646 }
3647
3648 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3649 llvm::Value *Address) const {
3650 // 0-31 are x0-x30 and sp: 8 bytes each
3651 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
3652 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 31);
3653
3654 // 64-95 are v0-v31: 16 bytes each
3655 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
3656 AssignToArrayRange(CGF.Builder, Address, Sixteen8, 64, 95);
3657
3658 return false;
3659 }
3660
3661};
3662
3663}
3664
3665void AArch64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
3666 int FreeIntRegs = 8, FreeVFPRegs = 8;
3667
3668 FI.getReturnInfo() = classifyGenericType(FI.getReturnType(),
3669 FreeIntRegs, FreeVFPRegs);
3670
3671 FreeIntRegs = FreeVFPRegs = 8;
3672 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3673 it != ie; ++it) {
3674 it->info = classifyGenericType(it->type, FreeIntRegs, FreeVFPRegs);
3675
3676 }
3677}
3678
3679ABIArgInfo
3680AArch64ABIInfo::tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded,
3681 bool IsInt, llvm::Type *DirectTy) const {
3682 if (FreeRegs >= RegsNeeded) {
3683 FreeRegs -= RegsNeeded;
3684 return ABIArgInfo::getDirect(DirectTy);
3685 }
3686
3687 llvm::Type *Padding = 0;
3688
3689 // We need padding so that later arguments don't get filled in anyway. That
3690 // wouldn't happen if only ByVal arguments followed in the same category, but
3691 // a large structure will simply seem to be a pointer as far as LLVM is
3692 // concerned.
3693 if (FreeRegs > 0) {
3694 if (IsInt)
3695 Padding = llvm::Type::getInt64Ty(getVMContext());
3696 else
3697 Padding = llvm::Type::getFloatTy(getVMContext());
3698
3699 // Either [N x i64] or [N x float].
3700 Padding = llvm::ArrayType::get(Padding, FreeRegs);
3701 FreeRegs = 0;
3702 }
3703
3704 return ABIArgInfo::getIndirect(getContext().getTypeAlign(Ty) / 8,
3705 /*IsByVal=*/ true, /*Realign=*/ false,
3706 Padding);
3707}
3708
3709
3710ABIArgInfo AArch64ABIInfo::classifyGenericType(QualType Ty,
3711 int &FreeIntRegs,
3712 int &FreeVFPRegs) const {
3713 // Can only occurs for return, but harmless otherwise.
3714 if (Ty->isVoidType())
3715 return ABIArgInfo::getIgnore();
3716
3717 // Large vector types should be returned via memory. There's no such concept
3718 // in the ABI, but they'd be over 16 bytes anyway so no matter how they're
3719 // classified they'd go into memory (see B.3).
3720 if (Ty->isVectorType() && getContext().getTypeSize(Ty) > 128) {
3721 if (FreeIntRegs > 0)
3722 --FreeIntRegs;
3723 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3724 }
3725
3726 // All non-aggregate LLVM types have a concrete ABI representation so they can
3727 // be passed directly. After this block we're guaranteed to be in a
3728 // complicated case.
3729 if (!isAggregateTypeForABI(Ty)) {
3730 // Treat an enum type as its underlying type.
3731 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3732 Ty = EnumTy->getDecl()->getIntegerType();
3733
3734 if (Ty->isFloatingType() || Ty->isVectorType())
3735 return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ false);
3736
3737 assert(getContext().getTypeSize(Ty) <= 128 &&
3738 "unexpectedly large scalar type");
3739
3740 int RegsNeeded = getContext().getTypeSize(Ty) > 64 ? 2 : 1;
3741
3742 // If the type may need padding registers to ensure "alignment", we must be
3743 // careful when this is accounted for. Increasing the effective size covers
3744 // all cases.
3745 if (getContext().getTypeAlign(Ty) == 128)
3746 RegsNeeded += FreeIntRegs % 2 != 0;
3747
3748 return tryUseRegs(Ty, FreeIntRegs, RegsNeeded, /*IsInt=*/ true);
3749 }
3750
3751 // Structures with either a non-trivial destructor or a non-trivial
3752 // copy constructor are always indirect.
3753 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) {
3754 if (FreeIntRegs > 0)
3755 --FreeIntRegs;
3756 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3757 }
3758
3759 if (isEmptyRecord(getContext(), Ty, true)) {
3760 if (!getContext().getLangOpts().CPlusPlus) {
3761 // Empty structs outside C++ mode are a GNU extension, so no ABI can
3762 // possibly tell us what to do. It turns out (I believe) that GCC ignores
3763 // the object for parameter-passsing purposes.
3764 return ABIArgInfo::getIgnore();
3765 }
3766
3767 // The combination of C++98 9p5 (sizeof(struct) != 0) and the pseudocode
3768 // description of va_arg in the PCS require that an empty struct does
3769 // actually occupy space for parameter-passing. I'm hoping for a
3770 // clarification giving an explicit paragraph to point to in future.
3771 return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ true,
3772 llvm::Type::getInt8Ty(getVMContext()));
3773 }
3774
3775 // Homogeneous vector aggregates get passed in registers or on the stack.
3776 const Type *Base = 0;
3777 uint64_t NumMembers = 0;
3778 if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)) {
3779 assert(Base && "Base class should be set for homogeneous aggregate");
3780 // Homogeneous aggregates are passed and returned directly.
3781 return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ NumMembers,
3782 /*IsInt=*/ false);
3783 }
3784
3785 uint64_t Size = getContext().getTypeSize(Ty);
3786 if (Size <= 128) {
3787 // Small structs can use the same direct type whether they're in registers
3788 // or on the stack.
3789 llvm::Type *BaseTy;
3790 unsigned NumBases;
3791 int SizeInRegs = (Size + 63) / 64;
3792
3793 if (getContext().getTypeAlign(Ty) == 128) {
3794 BaseTy = llvm::Type::getIntNTy(getVMContext(), 128);
3795 NumBases = 1;
3796
3797 // If the type may need padding registers to ensure "alignment", we must
3798 // be careful when this is accounted for. Increasing the effective size
3799 // covers all cases.
3800 SizeInRegs += FreeIntRegs % 2 != 0;
3801 } else {
3802 BaseTy = llvm::Type::getInt64Ty(getVMContext());
3803 NumBases = SizeInRegs;
3804 }
3805 llvm::Type *DirectTy = llvm::ArrayType::get(BaseTy, NumBases);
3806
3807 return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ SizeInRegs,
3808 /*IsInt=*/ true, DirectTy);
3809 }
3810
3811 // If the aggregate is > 16 bytes, it's passed and returned indirectly. In
3812 // LLVM terms the return uses an "sret" pointer, but that's handled elsewhere.
3813 --FreeIntRegs;
3814 return ABIArgInfo::getIndirect(0, /* byVal = */ false);
3815}
3816
3817llvm::Value *AArch64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3818 CodeGenFunction &CGF) const {
3819 // The AArch64 va_list type and handling is specified in the Procedure Call
3820 // Standard, section B.4:
3821 //
3822 // struct {
3823 // void *__stack;
3824 // void *__gr_top;
3825 // void *__vr_top;
3826 // int __gr_offs;
3827 // int __vr_offs;
3828 // };
3829
3830 assert(!CGF.CGM.getDataLayout().isBigEndian()
3831 && "va_arg not implemented for big-endian AArch64");
3832
3833 int FreeIntRegs = 8, FreeVFPRegs = 8;
3834 Ty = CGF.getContext().getCanonicalType(Ty);
3835 ABIArgInfo AI = classifyGenericType(Ty, FreeIntRegs, FreeVFPRegs);
3836
3837 llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
3838 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
3839 llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
3840 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
3841
3842 llvm::Value *reg_offs_p = 0, *reg_offs = 0;
3843 int reg_top_index;
3844 int RegSize;
3845 if (FreeIntRegs < 8) {
3846 assert(FreeVFPRegs == 8 && "Arguments never split between int & VFP regs");
3847 // 3 is the field number of __gr_offs
3848 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p");
3849 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
3850 reg_top_index = 1; // field number for __gr_top
3851 RegSize = 8 * (8 - FreeIntRegs);
3852 } else {
3853 assert(FreeVFPRegs < 8 && "Argument must go in VFP or int regs");
3854 // 4 is the field number of __vr_offs.
3855 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p");
3856 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
3857 reg_top_index = 2; // field number for __vr_top
3858 RegSize = 16 * (8 - FreeVFPRegs);
3859 }
3860
3861 //=======================================
3862 // Find out where argument was passed
3863 //=======================================
3864
3865 // If reg_offs >= 0 we're already using the stack for this type of
3866 // argument. We don't want to keep updating reg_offs (in case it overflows,
3867 // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
3868 // whatever they get).
3869 llvm::Value *UsingStack = 0;
3870 UsingStack = CGF.Builder.CreateICmpSGE(reg_offs,
3871 llvm::ConstantInt::get(CGF.Int32Ty, 0));
3872
3873 CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
3874
3875 // Otherwise, at least some kind of argument could go in these registers, the
3876 // quesiton is whether this particular type is too big.
3877 CGF.EmitBlock(MaybeRegBlock);
3878
3879 // Integer arguments may need to correct register alignment (for example a
3880 // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
3881 // align __gr_offs to calculate the potential address.
3882 if (FreeIntRegs < 8 && AI.isDirect() && getContext().getTypeAlign(Ty) > 64) {
3883 int Align = getContext().getTypeAlign(Ty) / 8;
3884
3885 reg_offs = CGF.Builder.CreateAdd(reg_offs,
3886 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
3887 "align_regoffs");
3888 reg_offs = CGF.Builder.CreateAnd(reg_offs,
3889 llvm::ConstantInt::get(CGF.Int32Ty, -Align),
3890 "aligned_regoffs");
3891 }
3892
3893 // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
3894 llvm::Value *NewOffset = 0;
3895 NewOffset = CGF.Builder.CreateAdd(reg_offs,
3896 llvm::ConstantInt::get(CGF.Int32Ty, RegSize),
3897 "new_reg_offs");
3898 CGF.Builder.CreateStore(NewOffset, reg_offs_p);
3899
3900 // Now we're in a position to decide whether this argument really was in
3901 // registers or not.
3902 llvm::Value *InRegs = 0;
3903 InRegs = CGF.Builder.CreateICmpSLE(NewOffset,
3904 llvm::ConstantInt::get(CGF.Int32Ty, 0),
3905 "inreg");
3906
3907 CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
3908
3909 //=======================================
3910 // Argument was in registers
3911 //=======================================
3912
3913 // Now we emit the code for if the argument was originally passed in
3914 // registers. First start the appropriate block:
3915 CGF.EmitBlock(InRegBlock);
3916
3917 llvm::Value *reg_top_p = 0, *reg_top = 0;
3918 reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p");
3919 reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
3920 llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs);
3921 llvm::Value *RegAddr = 0;
3922 llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
3923
3924 if (!AI.isDirect()) {
3925 // If it's been passed indirectly (actually a struct), whatever we find from
3926 // stored registers or on the stack will actually be a struct **.
3927 MemTy = llvm::PointerType::getUnqual(MemTy);
3928 }
3929
3930 const Type *Base = 0;
3931 uint64_t NumMembers;
3932 if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)
3933 && NumMembers > 1) {
3934 // Homogeneous aggregates passed in registers will have their elements split
3935 // and stored 16-bytes apart regardless of size (they're notionally in qN,
3936 // qN+1, ...). We reload and store into a temporary local variable
3937 // contiguously.
3938 assert(AI.isDirect() && "Homogeneous aggregates should be passed directly");
3939 llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
3940 llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
3941 llvm::Value *Tmp = CGF.CreateTempAlloca(HFATy);
3942
3943 for (unsigned i = 0; i < NumMembers; ++i) {
3944 llvm::Value *BaseOffset = llvm::ConstantInt::get(CGF.Int32Ty, 16 * i);
3945 llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset);
3946 LoadAddr = CGF.Builder.CreateBitCast(LoadAddr,
3947 llvm::PointerType::getUnqual(BaseTy));
3948 llvm::Value *StoreAddr = CGF.Builder.CreateStructGEP(Tmp, i);
3949
3950 llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
3951 CGF.Builder.CreateStore(Elem, StoreAddr);
3952 }
3953
3954 RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy);
3955 } else {
3956 // Otherwise the object is contiguous in memory
3957 RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy);
3958 }
3959
3960 CGF.EmitBranch(ContBlock);
3961
3962 //=======================================
3963 // Argument was on the stack
3964 //=======================================
3965 CGF.EmitBlock(OnStackBlock);
3966
3967 llvm::Value *stack_p = 0, *OnStackAddr = 0;
3968 stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p");
3969 OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack");
3970
3971 // Again, stack arguments may need realigmnent. In this case both integer and
3972 // floating-point ones might be affected.
3973 if (AI.isDirect() && getContext().getTypeAlign(Ty) > 64) {
3974 int Align = getContext().getTypeAlign(Ty) / 8;
3975
3976 OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty);
3977
3978 OnStackAddr = CGF.Builder.CreateAdd(OnStackAddr,
3979 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
3980 "align_stack");
3981 OnStackAddr = CGF.Builder.CreateAnd(OnStackAddr,
3982 llvm::ConstantInt::get(CGF.Int64Ty, -Align),
3983 "align_stack");
3984
3985 OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy);
3986 }
3987
3988 uint64_t StackSize;
3989 if (AI.isDirect())
3990 StackSize = getContext().getTypeSize(Ty) / 8;
3991 else
3992 StackSize = 8;
3993
3994 // All stack slots are 8 bytes
3995 StackSize = llvm::RoundUpToAlignment(StackSize, 8);
3996
3997 llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize);
3998 llvm::Value *NewStack = CGF.Builder.CreateGEP(OnStackAddr, StackSizeC,
3999 "new_stack");
4000
4001 // Write the new value of __stack for the next call to va_arg
4002 CGF.Builder.CreateStore(NewStack, stack_p);
4003
4004 OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy);
4005
4006 CGF.EmitBranch(ContBlock);
4007
4008 //=======================================
4009 // Tidy up
4010 //=======================================
4011 CGF.EmitBlock(ContBlock);
4012
4013 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr");
4014 ResAddr->addIncoming(RegAddr, InRegBlock);
4015 ResAddr->addIncoming(OnStackAddr, OnStackBlock);
4016
4017 if (AI.isDirect())
4018 return ResAddr;
4019
4020 return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr");
4021}
4022
4023//===----------------------------------------------------------------------===//
Justin Holewinski2c585b92012-05-24 17:43:12 +00004024// NVPTX ABI Implementation
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004025//===----------------------------------------------------------------------===//
4026
4027namespace {
4028
Justin Holewinski2c585b92012-05-24 17:43:12 +00004029class NVPTXABIInfo : public ABIInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004030public:
John McCallbd7370a2013-02-28 19:01:20 +00004031 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) { setRuntimeCC(); }
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004032
4033 ABIArgInfo classifyReturnType(QualType RetTy) const;
4034 ABIArgInfo classifyArgumentType(QualType Ty) const;
4035
4036 virtual void computeInfo(CGFunctionInfo &FI) const;
4037 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4038 CodeGenFunction &CFG) const;
John McCallbd7370a2013-02-28 19:01:20 +00004039private:
4040 void setRuntimeCC();
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004041};
4042
Justin Holewinski2c585b92012-05-24 17:43:12 +00004043class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004044public:
Justin Holewinski2c585b92012-05-24 17:43:12 +00004045 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
4046 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
Justin Holewinski818eafb2011-10-05 17:58:44 +00004047
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004048 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4049 CodeGen::CodeGenModule &M) const;
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004050};
4051
Justin Holewinski2c585b92012-05-24 17:43:12 +00004052ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004053 if (RetTy->isVoidType())
4054 return ABIArgInfo::getIgnore();
4055 if (isAggregateTypeForABI(RetTy))
4056 return ABIArgInfo::getIndirect(0);
4057 return ABIArgInfo::getDirect();
4058}
4059
Justin Holewinski2c585b92012-05-24 17:43:12 +00004060ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004061 if (isAggregateTypeForABI(Ty))
4062 return ABIArgInfo::getIndirect(0);
4063
4064 return ABIArgInfo::getDirect();
4065}
4066
Justin Holewinski2c585b92012-05-24 17:43:12 +00004067void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004068 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4069 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4070 it != ie; ++it)
4071 it->info = classifyArgumentType(it->type);
4072
4073 // Always honor user-specified calling convention.
4074 if (FI.getCallingConvention() != llvm::CallingConv::C)
4075 return;
4076
John McCallbd7370a2013-02-28 19:01:20 +00004077 FI.setEffectiveCallingConvention(getRuntimeCC());
4078}
4079
4080void NVPTXABIInfo::setRuntimeCC() {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004081 // Calling convention as default by an ABI.
Justin Holewinski2c585b92012-05-24 17:43:12 +00004082 // We're still using the PTX_Kernel/PTX_Device calling conventions here,
4083 // but we should switch to NVVM metadata later on.
David Blaikie4e4d0842012-03-11 07:00:24 +00004084 const LangOptions &LangOpts = getContext().getLangOpts();
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004085 if (LangOpts.OpenCL || LangOpts.CUDA) {
4086 // If we are in OpenCL or CUDA mode, then default to device functions
John McCallbd7370a2013-02-28 19:01:20 +00004087 RuntimeCC = llvm::CallingConv::PTX_Device;
Justin Holewinski818eafb2011-10-05 17:58:44 +00004088 } else {
4089 // If we are in standard C/C++ mode, use the triple to decide on the default
4090 StringRef Env =
4091 getContext().getTargetInfo().getTriple().getEnvironmentName();
4092 if (Env == "device")
John McCallbd7370a2013-02-28 19:01:20 +00004093 RuntimeCC = llvm::CallingConv::PTX_Device;
Justin Holewinski818eafb2011-10-05 17:58:44 +00004094 else
John McCallbd7370a2013-02-28 19:01:20 +00004095 RuntimeCC = llvm::CallingConv::PTX_Kernel;
Justin Holewinski818eafb2011-10-05 17:58:44 +00004096 }
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004097}
4098
Justin Holewinski2c585b92012-05-24 17:43:12 +00004099llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4100 CodeGenFunction &CFG) const {
4101 llvm_unreachable("NVPTX does not support varargs");
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004102}
4103
Justin Holewinski2c585b92012-05-24 17:43:12 +00004104void NVPTXTargetCodeGenInfo::
4105SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4106 CodeGen::CodeGenModule &M) const{
Justin Holewinski818eafb2011-10-05 17:58:44 +00004107 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4108 if (!FD) return;
4109
4110 llvm::Function *F = cast<llvm::Function>(GV);
4111
4112 // Perform special handling in OpenCL mode
David Blaikie4e4d0842012-03-11 07:00:24 +00004113 if (M.getLangOpts().OpenCL) {
Justin Holewinski818eafb2011-10-05 17:58:44 +00004114 // Use OpenCL function attributes to set proper calling conventions
4115 // By default, all functions are device functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00004116 if (FD->hasAttr<OpenCLKernelAttr>()) {
4117 // OpenCL __kernel functions get a kernel calling convention
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004118 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00004119 // And kernel functions are not subject to inlining
Bill Wendling72390b32012-12-20 19:27:06 +00004120 F->addFnAttr(llvm::Attribute::NoInline);
Justin Holewinski818eafb2011-10-05 17:58:44 +00004121 }
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004122 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00004123
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004124 // Perform special handling in CUDA mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00004125 if (M.getLangOpts().CUDA) {
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004126 // CUDA __global__ functions get a kernel calling convention. Since
4127 // __global__ functions cannot be called from the device, we do not
4128 // need to set the noinline attribute.
4129 if (FD->getAttr<CUDAGlobalAttr>())
4130 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00004131 }
4132}
4133
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004134}
4135
4136//===----------------------------------------------------------------------===//
Wesley Peck276fdf42010-12-19 19:57:51 +00004137// MBlaze ABI Implementation
4138//===----------------------------------------------------------------------===//
4139
4140namespace {
4141
4142class MBlazeABIInfo : public ABIInfo {
4143public:
4144 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
4145
4146 bool isPromotableIntegerType(QualType Ty) const;
4147
4148 ABIArgInfo classifyReturnType(QualType RetTy) const;
4149 ABIArgInfo classifyArgumentType(QualType RetTy) const;
4150
4151 virtual void computeInfo(CGFunctionInfo &FI) const {
4152 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4153 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4154 it != ie; ++it)
4155 it->info = classifyArgumentType(it->type);
4156 }
4157
4158 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4159 CodeGenFunction &CGF) const;
4160};
4161
4162class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
4163public:
4164 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
4165 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
4166 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4167 CodeGen::CodeGenModule &M) const;
4168};
4169
4170}
4171
4172bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
4173 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
4174 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4175 switch (BT->getKind()) {
4176 case BuiltinType::Bool:
4177 case BuiltinType::Char_S:
4178 case BuiltinType::Char_U:
4179 case BuiltinType::SChar:
4180 case BuiltinType::UChar:
4181 case BuiltinType::Short:
4182 case BuiltinType::UShort:
4183 return true;
4184 default:
4185 return false;
4186 }
4187 return false;
4188}
4189
4190llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4191 CodeGenFunction &CGF) const {
4192 // FIXME: Implement
4193 return 0;
4194}
4195
4196
4197ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
4198 if (RetTy->isVoidType())
4199 return ABIArgInfo::getIgnore();
4200 if (isAggregateTypeForABI(RetTy))
4201 return ABIArgInfo::getIndirect(0);
4202
4203 return (isPromotableIntegerType(RetTy) ?
4204 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4205}
4206
4207ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
4208 if (isAggregateTypeForABI(Ty))
4209 return ABIArgInfo::getIndirect(0);
4210
4211 return (isPromotableIntegerType(Ty) ?
4212 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4213}
4214
4215void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4216 llvm::GlobalValue *GV,
4217 CodeGen::CodeGenModule &M)
4218 const {
4219 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4220 if (!FD) return;
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00004221
Wesley Peck276fdf42010-12-19 19:57:51 +00004222 llvm::CallingConv::ID CC = llvm::CallingConv::C;
4223 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
4224 CC = llvm::CallingConv::MBLAZE_INTR;
4225 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
4226 CC = llvm::CallingConv::MBLAZE_SVOL;
4227
4228 if (CC != llvm::CallingConv::C) {
4229 // Handle 'interrupt_handler' attribute:
4230 llvm::Function *F = cast<llvm::Function>(GV);
4231
4232 // Step 1: Set ISR calling convention.
4233 F->setCallingConv(CC);
4234
4235 // Step 2: Add attributes goodness.
Bill Wendling72390b32012-12-20 19:27:06 +00004236 F->addFnAttr(llvm::Attribute::NoInline);
Wesley Peck276fdf42010-12-19 19:57:51 +00004237 }
4238
4239 // Step 3: Emit _interrupt_handler alias.
4240 if (CC == llvm::CallingConv::MBLAZE_INTR)
4241 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
4242 "_interrupt_handler", GV, &M.getModule());
4243}
4244
4245
4246//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004247// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004248//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004249
4250namespace {
4251
4252class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
4253public:
Chris Lattnerea044322010-07-29 02:01:43 +00004254 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
4255 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004256 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4257 CodeGen::CodeGenModule &M) const;
4258};
4259
4260}
4261
4262void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4263 llvm::GlobalValue *GV,
4264 CodeGen::CodeGenModule &M) const {
4265 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4266 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
4267 // Handle 'interrupt' attribute:
4268 llvm::Function *F = cast<llvm::Function>(GV);
4269
4270 // Step 1: Set ISR calling convention.
4271 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
4272
4273 // Step 2: Add attributes goodness.
Bill Wendling72390b32012-12-20 19:27:06 +00004274 F->addFnAttr(llvm::Attribute::NoInline);
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004275
4276 // Step 3: Emit ISR vector alias.
Anton Korobeynikovf419a852012-11-26 18:59:10 +00004277 unsigned Num = attr->getNumber() / 2;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004278 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Anton Korobeynikovf419a852012-11-26 18:59:10 +00004279 "__isr_" + Twine(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004280 GV, &M.getModule());
4281 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004282 }
4283}
4284
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004285//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00004286// MIPS ABI Implementation. This works for both little-endian and
4287// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004288//===----------------------------------------------------------------------===//
4289
John McCallaeeb7012010-05-27 06:19:26 +00004290namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00004291class MipsABIInfo : public ABIInfo {
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004292 bool IsO32;
Akira Hatanakac359f202012-07-03 19:24:06 +00004293 unsigned MinABIStackAlignInBytes, StackAlignInBytes;
4294 void CoerceToIntArgs(uint64_t TySize,
4295 SmallVector<llvm::Type*, 8> &ArgList) const;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004296 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004297 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004298 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004299public:
Akira Hatanakab551dd32011-11-03 00:05:50 +00004300 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
Akira Hatanakac359f202012-07-03 19:24:06 +00004301 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
4302 StackAlignInBytes(IsO32 ? 8 : 16) {}
Akira Hatanaka619e8872011-06-02 00:09:17 +00004303
4304 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004305 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004306 virtual void computeInfo(CGFunctionInfo &FI) const;
4307 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4308 CodeGenFunction &CGF) const;
4309};
4310
John McCallaeeb7012010-05-27 06:19:26 +00004311class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanakae624fa02011-09-20 18:23:28 +00004312 unsigned SizeOfUnwindException;
John McCallaeeb7012010-05-27 06:19:26 +00004313public:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004314 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
4315 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
4316 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCallaeeb7012010-05-27 06:19:26 +00004317
4318 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
4319 return 29;
4320 }
4321
Reed Kotler7dfd1822013-01-16 17:10:28 +00004322 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4323 CodeGen::CodeGenModule &CGM) const {
Reed Kotlerad4b8b42013-03-13 20:40:30 +00004324 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4325 if (!FD) return;
Rafael Espindolad8e6d6d2013-03-19 14:32:23 +00004326 llvm::Function *Fn = cast<llvm::Function>(GV);
Reed Kotlerad4b8b42013-03-13 20:40:30 +00004327 if (FD->hasAttr<Mips16Attr>()) {
4328 Fn->addFnAttr("mips16");
4329 }
4330 else if (FD->hasAttr<NoMips16Attr>()) {
4331 Fn->addFnAttr("nomips16");
4332 }
Reed Kotler7dfd1822013-01-16 17:10:28 +00004333 }
Reed Kotlerad4b8b42013-03-13 20:40:30 +00004334
John McCallaeeb7012010-05-27 06:19:26 +00004335 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00004336 llvm::Value *Address) const;
John McCall49e34be2011-08-30 01:42:09 +00004337
4338 unsigned getSizeOfUnwindException() const {
Akira Hatanakae624fa02011-09-20 18:23:28 +00004339 return SizeOfUnwindException;
John McCall49e34be2011-08-30 01:42:09 +00004340 }
John McCallaeeb7012010-05-27 06:19:26 +00004341};
4342}
4343
Akira Hatanakac359f202012-07-03 19:24:06 +00004344void MipsABIInfo::CoerceToIntArgs(uint64_t TySize,
4345 SmallVector<llvm::Type*, 8> &ArgList) const {
4346 llvm::IntegerType *IntTy =
4347 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004348
4349 // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
4350 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
4351 ArgList.push_back(IntTy);
4352
4353 // If necessary, add one more integer type to ArgList.
4354 unsigned R = TySize % (MinABIStackAlignInBytes * 8);
4355
4356 if (R)
4357 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004358}
4359
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004360// In N32/64, an aligned double precision floating point field is passed in
4361// a register.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004362llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
Akira Hatanakac359f202012-07-03 19:24:06 +00004363 SmallVector<llvm::Type*, 8> ArgList, IntArgList;
4364
4365 if (IsO32) {
4366 CoerceToIntArgs(TySize, ArgList);
4367 return llvm::StructType::get(getVMContext(), ArgList);
4368 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004369
Akira Hatanaka2afd23d2012-01-12 00:52:17 +00004370 if (Ty->isComplexType())
4371 return CGT.ConvertType(Ty);
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00004372
Akira Hatanakaa34e9212012-02-09 19:54:16 +00004373 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004374
Akira Hatanakac359f202012-07-03 19:24:06 +00004375 // Unions/vectors are passed in integer registers.
4376 if (!RT || !RT->isStructureOrClassType()) {
4377 CoerceToIntArgs(TySize, ArgList);
4378 return llvm::StructType::get(getVMContext(), ArgList);
4379 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004380
4381 const RecordDecl *RD = RT->getDecl();
4382 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004383 assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004384
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004385 uint64_t LastOffset = 0;
4386 unsigned idx = 0;
4387 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
4388
Akira Hatanakaa34e9212012-02-09 19:54:16 +00004389 // Iterate over fields in the struct/class and check if there are any aligned
4390 // double fields.
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004391 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
4392 i != e; ++i, ++idx) {
David Blaikie262bc182012-04-30 02:36:29 +00004393 const QualType Ty = i->getType();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004394 const BuiltinType *BT = Ty->getAs<BuiltinType>();
4395
4396 if (!BT || BT->getKind() != BuiltinType::Double)
4397 continue;
4398
4399 uint64_t Offset = Layout.getFieldOffset(idx);
4400 if (Offset % 64) // Ignore doubles that are not aligned.
4401 continue;
4402
4403 // Add ((Offset - LastOffset) / 64) args of type i64.
4404 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
4405 ArgList.push_back(I64);
4406
4407 // Add double type.
4408 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
4409 LastOffset = Offset + 64;
4410 }
4411
Akira Hatanakac359f202012-07-03 19:24:06 +00004412 CoerceToIntArgs(TySize - LastOffset, IntArgList);
4413 ArgList.append(IntArgList.begin(), IntArgList.end());
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004414
4415 return llvm::StructType::get(getVMContext(), ArgList);
4416}
4417
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004418llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004419 assert((Offset % MinABIStackAlignInBytes) == 0);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004420
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004421 if ((Align - 1) & Offset)
4422 return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
4423
4424 return 0;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004425}
Akira Hatanaka9659d592012-01-10 22:44:52 +00004426
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004427ABIArgInfo
4428MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004429 uint64_t OrigOffset = Offset;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004430 uint64_t TySize = getContext().getTypeSize(Ty);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004431 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004432
Akira Hatanakac359f202012-07-03 19:24:06 +00004433 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
4434 (uint64_t)StackAlignInBytes);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004435 Offset = llvm::RoundUpToAlignment(Offset, Align);
4436 Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004437
Akira Hatanakac359f202012-07-03 19:24:06 +00004438 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
Akira Hatanaka619e8872011-06-02 00:09:17 +00004439 // Ignore empty aggregates.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004440 if (TySize == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00004441 return ABIArgInfo::getIgnore();
4442
Akira Hatanaka511949b2011-08-01 18:09:58 +00004443 // Records with non trivial destructors/constructors should not be passed
4444 // by value.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004445 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004446 Offset = OrigOffset + MinABIStackAlignInBytes;
Akira Hatanaka511949b2011-08-01 18:09:58 +00004447 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004448 }
Akira Hatanaka511949b2011-08-01 18:09:58 +00004449
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004450 // If we have reached here, aggregates are passed directly by coercing to
4451 // another structure type. Padding is inserted if the offset of the
4452 // aggregate is unaligned.
4453 return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
4454 getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00004455 }
4456
4457 // Treat an enum type as its underlying type.
4458 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4459 Ty = EnumTy->getDecl()->getIntegerType();
4460
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004461 if (Ty->isPromotableIntegerType())
4462 return ABIArgInfo::getExtend();
4463
Akira Hatanaka4055cfc2013-01-24 21:47:33 +00004464 return ABIArgInfo::getDirect(0, 0,
4465 IsO32 ? 0 : getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00004466}
4467
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004468llvm::Type*
4469MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakada54ff32012-02-09 18:49:26 +00004470 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakac359f202012-07-03 19:24:06 +00004471 SmallVector<llvm::Type*, 8> RTList;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004472
Akira Hatanakada54ff32012-02-09 18:49:26 +00004473 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004474 const RecordDecl *RD = RT->getDecl();
Akira Hatanakada54ff32012-02-09 18:49:26 +00004475 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
4476 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004477
Akira Hatanakada54ff32012-02-09 18:49:26 +00004478 // N32/64 returns struct/classes in floating point registers if the
4479 // following conditions are met:
4480 // 1. The size of the struct/class is no larger than 128-bit.
4481 // 2. The struct/class has one or two fields all of which are floating
4482 // point types.
4483 // 3. The offset of the first field is zero (this follows what gcc does).
4484 //
4485 // Any other composite results are returned in integer registers.
4486 //
4487 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
4488 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
4489 for (; b != e; ++b) {
David Blaikie262bc182012-04-30 02:36:29 +00004490 const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004491
Akira Hatanakada54ff32012-02-09 18:49:26 +00004492 if (!BT || !BT->isFloatingPoint())
4493 break;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004494
David Blaikie262bc182012-04-30 02:36:29 +00004495 RTList.push_back(CGT.ConvertType(b->getType()));
Akira Hatanakada54ff32012-02-09 18:49:26 +00004496 }
4497
4498 if (b == e)
4499 return llvm::StructType::get(getVMContext(), RTList,
4500 RD->hasAttr<PackedAttr>());
4501
4502 RTList.clear();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004503 }
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004504 }
4505
Akira Hatanakac359f202012-07-03 19:24:06 +00004506 CoerceToIntArgs(Size, RTList);
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004507 return llvm::StructType::get(getVMContext(), RTList);
4508}
4509
Akira Hatanaka619e8872011-06-02 00:09:17 +00004510ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanakaa8536c02012-01-23 23:18:57 +00004511 uint64_t Size = getContext().getTypeSize(RetTy);
4512
4513 if (RetTy->isVoidType() || Size == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00004514 return ABIArgInfo::getIgnore();
4515
Akira Hatanaka8aeb1472012-05-11 21:01:17 +00004516 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004517 if (Size <= 128) {
4518 if (RetTy->isAnyComplexType())
4519 return ABIArgInfo::getDirect();
4520
Akira Hatanakac359f202012-07-03 19:24:06 +00004521 // O32 returns integer vectors in registers.
4522 if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())
4523 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
4524
Akira Hatanaka526cdfb2012-02-08 01:31:22 +00004525 if (!IsO32 && !isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004526 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
4527 }
Akira Hatanaka619e8872011-06-02 00:09:17 +00004528
4529 return ABIArgInfo::getIndirect(0);
4530 }
4531
4532 // Treat an enum type as its underlying type.
4533 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4534 RetTy = EnumTy->getDecl()->getIntegerType();
4535
4536 return (RetTy->isPromotableIntegerType() ?
4537 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4538}
4539
4540void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanakacc662542012-01-12 01:10:09 +00004541 ABIArgInfo &RetInfo = FI.getReturnInfo();
4542 RetInfo = classifyReturnType(FI.getReturnType());
4543
4544 // Check if a pointer to an aggregate is passed as a hidden argument.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004545 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
Akira Hatanakacc662542012-01-12 01:10:09 +00004546
Akira Hatanaka619e8872011-06-02 00:09:17 +00004547 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4548 it != ie; ++it)
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004549 it->info = classifyArgumentType(it->type, Offset);
Akira Hatanaka619e8872011-06-02 00:09:17 +00004550}
4551
4552llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4553 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00004554 llvm::Type *BP = CGF.Int8PtrTy;
4555 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004556
4557 CGBuilderTy &Builder = CGF.Builder;
4558 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
4559 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004560 int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004561 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
4562 llvm::Value *AddrTyped;
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004563 unsigned PtrWidth = getContext().getTargetInfo().getPointerWidth(0);
4564 llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004565
4566 if (TypeAlign > MinABIStackAlignInBytes) {
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004567 llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
4568 llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
4569 llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
4570 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004571 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
4572 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
4573 }
4574 else
4575 AddrTyped = Builder.CreateBitCast(Addr, PTy);
4576
4577 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004578 TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004579 uint64_t Offset =
4580 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
4581 llvm::Value *NextAddr =
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004582 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004583 "ap.next");
4584 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
4585
4586 return AddrTyped;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004587}
4588
John McCallaeeb7012010-05-27 06:19:26 +00004589bool
4590MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4591 llvm::Value *Address) const {
4592 // This information comes from gcc's implementation, which seems to
4593 // as canonical as it gets.
4594
John McCallaeeb7012010-05-27 06:19:26 +00004595 // Everything on MIPS is 4 bytes. Double-precision FP registers
4596 // are aliased to pairs of single-precision FP registers.
Chris Lattner8b418682012-02-07 00:39:47 +00004597 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCallaeeb7012010-05-27 06:19:26 +00004598
4599 // 0-31 are the general purpose registers, $0 - $31.
4600 // 32-63 are the floating-point registers, $f0 - $f31.
4601 // 64 and 65 are the multiply/divide registers, $hi and $lo.
4602 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattner8b418682012-02-07 00:39:47 +00004603 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCallaeeb7012010-05-27 06:19:26 +00004604
4605 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
4606 // They are one bit wide and ignored here.
4607
4608 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
4609 // (coprocessor 1 is the FP unit)
4610 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
4611 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
4612 // 176-181 are the DSP accumulator registers.
Chris Lattner8b418682012-02-07 00:39:47 +00004613 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCallaeeb7012010-05-27 06:19:26 +00004614 return false;
4615}
4616
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004617//===----------------------------------------------------------------------===//
4618// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
4619// Currently subclassed only to implement custom OpenCL C function attribute
4620// handling.
4621//===----------------------------------------------------------------------===//
4622
4623namespace {
4624
4625class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
4626public:
4627 TCETargetCodeGenInfo(CodeGenTypes &CGT)
4628 : DefaultTargetCodeGenInfo(CGT) {}
4629
4630 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4631 CodeGen::CodeGenModule &M) const;
4632};
4633
4634void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4635 llvm::GlobalValue *GV,
4636 CodeGen::CodeGenModule &M) const {
4637 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4638 if (!FD) return;
4639
4640 llvm::Function *F = cast<llvm::Function>(GV);
4641
David Blaikie4e4d0842012-03-11 07:00:24 +00004642 if (M.getLangOpts().OpenCL) {
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004643 if (FD->hasAttr<OpenCLKernelAttr>()) {
4644 // OpenCL C Kernel functions are not subject to inlining
Bill Wendling72390b32012-12-20 19:27:06 +00004645 F->addFnAttr(llvm::Attribute::NoInline);
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004646
4647 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
4648
4649 // Convert the reqd_work_group_size() attributes to metadata.
4650 llvm::LLVMContext &Context = F->getContext();
4651 llvm::NamedMDNode *OpenCLMetadata =
4652 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
4653
4654 SmallVector<llvm::Value*, 5> Operands;
4655 Operands.push_back(F);
4656
Chris Lattner8b418682012-02-07 00:39:47 +00004657 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4658 llvm::APInt(32,
4659 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
4660 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4661 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004662 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
Chris Lattner8b418682012-02-07 00:39:47 +00004663 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4664 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004665 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
4666
4667 // Add a boolean constant operand for "required" (true) or "hint" (false)
4668 // for implementing the work_group_size_hint attr later. Currently
4669 // always true as the hint is not yet implemented.
Chris Lattner8b418682012-02-07 00:39:47 +00004670 Operands.push_back(llvm::ConstantInt::getTrue(Context));
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004671 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
4672 }
4673 }
4674 }
4675}
4676
4677}
John McCallaeeb7012010-05-27 06:19:26 +00004678
Tony Linthicum96319392011-12-12 21:14:55 +00004679//===----------------------------------------------------------------------===//
4680// Hexagon ABI Implementation
4681//===----------------------------------------------------------------------===//
4682
4683namespace {
4684
4685class HexagonABIInfo : public ABIInfo {
4686
4687
4688public:
4689 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
4690
4691private:
4692
4693 ABIArgInfo classifyReturnType(QualType RetTy) const;
4694 ABIArgInfo classifyArgumentType(QualType RetTy) const;
4695
4696 virtual void computeInfo(CGFunctionInfo &FI) const;
4697
4698 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4699 CodeGenFunction &CGF) const;
4700};
4701
4702class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
4703public:
4704 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
4705 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
4706
4707 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
4708 return 29;
4709 }
4710};
4711
4712}
4713
4714void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
4715 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4716 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4717 it != ie; ++it)
4718 it->info = classifyArgumentType(it->type);
4719}
4720
4721ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
4722 if (!isAggregateTypeForABI(Ty)) {
4723 // Treat an enum type as its underlying type.
4724 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4725 Ty = EnumTy->getDecl()->getIntegerType();
4726
4727 return (Ty->isPromotableIntegerType() ?
4728 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4729 }
4730
4731 // Ignore empty records.
4732 if (isEmptyRecord(getContext(), Ty, true))
4733 return ABIArgInfo::getIgnore();
4734
4735 // Structures with either a non-trivial destructor or a non-trivial
4736 // copy constructor are always indirect.
4737 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
4738 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
4739
4740 uint64_t Size = getContext().getTypeSize(Ty);
4741 if (Size > 64)
4742 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
4743 // Pass in the smallest viable integer type.
4744 else if (Size > 32)
4745 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
4746 else if (Size > 16)
4747 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
4748 else if (Size > 8)
4749 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
4750 else
4751 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
4752}
4753
4754ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
4755 if (RetTy->isVoidType())
4756 return ABIArgInfo::getIgnore();
4757
4758 // Large vector types should be returned via memory.
4759 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
4760 return ABIArgInfo::getIndirect(0);
4761
4762 if (!isAggregateTypeForABI(RetTy)) {
4763 // Treat an enum type as its underlying type.
4764 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4765 RetTy = EnumTy->getDecl()->getIntegerType();
4766
4767 return (RetTy->isPromotableIntegerType() ?
4768 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4769 }
4770
4771 // Structures with either a non-trivial destructor or a non-trivial
4772 // copy constructor are always indirect.
4773 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
4774 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
4775
4776 if (isEmptyRecord(getContext(), RetTy, true))
4777 return ABIArgInfo::getIgnore();
4778
4779 // Aggregates <= 8 bytes are returned in r0; other aggregates
4780 // are returned indirectly.
4781 uint64_t Size = getContext().getTypeSize(RetTy);
4782 if (Size <= 64) {
4783 // Return in the smallest viable integer type.
4784 if (Size <= 8)
4785 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
4786 if (Size <= 16)
4787 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
4788 if (Size <= 32)
4789 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
4790 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
4791 }
4792
4793 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
4794}
4795
4796llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner8b418682012-02-07 00:39:47 +00004797 CodeGenFunction &CGF) const {
Tony Linthicum96319392011-12-12 21:14:55 +00004798 // FIXME: Need to handle alignment
Chris Lattner8b418682012-02-07 00:39:47 +00004799 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Tony Linthicum96319392011-12-12 21:14:55 +00004800
4801 CGBuilderTy &Builder = CGF.Builder;
4802 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
4803 "ap");
4804 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
4805 llvm::Type *PTy =
4806 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
4807 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
4808
4809 uint64_t Offset =
4810 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
4811 llvm::Value *NextAddr =
4812 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
4813 "ap.next");
4814 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
4815
4816 return AddrTyped;
4817}
4818
4819
Chris Lattnerea044322010-07-29 02:01:43 +00004820const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004821 if (TheTargetCodeGenInfo)
4822 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004823
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004824 const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00004825 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004826 default:
Chris Lattnerea044322010-07-29 02:01:43 +00004827 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004828
Derek Schuff9ed63f82012-09-06 17:37:28 +00004829 case llvm::Triple::le32:
4830 return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
John McCallaeeb7012010-05-27 06:19:26 +00004831 case llvm::Triple::mips:
4832 case llvm::Triple::mipsel:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004833 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCallaeeb7012010-05-27 06:19:26 +00004834
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00004835 case llvm::Triple::mips64:
4836 case llvm::Triple::mips64el:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004837 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00004838
Tim Northoverc264e162013-01-31 12:13:10 +00004839 case llvm::Triple::aarch64:
4840 return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types));
4841
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004842 case llvm::Triple::arm:
4843 case llvm::Triple::thumb:
Sandeep Patel34c1af82011-04-05 00:23:47 +00004844 {
4845 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004846 if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
Sandeep Patel34c1af82011-04-05 00:23:47 +00004847 Kind = ARMABIInfo::APCS;
David Tweedb16abb12012-10-25 13:33:01 +00004848 else if (CodeGenOpts.FloatABI == "hard" ||
4849 (CodeGenOpts.FloatABI != "soft" && Triple.getEnvironment()==llvm::Triple::GNUEABIHF))
Sandeep Patel34c1af82011-04-05 00:23:47 +00004850 Kind = ARMABIInfo::AAPCS_VFP;
4851
Derek Schuff263366f2012-10-16 22:30:41 +00004852 switch (Triple.getOS()) {
Eli Bendersky441d9f72012-12-04 18:38:10 +00004853 case llvm::Triple::NaCl:
Derek Schuff263366f2012-10-16 22:30:41 +00004854 return *(TheTargetCodeGenInfo =
4855 new NaClARMTargetCodeGenInfo(Types, Kind));
4856 default:
4857 return *(TheTargetCodeGenInfo =
4858 new ARMTargetCodeGenInfo(Types, Kind));
4859 }
Sandeep Patel34c1af82011-04-05 00:23:47 +00004860 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004861
John McCallec853ba2010-03-11 00:10:12 +00004862 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00004863 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
Roman Divacky0fbc4b92012-05-09 18:22:46 +00004864 case llvm::Triple::ppc64:
Bill Schmidt2fc107f2012-10-03 19:18:57 +00004865 if (Triple.isOSBinFormatELF())
4866 return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types));
4867 else
4868 return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00004869
Peter Collingbourneedb66f32012-05-20 23:28:41 +00004870 case llvm::Triple::nvptx:
4871 case llvm::Triple::nvptx64:
Justin Holewinski2c585b92012-05-24 17:43:12 +00004872 return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types));
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004873
Wesley Peck276fdf42010-12-19 19:57:51 +00004874 case llvm::Triple::mblaze:
4875 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
4876
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004877 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00004878 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004879
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004880 case llvm::Triple::tce:
4881 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
4882
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004883 case llvm::Triple::x86: {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004884 bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004885
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00004886 if (Triple.isOSDarwin())
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004887 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004888 new X86_32TargetCodeGenInfo(Types, true, true, DisableMMX, false,
4889 CodeGenOpts.NumRegisterParameters));
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00004890
4891 switch (Triple.getOS()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004892 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004893 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00004894 case llvm::Triple::AuroraUX:
4895 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00004896 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004897 case llvm::Triple::OpenBSD:
Eli Friedman42f74f22012-08-08 23:57:20 +00004898 case llvm::Triple::Bitrig:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004899 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004900 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX,
4901 false,
4902 CodeGenOpts.NumRegisterParameters));
Eli Friedman55fc7e22012-01-25 22:46:34 +00004903
4904 case llvm::Triple::Win32:
4905 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004906 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX, true,
4907 CodeGenOpts.NumRegisterParameters));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004908
4909 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004910 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004911 new X86_32TargetCodeGenInfo(Types, false, false, DisableMMX,
4912 false,
4913 CodeGenOpts.NumRegisterParameters));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004914 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004915 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004916
Eli Friedmanee1ad992011-12-02 00:11:43 +00004917 case llvm::Triple::x86_64: {
4918 bool HasAVX = strcmp(getContext().getTargetInfo().getABI(), "avx") == 0;
4919
Chris Lattnerf13721d2010-08-31 16:44:54 +00004920 switch (Triple.getOS()) {
4921 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00004922 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00004923 case llvm::Triple::Cygwin:
4924 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
Eli Bendersky441d9f72012-12-04 18:38:10 +00004925 case llvm::Triple::NaCl:
Derek Schuff263366f2012-10-16 22:30:41 +00004926 return *(TheTargetCodeGenInfo = new NaClX86_64TargetCodeGenInfo(Types, HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00004927 default:
Eli Friedmanee1ad992011-12-02 00:11:43 +00004928 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
4929 HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00004930 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004931 }
Tony Linthicum96319392011-12-12 21:14:55 +00004932 case llvm::Triple::hexagon:
4933 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
Eli Friedmanee1ad992011-12-02 00:11:43 +00004934 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004935}