blob: 9ad17a454efc94df808d1d056f3bc172f8cd3b0d [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"
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +000017#include "CGCXXABI.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000018#include "CodeGenFunction.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000019#include "clang/AST/RecordLayout.h"
Sandeep Patel34c1af82011-04-05 00:23:47 +000020#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbar2c0843f2009-08-24 08:52:16 +000021#include "llvm/ADT/Triple.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000022#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/Type.h"
Daniel Dunbar28df7a52009-12-03 09:13:49 +000024#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000025using namespace clang;
26using namespace CodeGen;
27
John McCallaeeb7012010-05-27 06:19:26 +000028static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
29 llvm::Value *Array,
30 llvm::Value *Value,
31 unsigned FirstIndex,
32 unsigned LastIndex) {
33 // Alternatively, we could emit this as a loop in the source.
34 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
35 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
36 Builder.CreateStore(Value, Cell);
37 }
38}
39
John McCalld608cdb2010-08-22 10:59:02 +000040static bool isAggregateTypeForABI(QualType T) {
John McCall9d232c82013-03-07 21:37:08 +000041 return !CodeGenFunction::hasScalarEvaluationKind(T) ||
John McCalld608cdb2010-08-22 10:59:02 +000042 T->isMemberFunctionPointerType();
43}
44
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000045ABIInfo::~ABIInfo() {}
46
Mark Lacey23630722013-10-06 01:33:34 +000047static bool isRecordReturnIndirect(const RecordType *RT,
48 CGCXXABI &CXXABI) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +000049 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
50 if (!RD)
51 return false;
Mark Lacey23630722013-10-06 01:33:34 +000052 return CXXABI.isReturnTypeIndirect(RD);
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +000053}
54
55
Mark Lacey23630722013-10-06 01:33:34 +000056static bool isRecordReturnIndirect(QualType T, CGCXXABI &CXXABI) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +000057 const RecordType *RT = T->getAs<RecordType>();
58 if (!RT)
59 return false;
Mark Lacey23630722013-10-06 01:33:34 +000060 return isRecordReturnIndirect(RT, CXXABI);
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +000061}
62
63static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT,
Mark Lacey23630722013-10-06 01:33:34 +000064 CGCXXABI &CXXABI) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +000065 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
66 if (!RD)
67 return CGCXXABI::RAA_Default;
Mark Lacey23630722013-10-06 01:33:34 +000068 return CXXABI.getRecordArgABI(RD);
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +000069}
70
71static CGCXXABI::RecordArgABI getRecordArgABI(QualType T,
Mark Lacey23630722013-10-06 01:33:34 +000072 CGCXXABI &CXXABI) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +000073 const RecordType *RT = T->getAs<RecordType>();
74 if (!RT)
75 return CGCXXABI::RAA_Default;
Mark Lacey23630722013-10-06 01:33:34 +000076 return getRecordArgABI(RT, CXXABI);
77}
78
79CGCXXABI &ABIInfo::getCXXABI() const {
80 return CGT.getCXXABI();
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +000081}
82
Chris Lattnerea044322010-07-29 02:01:43 +000083ASTContext &ABIInfo::getContext() const {
84 return CGT.getContext();
85}
86
87llvm::LLVMContext &ABIInfo::getVMContext() const {
88 return CGT.getLLVMContext();
89}
90
Micah Villmow25a6a842012-10-08 16:25:52 +000091const llvm::DataLayout &ABIInfo::getDataLayout() const {
92 return CGT.getDataLayout();
Chris Lattnerea044322010-07-29 02:01:43 +000093}
94
John McCall64aa4b32013-04-16 22:48:15 +000095const TargetInfo &ABIInfo::getTarget() const {
96 return CGT.getTarget();
97}
Chris Lattnerea044322010-07-29 02:01:43 +000098
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000099void ABIArgInfo::dump() const {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000100 raw_ostream &OS = llvm::errs();
Daniel Dunbar28df7a52009-12-03 09:13:49 +0000101 OS << "(ABIArgInfo Kind=";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000102 switch (TheKind) {
103 case Direct:
Chris Lattner800588f2010-07-29 06:26:06 +0000104 OS << "Direct Type=";
Chris Lattner2acc6e32011-07-18 04:24:23 +0000105 if (llvm::Type *Ty = getCoerceToType())
Chris Lattner800588f2010-07-29 06:26:06 +0000106 Ty->print(OS);
107 else
108 OS << "null";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000109 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000110 case Extend:
Daniel Dunbar28df7a52009-12-03 09:13:49 +0000111 OS << "Extend";
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000112 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000113 case Ignore:
Daniel Dunbar28df7a52009-12-03 09:13:49 +0000114 OS << "Ignore";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000115 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000116 case Indirect:
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000117 OS << "Indirect Align=" << getIndirectAlign()
Joerg Sonnenbergere9b5d772011-07-15 18:23:44 +0000118 << " ByVal=" << getIndirectByVal()
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000119 << " Realign=" << getIndirectRealign();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000120 break;
121 case Expand:
Daniel Dunbar28df7a52009-12-03 09:13:49 +0000122 OS << "Expand";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000123 break;
124 }
Daniel Dunbar28df7a52009-12-03 09:13:49 +0000125 OS << ")\n";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000126}
127
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000128TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
129
John McCall49e34be2011-08-30 01:42:09 +0000130// If someone can figure out a general rule for this, that would be great.
131// It's probably just doomed to be platform-dependent, though.
132unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
133 // Verified for:
134 // x86-64 FreeBSD, Linux, Darwin
135 // x86-32 FreeBSD, Linux, Darwin
136 // PowerPC Linux, Darwin
137 // ARM Darwin (*not* EABI)
Tim Northoverc264e162013-01-31 12:13:10 +0000138 // AArch64 Linux
John McCall49e34be2011-08-30 01:42:09 +0000139 return 32;
140}
141
John McCallde5d3c72012-02-17 03:33:10 +0000142bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
143 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +0000144 // The following conventions are known to require this to be false:
145 // x86_stdcall
146 // MIPS
147 // For everything else, we just prefer false unless we opt out.
148 return false;
149}
150
Reid Kleckner3190ca92013-05-08 13:44:39 +0000151void
152TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib,
153 llvm::SmallString<24> &Opt) const {
154 // This assumes the user is passing a library name like "rt" instead of a
155 // filename like "librt.a/so", and that they don't care whether it's static or
156 // dynamic.
157 Opt = "-l";
158 Opt += Lib;
159}
160
Daniel Dunbar98303b92009-09-13 08:03:58 +0000161static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000162
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000163/// isEmptyField - Return true iff a the field is "empty", that is it
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000164/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +0000165static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
166 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000167 if (FD->isUnnamedBitfield())
168 return true;
169
170 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000171
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000172 // Constant arrays of empty records count as empty, strip them off.
173 // Constant arrays of zero length always count as empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000174 if (AllowArrays)
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000175 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
176 if (AT->getSize() == 0)
177 return true;
Daniel Dunbar98303b92009-09-13 08:03:58 +0000178 FT = AT->getElementType();
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000179 }
Daniel Dunbar98303b92009-09-13 08:03:58 +0000180
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000181 const RecordType *RT = FT->getAs<RecordType>();
182 if (!RT)
183 return false;
184
185 // C++ record fields are never empty, at least in the Itanium ABI.
186 //
187 // FIXME: We should use a predicate for whether this behavior is true in the
188 // current ABI.
189 if (isa<CXXRecordDecl>(RT->getDecl()))
190 return false;
191
Daniel Dunbar98303b92009-09-13 08:03:58 +0000192 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000193}
194
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000195/// isEmptyRecord - Return true iff a structure contains only empty
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000196/// fields. Note that a structure with a flexible array member is not
197/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000198static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000199 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000200 if (!RT)
201 return 0;
202 const RecordDecl *RD = RT->getDecl();
203 if (RD->hasFlexibleArrayMember())
204 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000205
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000206 // If this is a C++ record, check the bases first.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000207 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000208 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
209 e = CXXRD->bases_end(); i != e; ++i)
210 if (!isEmptyRecord(Context, i->getType(), true))
211 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000212
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000213 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
214 i != e; ++i)
David Blaikie581deb32012-06-06 20:45:41 +0000215 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000216 return false;
217 return true;
218}
219
220/// isSingleElementStruct - Determine if a structure is a "single
221/// element struct", i.e. it has exactly one non-empty field or
222/// exactly one field which is itself a single element
223/// struct. Structures with flexible array members are never
224/// considered single element structs.
225///
226/// \return The field declaration for the single non-empty field, if
227/// it exists.
228static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
229 const RecordType *RT = T->getAsStructureType();
230 if (!RT)
231 return 0;
232
233 const RecordDecl *RD = RT->getDecl();
234 if (RD->hasFlexibleArrayMember())
235 return 0;
236
237 const Type *Found = 0;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000238
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000239 // If this is a C++ record, check the bases first.
240 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
241 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
242 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000243 // Ignore empty records.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000244 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000245 continue;
246
247 // If we already found an element then this isn't a single-element struct.
248 if (Found)
249 return 0;
250
251 // If this is non-empty and not a single element struct, the composite
252 // cannot be a single element struct.
253 Found = isSingleElementStruct(i->getType(), Context);
254 if (!Found)
255 return 0;
256 }
257 }
258
259 // Check for single element.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000260 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
261 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000262 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000263 QualType FT = FD->getType();
264
265 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000266 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000267 continue;
268
269 // If we already found an element then this isn't a single-element
270 // struct.
271 if (Found)
272 return 0;
273
274 // Treat single element arrays as the element.
275 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
276 if (AT->getSize().getZExtValue() != 1)
277 break;
278 FT = AT->getElementType();
279 }
280
John McCalld608cdb2010-08-22 10:59:02 +0000281 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000282 Found = FT.getTypePtr();
283 } else {
284 Found = isSingleElementStruct(FT, Context);
285 if (!Found)
286 return 0;
287 }
288 }
289
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000290 // We don't consider a struct a single-element struct if it has
291 // padding beyond the element type.
292 if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
293 return 0;
294
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000295 return Found;
296}
297
298static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Eli Friedmandb748a32012-11-29 23:21:04 +0000299 // Treat complex types as the element type.
300 if (const ComplexType *CTy = Ty->getAs<ComplexType>())
301 Ty = CTy->getElementType();
302
303 // Check for a type which we know has a simple scalar argument-passing
304 // convention without any padding. (We're specifically looking for 32
305 // and 64-bit integer and integer-equivalents, float, and double.)
Daniel Dunbara1842d32010-05-14 03:40:53 +0000306 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Eli Friedmandb748a32012-11-29 23:21:04 +0000307 !Ty->isEnumeralType() && !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000308 return false;
309
310 uint64_t Size = Context.getTypeSize(Ty);
311 return Size == 32 || Size == 64;
312}
313
Daniel Dunbar53012f42009-11-09 01:33:53 +0000314/// canExpandIndirectArgument - Test whether an argument type which is to be
315/// passed indirectly (on the stack) would have the equivalent layout if it was
316/// expanded into separate arguments. If so, we prefer to do the latter to avoid
317/// inhibiting optimizations.
318///
319// FIXME: This predicate is missing many cases, currently it just follows
320// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
321// should probably make this smarter, or better yet make the LLVM backend
322// capable of handling it.
323static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
324 // We can only expand structure types.
325 const RecordType *RT = Ty->getAs<RecordType>();
326 if (!RT)
327 return false;
328
329 // We can only expand (C) structures.
330 //
331 // FIXME: This needs to be generalized to handle classes as well.
332 const RecordDecl *RD = RT->getDecl();
333 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
334 return false;
335
Eli Friedman506d4e32011-11-18 01:32:26 +0000336 uint64_t Size = 0;
337
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000338 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
339 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000340 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000341
342 if (!is32Or64BitBasicType(FD->getType(), Context))
343 return false;
344
345 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
346 // how to expand them yet, and the predicate for telling if a bitfield still
347 // counts as "basic" is more complicated than what we were doing previously.
348 if (FD->isBitField())
349 return false;
Eli Friedman506d4e32011-11-18 01:32:26 +0000350
351 Size += Context.getTypeSize(FD->getType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000352 }
353
Eli Friedman506d4e32011-11-18 01:32:26 +0000354 // Make sure there are not any holes in the struct.
355 if (Size != Context.getTypeSize(Ty))
356 return false;
357
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000358 return true;
359}
360
361namespace {
362/// DefaultABIInfo - The default implementation for ABI specific
363/// details. This implementation provides information which results in
364/// self-consistent and sensible LLVM IR generation, but does not
365/// conform to any particular ABI.
366class DefaultABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +0000367public:
368 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000369
Chris Lattnera3c109b2010-07-29 02:16:43 +0000370 ABIArgInfo classifyReturnType(QualType RetTy) const;
371 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000372
Chris Lattneree5dcd02010-07-29 02:31:05 +0000373 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000374 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000375 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
376 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000377 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000378 }
379
380 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
381 CodeGenFunction &CGF) const;
382};
383
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000384class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
385public:
Chris Lattnerea044322010-07-29 02:01:43 +0000386 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
387 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000388};
389
390llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
391 CodeGenFunction &CGF) const {
392 return 0;
393}
394
Chris Lattnera3c109b2010-07-29 02:16:43 +0000395ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
Jan Wen Voung90306932011-11-03 00:59:44 +0000396 if (isAggregateTypeForABI(Ty)) {
397 // Records with non trivial destructors/constructors should not be passed
398 // by value.
Mark Lacey23630722013-10-06 01:33:34 +0000399 if (isRecordReturnIndirect(Ty, getCXXABI()))
Jan Wen Voung90306932011-11-03 00:59:44 +0000400 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
401
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000402 return ABIArgInfo::getIndirect(0);
Jan Wen Voung90306932011-11-03 00:59:44 +0000403 }
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000404
Chris Lattnera14db752010-03-11 18:19:55 +0000405 // Treat an enum type as its underlying type.
406 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
407 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000408
Chris Lattnera14db752010-03-11 18:19:55 +0000409 return (Ty->isPromotableIntegerType() ?
410 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000411}
412
Bob Wilson0024f942011-01-10 23:54:17 +0000413ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
414 if (RetTy->isVoidType())
415 return ABIArgInfo::getIgnore();
416
417 if (isAggregateTypeForABI(RetTy))
418 return ABIArgInfo::getIndirect(0);
419
420 // Treat an enum type as its underlying type.
421 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
422 RetTy = EnumTy->getDecl()->getIntegerType();
423
424 return (RetTy->isPromotableIntegerType() ?
425 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
426}
427
Derek Schuff9ed63f82012-09-06 17:37:28 +0000428//===----------------------------------------------------------------------===//
429// le32/PNaCl bitcode ABI Implementation
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000430//
431// This is a simplified version of the x86_32 ABI. Arguments and return values
432// are always passed on the stack.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000433//===----------------------------------------------------------------------===//
434
435class PNaClABIInfo : public ABIInfo {
436 public:
437 PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
438
439 ABIArgInfo classifyReturnType(QualType RetTy) const;
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000440 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Derek Schuff9ed63f82012-09-06 17:37:28 +0000441
442 virtual void computeInfo(CGFunctionInfo &FI) const;
443 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
444 CodeGenFunction &CGF) const;
445};
446
447class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
448 public:
449 PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
450 : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {}
451};
452
453void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
454 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
455
Derek Schuff9ed63f82012-09-06 17:37:28 +0000456 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
457 it != ie; ++it)
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000458 it->info = classifyArgumentType(it->type);
Derek Schuff9ed63f82012-09-06 17:37:28 +0000459 }
460
461llvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
462 CodeGenFunction &CGF) const {
463 return 0;
464}
465
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000466/// \brief Classify argument of given type \p Ty.
467ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const {
Derek Schuff9ed63f82012-09-06 17:37:28 +0000468 if (isAggregateTypeForABI(Ty)) {
Mark Lacey23630722013-10-06 01:33:34 +0000469 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000470 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Derek Schuff9ed63f82012-09-06 17:37:28 +0000471 return ABIArgInfo::getIndirect(0);
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000472 } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
473 // Treat an enum type as its underlying type.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000474 Ty = EnumTy->getDecl()->getIntegerType();
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000475 } else if (Ty->isFloatingType()) {
476 // Floating-point types don't go inreg.
477 return ABIArgInfo::getDirect();
Derek Schuff9ed63f82012-09-06 17:37:28 +0000478 }
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000479
480 return (Ty->isPromotableIntegerType() ?
481 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Derek Schuff9ed63f82012-09-06 17:37:28 +0000482}
483
484ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
485 if (RetTy->isVoidType())
486 return ABIArgInfo::getIgnore();
487
Eli Benderskye45dfd12013-04-04 22:49:35 +0000488 // In the PNaCl ABI we always return records/structures on the stack.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000489 if (isAggregateTypeForABI(RetTy))
490 return ABIArgInfo::getIndirect(0);
491
492 // Treat an enum type as its underlying type.
493 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
494 RetTy = EnumTy->getDecl()->getIntegerType();
495
496 return (RetTy->isPromotableIntegerType() ?
497 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
498}
499
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000500/// IsX86_MMXType - Return true if this is an MMX type.
501bool IsX86_MMXType(llvm::Type *IRType) {
502 // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>.
Bill Wendlingbb465d72010-10-18 03:41:31 +0000503 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
504 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
505 IRType->getScalarSizeInBits() != 64;
506}
507
Jay Foadef6de3d2011-07-11 09:56:20 +0000508static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000509 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000510 llvm::Type* Ty) {
Tim Northover1bea6532013-06-07 00:04:50 +0000511 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) {
512 if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) {
513 // Invalid MMX constraint
514 return 0;
515 }
516
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000517 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
Tim Northover1bea6532013-06-07 00:04:50 +0000518 }
519
520 // No operation needed
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000521 return Ty;
522}
523
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000524//===----------------------------------------------------------------------===//
525// X86-32 ABI Implementation
526//===----------------------------------------------------------------------===//
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000527
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000528/// X86_32ABIInfo - The X86-32 ABI information.
529class X86_32ABIInfo : public ABIInfo {
Rafael Espindolab48280b2012-07-31 02:44:24 +0000530 enum Class {
531 Integer,
532 Float
533 };
534
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000535 static const unsigned MinABIStackAlignInBytes = 4;
536
David Chisnall1e4249c2009-08-17 23:08:21 +0000537 bool IsDarwinVectorABI;
538 bool IsSmallStructInRegABI;
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000539 bool IsWin32StructABI;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000540 unsigned DefaultNumRegisterParameters;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000541
542 static bool isRegisterSize(unsigned Size) {
543 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
544 }
545
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000546 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context,
547 unsigned callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000548
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000549 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
550 /// such that the argument will be passed in memory.
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000551 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal,
552 unsigned &FreeRegs) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000553
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000554 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbare59d8582010-09-16 20:42:06 +0000555 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000556
Rafael Espindolab48280b2012-07-31 02:44:24 +0000557 Class classify(QualType Ty) const;
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000558 ABIArgInfo classifyReturnType(QualType RetTy,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000559 unsigned callingConvention) const;
Rafael Espindolab6932692012-10-24 01:58:58 +0000560 ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &FreeRegs,
561 bool IsFastCall) const;
562 bool shouldUseInReg(QualType Ty, unsigned &FreeRegs,
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000563 bool IsFastCall, bool &NeedsPadding) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000564
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000565public:
566
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000567 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000568 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
569 CodeGenFunction &CGF) const;
570
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000571 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool w,
Rafael Espindolab48280b2012-07-31 02:44:24 +0000572 unsigned r)
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000573 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000574 IsWin32StructABI(w), DefaultNumRegisterParameters(r) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000575};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000576
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000577class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
578public:
Eli Friedman55fc7e22012-01-25 22:46:34 +0000579 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000580 bool d, bool p, bool w, unsigned r)
581 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, w, r)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000582
John McCallb8b52972013-06-18 02:46:29 +0000583 static bool isStructReturnInRegABI(
584 const llvm::Triple &Triple, const CodeGenOptions &Opts);
585
Charles Davis74f72932010-02-13 15:54:06 +0000586 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
587 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000588
589 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
590 // Darwin uses different dwarf register numbers for EH.
John McCall64aa4b32013-04-16 22:48:15 +0000591 if (CGM.getTarget().getTriple().isOSDarwin()) return 5;
John McCall6374c332010-03-06 00:35:14 +0000592 return 4;
593 }
594
595 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
596 llvm::Value *Address) const;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000597
Jay Foadef6de3d2011-07-11 09:56:20 +0000598 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000599 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000600 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000601 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
602 }
603
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000604};
605
606}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000607
608/// shouldReturnTypeInRegister - Determine if the given type should be
609/// passed in a register (for the Darwin ABI).
610bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000611 ASTContext &Context,
612 unsigned callingConvention) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000613 uint64_t Size = Context.getTypeSize(Ty);
614
615 // Type must be register sized.
616 if (!isRegisterSize(Size))
617 return false;
618
619 if (Ty->isVectorType()) {
620 // 64- and 128- bit vectors inside structures are not returned in
621 // registers.
622 if (Size == 64 || Size == 128)
623 return false;
624
625 return true;
626 }
627
Daniel Dunbar77115232010-05-15 00:00:30 +0000628 // If this is a builtin, pointer, enum, complex type, member pointer, or
629 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000630 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000631 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000632 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000633 return true;
634
635 // Arrays are treated like records.
636 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000637 return shouldReturnTypeInRegister(AT->getElementType(), Context,
638 callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000639
640 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000641 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000642 if (!RT) return false;
643
Anders Carlssona8874232010-01-27 03:25:19 +0000644 // FIXME: Traverse bases here too.
645
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000646 // For thiscall conventions, structures will never be returned in
647 // a register. This is for compatibility with the MSVC ABI
648 if (callingConvention == llvm::CallingConv::X86_ThisCall &&
649 RT->isStructureType()) {
650 return false;
651 }
652
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000653 // Structure types are passed in register if all fields would be
654 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000655 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
656 e = RT->getDecl()->field_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000657 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000658
659 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000660 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000661 continue;
662
663 // Check fields recursively.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000664 if (!shouldReturnTypeInRegister(FD->getType(), Context,
665 callingConvention))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000666 return false;
667 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000668 return true;
669}
670
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000671ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
672 unsigned callingConvention) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000673 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000674 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000675
Chris Lattnera3c109b2010-07-29 02:16:43 +0000676 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000677 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000678 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000679 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000680
681 // 128-bit vectors are a special case; they are returned in
682 // registers and we need to make sure to pick a type the LLVM
683 // backend will like.
684 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +0000685 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000686 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000687
688 // Always return in register if it fits in a general purpose
689 // register, or if it is 64 bits and has a single element.
690 if ((Size == 8 || Size == 16 || Size == 32) ||
691 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +0000692 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000693 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000694
695 return ABIArgInfo::getIndirect(0);
696 }
697
698 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000699 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000700
John McCalld608cdb2010-08-22 10:59:02 +0000701 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000702 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Mark Lacey23630722013-10-06 01:33:34 +0000703 if (isRecordReturnIndirect(RT, getCXXABI()))
Anders Carlsson40092972009-10-20 22:07:59 +0000704 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000705
Anders Carlsson40092972009-10-20 22:07:59 +0000706 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000707 if (RT->getDecl()->hasFlexibleArrayMember())
708 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000709 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000710
David Chisnall1e4249c2009-08-17 23:08:21 +0000711 // If specified, structs and unions are always indirect.
712 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000713 return ABIArgInfo::getIndirect(0);
714
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000715 // Small structures which are register sized are generally returned
716 // in a register.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000717 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(),
718 callingConvention)) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000719 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000720
721 // As a special-case, if the struct is a "single-element" struct, and
722 // the field is of type "float" or "double", return it in a
Eli Friedman55fc7e22012-01-25 22:46:34 +0000723 // floating-point register. (MSVC does not apply this special case.)
724 // We apply a similar transformation for pointer types to improve the
725 // quality of the generated IR.
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000726 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000727 if ((!IsWin32StructABI && SeltTy->isRealFloatingType())
Eli Friedman55fc7e22012-01-25 22:46:34 +0000728 || SeltTy->hasPointerRepresentation())
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000729 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
730
731 // FIXME: We should be able to narrow this integer in cases with dead
732 // padding.
Chris Lattner800588f2010-07-29 06:26:06 +0000733 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000734 }
735
736 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000737 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000738
Chris Lattnera3c109b2010-07-29 02:16:43 +0000739 // Treat an enum type as its underlying type.
740 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
741 RetTy = EnumTy->getDecl()->getIntegerType();
742
743 return (RetTy->isPromotableIntegerType() ?
744 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000745}
746
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000747static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
748 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
749}
750
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000751static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
752 const RecordType *RT = Ty->getAs<RecordType>();
753 if (!RT)
754 return 0;
755 const RecordDecl *RD = RT->getDecl();
756
757 // If this is a C++ record, check the bases first.
758 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
759 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
760 e = CXXRD->bases_end(); i != e; ++i)
761 if (!isRecordWithSSEVectorType(Context, i->getType()))
762 return false;
763
764 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
765 i != e; ++i) {
766 QualType FT = i->getType();
767
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000768 if (isSSEVectorType(Context, FT))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000769 return true;
770
771 if (isRecordWithSSEVectorType(Context, FT))
772 return true;
773 }
774
775 return false;
776}
777
Daniel Dunbare59d8582010-09-16 20:42:06 +0000778unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
779 unsigned Align) const {
780 // Otherwise, if the alignment is less than or equal to the minimum ABI
781 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000782 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +0000783 return 0; // Use default alignment.
784
785 // On non-Darwin, the stack type alignment is always 4.
786 if (!IsDarwinVectorABI) {
787 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000788 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +0000789 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000790
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000791 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000792 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
793 isRecordWithSSEVectorType(getContext(), Ty)))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000794 return 16;
795
796 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000797}
798
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000799ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
800 unsigned &FreeRegs) const {
801 if (!ByVal) {
802 if (FreeRegs) {
803 --FreeRegs; // Non byval indirects just use one pointer.
804 return ABIArgInfo::getIndirectInReg(0, false);
805 }
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000806 return ABIArgInfo::getIndirect(0, false);
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000807 }
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000808
Daniel Dunbare59d8582010-09-16 20:42:06 +0000809 // Compute the byval alignment.
810 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
811 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
812 if (StackAlign == 0)
Chris Lattnerde92d732011-05-22 23:35:00 +0000813 return ABIArgInfo::getIndirect(4);
Daniel Dunbare59d8582010-09-16 20:42:06 +0000814
815 // If the stack alignment is less than the type alignment, realign the
816 // argument.
817 if (StackAlign < TypeAlign)
818 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
819 /*Realign=*/true);
820
821 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000822}
823
Rafael Espindolab48280b2012-07-31 02:44:24 +0000824X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
825 const Type *T = isSingleElementStruct(Ty, getContext());
826 if (!T)
827 T = Ty.getTypePtr();
828
829 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
830 BuiltinType::Kind K = BT->getKind();
831 if (K == BuiltinType::Float || K == BuiltinType::Double)
832 return Float;
833 }
834 return Integer;
835}
836
Rafael Espindolab6932692012-10-24 01:58:58 +0000837bool X86_32ABIInfo::shouldUseInReg(QualType Ty, unsigned &FreeRegs,
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000838 bool IsFastCall, bool &NeedsPadding) const {
839 NeedsPadding = false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000840 Class C = classify(Ty);
841 if (C == Float)
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000842 return false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000843
Rafael Espindolab6932692012-10-24 01:58:58 +0000844 unsigned Size = getContext().getTypeSize(Ty);
845 unsigned SizeInRegs = (Size + 31) / 32;
Rafael Espindola5f14fcb2012-10-23 02:04:01 +0000846
847 if (SizeInRegs == 0)
848 return false;
849
Rafael Espindolab48280b2012-07-31 02:44:24 +0000850 if (SizeInRegs > FreeRegs) {
851 FreeRegs = 0;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000852 return false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000853 }
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000854
Rafael Espindolab48280b2012-07-31 02:44:24 +0000855 FreeRegs -= SizeInRegs;
Rafael Espindolab6932692012-10-24 01:58:58 +0000856
857 if (IsFastCall) {
858 if (Size > 32)
859 return false;
860
861 if (Ty->isIntegralOrEnumerationType())
862 return true;
863
864 if (Ty->isPointerType())
865 return true;
866
867 if (Ty->isReferenceType())
868 return true;
869
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000870 if (FreeRegs)
871 NeedsPadding = true;
872
Rafael Espindolab6932692012-10-24 01:58:58 +0000873 return false;
874 }
875
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000876 return true;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000877}
878
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000879ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Rafael Espindolab6932692012-10-24 01:58:58 +0000880 unsigned &FreeRegs,
881 bool IsFastCall) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000882 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000883 if (isAggregateTypeForABI(Ty)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000884 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000885 if (IsWin32StructABI)
886 return getIndirectResult(Ty, true, FreeRegs);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000887
Mark Lacey23630722013-10-06 01:33:34 +0000888 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()))
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000889 return getIndirectResult(Ty, RAA == CGCXXABI::RAA_DirectInMemory, FreeRegs);
890
891 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000892 if (RT->getDecl()->hasFlexibleArrayMember())
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000893 return getIndirectResult(Ty, true, FreeRegs);
Anders Carlssona8874232010-01-27 03:25:19 +0000894 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000895
Eli Friedman5a4d3522011-11-18 00:28:11 +0000896 // Ignore empty structs/unions.
Eli Friedman5a1ac892011-11-18 04:01:36 +0000897 if (isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000898 return ABIArgInfo::getIgnore();
899
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000900 llvm::LLVMContext &LLVMContext = getVMContext();
901 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
902 bool NeedsPadding;
903 if (shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding)) {
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000904 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Craig Topperb9bad792013-07-08 04:47:18 +0000905 SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32);
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000906 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
907 return ABIArgInfo::getDirectInReg(Result);
908 }
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000909 llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : 0;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000910
Daniel Dunbar53012f42009-11-09 01:33:53 +0000911 // Expand small (<= 128-bit) record types when we know that the stack layout
912 // of those arguments will match the struct. This is important because the
913 // LLVM backend isn't smart enough to remove byval, which inhibits many
914 // optimizations.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000915 if (getContext().getTypeSize(Ty) <= 4*32 &&
916 canExpandIndirectArgument(Ty, getContext()))
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000917 return ABIArgInfo::getExpandWithPadding(IsFastCall, PaddingType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000918
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000919 return getIndirectResult(Ty, true, FreeRegs);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000920 }
921
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000922 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +0000923 // On Darwin, some vectors are passed in memory, we handle this by passing
924 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000925 if (IsDarwinVectorABI) {
926 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000927 if ((Size == 8 || Size == 16 || Size == 32) ||
928 (Size == 64 && VT->getNumElements() == 1))
929 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
930 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000931 }
Bill Wendlingbb465d72010-10-18 03:41:31 +0000932
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000933 if (IsX86_MMXType(CGT.ConvertType(Ty)))
934 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64));
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000935
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000936 return ABIArgInfo::getDirect();
937 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000938
939
Chris Lattnera3c109b2010-07-29 02:16:43 +0000940 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
941 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000942
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000943 bool NeedsPadding;
944 bool InReg = shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding);
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000945
946 if (Ty->isPromotableIntegerType()) {
947 if (InReg)
948 return ABIArgInfo::getExtendInReg();
949 return ABIArgInfo::getExtend();
950 }
951 if (InReg)
952 return ABIArgInfo::getDirectInReg();
953 return ABIArgInfo::getDirect();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000954}
955
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000956void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
957 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
958 FI.getCallingConvention());
Rafael Espindolab48280b2012-07-31 02:44:24 +0000959
Rafael Espindolab6932692012-10-24 01:58:58 +0000960 unsigned CC = FI.getCallingConvention();
961 bool IsFastCall = CC == llvm::CallingConv::X86_FastCall;
962 unsigned FreeRegs;
963 if (IsFastCall)
964 FreeRegs = 2;
965 else if (FI.getHasRegParm())
966 FreeRegs = FI.getRegParm();
967 else
968 FreeRegs = DefaultNumRegisterParameters;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000969
970 // If the return value is indirect, then the hidden argument is consuming one
971 // integer register.
972 if (FI.getReturnInfo().isIndirect() && FreeRegs) {
973 --FreeRegs;
974 ABIArgInfo &Old = FI.getReturnInfo();
975 Old = ABIArgInfo::getIndirectInReg(Old.getIndirectAlign(),
976 Old.getIndirectByVal(),
977 Old.getIndirectRealign());
978 }
979
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000980 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
981 it != ie; ++it)
Rafael Espindolab6932692012-10-24 01:58:58 +0000982 it->info = classifyArgumentType(it->type, FreeRegs, IsFastCall);
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000983}
984
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000985llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
986 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +0000987 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000988
989 CGBuilderTy &Builder = CGF.Builder;
990 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
991 "ap");
992 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Eli Friedman7b1fb812011-11-18 02:12:09 +0000993
994 // Compute if the address needs to be aligned
995 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
996 Align = getTypeStackAlignInBytes(Ty, Align);
997 Align = std::max(Align, 4U);
998 if (Align > 4) {
999 // addr = (addr + align - 1) & -align;
1000 llvm::Value *Offset =
1001 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
1002 Addr = CGF.Builder.CreateGEP(Addr, Offset);
1003 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
1004 CGF.Int32Ty);
1005 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
1006 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1007 Addr->getType(),
1008 "ap.cur.aligned");
1009 }
1010
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001011 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00001012 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001013 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1014
1015 uint64_t Offset =
Eli Friedman7b1fb812011-11-18 02:12:09 +00001016 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001017 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00001018 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001019 "ap.next");
1020 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1021
1022 return AddrTyped;
1023}
1024
Charles Davis74f72932010-02-13 15:54:06 +00001025void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
1026 llvm::GlobalValue *GV,
1027 CodeGen::CodeGenModule &CGM) const {
1028 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1029 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
1030 // Get the LLVM function.
1031 llvm::Function *Fn = cast<llvm::Function>(GV);
1032
1033 // Now add the 'alignstack' attribute with a value of 16.
Bill Wendling0d583392012-10-15 20:36:26 +00001034 llvm::AttrBuilder B;
Bill Wendlinge91e9ec2012-10-14 03:28:14 +00001035 B.addStackAlignmentAttr(16);
Bill Wendling909b6de2013-01-23 00:21:06 +00001036 Fn->addAttributes(llvm::AttributeSet::FunctionIndex,
1037 llvm::AttributeSet::get(CGM.getLLVMContext(),
1038 llvm::AttributeSet::FunctionIndex,
1039 B));
Charles Davis74f72932010-02-13 15:54:06 +00001040 }
1041 }
1042}
1043
John McCall6374c332010-03-06 00:35:14 +00001044bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
1045 CodeGen::CodeGenFunction &CGF,
1046 llvm::Value *Address) const {
1047 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCall6374c332010-03-06 00:35:14 +00001048
Chris Lattner8b418682012-02-07 00:39:47 +00001049 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001050
John McCall6374c332010-03-06 00:35:14 +00001051 // 0-7 are the eight integer registers; the order is different
1052 // on Darwin (for EH), but the range is the same.
1053 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +00001054 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +00001055
John McCall64aa4b32013-04-16 22:48:15 +00001056 if (CGF.CGM.getTarget().getTriple().isOSDarwin()) {
John McCall6374c332010-03-06 00:35:14 +00001057 // 12-16 are st(0..4). Not sure why we stop at 4.
1058 // These have size 16, which is sizeof(long double) on
1059 // platforms with 8-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001060 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCallaeeb7012010-05-27 06:19:26 +00001061 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001062
John McCall6374c332010-03-06 00:35:14 +00001063 } else {
1064 // 9 is %eflags, which doesn't get a size on Darwin for some
1065 // reason.
1066 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
1067
1068 // 11-16 are st(0..5). Not sure why we stop at 5.
1069 // These have size 12, which is sizeof(long double) on
1070 // platforms with 4-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001071 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCallaeeb7012010-05-27 06:19:26 +00001072 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1073 }
John McCall6374c332010-03-06 00:35:14 +00001074
1075 return false;
1076}
1077
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001078//===----------------------------------------------------------------------===//
1079// X86-64 ABI Implementation
1080//===----------------------------------------------------------------------===//
1081
1082
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001083namespace {
1084/// X86_64ABIInfo - The X86_64 ABI information.
1085class X86_64ABIInfo : public ABIInfo {
1086 enum Class {
1087 Integer = 0,
1088 SSE,
1089 SSEUp,
1090 X87,
1091 X87Up,
1092 ComplexX87,
1093 NoClass,
1094 Memory
1095 };
1096
1097 /// merge - Implement the X86_64 ABI merging algorithm.
1098 ///
1099 /// Merge an accumulating classification \arg Accum with a field
1100 /// classification \arg Field.
1101 ///
1102 /// \param Accum - The accumulating classification. This should
1103 /// always be either NoClass or the result of a previous merge
1104 /// call. In addition, this should never be Memory (the caller
1105 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001106 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001107
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001108 /// postMerge - Implement the X86_64 ABI post merging algorithm.
1109 ///
1110 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
1111 /// final MEMORY or SSE classes when necessary.
1112 ///
1113 /// \param AggregateSize - The size of the current aggregate in
1114 /// the classification process.
1115 ///
1116 /// \param Lo - The classification for the parts of the type
1117 /// residing in the low word of the containing object.
1118 ///
1119 /// \param Hi - The classification for the parts of the type
1120 /// residing in the higher words of the containing object.
1121 ///
1122 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
1123
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001124 /// classify - Determine the x86_64 register classes in which the
1125 /// given type T should be passed.
1126 ///
1127 /// \param Lo - The classification for the parts of the type
1128 /// residing in the low word of the containing object.
1129 ///
1130 /// \param Hi - The classification for the parts of the type
1131 /// residing in the high word of the containing object.
1132 ///
1133 /// \param OffsetBase - The bit offset of this type in the
1134 /// containing object. Some parameters are classified different
1135 /// depending on whether they straddle an eightbyte boundary.
1136 ///
Eli Friedman7a1b5862013-06-12 00:13:45 +00001137 /// \param isNamedArg - Whether the argument in question is a "named"
1138 /// argument, as used in AMD64-ABI 3.5.7.
1139 ///
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001140 /// If a word is unused its result will be NoClass; if a type should
1141 /// be passed in Memory then at least the classification of \arg Lo
1142 /// will be Memory.
1143 ///
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001144 /// The \arg Lo class will be NoClass iff the argument is ignored.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001145 ///
1146 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
1147 /// also be ComplexX87.
Eli Friedman7a1b5862013-06-12 00:13:45 +00001148 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi,
1149 bool isNamedArg) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001150
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001151 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001152 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
1153 unsigned IROffset, QualType SourceTy,
1154 unsigned SourceOffset) const;
1155 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
1156 unsigned IROffset, QualType SourceTy,
1157 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001158
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001159 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001160 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +00001161 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001162
1163 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001164 /// such that the argument will be passed in memory.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001165 ///
1166 /// \param freeIntRegs - The number of free integer registers remaining
1167 /// available.
1168 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001169
Chris Lattnera3c109b2010-07-29 02:16:43 +00001170 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001171
Bill Wendlingbb465d72010-10-18 03:41:31 +00001172 ABIArgInfo classifyArgumentType(QualType Ty,
Daniel Dunbaredfac032012-03-10 01:03:58 +00001173 unsigned freeIntRegs,
Bill Wendlingbb465d72010-10-18 03:41:31 +00001174 unsigned &neededInt,
Eli Friedman7a1b5862013-06-12 00:13:45 +00001175 unsigned &neededSSE,
1176 bool isNamedArg) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001177
Eli Friedmanee1ad992011-12-02 00:11:43 +00001178 bool IsIllegalVectorType(QualType Ty) const;
1179
John McCall67a57732011-04-21 01:20:55 +00001180 /// The 0.98 ABI revision clarified a lot of ambiguities,
1181 /// unfortunately in ways that were not always consistent with
1182 /// certain previous compilers. In particular, platforms which
1183 /// required strict binary compatibility with older versions of GCC
1184 /// may need to exempt themselves.
1185 bool honorsRevision0_98() const {
John McCall64aa4b32013-04-16 22:48:15 +00001186 return !getTarget().getTriple().isOSDarwin();
John McCall67a57732011-04-21 01:20:55 +00001187 }
1188
Eli Friedmanee1ad992011-12-02 00:11:43 +00001189 bool HasAVX;
Derek Schuffbabaf312012-10-11 15:52:22 +00001190 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
1191 // 64-bit hardware.
1192 bool Has64BitPointers;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001193
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001194public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001195 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
Derek Schuffbabaf312012-10-11 15:52:22 +00001196 ABIInfo(CGT), HasAVX(hasavx),
Derek Schuff90da80c2012-10-11 18:21:13 +00001197 Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001198 }
Chris Lattner9c254f02010-06-29 06:01:59 +00001199
John McCallde5d3c72012-02-17 03:33:10 +00001200 bool isPassedUsingAVXType(QualType type) const {
1201 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00001202 // The freeIntRegs argument doesn't matter here.
Eli Friedman7a1b5862013-06-12 00:13:45 +00001203 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE,
1204 /*isNamedArg*/true);
John McCallde5d3c72012-02-17 03:33:10 +00001205 if (info.isDirect()) {
1206 llvm::Type *ty = info.getCoerceToType();
1207 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
1208 return (vectorTy->getBitWidth() > 128);
1209 }
1210 return false;
1211 }
1212
Chris Lattneree5dcd02010-07-29 02:31:05 +00001213 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001214
1215 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1216 CodeGenFunction &CGF) const;
1217};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001218
Chris Lattnerf13721d2010-08-31 16:44:54 +00001219/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001220class WinX86_64ABIInfo : public ABIInfo {
1221
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00001222 ABIArgInfo classify(QualType Ty, bool IsReturnType) const;
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001223
Chris Lattnerf13721d2010-08-31 16:44:54 +00001224public:
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001225 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
1226
1227 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattnerf13721d2010-08-31 16:44:54 +00001228
1229 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1230 CodeGenFunction &CGF) const;
1231};
1232
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001233class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1234public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001235 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
Derek Schuffbabaf312012-10-11 15:52:22 +00001236 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
John McCall6374c332010-03-06 00:35:14 +00001237
John McCallde5d3c72012-02-17 03:33:10 +00001238 const X86_64ABIInfo &getABIInfo() const {
1239 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
1240 }
1241
John McCall6374c332010-03-06 00:35:14 +00001242 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1243 return 7;
1244 }
1245
1246 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1247 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001248 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001249
John McCallaeeb7012010-05-27 06:19:26 +00001250 // 0-15 are the 16 integer registers.
1251 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001252 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +00001253 return false;
1254 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001255
Jay Foadef6de3d2011-07-11 09:56:20 +00001256 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001257 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +00001258 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001259 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1260 }
1261
John McCallde5d3c72012-02-17 03:33:10 +00001262 bool isNoProtoCallVariadic(const CallArgList &args,
1263 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +00001264 // The default CC on x86-64 sets %al to the number of SSA
1265 // registers used, and GCC sets this when calling an unprototyped
Eli Friedman3ed79032011-12-01 04:53:19 +00001266 // function, so we override the default behavior. However, don't do
Eli Friedman68805fe2011-12-06 03:08:26 +00001267 // that when AVX types are involved: the ABI explicitly states it is
1268 // undefined, and it doesn't work in practice because of how the ABI
1269 // defines varargs anyway.
Reid Kleckneref072032013-08-27 23:08:25 +00001270 if (fnType->getCallConv() == CC_C) {
Eli Friedman3ed79032011-12-01 04:53:19 +00001271 bool HasAVXType = false;
John McCallde5d3c72012-02-17 03:33:10 +00001272 for (CallArgList::const_iterator
1273 it = args.begin(), ie = args.end(); it != ie; ++it) {
1274 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1275 HasAVXType = true;
1276 break;
Eli Friedman3ed79032011-12-01 04:53:19 +00001277 }
1278 }
John McCallde5d3c72012-02-17 03:33:10 +00001279
Eli Friedman3ed79032011-12-01 04:53:19 +00001280 if (!HasAVXType)
1281 return true;
1282 }
John McCall01f151e2011-09-21 08:08:30 +00001283
John McCallde5d3c72012-02-17 03:33:10 +00001284 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
John McCall01f151e2011-09-21 08:08:30 +00001285 }
1286
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001287};
1288
Aaron Ballman89735b92013-05-24 15:06:56 +00001289static std::string qualifyWindowsLibrary(llvm::StringRef Lib) {
1290 // If the argument does not end in .lib, automatically add the suffix. This
1291 // matches the behavior of MSVC.
1292 std::string ArgStr = Lib;
1293 if (Lib.size() <= 4 ||
1294 Lib.substr(Lib.size() - 4).compare_lower(".lib") != 0) {
1295 ArgStr += ".lib";
1296 }
1297 return ArgStr;
1298}
1299
Reid Kleckner3190ca92013-05-08 13:44:39 +00001300class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo {
1301public:
John McCallb8b52972013-06-18 02:46:29 +00001302 WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
1303 bool d, bool p, bool w, unsigned RegParms)
1304 : X86_32TargetCodeGenInfo(CGT, d, p, w, RegParms) {}
Reid Kleckner3190ca92013-05-08 13:44:39 +00001305
1306 void getDependentLibraryOption(llvm::StringRef Lib,
1307 llvm::SmallString<24> &Opt) const {
1308 Opt = "/DEFAULTLIB:";
Aaron Ballman89735b92013-05-24 15:06:56 +00001309 Opt += qualifyWindowsLibrary(Lib);
Reid Kleckner3190ca92013-05-08 13:44:39 +00001310 }
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00001311
1312 void getDetectMismatchOption(llvm::StringRef Name,
1313 llvm::StringRef Value,
1314 llvm::SmallString<32> &Opt) const {
Eli Friedman572ac322013-06-07 22:42:22 +00001315 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00001316 }
Reid Kleckner3190ca92013-05-08 13:44:39 +00001317};
1318
Chris Lattnerf13721d2010-08-31 16:44:54 +00001319class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1320public:
1321 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1322 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1323
1324 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1325 return 7;
1326 }
1327
1328 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1329 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001330 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001331
Chris Lattnerf13721d2010-08-31 16:44:54 +00001332 // 0-15 are the 16 integer registers.
1333 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001334 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattnerf13721d2010-08-31 16:44:54 +00001335 return false;
1336 }
Reid Kleckner3190ca92013-05-08 13:44:39 +00001337
1338 void getDependentLibraryOption(llvm::StringRef Lib,
1339 llvm::SmallString<24> &Opt) const {
1340 Opt = "/DEFAULTLIB:";
Aaron Ballman89735b92013-05-24 15:06:56 +00001341 Opt += qualifyWindowsLibrary(Lib);
Reid Kleckner3190ca92013-05-08 13:44:39 +00001342 }
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00001343
1344 void getDetectMismatchOption(llvm::StringRef Name,
1345 llvm::StringRef Value,
1346 llvm::SmallString<32> &Opt) const {
Eli Friedman572ac322013-06-07 22:42:22 +00001347 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00001348 }
Chris Lattnerf13721d2010-08-31 16:44:54 +00001349};
1350
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001351}
1352
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001353void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1354 Class &Hi) const {
1355 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1356 //
1357 // (a) If one of the classes is Memory, the whole argument is passed in
1358 // memory.
1359 //
1360 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1361 // memory.
1362 //
1363 // (c) If the size of the aggregate exceeds two eightbytes and the first
1364 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1365 // argument is passed in memory. NOTE: This is necessary to keep the
1366 // ABI working for processors that don't support the __m256 type.
1367 //
1368 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1369 //
1370 // Some of these are enforced by the merging logic. Others can arise
1371 // only with unions; for example:
1372 // union { _Complex double; unsigned; }
1373 //
1374 // Note that clauses (b) and (c) were added in 0.98.
1375 //
1376 if (Hi == Memory)
1377 Lo = Memory;
1378 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1379 Lo = Memory;
1380 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1381 Lo = Memory;
1382 if (Hi == SSEUp && Lo != SSE)
1383 Hi = SSE;
1384}
1385
Chris Lattner1090a9b2010-06-28 21:43:59 +00001386X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001387 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1388 // classified recursively so that always two fields are
1389 // considered. The resulting class is calculated according to
1390 // the classes of the fields in the eightbyte:
1391 //
1392 // (a) If both classes are equal, this is the resulting class.
1393 //
1394 // (b) If one of the classes is NO_CLASS, the resulting class is
1395 // the other class.
1396 //
1397 // (c) If one of the classes is MEMORY, the result is the MEMORY
1398 // class.
1399 //
1400 // (d) If one of the classes is INTEGER, the result is the
1401 // INTEGER.
1402 //
1403 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1404 // MEMORY is used as class.
1405 //
1406 // (f) Otherwise class SSE is used.
1407
1408 // Accum should never be memory (we should have returned) or
1409 // ComplexX87 (because this cannot be passed in a structure).
1410 assert((Accum != Memory && Accum != ComplexX87) &&
1411 "Invalid accumulated classification during merge.");
1412 if (Accum == Field || Field == NoClass)
1413 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001414 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001415 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001416 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001417 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001418 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001419 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001420 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1421 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001422 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001423 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001424}
1425
Chris Lattnerbcaedae2010-06-30 19:14:05 +00001426void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Eli Friedman7a1b5862013-06-12 00:13:45 +00001427 Class &Lo, Class &Hi, bool isNamedArg) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001428 // FIXME: This code can be simplified by introducing a simple value class for
1429 // Class pairs with appropriate constructor methods for the various
1430 // situations.
1431
1432 // FIXME: Some of the split computations are wrong; unaligned vectors
1433 // shouldn't be passed in registers for example, so there is no chance they
1434 // can straddle an eightbyte. Verify & simplify.
1435
1436 Lo = Hi = NoClass;
1437
1438 Class &Current = OffsetBase < 64 ? Lo : Hi;
1439 Current = Memory;
1440
John McCall183700f2009-09-21 23:43:11 +00001441 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001442 BuiltinType::Kind k = BT->getKind();
1443
1444 if (k == BuiltinType::Void) {
1445 Current = NoClass;
1446 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1447 Lo = Integer;
1448 Hi = Integer;
1449 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1450 Current = Integer;
Derek Schuff7da46f92012-10-11 16:55:58 +00001451 } else if ((k == BuiltinType::Float || k == BuiltinType::Double) ||
1452 (k == BuiltinType::LongDouble &&
Cameron Esfahani57b1da12013-09-14 01:09:11 +00001453 getTarget().getTriple().isOSNaCl())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001454 Current = SSE;
1455 } else if (k == BuiltinType::LongDouble) {
1456 Lo = X87;
1457 Hi = X87Up;
1458 }
1459 // FIXME: _Decimal32 and _Decimal64 are SSE.
1460 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001461 return;
1462 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001463
Chris Lattner1090a9b2010-06-28 21:43:59 +00001464 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001465 // Classify the underlying integer type.
Eli Friedman7a1b5862013-06-12 00:13:45 +00001466 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001467 return;
1468 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001469
Chris Lattner1090a9b2010-06-28 21:43:59 +00001470 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001471 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001472 return;
1473 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001474
Chris Lattner1090a9b2010-06-28 21:43:59 +00001475 if (Ty->isMemberPointerType()) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001476 if (Ty->isMemberFunctionPointerType() && Has64BitPointers)
Daniel Dunbar67d438d2010-05-15 00:00:37 +00001477 Lo = Hi = Integer;
1478 else
1479 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001480 return;
1481 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001482
Chris Lattner1090a9b2010-06-28 21:43:59 +00001483 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001484 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001485 if (Size == 32) {
1486 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1487 // float> as integer.
1488 Current = Integer;
1489
1490 // If this type crosses an eightbyte boundary, it should be
1491 // split.
1492 uint64_t EB_Real = (OffsetBase) / 64;
1493 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1494 if (EB_Real != EB_Imag)
1495 Hi = Lo;
1496 } else if (Size == 64) {
1497 // gcc passes <1 x double> in memory. :(
1498 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1499 return;
1500
1501 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001502 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001503 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1504 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1505 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001506 Current = Integer;
1507 else
1508 Current = SSE;
1509
1510 // If this type crosses an eightbyte boundary, it should be
1511 // split.
1512 if (OffsetBase && OffsetBase != 64)
1513 Hi = Lo;
Eli Friedman7a1b5862013-06-12 00:13:45 +00001514 } else if (Size == 128 || (HasAVX && isNamedArg && Size == 256)) {
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001515 // Arguments of 256-bits are split into four eightbyte chunks. The
1516 // least significant one belongs to class SSE and all the others to class
1517 // SSEUP. The original Lo and Hi design considers that types can't be
1518 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1519 // This design isn't correct for 256-bits, but since there're no cases
1520 // where the upper parts would need to be inspected, avoid adding
1521 // complexity and just consider Hi to match the 64-256 part.
Eli Friedman7a1b5862013-06-12 00:13:45 +00001522 //
1523 // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in
1524 // registers if they are "named", i.e. not part of the "..." of a
1525 // variadic function.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001526 Lo = SSE;
1527 Hi = SSEUp;
1528 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001529 return;
1530 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001531
Chris Lattner1090a9b2010-06-28 21:43:59 +00001532 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001533 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001534
Chris Lattnerea044322010-07-29 02:01:43 +00001535 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001536 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001537 if (Size <= 64)
1538 Current = Integer;
1539 else if (Size <= 128)
1540 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001541 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001542 Current = SSE;
Derek Schuff7da46f92012-10-11 16:55:58 +00001543 else if (ET == getContext().DoubleTy ||
1544 (ET == getContext().LongDoubleTy &&
Cameron Esfahani57b1da12013-09-14 01:09:11 +00001545 getTarget().getTriple().isOSNaCl()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001546 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001547 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001548 Current = ComplexX87;
1549
1550 // If this complex type crosses an eightbyte boundary then it
1551 // should be split.
1552 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001553 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001554 if (Hi == NoClass && EB_Real != EB_Imag)
1555 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001556
Chris Lattner1090a9b2010-06-28 21:43:59 +00001557 return;
1558 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001559
Chris Lattnerea044322010-07-29 02:01:43 +00001560 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001561 // Arrays are treated like structures.
1562
Chris Lattnerea044322010-07-29 02:01:43 +00001563 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001564
1565 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001566 // than four eightbytes, ..., it has class MEMORY.
1567 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001568 return;
1569
1570 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1571 // fields, it has class MEMORY.
1572 //
1573 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001574 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001575 return;
1576
1577 // Otherwise implement simplified merge. We could be smarter about
1578 // this, but it isn't worth it and would be harder to verify.
1579 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001580 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001581 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes089d8922011-07-12 01:27:38 +00001582
1583 // The only case a 256-bit wide vector could be used is when the array
1584 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1585 // to work for sizes wider than 128, early check and fallback to memory.
1586 if (Size > 128 && EltSize != 256)
1587 return;
1588
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001589 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1590 Class FieldLo, FieldHi;
Eli Friedman7a1b5862013-06-12 00:13:45 +00001591 classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001592 Lo = merge(Lo, FieldLo);
1593 Hi = merge(Hi, FieldHi);
1594 if (Lo == Memory || Hi == Memory)
1595 break;
1596 }
1597
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001598 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001599 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001600 return;
1601 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001602
Chris Lattner1090a9b2010-06-28 21:43:59 +00001603 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001604 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001605
1606 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001607 // than four eightbytes, ..., it has class MEMORY.
1608 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001609 return;
1610
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001611 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1612 // copy constructor or a non-trivial destructor, it is passed by invisible
1613 // reference.
Mark Lacey23630722013-10-06 01:33:34 +00001614 if (getRecordArgABI(RT, getCXXABI()))
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001615 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001616
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001617 const RecordDecl *RD = RT->getDecl();
1618
1619 // Assume variable sized types are passed in memory.
1620 if (RD->hasFlexibleArrayMember())
1621 return;
1622
Chris Lattnerea044322010-07-29 02:01:43 +00001623 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001624
1625 // Reset Lo class, this will be recomputed.
1626 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001627
1628 // If this is a C++ record, classify the bases first.
1629 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1630 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1631 e = CXXRD->bases_end(); i != e; ++i) {
1632 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1633 "Unexpected base class!");
1634 const CXXRecordDecl *Base =
1635 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1636
1637 // Classify this field.
1638 //
1639 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1640 // single eightbyte, each is classified separately. Each eightbyte gets
1641 // initialized to class NO_CLASS.
1642 Class FieldLo, FieldHi;
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001643 uint64_t Offset =
1644 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
Eli Friedman7a1b5862013-06-12 00:13:45 +00001645 classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001646 Lo = merge(Lo, FieldLo);
1647 Hi = merge(Hi, FieldHi);
1648 if (Lo == Memory || Hi == Memory)
1649 break;
1650 }
1651 }
1652
1653 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001654 unsigned idx = 0;
Bruno Cardoso Lopes548e4782011-07-12 22:30:58 +00001655 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001656 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001657 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1658 bool BitField = i->isBitField();
1659
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001660 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1661 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001662 //
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001663 // The only case a 256-bit wide vector could be used is when the struct
1664 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1665 // to work for sizes wider than 128, early check and fallback to memory.
1666 //
1667 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1668 Lo = Memory;
1669 return;
1670 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001671 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001672 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001673 Lo = Memory;
1674 return;
1675 }
1676
1677 // Classify this field.
1678 //
1679 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1680 // exceeds a single eightbyte, each is classified
1681 // separately. Each eightbyte gets initialized to class
1682 // NO_CLASS.
1683 Class FieldLo, FieldHi;
1684
1685 // Bit-fields require special handling, they do not force the
1686 // structure to be passed in memory even if unaligned, and
1687 // therefore they can straddle an eightbyte.
1688 if (BitField) {
1689 // Ignore padding bit-fields.
1690 if (i->isUnnamedBitfield())
1691 continue;
1692
1693 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001694 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001695
1696 uint64_t EB_Lo = Offset / 64;
1697 uint64_t EB_Hi = (Offset + Size - 1) / 64;
Sylvestre Ledru9a6002a2013-10-06 09:54:18 +00001698
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001699 if (EB_Lo) {
1700 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1701 FieldLo = NoClass;
1702 FieldHi = Integer;
1703 } else {
1704 FieldLo = Integer;
1705 FieldHi = EB_Hi ? Integer : NoClass;
1706 }
1707 } else
Eli Friedman7a1b5862013-06-12 00:13:45 +00001708 classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001709 Lo = merge(Lo, FieldLo);
1710 Hi = merge(Hi, FieldHi);
1711 if (Lo == Memory || Hi == Memory)
1712 break;
1713 }
1714
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001715 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001716 }
1717}
1718
Chris Lattner9c254f02010-06-29 06:01:59 +00001719ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001720 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1721 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001722 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001723 // Treat an enum type as its underlying type.
1724 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1725 Ty = EnumTy->getDecl()->getIntegerType();
1726
1727 return (Ty->isPromotableIntegerType() ?
1728 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1729 }
1730
1731 return ABIArgInfo::getIndirect(0);
1732}
1733
Eli Friedmanee1ad992011-12-02 00:11:43 +00001734bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1735 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1736 uint64_t Size = getContext().getTypeSize(VecTy);
1737 unsigned LargestVector = HasAVX ? 256 : 128;
1738 if (Size <= 64 || Size > LargestVector)
1739 return true;
1740 }
1741
1742 return false;
1743}
1744
Daniel Dunbaredfac032012-03-10 01:03:58 +00001745ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1746 unsigned freeIntRegs) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001747 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1748 // place naturally.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001749 //
1750 // This assumption is optimistic, as there could be free registers available
1751 // when we need to pass this argument in memory, and LLVM could try to pass
1752 // the argument in the free register. This does not seem to happen currently,
1753 // but this code would be much safer if we could mark the argument with
1754 // 'onstack'. See PR12193.
Eli Friedmanee1ad992011-12-02 00:11:43 +00001755 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001756 // Treat an enum type as its underlying type.
1757 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1758 Ty = EnumTy->getDecl()->getIntegerType();
1759
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001760 return (Ty->isPromotableIntegerType() ?
1761 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001762 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001763
Mark Lacey23630722013-10-06 01:33:34 +00001764 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00001765 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001766
Chris Lattner855d2272011-05-22 23:21:23 +00001767 // Compute the byval alignment. We specify the alignment of the byval in all
1768 // cases so that the mid-level optimizer knows the alignment of the byval.
1769 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
Daniel Dunbaredfac032012-03-10 01:03:58 +00001770
1771 // Attempt to avoid passing indirect results using byval when possible. This
1772 // is important for good codegen.
1773 //
1774 // We do this by coercing the value into a scalar type which the backend can
1775 // handle naturally (i.e., without using byval).
1776 //
1777 // For simplicity, we currently only do this when we have exhausted all of the
1778 // free integer registers. Doing this when there are free integer registers
1779 // would require more care, as we would have to ensure that the coerced value
1780 // did not claim the unused register. That would require either reording the
1781 // arguments to the function (so that any subsequent inreg values came first),
1782 // or only doing this optimization when there were no following arguments that
1783 // might be inreg.
1784 //
1785 // We currently expect it to be rare (particularly in well written code) for
1786 // arguments to be passed on the stack when there are still free integer
1787 // registers available (this would typically imply large structs being passed
1788 // by value), so this seems like a fair tradeoff for now.
1789 //
1790 // We can revisit this if the backend grows support for 'onstack' parameter
1791 // attributes. See PR12193.
1792 if (freeIntRegs == 0) {
1793 uint64_t Size = getContext().getTypeSize(Ty);
1794
1795 // If this type fits in an eightbyte, coerce it into the matching integral
1796 // type, which will end up on the stack (with alignment 8).
1797 if (Align == 8 && Size <= 64)
1798 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1799 Size));
1800 }
1801
Chris Lattner855d2272011-05-22 23:21:23 +00001802 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001803}
1804
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001805/// GetByteVectorType - The ABI specifies that a value should be passed in an
1806/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner0f408f52010-07-29 04:56:46 +00001807/// vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001808llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001809 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001810
Chris Lattner15842bd2010-07-29 05:02:29 +00001811 // Wrapper structs that just contain vectors are passed just like vectors,
1812 // strip them off if present.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001813 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner15842bd2010-07-29 05:02:29 +00001814 while (STy && STy->getNumElements() == 1) {
1815 IRType = STy->getElementType(0);
1816 STy = dyn_cast<llvm::StructType>(IRType);
1817 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001818
Bruno Cardoso Lopes528a8c72011-07-08 22:57:35 +00001819 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001820 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1821 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001822 unsigned BitWidth = VT->getBitWidth();
Tanya Lattnerce275672011-11-28 23:18:11 +00001823 if ((BitWidth >= 128 && BitWidth <= 256) &&
Chris Lattner0f408f52010-07-29 04:56:46 +00001824 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1825 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1826 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1827 EltTy->isIntegerTy(128)))
1828 return VT;
1829 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001830
Chris Lattner0f408f52010-07-29 04:56:46 +00001831 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1832}
1833
Chris Lattnere2962be2010-07-29 07:30:00 +00001834/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1835/// is known to either be off the end of the specified type or being in
1836/// alignment padding. The user type specified is known to be at most 128 bits
1837/// in size, and have passed through X86_64ABIInfo::classify with a successful
1838/// classification that put one of the two halves in the INTEGER class.
1839///
1840/// It is conservatively correct to return false.
1841static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1842 unsigned EndBit, ASTContext &Context) {
1843 // If the bytes being queried are off the end of the type, there is no user
1844 // data hiding here. This handles analysis of builtins, vectors and other
1845 // types that don't contain interesting padding.
1846 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1847 if (TySize <= StartBit)
1848 return true;
1849
Chris Lattner021c3a32010-07-29 07:43:55 +00001850 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1851 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1852 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1853
1854 // Check each element to see if the element overlaps with the queried range.
1855 for (unsigned i = 0; i != NumElts; ++i) {
1856 // If the element is after the span we care about, then we're done..
1857 unsigned EltOffset = i*EltSize;
1858 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001859
Chris Lattner021c3a32010-07-29 07:43:55 +00001860 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1861 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1862 EndBit-EltOffset, Context))
1863 return false;
1864 }
1865 // If it overlaps no elements, then it is safe to process as padding.
1866 return true;
1867 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001868
Chris Lattnere2962be2010-07-29 07:30:00 +00001869 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1870 const RecordDecl *RD = RT->getDecl();
1871 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001872
Chris Lattnere2962be2010-07-29 07:30:00 +00001873 // If this is a C++ record, check the bases first.
1874 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1875 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1876 e = CXXRD->bases_end(); i != e; ++i) {
1877 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1878 "Unexpected base class!");
1879 const CXXRecordDecl *Base =
1880 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001881
Chris Lattnere2962be2010-07-29 07:30:00 +00001882 // If the base is after the span we care about, ignore it.
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001883 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
Chris Lattnere2962be2010-07-29 07:30:00 +00001884 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001885
Chris Lattnere2962be2010-07-29 07:30:00 +00001886 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1887 if (!BitsContainNoUserData(i->getType(), BaseStart,
1888 EndBit-BaseOffset, Context))
1889 return false;
1890 }
1891 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001892
Chris Lattnere2962be2010-07-29 07:30:00 +00001893 // Verify that no field has data that overlaps the region of interest. Yes
1894 // this could be sped up a lot by being smarter about queried fields,
1895 // however we're only looking at structs up to 16 bytes, so we don't care
1896 // much.
1897 unsigned idx = 0;
1898 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1899 i != e; ++i, ++idx) {
1900 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001901
Chris Lattnere2962be2010-07-29 07:30:00 +00001902 // If we found a field after the region we care about, then we're done.
1903 if (FieldOffset >= EndBit) break;
1904
1905 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1906 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1907 Context))
1908 return false;
1909 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001910
Chris Lattnere2962be2010-07-29 07:30:00 +00001911 // If nothing in this record overlapped the area of interest, then we're
1912 // clean.
1913 return true;
1914 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001915
Chris Lattnere2962be2010-07-29 07:30:00 +00001916 return false;
1917}
1918
Chris Lattner0b362002010-07-29 18:39:32 +00001919/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1920/// float member at the specified offset. For example, {int,{float}} has a
1921/// float at offset 4. It is conservatively correct for this routine to return
1922/// false.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001923static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Micah Villmow25a6a842012-10-08 16:25:52 +00001924 const llvm::DataLayout &TD) {
Chris Lattner0b362002010-07-29 18:39:32 +00001925 // Base case if we find a float.
1926 if (IROffset == 0 && IRType->isFloatTy())
1927 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001928
Chris Lattner0b362002010-07-29 18:39:32 +00001929 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001930 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner0b362002010-07-29 18:39:32 +00001931 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1932 unsigned Elt = SL->getElementContainingOffset(IROffset);
1933 IROffset -= SL->getElementOffset(Elt);
1934 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1935 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001936
Chris Lattner0b362002010-07-29 18:39:32 +00001937 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001938 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1939 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner0b362002010-07-29 18:39:32 +00001940 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1941 IROffset -= IROffset/EltSize*EltSize;
1942 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1943 }
1944
1945 return false;
1946}
1947
Chris Lattnerf47c9442010-07-29 18:13:09 +00001948
1949/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1950/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001951llvm::Type *X86_64ABIInfo::
1952GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00001953 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001954 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001955 // pass as float if the last 4 bytes is just padding. This happens for
1956 // structs that contain 3 floats.
1957 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1958 SourceOffset*8+64, getContext()))
1959 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001960
Chris Lattner0b362002010-07-29 18:39:32 +00001961 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1962 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1963 // case.
Micah Villmow25a6a842012-10-08 16:25:52 +00001964 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
1965 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001966 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001967
Chris Lattnerf47c9442010-07-29 18:13:09 +00001968 return llvm::Type::getDoubleTy(getVMContext());
1969}
1970
1971
Chris Lattner0d2656d2010-07-29 17:40:35 +00001972/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1973/// an 8-byte GPR. This means that we either have a scalar or we are talking
1974/// about the high or low part of an up-to-16-byte struct. This routine picks
1975/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001976/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1977/// etc).
1978///
1979/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1980/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1981/// the 8-byte value references. PrefType may be null.
1982///
1983/// SourceTy is the source level type for the entire argument. SourceOffset is
1984/// an offset into this that we're processing (which is always either 0 or 8).
1985///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001986llvm::Type *X86_64ABIInfo::
1987GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00001988 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001989 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1990 // returning an 8-byte unit starting with it. See if we can safely use it.
1991 if (IROffset == 0) {
1992 // Pointers and int64's always fill the 8-byte unit.
Derek Schuffbabaf312012-10-11 15:52:22 +00001993 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
1994 IRType->isIntegerTy(64))
Chris Lattnere2962be2010-07-29 07:30:00 +00001995 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001996
Chris Lattnere2962be2010-07-29 07:30:00 +00001997 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1998 // goodness in the source type is just tail padding. This is allowed to
1999 // kick in for struct {double,int} on the int, but not on
2000 // struct{double,int,int} because we wouldn't return the second int. We
2001 // have to do this analysis on the source type because we can't depend on
2002 // unions being lowered a specific way etc.
2003 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
Derek Schuffbabaf312012-10-11 15:52:22 +00002004 IRType->isIntegerTy(32) ||
2005 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
2006 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
2007 cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002008
Chris Lattnere2962be2010-07-29 07:30:00 +00002009 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
2010 SourceOffset*8+64, getContext()))
2011 return IRType;
2012 }
2013 }
Chris Lattner49382de2010-07-28 22:44:07 +00002014
Chris Lattner2acc6e32011-07-18 04:24:23 +00002015 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00002016 // If this is a struct, recurse into the field at the specified offset.
Micah Villmow25a6a842012-10-08 16:25:52 +00002017 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00002018 if (IROffset < SL->getSizeInBytes()) {
2019 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
2020 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002021
Chris Lattner0d2656d2010-07-29 17:40:35 +00002022 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
2023 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002024 }
Chris Lattner49382de2010-07-28 22:44:07 +00002025 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002026
Chris Lattner2acc6e32011-07-18 04:24:23 +00002027 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002028 llvm::Type *EltTy = ATy->getElementType();
Micah Villmow25a6a842012-10-08 16:25:52 +00002029 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
Chris Lattner021c3a32010-07-29 07:43:55 +00002030 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00002031 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
2032 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00002033 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002034
Chris Lattner49382de2010-07-28 22:44:07 +00002035 // Okay, we don't have any better idea of what to pass, so we pass this in an
2036 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00002037 unsigned TySizeInBytes =
2038 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00002039
Chris Lattner9e45a3d2010-07-29 17:34:39 +00002040 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002041
Chris Lattner49382de2010-07-28 22:44:07 +00002042 // It is always safe to classify this as an integer type up to i64 that
2043 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00002044 return llvm::IntegerType::get(getVMContext(),
2045 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00002046}
2047
Chris Lattner66e7b682010-09-01 00:50:20 +00002048
2049/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
2050/// be used as elements of a two register pair to pass or return, return a
2051/// first class aggregate to represent them. For example, if the low part of
2052/// a by-value argument should be passed as i32* and the high part as float,
2053/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002054static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00002055GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Micah Villmow25a6a842012-10-08 16:25:52 +00002056 const llvm::DataLayout &TD) {
Chris Lattner66e7b682010-09-01 00:50:20 +00002057 // In order to correctly satisfy the ABI, we need to the high part to start
2058 // at offset 8. If the high and low parts we inferred are both 4-byte types
2059 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
2060 // the second element at offset 8. Check for this:
2061 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
2062 unsigned HiAlign = TD.getABITypeAlignment(Hi);
Micah Villmow25a6a842012-10-08 16:25:52 +00002063 unsigned HiStart = llvm::DataLayout::RoundUpAlignment(LoSize, HiAlign);
Chris Lattner66e7b682010-09-01 00:50:20 +00002064 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002065
Chris Lattner66e7b682010-09-01 00:50:20 +00002066 // To handle this, we have to increase the size of the low part so that the
2067 // second element will start at an 8 byte offset. We can't increase the size
2068 // of the second element because it might make us access off the end of the
2069 // struct.
2070 if (HiStart != 8) {
2071 // There are only two sorts of types the ABI generation code can produce for
2072 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
2073 // Promote these to a larger type.
2074 if (Lo->isFloatTy())
2075 Lo = llvm::Type::getDoubleTy(Lo->getContext());
2076 else {
2077 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
2078 Lo = llvm::Type::getInt64Ty(Lo->getContext());
2079 }
2080 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002081
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002082 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002083
2084
Chris Lattner66e7b682010-09-01 00:50:20 +00002085 // Verify that the second element is at an 8-byte offset.
2086 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
2087 "Invalid x86-64 argument pair!");
2088 return Result;
2089}
2090
Chris Lattner519f68c2010-07-28 23:06:14 +00002091ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00002092classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00002093 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
2094 // classification algorithm.
2095 X86_64ABIInfo::Class Lo, Hi;
Eli Friedman7a1b5862013-06-12 00:13:45 +00002096 classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true);
Chris Lattner519f68c2010-07-28 23:06:14 +00002097
2098 // Check some invariants.
2099 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002100 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2101
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002102 llvm::Type *ResType = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002103 switch (Lo) {
2104 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002105 if (Hi == NoClass)
2106 return ABIArgInfo::getIgnore();
2107 // If the low part is just padding, it takes no register, leave ResType
2108 // null.
2109 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2110 "Unknown missing lo part");
2111 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002112
2113 case SSEUp:
2114 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002115 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002116
2117 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
2118 // hidden argument.
2119 case Memory:
2120 return getIndirectReturnResult(RetTy);
2121
2122 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
2123 // available register of the sequence %rax, %rdx is used.
2124 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002125 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002126
Chris Lattnereb518b42010-07-29 21:42:50 +00002127 // If we have a sign or zero extended integer, make sure to return Extend
2128 // so that the parameter gets the right LLVM IR attributes.
2129 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2130 // Treat an enum type as its underlying type.
2131 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2132 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002133
Chris Lattnereb518b42010-07-29 21:42:50 +00002134 if (RetTy->isIntegralOrEnumerationType() &&
2135 RetTy->isPromotableIntegerType())
2136 return ABIArgInfo::getExtend();
2137 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002138 break;
2139
2140 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
2141 // available SSE register of the sequence %xmm0, %xmm1 is used.
2142 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002143 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00002144 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002145
2146 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
2147 // returned on the X87 stack in %st0 as 80-bit x87 number.
2148 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00002149 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00002150 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002151
2152 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
2153 // part of the value is returned in %st0 and the imaginary part in
2154 // %st1.
2155 case ComplexX87:
2156 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00002157 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00002158 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00002159 NULL);
2160 break;
2161 }
2162
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002163 llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002164 switch (Hi) {
2165 // Memory was handled previously and X87 should
2166 // never occur as a hi class.
2167 case Memory:
2168 case X87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002169 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002170
2171 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00002172 case NoClass:
2173 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002174
Chris Lattner3db4dde2010-09-01 00:20:33 +00002175 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002176 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002177 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2178 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002179 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00002180 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002181 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002182 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2183 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002184 break;
2185
2186 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002187 // is passed in the next available eightbyte chunk if the last used
2188 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00002189 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002190 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00002191 case SSEUp:
2192 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002193 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00002194 break;
2195
2196 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
2197 // returned together with the previous X87 value in %st0.
2198 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002199 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00002200 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002201 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00002202 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00002203 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002204 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002205 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2206 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00002207 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002208 break;
2209 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002210
Chris Lattner3db4dde2010-09-01 00:20:33 +00002211 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00002212 // known to pass in the high eightbyte of the result. We do this by forming a
2213 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00002214 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002215 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Chris Lattner519f68c2010-07-28 23:06:14 +00002216
Chris Lattnereb518b42010-07-29 21:42:50 +00002217 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00002218}
2219
Daniel Dunbaredfac032012-03-10 01:03:58 +00002220ABIArgInfo X86_64ABIInfo::classifyArgumentType(
Eli Friedman7a1b5862013-06-12 00:13:45 +00002221 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE,
2222 bool isNamedArg)
Daniel Dunbaredfac032012-03-10 01:03:58 +00002223 const
2224{
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002225 X86_64ABIInfo::Class Lo, Hi;
Eli Friedman7a1b5862013-06-12 00:13:45 +00002226 classify(Ty, 0, Lo, Hi, isNamedArg);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002227
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002228 // Check some invariants.
2229 // FIXME: Enforce these by construction.
2230 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002231 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2232
2233 neededInt = 0;
2234 neededSSE = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002235 llvm::Type *ResType = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002236 switch (Lo) {
2237 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002238 if (Hi == NoClass)
2239 return ABIArgInfo::getIgnore();
2240 // If the low part is just padding, it takes no register, leave ResType
2241 // null.
2242 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2243 "Unknown missing lo part");
2244 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002245
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002246 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
2247 // on the stack.
2248 case Memory:
2249
2250 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
2251 // COMPLEX_X87, it is passed in memory.
2252 case X87:
2253 case ComplexX87:
Mark Lacey23630722013-10-06 01:33:34 +00002254 if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect)
Eli Friedmanded137f2011-06-29 07:04:55 +00002255 ++neededInt;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002256 return getIndirectResult(Ty, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002257
2258 case SSEUp:
2259 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002260 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002261
2262 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
2263 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
2264 // and %r9 is used.
2265 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00002266 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002267
Chris Lattner49382de2010-07-28 22:44:07 +00002268 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002269 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00002270
2271 // If we have a sign or zero extended integer, make sure to return Extend
2272 // so that the parameter gets the right LLVM IR attributes.
2273 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2274 // Treat an enum type as its underlying type.
2275 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2276 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002277
Chris Lattnereb518b42010-07-29 21:42:50 +00002278 if (Ty->isIntegralOrEnumerationType() &&
2279 Ty->isPromotableIntegerType())
2280 return ABIArgInfo::getExtend();
2281 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002282
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002283 break;
2284
2285 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
2286 // available SSE register is used, the registers are taken in the
2287 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00002288 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002289 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00002290 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00002291 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002292 break;
2293 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00002294 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002295
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002296 llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002297 switch (Hi) {
2298 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002299 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002300 // which is passed in memory.
2301 case Memory:
2302 case X87:
2303 case ComplexX87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002304 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002305
2306 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002307
Chris Lattner645406a2010-09-01 00:24:35 +00002308 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002309 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00002310 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002311 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002312
Chris Lattner645406a2010-09-01 00:24:35 +00002313 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2314 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002315 break;
2316
2317 // X87Up generally doesn't occur here (long double is passed in
2318 // memory), except in situations involving unions.
2319 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00002320 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002321 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002322
Chris Lattner645406a2010-09-01 00:24:35 +00002323 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2324 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00002325
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002326 ++neededSSE;
2327 break;
2328
2329 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
2330 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002331 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002332 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00002333 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002334 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002335 break;
2336 }
2337
Chris Lattner645406a2010-09-01 00:24:35 +00002338 // If a high part was specified, merge it together with the low part. It is
2339 // known to pass in the high eightbyte of the result. We do this by forming a
2340 // first class struct aggregate with the high and low part: {low, high}
2341 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002342 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002343
Chris Lattnereb518b42010-07-29 21:42:50 +00002344 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002345}
2346
Chris Lattneree5dcd02010-07-29 02:31:05 +00002347void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002348
Chris Lattnera3c109b2010-07-29 02:16:43 +00002349 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002350
2351 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00002352 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002353
2354 // If the return value is indirect, then the hidden argument is consuming one
2355 // integer register.
2356 if (FI.getReturnInfo().isIndirect())
2357 --freeIntRegs;
2358
Eli Friedman7a1b5862013-06-12 00:13:45 +00002359 bool isVariadic = FI.isVariadic();
2360 unsigned numRequiredArgs = 0;
2361 if (isVariadic)
2362 numRequiredArgs = FI.getRequiredArgs().getNumRequiredArgs();
2363
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002364 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2365 // get assigned (in left-to-right order) for passing as follows...
2366 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2367 it != ie; ++it) {
Eli Friedman7a1b5862013-06-12 00:13:45 +00002368 bool isNamedArg = true;
2369 if (isVariadic)
Aaron Ballmaneba7d2f2013-06-12 15:03:45 +00002370 isNamedArg = (it - FI.arg_begin()) <
2371 static_cast<signed>(numRequiredArgs);
Eli Friedman7a1b5862013-06-12 00:13:45 +00002372
Bill Wendling99aaae82010-10-18 23:51:38 +00002373 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002374 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
Eli Friedman7a1b5862013-06-12 00:13:45 +00002375 neededSSE, isNamedArg);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002376
2377 // AMD64-ABI 3.2.3p3: If there are no registers available for any
2378 // eightbyte of an argument, the whole argument is passed on the
2379 // stack. If registers have already been assigned for some
2380 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00002381 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002382 freeIntRegs -= neededInt;
2383 freeSSERegs -= neededSSE;
2384 } else {
Daniel Dunbaredfac032012-03-10 01:03:58 +00002385 it->info = getIndirectResult(it->type, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002386 }
2387 }
2388}
2389
2390static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2391 QualType Ty,
2392 CodeGenFunction &CGF) {
2393 llvm::Value *overflow_arg_area_p =
2394 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2395 llvm::Value *overflow_arg_area =
2396 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2397
2398 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2399 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedman8d2fe422011-11-18 02:44:19 +00002400 // It isn't stated explicitly in the standard, but in practice we use
2401 // alignment greater than 16 where necessary.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002402 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2403 if (Align > 8) {
Eli Friedman8d2fe422011-11-18 02:44:19 +00002404 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
Owen Anderson0032b272009-08-13 21:57:51 +00002405 llvm::Value *Offset =
Eli Friedman8d2fe422011-11-18 02:44:19 +00002406 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002407 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2408 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00002409 CGF.Int64Ty);
Eli Friedman8d2fe422011-11-18 02:44:19 +00002410 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002411 overflow_arg_area =
2412 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2413 overflow_arg_area->getType(),
2414 "overflow_arg_area.align");
2415 }
2416
2417 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002418 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002419 llvm::Value *Res =
2420 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002421 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002422
2423 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2424 // l->overflow_arg_area + sizeof(type).
2425 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2426 // an 8 byte boundary.
2427
2428 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00002429 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00002430 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002431 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2432 "overflow_arg_area.next");
2433 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2434
2435 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2436 return Res;
2437}
2438
2439llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2440 CodeGenFunction &CGF) const {
2441 // Assume that va_list type is correct; should be pointer to LLVM type:
2442 // struct {
2443 // i32 gp_offset;
2444 // i32 fp_offset;
2445 // i8* overflow_arg_area;
2446 // i8* reg_save_area;
2447 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00002448 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002449
Chris Lattnera14db752010-03-11 18:19:55 +00002450 Ty = CGF.getContext().getCanonicalType(Ty);
Eli Friedman7a1b5862013-06-12 00:13:45 +00002451 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE,
2452 /*isNamedArg*/false);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002453
2454 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2455 // in the registers. If not go to step 7.
2456 if (!neededInt && !neededSSE)
2457 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2458
2459 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2460 // general purpose registers needed to pass type and num_fp to hold
2461 // the number of floating point registers needed.
2462
2463 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2464 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2465 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2466 //
2467 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2468 // register save space).
2469
2470 llvm::Value *InRegs = 0;
2471 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2472 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2473 if (neededInt) {
2474 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2475 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002476 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2477 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002478 }
2479
2480 if (neededSSE) {
2481 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2482 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2483 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00002484 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2485 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002486 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2487 }
2488
2489 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2490 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2491 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2492 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2493
2494 // Emit code to load the value if it was passed in registers.
2495
2496 CGF.EmitBlock(InRegBlock);
2497
2498 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2499 // an offset of l->gp_offset and/or l->fp_offset. This may require
2500 // copying to a temporary location in case the parameter is passed
2501 // in different register classes or requires an alignment greater
2502 // than 8 for general purpose registers and 16 for XMM registers.
2503 //
2504 // FIXME: This really results in shameful code when we end up needing to
2505 // collect arguments from different places; often what should result in a
2506 // simple assembling of a structure from scattered addresses has many more
2507 // loads than necessary. Can we clean this up?
Chris Lattner2acc6e32011-07-18 04:24:23 +00002508 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002509 llvm::Value *RegAddr =
2510 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2511 "reg_save_area");
2512 if (neededInt && neededSSE) {
2513 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002514 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002515 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Eli Friedmaneeb00622013-06-07 23:20:55 +00002516 llvm::Value *Tmp = CGF.CreateMemTemp(Ty);
2517 Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002518 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002519 llvm::Type *TyLo = ST->getElementType(0);
2520 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002521 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002522 "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002523 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2524 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002525 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2526 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002527 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2528 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002529 llvm::Value *V =
2530 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2531 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2532 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2533 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2534
Owen Andersona1cf15f2009-07-14 23:10:40 +00002535 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002536 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002537 } else if (neededInt) {
2538 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2539 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002540 llvm::PointerType::getUnqual(LTy));
Eli Friedmaneeb00622013-06-07 23:20:55 +00002541
2542 // Copy to a temporary if necessary to ensure the appropriate alignment.
2543 std::pair<CharUnits, CharUnits> SizeAlign =
2544 CGF.getContext().getTypeInfoInChars(Ty);
2545 uint64_t TySize = SizeAlign.first.getQuantity();
2546 unsigned TyAlign = SizeAlign.second.getQuantity();
2547 if (TyAlign > 8) {
Eli Friedmaneeb00622013-06-07 23:20:55 +00002548 llvm::Value *Tmp = CGF.CreateMemTemp(Ty);
2549 CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, 8, false);
2550 RegAddr = Tmp;
2551 }
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002552 } else if (neededSSE == 1) {
2553 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2554 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2555 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002556 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002557 assert(neededSSE == 2 && "Invalid number of needed registers!");
2558 // SSE registers are spaced 16 bytes apart in the register save
2559 // area, we need to collect the two eightbytes together.
2560 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002561 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner8b418682012-02-07 00:39:47 +00002562 llvm::Type *DoubleTy = CGF.DoubleTy;
Chris Lattner2acc6e32011-07-18 04:24:23 +00002563 llvm::Type *DblPtrTy =
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002564 llvm::PointerType::getUnqual(DoubleTy);
Eli Friedmaneeb00622013-06-07 23:20:55 +00002565 llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, NULL);
2566 llvm::Value *V, *Tmp = CGF.CreateMemTemp(Ty);
2567 Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo());
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002568 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2569 DblPtrTy));
2570 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2571 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2572 DblPtrTy));
2573 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2574 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2575 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002576 }
2577
2578 // AMD64-ABI 3.5.7p5: Step 5. Set:
2579 // l->gp_offset = l->gp_offset + num_gp * 8
2580 // l->fp_offset = l->fp_offset + num_fp * 16.
2581 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002582 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002583 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2584 gp_offset_p);
2585 }
2586 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002587 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002588 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2589 fp_offset_p);
2590 }
2591 CGF.EmitBranch(ContBlock);
2592
2593 // Emit code to load the value if it was passed in memory.
2594
2595 CGF.EmitBlock(InMemBlock);
2596 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2597
2598 // Return the appropriate result.
2599
2600 CGF.EmitBlock(ContBlock);
Jay Foadbbf3bac2011-03-30 11:28:58 +00002601 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002602 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002603 ResAddr->addIncoming(RegAddr, InRegBlock);
2604 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002605 return ResAddr;
2606}
2607
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002608ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, bool IsReturnType) const {
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002609
2610 if (Ty->isVoidType())
2611 return ABIArgInfo::getIgnore();
2612
2613 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2614 Ty = EnumTy->getDecl()->getIntegerType();
2615
2616 uint64_t Size = getContext().getTypeSize(Ty);
2617
2618 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002619 if (IsReturnType) {
Mark Lacey23630722013-10-06 01:33:34 +00002620 if (isRecordReturnIndirect(RT, getCXXABI()))
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002621 return ABIArgInfo::getIndirect(0, false);
2622 } else {
Mark Lacey23630722013-10-06 01:33:34 +00002623 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()))
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002624 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
2625 }
2626
2627 if (RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002628 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2629
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002630 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
John McCall64aa4b32013-04-16 22:48:15 +00002631 if (Size == 128 && getTarget().getTriple().getOS() == llvm::Triple::MinGW32)
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002632 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2633 Size));
2634
2635 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2636 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2637 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002638 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002639 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2640 Size));
2641
2642 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2643 }
2644
2645 if (Ty->isPromotableIntegerType())
2646 return ABIArgInfo::getExtend();
2647
2648 return ABIArgInfo::getDirect();
2649}
2650
2651void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2652
2653 QualType RetTy = FI.getReturnType();
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002654 FI.getReturnInfo() = classify(RetTy, true);
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002655
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002656 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2657 it != ie; ++it)
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002658 it->info = classify(it->type, false);
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002659}
2660
Chris Lattnerf13721d2010-08-31 16:44:54 +00002661llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2662 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002663 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002664
Chris Lattnerf13721d2010-08-31 16:44:54 +00002665 CGBuilderTy &Builder = CGF.Builder;
2666 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2667 "ap");
2668 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2669 llvm::Type *PTy =
2670 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2671 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2672
2673 uint64_t Offset =
2674 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2675 llvm::Value *NextAddr =
2676 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2677 "ap.next");
2678 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2679
2680 return AddrTyped;
2681}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002682
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00002683namespace {
2684
Derek Schuff263366f2012-10-16 22:30:41 +00002685class NaClX86_64ABIInfo : public ABIInfo {
2686 public:
2687 NaClX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2688 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, HasAVX) {}
2689 virtual void computeInfo(CGFunctionInfo &FI) const;
2690 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2691 CodeGenFunction &CGF) const;
2692 private:
2693 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
2694 X86_64ABIInfo NInfo; // Used for everything else.
2695};
2696
2697class NaClX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2698 public:
2699 NaClX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2700 : TargetCodeGenInfo(new NaClX86_64ABIInfo(CGT, HasAVX)) {}
2701};
2702
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00002703}
2704
Derek Schuff263366f2012-10-16 22:30:41 +00002705void NaClX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2706 if (FI.getASTCallingConvention() == CC_PnaclCall)
2707 PInfo.computeInfo(FI);
2708 else
2709 NInfo.computeInfo(FI);
2710}
2711
2712llvm::Value *NaClX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2713 CodeGenFunction &CGF) const {
2714 // Always use the native convention; calling pnacl-style varargs functions
2715 // is unuspported.
2716 return NInfo.EmitVAArg(VAListAddr, Ty, CGF);
2717}
2718
2719
John McCallec853ba2010-03-11 00:10:12 +00002720// PowerPC-32
2721
2722namespace {
2723class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2724public:
Chris Lattnerea044322010-07-29 02:01:43 +00002725 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002726
John McCallec853ba2010-03-11 00:10:12 +00002727 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2728 // This is recovered from gcc output.
2729 return 1; // r1 is the dedicated stack pointer
2730 }
2731
2732 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002733 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002734};
2735
2736}
2737
2738bool
2739PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2740 llvm::Value *Address) const {
2741 // This is calculated from the LLVM and GCC tables and verified
2742 // against gcc output. AFAIK all ABIs use the same encoding.
2743
2744 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallec853ba2010-03-11 00:10:12 +00002745
Chris Lattner8b418682012-02-07 00:39:47 +00002746 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallec853ba2010-03-11 00:10:12 +00002747 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2748 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2749 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2750
2751 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002752 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002753
2754 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002755 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002756
2757 // 64-76 are various 4-byte special-purpose registers:
2758 // 64: mq
2759 // 65: lr
2760 // 66: ctr
2761 // 67: ap
2762 // 68-75 cr0-7
2763 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002764 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002765
2766 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002767 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002768
2769 // 109: vrsave
2770 // 110: vscr
2771 // 111: spe_acc
2772 // 112: spefscr
2773 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002774 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002775
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002776 return false;
John McCallec853ba2010-03-11 00:10:12 +00002777}
2778
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002779// PowerPC-64
2780
2781namespace {
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002782/// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
2783class PPC64_SVR4_ABIInfo : public DefaultABIInfo {
2784
2785public:
2786 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
2787
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002788 bool isPromotableTypeForABI(QualType Ty) const;
2789
2790 ABIArgInfo classifyReturnType(QualType RetTy) const;
2791 ABIArgInfo classifyArgumentType(QualType Ty) const;
2792
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00002793 // TODO: We can add more logic to computeInfo to improve performance.
2794 // Example: For aggregate arguments that fit in a register, we could
2795 // use getDirectInReg (as is done below for structs containing a single
2796 // floating-point value) to avoid pushing them to memory on function
2797 // entry. This would require changing the logic in PPCISelLowering
2798 // when lowering the parameters in the caller and args in the callee.
2799 virtual void computeInfo(CGFunctionInfo &FI) const {
2800 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2801 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2802 it != ie; ++it) {
2803 // We rely on the default argument classification for the most part.
2804 // One exception: An aggregate containing a single floating-point
Bill Schmidtb1993102013-07-23 22:15:57 +00002805 // or vector item must be passed in a register if one is available.
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00002806 const Type *T = isSingleElementStruct(it->type, getContext());
2807 if (T) {
2808 const BuiltinType *BT = T->getAs<BuiltinType>();
Bill Schmidtb1993102013-07-23 22:15:57 +00002809 if (T->isVectorType() || (BT && BT->isFloatingPoint())) {
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00002810 QualType QT(T, 0);
2811 it->info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
2812 continue;
2813 }
2814 }
2815 it->info = classifyArgumentType(it->type);
2816 }
2817 }
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002818
2819 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr,
2820 QualType Ty,
2821 CodeGenFunction &CGF) const;
2822};
2823
2824class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
2825public:
2826 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT)
2827 : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT)) {}
2828
2829 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2830 // This is recovered from gcc output.
2831 return 1; // r1 is the dedicated stack pointer
2832 }
2833
2834 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2835 llvm::Value *Address) const;
2836};
2837
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002838class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2839public:
2840 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
2841
2842 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2843 // This is recovered from gcc output.
2844 return 1; // r1 is the dedicated stack pointer
2845 }
2846
2847 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2848 llvm::Value *Address) const;
2849};
2850
2851}
2852
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002853// Return true if the ABI requires Ty to be passed sign- or zero-
2854// extended to 64 bits.
2855bool
2856PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
2857 // Treat an enum type as its underlying type.
2858 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2859 Ty = EnumTy->getDecl()->getIntegerType();
2860
2861 // Promotable integer types are required to be promoted by the ABI.
2862 if (Ty->isPromotableIntegerType())
2863 return true;
2864
2865 // In addition to the usual promotable integer types, we also need to
2866 // extend all 32-bit types, since the ABI requires promotion to 64 bits.
2867 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2868 switch (BT->getKind()) {
2869 case BuiltinType::Int:
2870 case BuiltinType::UInt:
2871 return true;
2872 default:
2873 break;
2874 }
2875
2876 return false;
2877}
2878
2879ABIArgInfo
2880PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
Bill Schmidtc9715fc2012-11-27 02:46:43 +00002881 if (Ty->isAnyComplexType())
2882 return ABIArgInfo::getDirect();
2883
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002884 if (isAggregateTypeForABI(Ty)) {
Mark Lacey23630722013-10-06 01:33:34 +00002885 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002886 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002887
2888 return ABIArgInfo::getIndirect(0);
2889 }
2890
2891 return (isPromotableTypeForABI(Ty) ?
2892 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2893}
2894
2895ABIArgInfo
2896PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
2897 if (RetTy->isVoidType())
2898 return ABIArgInfo::getIgnore();
2899
Bill Schmidt9e6111a2012-12-17 04:20:17 +00002900 if (RetTy->isAnyComplexType())
2901 return ABIArgInfo::getDirect();
2902
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002903 if (isAggregateTypeForABI(RetTy))
2904 return ABIArgInfo::getIndirect(0);
2905
2906 return (isPromotableTypeForABI(RetTy) ?
2907 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2908}
2909
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002910// Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
2911llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr,
2912 QualType Ty,
2913 CodeGenFunction &CGF) const {
2914 llvm::Type *BP = CGF.Int8PtrTy;
2915 llvm::Type *BPP = CGF.Int8PtrPtrTy;
2916
2917 CGBuilderTy &Builder = CGF.Builder;
2918 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
2919 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2920
Bill Schmidt19f8e852013-01-14 17:45:36 +00002921 // Update the va_list pointer. The pointer should be bumped by the
2922 // size of the object. We can trust getTypeSize() except for a complex
2923 // type whose base type is smaller than a doubleword. For these, the
2924 // size of the object is 16 bytes; see below for further explanation.
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002925 unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8;
Bill Schmidt19f8e852013-01-14 17:45:36 +00002926 QualType BaseTy;
2927 unsigned CplxBaseSize = 0;
2928
2929 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
2930 BaseTy = CTy->getElementType();
2931 CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8;
2932 if (CplxBaseSize < 8)
2933 SizeInBytes = 16;
2934 }
2935
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002936 unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8);
2937 llvm::Value *NextAddr =
2938 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset),
2939 "ap.next");
2940 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2941
Bill Schmidt19f8e852013-01-14 17:45:36 +00002942 // If we have a complex type and the base type is smaller than 8 bytes,
2943 // the ABI calls for the real and imaginary parts to be right-adjusted
2944 // in separate doublewords. However, Clang expects us to produce a
2945 // pointer to a structure with the two parts packed tightly. So generate
2946 // loads of the real and imaginary parts relative to the va_list pointer,
2947 // and store them to a temporary structure.
2948 if (CplxBaseSize && CplxBaseSize < 8) {
2949 llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2950 llvm::Value *ImagAddr = RealAddr;
2951 RealAddr = Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize));
2952 ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize));
2953 llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy));
2954 RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy);
2955 ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy);
2956 llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal");
2957 llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag");
2958 llvm::Value *Ptr = CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty),
2959 "vacplx");
2960 llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, ".real");
2961 llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, ".imag");
2962 Builder.CreateStore(Real, RealPtr, false);
2963 Builder.CreateStore(Imag, ImagPtr, false);
2964 return Ptr;
2965 }
2966
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002967 // If the argument is smaller than 8 bytes, it is right-adjusted in
2968 // its doubleword slot. Adjust the pointer to pick it up from the
2969 // correct offset.
2970 if (SizeInBytes < 8) {
2971 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2972 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes));
2973 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2974 }
2975
2976 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2977 return Builder.CreateBitCast(Addr, PTy);
2978}
2979
2980static bool
2981PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2982 llvm::Value *Address) {
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002983 // This is calculated from the LLVM and GCC tables and verified
2984 // against gcc output. AFAIK all ABIs use the same encoding.
2985
2986 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2987
2988 llvm::IntegerType *i8 = CGF.Int8Ty;
2989 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2990 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2991 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2992
2993 // 0-31: r0-31, the 8-byte general-purpose registers
2994 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
2995
2996 // 32-63: fp0-31, the 8-byte floating-point registers
2997 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
2998
2999 // 64-76 are various 4-byte special-purpose registers:
3000 // 64: mq
3001 // 65: lr
3002 // 66: ctr
3003 // 67: ap
3004 // 68-75 cr0-7
3005 // 76: xer
3006 AssignToArrayRange(Builder, Address, Four8, 64, 76);
3007
3008 // 77-108: v0-31, the 16-byte vector registers
3009 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
3010
3011 // 109: vrsave
3012 // 110: vscr
3013 // 111: spe_acc
3014 // 112: spefscr
3015 // 113: sfp
3016 AssignToArrayRange(Builder, Address, Four8, 109, 113);
3017
3018 return false;
3019}
John McCallec853ba2010-03-11 00:10:12 +00003020
Bill Schmidt2fc107f2012-10-03 19:18:57 +00003021bool
3022PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
3023 CodeGen::CodeGenFunction &CGF,
3024 llvm::Value *Address) const {
3025
3026 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
3027}
3028
3029bool
3030PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3031 llvm::Value *Address) const {
3032
3033 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
3034}
3035
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003036//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003037// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003038//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003039
3040namespace {
3041
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003042class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003043public:
3044 enum ABIKind {
3045 APCS = 0,
3046 AAPCS = 1,
3047 AAPCS_VFP
3048 };
3049
3050private:
3051 ABIKind Kind;
3052
3053public:
John McCallbd7370a2013-02-28 19:01:20 +00003054 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {
3055 setRuntimeCC();
3056 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003057
John McCall49e34be2011-08-30 01:42:09 +00003058 bool isEABI() const {
John McCall64aa4b32013-04-16 22:48:15 +00003059 StringRef Env = getTarget().getTriple().getEnvironmentName();
Logan Chien94a71422012-09-02 09:30:11 +00003060 return (Env == "gnueabi" || Env == "eabi" ||
3061 Env == "android" || Env == "androideabi");
John McCall49e34be2011-08-30 01:42:09 +00003062 }
3063
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003064 ABIKind getABIKind() const { return Kind; }
3065
Tim Northover64eac852013-10-01 14:34:25 +00003066private:
Chris Lattnera3c109b2010-07-29 02:16:43 +00003067 ABIArgInfo classifyReturnType(QualType RetTy) const;
Manman Ren710c5172012-10-31 19:02:26 +00003068 ABIArgInfo classifyArgumentType(QualType RetTy, int *VFPRegs,
3069 unsigned &AllocatedVFP,
Manman Renb3fa55f2012-10-30 23:21:41 +00003070 bool &IsHA) const;
Manman Ren97f81572012-10-16 19:18:39 +00003071 bool isIllegalVectorType(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003072
Chris Lattneree5dcd02010-07-29 02:31:05 +00003073 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003074
3075 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3076 CodeGenFunction &CGF) const;
John McCallbd7370a2013-02-28 19:01:20 +00003077
3078 llvm::CallingConv::ID getLLVMDefaultCC() const;
3079 llvm::CallingConv::ID getABIDefaultCC() const;
3080 void setRuntimeCC();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003081};
3082
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003083class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
3084public:
Chris Lattnerea044322010-07-29 02:01:43 +00003085 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
3086 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00003087
John McCall49e34be2011-08-30 01:42:09 +00003088 const ARMABIInfo &getABIInfo() const {
3089 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
3090 }
3091
John McCall6374c332010-03-06 00:35:14 +00003092 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3093 return 13;
3094 }
Roman Divacky09345d12011-05-18 19:36:54 +00003095
Chris Lattner5f9e2722011-07-23 10:55:15 +00003096 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCallf85e1932011-06-15 23:02:42 +00003097 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
3098 }
3099
Roman Divacky09345d12011-05-18 19:36:54 +00003100 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3101 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003102 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divacky09345d12011-05-18 19:36:54 +00003103
3104 // 0-15 are the 16 integer registers.
Chris Lattner8b418682012-02-07 00:39:47 +00003105 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divacky09345d12011-05-18 19:36:54 +00003106 return false;
3107 }
John McCall49e34be2011-08-30 01:42:09 +00003108
3109 unsigned getSizeOfUnwindException() const {
3110 if (getABIInfo().isEABI()) return 88;
3111 return TargetCodeGenInfo::getSizeOfUnwindException();
3112 }
Tim Northover64eac852013-10-01 14:34:25 +00003113
3114 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3115 CodeGen::CodeGenModule &CGM) const {
3116 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3117 if (!FD)
3118 return;
3119
3120 const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>();
3121 if (!Attr)
3122 return;
3123
3124 const char *Kind;
3125 switch (Attr->getInterrupt()) {
3126 case ARMInterruptAttr::Generic: Kind = ""; break;
3127 case ARMInterruptAttr::IRQ: Kind = "IRQ"; break;
3128 case ARMInterruptAttr::FIQ: Kind = "FIQ"; break;
3129 case ARMInterruptAttr::SWI: Kind = "SWI"; break;
3130 case ARMInterruptAttr::ABORT: Kind = "ABORT"; break;
3131 case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break;
3132 }
3133
3134 llvm::Function *Fn = cast<llvm::Function>(GV);
3135
3136 Fn->addFnAttr("interrupt", Kind);
3137
3138 if (cast<ARMABIInfo>(getABIInfo()).getABIKind() == ARMABIInfo::APCS)
3139 return;
3140
3141 // AAPCS guarantees that sp will be 8-byte aligned on any public interface,
3142 // however this is not necessarily true on taking any interrupt. Instruct
3143 // the backend to perform a realignment as part of the function prologue.
3144 llvm::AttrBuilder B;
3145 B.addStackAlignmentAttr(8);
3146 Fn->addAttributes(llvm::AttributeSet::FunctionIndex,
3147 llvm::AttributeSet::get(CGM.getLLVMContext(),
3148 llvm::AttributeSet::FunctionIndex,
3149 B));
3150 }
3151
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003152};
3153
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003154}
3155
Chris Lattneree5dcd02010-07-29 02:31:05 +00003156void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Manman Renb3fa55f2012-10-30 23:21:41 +00003157 // To correctly handle Homogeneous Aggregate, we need to keep track of the
Manman Ren710c5172012-10-31 19:02:26 +00003158 // VFP registers allocated so far.
Manman Renb3fa55f2012-10-30 23:21:41 +00003159 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
3160 // VFP registers of the appropriate type unallocated then the argument is
3161 // allocated to the lowest-numbered sequence of such registers.
3162 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
3163 // unallocated are marked as unavailable.
3164 unsigned AllocatedVFP = 0;
Manman Ren710c5172012-10-31 19:02:26 +00003165 int VFPRegs[16] = { 0 };
Chris Lattnera3c109b2010-07-29 02:16:43 +00003166 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003167 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Manman Renb3fa55f2012-10-30 23:21:41 +00003168 it != ie; ++it) {
3169 unsigned PreAllocation = AllocatedVFP;
3170 bool IsHA = false;
3171 // 6.1.2.3 There is one VFP co-processor register class using registers
3172 // s0-s15 (d0-d7) for passing arguments.
3173 const unsigned NumVFPs = 16;
Manman Ren710c5172012-10-31 19:02:26 +00003174 it->info = classifyArgumentType(it->type, VFPRegs, AllocatedVFP, IsHA);
Manman Renb3fa55f2012-10-30 23:21:41 +00003175 // If we do not have enough VFP registers for the HA, any VFP registers
3176 // that are unallocated are marked as unavailable. To achieve this, we add
3177 // padding of (NumVFPs - PreAllocation) floats.
3178 if (IsHA && AllocatedVFP > NumVFPs && PreAllocation < NumVFPs) {
3179 llvm::Type *PaddingTy = llvm::ArrayType::get(
3180 llvm::Type::getFloatTy(getVMContext()), NumVFPs - PreAllocation);
3181 it->info = ABIArgInfo::getExpandWithPadding(false, PaddingTy);
3182 }
3183 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003184
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003185 // Always honor user-specified calling convention.
3186 if (FI.getCallingConvention() != llvm::CallingConv::C)
3187 return;
3188
John McCallbd7370a2013-02-28 19:01:20 +00003189 llvm::CallingConv::ID cc = getRuntimeCC();
3190 if (cc != llvm::CallingConv::C)
3191 FI.setEffectiveCallingConvention(cc);
3192}
Rafael Espindola25117ab2010-06-16 16:13:39 +00003193
John McCallbd7370a2013-02-28 19:01:20 +00003194/// Return the default calling convention that LLVM will use.
3195llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
3196 // The default calling convention that LLVM will infer.
John McCall64aa4b32013-04-16 22:48:15 +00003197 if (getTarget().getTriple().getEnvironmentName()=="gnueabihf")
John McCallbd7370a2013-02-28 19:01:20 +00003198 return llvm::CallingConv::ARM_AAPCS_VFP;
3199 else if (isEABI())
3200 return llvm::CallingConv::ARM_AAPCS;
3201 else
3202 return llvm::CallingConv::ARM_APCS;
3203}
3204
3205/// Return the calling convention that our ABI would like us to use
3206/// as the C calling convention.
3207llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003208 switch (getABIKind()) {
John McCallbd7370a2013-02-28 19:01:20 +00003209 case APCS: return llvm::CallingConv::ARM_APCS;
3210 case AAPCS: return llvm::CallingConv::ARM_AAPCS;
3211 case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003212 }
John McCallbd7370a2013-02-28 19:01:20 +00003213 llvm_unreachable("bad ABI kind");
3214}
3215
3216void ARMABIInfo::setRuntimeCC() {
3217 assert(getRuntimeCC() == llvm::CallingConv::C);
3218
3219 // Don't muddy up the IR with a ton of explicit annotations if
3220 // they'd just match what LLVM will infer from the triple.
3221 llvm::CallingConv::ID abiCC = getABIDefaultCC();
3222 if (abiCC != getLLVMDefaultCC())
3223 RuntimeCC = abiCC;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003224}
3225
Bob Wilson194f06a2011-08-03 05:58:22 +00003226/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
3227/// aggregate. If HAMembers is non-null, the number of base elements
3228/// contained in the type is returned through it; this is used for the
3229/// recursive calls that check aggregate component types.
3230static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
3231 ASTContext &Context,
3232 uint64_t *HAMembers = 0) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003233 uint64_t Members = 0;
Bob Wilson194f06a2011-08-03 05:58:22 +00003234 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
3235 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
3236 return false;
3237 Members *= AT->getSize().getZExtValue();
3238 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
3239 const RecordDecl *RD = RT->getDecl();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003240 if (RD->hasFlexibleArrayMember())
Bob Wilson194f06a2011-08-03 05:58:22 +00003241 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003242
Bob Wilson194f06a2011-08-03 05:58:22 +00003243 Members = 0;
3244 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3245 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00003246 const FieldDecl *FD = *i;
Bob Wilson194f06a2011-08-03 05:58:22 +00003247 uint64_t FldMembers;
3248 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
3249 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003250
3251 Members = (RD->isUnion() ?
3252 std::max(Members, FldMembers) : Members + FldMembers);
Bob Wilson194f06a2011-08-03 05:58:22 +00003253 }
3254 } else {
3255 Members = 1;
3256 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
3257 Members = 2;
3258 Ty = CT->getElementType();
3259 }
3260
3261 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
3262 // double, or 64-bit or 128-bit vectors.
3263 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3264 if (BT->getKind() != BuiltinType::Float &&
Tim Northoveradfa45f2012-07-20 22:29:29 +00003265 BT->getKind() != BuiltinType::Double &&
3266 BT->getKind() != BuiltinType::LongDouble)
Bob Wilson194f06a2011-08-03 05:58:22 +00003267 return false;
3268 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
3269 unsigned VecSize = Context.getTypeSize(VT);
3270 if (VecSize != 64 && VecSize != 128)
3271 return false;
3272 } else {
3273 return false;
3274 }
3275
3276 // The base type must be the same for all members. Vector types of the
3277 // same total size are treated as being equivalent here.
3278 const Type *TyPtr = Ty.getTypePtr();
3279 if (!Base)
3280 Base = TyPtr;
3281 if (Base != TyPtr &&
3282 (!Base->isVectorType() || !TyPtr->isVectorType() ||
3283 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
3284 return false;
3285 }
3286
3287 // Homogeneous Aggregates can have at most 4 members of the base type.
3288 if (HAMembers)
3289 *HAMembers = Members;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003290
3291 return (Members > 0 && Members <= 4);
Bob Wilson194f06a2011-08-03 05:58:22 +00003292}
3293
Manman Ren710c5172012-10-31 19:02:26 +00003294/// markAllocatedVFPs - update VFPRegs according to the alignment and
3295/// number of VFP registers (unit is S register) requested.
3296static void markAllocatedVFPs(int *VFPRegs, unsigned &AllocatedVFP,
3297 unsigned Alignment,
3298 unsigned NumRequired) {
3299 // Early Exit.
3300 if (AllocatedVFP >= 16)
3301 return;
3302 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
3303 // VFP registers of the appropriate type unallocated then the argument is
3304 // allocated to the lowest-numbered sequence of such registers.
3305 for (unsigned I = 0; I < 16; I += Alignment) {
3306 bool FoundSlot = true;
3307 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3308 if (J >= 16 || VFPRegs[J]) {
3309 FoundSlot = false;
3310 break;
3311 }
3312 if (FoundSlot) {
3313 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3314 VFPRegs[J] = 1;
3315 AllocatedVFP += NumRequired;
3316 return;
3317 }
3318 }
3319 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
3320 // unallocated are marked as unavailable.
3321 for (unsigned I = 0; I < 16; I++)
3322 VFPRegs[I] = 1;
3323 AllocatedVFP = 17; // We do not have enough VFP registers.
3324}
3325
3326ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, int *VFPRegs,
3327 unsigned &AllocatedVFP,
Manman Renb3fa55f2012-10-30 23:21:41 +00003328 bool &IsHA) const {
3329 // We update number of allocated VFPs according to
3330 // 6.1.2.1 The following argument types are VFP CPRCs:
3331 // A single-precision floating-point type (including promoted
3332 // half-precision types); A double-precision floating-point type;
3333 // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
3334 // with a Base Type of a single- or double-precision floating-point type,
3335 // 64-bit containerized vectors or 128-bit containerized vectors with one
3336 // to four Elements.
3337
Manman Ren97f81572012-10-16 19:18:39 +00003338 // Handle illegal vector types here.
3339 if (isIllegalVectorType(Ty)) {
3340 uint64_t Size = getContext().getTypeSize(Ty);
3341 if (Size <= 32) {
3342 llvm::Type *ResType =
3343 llvm::Type::getInt32Ty(getVMContext());
3344 return ABIArgInfo::getDirect(ResType);
3345 }
3346 if (Size == 64) {
3347 llvm::Type *ResType = llvm::VectorType::get(
3348 llvm::Type::getInt32Ty(getVMContext()), 2);
Manman Ren710c5172012-10-31 19:02:26 +00003349 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
Manman Ren97f81572012-10-16 19:18:39 +00003350 return ABIArgInfo::getDirect(ResType);
3351 }
3352 if (Size == 128) {
3353 llvm::Type *ResType = llvm::VectorType::get(
3354 llvm::Type::getInt32Ty(getVMContext()), 4);
Manman Ren710c5172012-10-31 19:02:26 +00003355 markAllocatedVFPs(VFPRegs, AllocatedVFP, 4, 4);
Manman Ren97f81572012-10-16 19:18:39 +00003356 return ABIArgInfo::getDirect(ResType);
3357 }
3358 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3359 }
Manman Ren710c5172012-10-31 19:02:26 +00003360 // Update VFPRegs for legal vector types.
Manman Renb3fa55f2012-10-30 23:21:41 +00003361 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3362 uint64_t Size = getContext().getTypeSize(VT);
3363 // Size of a legal vector should be power of 2 and above 64.
Manman Ren710c5172012-10-31 19:02:26 +00003364 markAllocatedVFPs(VFPRegs, AllocatedVFP, Size >= 128 ? 4 : 2, Size / 32);
Manman Renb3fa55f2012-10-30 23:21:41 +00003365 }
Manman Ren710c5172012-10-31 19:02:26 +00003366 // Update VFPRegs for floating point types.
Manman Renb3fa55f2012-10-30 23:21:41 +00003367 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3368 if (BT->getKind() == BuiltinType::Half ||
3369 BT->getKind() == BuiltinType::Float)
Manman Ren710c5172012-10-31 19:02:26 +00003370 markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, 1);
Manman Renb3fa55f2012-10-30 23:21:41 +00003371 if (BT->getKind() == BuiltinType::Double ||
Manman Ren710c5172012-10-31 19:02:26 +00003372 BT->getKind() == BuiltinType::LongDouble)
3373 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
Manman Renb3fa55f2012-10-30 23:21:41 +00003374 }
Manman Ren97f81572012-10-16 19:18:39 +00003375
John McCalld608cdb2010-08-22 10:59:02 +00003376 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003377 // Treat an enum type as its underlying type.
3378 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3379 Ty = EnumTy->getDecl()->getIntegerType();
3380
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003381 return (Ty->isPromotableIntegerType() ?
3382 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003383 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003384
Mark Lacey23630722013-10-06 01:33:34 +00003385 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Tim Northoverf5c3a252013-06-21 22:49:34 +00003386 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
3387
Daniel Dunbar42025572009-09-14 21:54:03 +00003388 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003389 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00003390 return ABIArgInfo::getIgnore();
3391
Bob Wilson194f06a2011-08-03 05:58:22 +00003392 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
Manman Renb3fa55f2012-10-30 23:21:41 +00003393 // Homogeneous Aggregates need to be expanded when we can fit the aggregate
3394 // into VFP registers.
Bob Wilson194f06a2011-08-03 05:58:22 +00003395 const Type *Base = 0;
Manman Renb3fa55f2012-10-30 23:21:41 +00003396 uint64_t Members = 0;
3397 if (isHomogeneousAggregate(Ty, Base, getContext(), &Members)) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003398 assert(Base && "Base class should be set for homogeneous aggregate");
Manman Renb3fa55f2012-10-30 23:21:41 +00003399 // Base can be a floating-point or a vector.
3400 if (Base->isVectorType()) {
3401 // ElementSize is in number of floats.
3402 unsigned ElementSize = getContext().getTypeSize(Base) == 64 ? 2 : 4;
Manman Rencb489dd2012-11-06 19:05:29 +00003403 markAllocatedVFPs(VFPRegs, AllocatedVFP, ElementSize,
3404 Members * ElementSize);
Manman Renb3fa55f2012-10-30 23:21:41 +00003405 } else if (Base->isSpecificBuiltinType(BuiltinType::Float))
Manman Ren710c5172012-10-31 19:02:26 +00003406 markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, Members);
Manman Renb3fa55f2012-10-30 23:21:41 +00003407 else {
3408 assert(Base->isSpecificBuiltinType(BuiltinType::Double) ||
3409 Base->isSpecificBuiltinType(BuiltinType::LongDouble));
Manman Ren710c5172012-10-31 19:02:26 +00003410 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, Members * 2);
Manman Renb3fa55f2012-10-30 23:21:41 +00003411 }
3412 IsHA = true;
Bob Wilson194f06a2011-08-03 05:58:22 +00003413 return ABIArgInfo::getExpand();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003414 }
Bob Wilson194f06a2011-08-03 05:58:22 +00003415 }
3416
Manman Ren634b3d22012-08-13 21:23:55 +00003417 // Support byval for ARM.
Manman Rencb489dd2012-11-06 19:05:29 +00003418 // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
3419 // most 8-byte. We realign the indirect argument if type alignment is bigger
3420 // than ABI alignment.
Manman Renfd1ba912012-11-05 22:42:46 +00003421 uint64_t ABIAlign = 4;
3422 uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8;
3423 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3424 getABIKind() == ARMABIInfo::AAPCS)
3425 ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
Manman Ren885ad692012-11-06 04:58:01 +00003426 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
3427 return ABIArgInfo::getIndirect(0, /*ByVal=*/true,
Manman Rencb489dd2012-11-06 19:05:29 +00003428 /*Realign=*/TyAlign > ABIAlign);
Eli Friedman79f30982012-08-09 00:31:40 +00003429 }
3430
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00003431 // Otherwise, pass by coercing to a structure of the appropriate size.
Chris Lattner2acc6e32011-07-18 04:24:23 +00003432 llvm::Type* ElemTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003433 unsigned SizeRegs;
Eli Friedman79f30982012-08-09 00:31:40 +00003434 // FIXME: Try to match the types of the arguments more accurately where
3435 // we can.
3436 if (getContext().getTypeAlign(Ty) <= 32) {
Bob Wilson53fc1a62011-08-01 23:39:04 +00003437 ElemTy = llvm::Type::getInt32Ty(getVMContext());
3438 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Manman Ren78eb76e2012-06-25 22:04:00 +00003439 } else {
Manman Ren78eb76e2012-06-25 22:04:00 +00003440 ElemTy = llvm::Type::getInt64Ty(getVMContext());
3441 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastings67d097e2011-04-27 17:24:02 +00003442 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003443
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003444 llvm::Type *STy =
Chris Lattner7650d952011-06-18 22:49:11 +00003445 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003446 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003447}
3448
Chris Lattnera3c109b2010-07-29 02:16:43 +00003449static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00003450 llvm::LLVMContext &VMContext) {
3451 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
3452 // is called integer-like if its size is less than or equal to one word, and
3453 // the offset of each of its addressable sub-fields is zero.
3454
3455 uint64_t Size = Context.getTypeSize(Ty);
3456
3457 // Check that the type fits in a word.
3458 if (Size > 32)
3459 return false;
3460
3461 // FIXME: Handle vector types!
3462 if (Ty->isVectorType())
3463 return false;
3464
Daniel Dunbarb0d58192009-09-14 02:20:34 +00003465 // Float types are never treated as "integer like".
3466 if (Ty->isRealFloatingType())
3467 return false;
3468
Daniel Dunbar98303b92009-09-13 08:03:58 +00003469 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00003470 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00003471 return true;
3472
Daniel Dunbar45815812010-02-01 23:31:26 +00003473 // Small complex integer types are "integer like".
3474 if (const ComplexType *CT = Ty->getAs<ComplexType>())
3475 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003476
3477 // Single element and zero sized arrays should be allowed, by the definition
3478 // above, but they are not.
3479
3480 // Otherwise, it must be a record type.
3481 const RecordType *RT = Ty->getAs<RecordType>();
3482 if (!RT) return false;
3483
3484 // Ignore records with flexible arrays.
3485 const RecordDecl *RD = RT->getDecl();
3486 if (RD->hasFlexibleArrayMember())
3487 return false;
3488
3489 // Check that all sub-fields are at offset 0, and are themselves "integer
3490 // like".
3491 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3492
3493 bool HadField = false;
3494 unsigned idx = 0;
3495 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3496 i != e; ++i, ++idx) {
David Blaikie581deb32012-06-06 20:45:41 +00003497 const FieldDecl *FD = *i;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003498
Daniel Dunbar679855a2010-01-29 03:22:29 +00003499 // Bit-fields are not addressable, we only need to verify they are "integer
3500 // like". We still have to disallow a subsequent non-bitfield, for example:
3501 // struct { int : 0; int x }
3502 // is non-integer like according to gcc.
3503 if (FD->isBitField()) {
3504 if (!RD->isUnion())
3505 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003506
Daniel Dunbar679855a2010-01-29 03:22:29 +00003507 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3508 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003509
Daniel Dunbar679855a2010-01-29 03:22:29 +00003510 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003511 }
3512
Daniel Dunbar679855a2010-01-29 03:22:29 +00003513 // Check if this field is at offset 0.
3514 if (Layout.getFieldOffset(idx) != 0)
3515 return false;
3516
Daniel Dunbar98303b92009-09-13 08:03:58 +00003517 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3518 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003519
Daniel Dunbar679855a2010-01-29 03:22:29 +00003520 // Only allow at most one field in a structure. This doesn't match the
3521 // wording above, but follows gcc in situations with a field following an
3522 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00003523 if (!RD->isUnion()) {
3524 if (HadField)
3525 return false;
3526
3527 HadField = true;
3528 }
3529 }
3530
3531 return true;
3532}
3533
Chris Lattnera3c109b2010-07-29 02:16:43 +00003534ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003535 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003536 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00003537
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00003538 // Large vector types should be returned via memory.
3539 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
3540 return ABIArgInfo::getIndirect(0);
3541
John McCalld608cdb2010-08-22 10:59:02 +00003542 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003543 // Treat an enum type as its underlying type.
3544 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3545 RetTy = EnumTy->getDecl()->getIntegerType();
3546
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003547 return (RetTy->isPromotableIntegerType() ?
3548 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003549 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003550
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003551 // Structures with either a non-trivial destructor or a non-trivial
3552 // copy constructor are always indirect.
Mark Lacey23630722013-10-06 01:33:34 +00003553 if (isRecordReturnIndirect(RetTy, getCXXABI()))
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003554 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3555
Daniel Dunbar98303b92009-09-13 08:03:58 +00003556 // Are we following APCS?
3557 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00003558 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00003559 return ABIArgInfo::getIgnore();
3560
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003561 // Complex types are all returned as packed integers.
3562 //
3563 // FIXME: Consider using 2 x vector types if the back end handles them
3564 // correctly.
3565 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00003566 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00003567 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003568
Daniel Dunbar98303b92009-09-13 08:03:58 +00003569 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003570 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003571 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003572 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003573 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003574 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003575 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003576 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3577 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003578 }
3579
3580 // Otherwise return in memory.
3581 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003582 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003583
3584 // Otherwise this is an AAPCS variant.
3585
Chris Lattnera3c109b2010-07-29 02:16:43 +00003586 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00003587 return ABIArgInfo::getIgnore();
3588
Bob Wilson3b694fa2011-11-02 04:51:36 +00003589 // Check for homogeneous aggregates with AAPCS-VFP.
3590 if (getABIKind() == AAPCS_VFP) {
3591 const Type *Base = 0;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003592 if (isHomogeneousAggregate(RetTy, Base, getContext())) {
3593 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson3b694fa2011-11-02 04:51:36 +00003594 // Homogeneous Aggregates are returned directly.
3595 return ABIArgInfo::getDirect();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003596 }
Bob Wilson3b694fa2011-11-02 04:51:36 +00003597 }
3598
Daniel Dunbar98303b92009-09-13 08:03:58 +00003599 // Aggregates <= 4 bytes are returned in r0; other aggregates
3600 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003601 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00003602 if (Size <= 32) {
3603 // Return in the smallest viable integer type.
3604 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003605 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003606 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003607 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3608 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003609 }
3610
Daniel Dunbar98303b92009-09-13 08:03:58 +00003611 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003612}
3613
Manman Ren97f81572012-10-16 19:18:39 +00003614/// isIllegalVector - check whether Ty is an illegal vector type.
3615bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
3616 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3617 // Check whether VT is legal.
3618 unsigned NumElements = VT->getNumElements();
3619 uint64_t Size = getContext().getTypeSize(VT);
3620 // NumElements should be power of 2.
3621 if ((NumElements & (NumElements - 1)) != 0)
3622 return true;
3623 // Size should be greater than 32 bits.
3624 return Size <= 32;
3625 }
3626 return false;
3627}
3628
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003629llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00003630 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003631 llvm::Type *BP = CGF.Int8PtrTy;
3632 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003633
3634 CGBuilderTy &Builder = CGF.Builder;
Chris Lattner8b418682012-02-07 00:39:47 +00003635 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003636 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Manman Rend105e732012-10-16 19:01:37 +00003637
Tim Northover373ac0a2013-06-21 23:05:33 +00003638 if (isEmptyRecord(getContext(), Ty, true)) {
3639 // These are ignored for parameter passing purposes.
3640 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3641 return Builder.CreateBitCast(Addr, PTy);
3642 }
3643
Manman Rend105e732012-10-16 19:01:37 +00003644 uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8;
Rafael Espindolae164c182011-08-02 22:33:37 +00003645 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
Manman Ren97f81572012-10-16 19:18:39 +00003646 bool IsIndirect = false;
Manman Rend105e732012-10-16 19:01:37 +00003647
3648 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
3649 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
Manman Ren93371022012-10-16 19:51:48 +00003650 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3651 getABIKind() == ARMABIInfo::AAPCS)
3652 TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
3653 else
3654 TyAlign = 4;
Manman Ren97f81572012-10-16 19:18:39 +00003655 // Use indirect if size of the illegal vector is bigger than 16 bytes.
3656 if (isIllegalVectorType(Ty) && Size > 16) {
3657 IsIndirect = true;
3658 Size = 4;
3659 TyAlign = 4;
3660 }
Manman Rend105e732012-10-16 19:01:37 +00003661
3662 // Handle address alignment for ABI alignment > 4 bytes.
Rafael Espindolae164c182011-08-02 22:33:37 +00003663 if (TyAlign > 4) {
3664 assert((TyAlign & (TyAlign - 1)) == 0 &&
3665 "Alignment is not power of 2!");
3666 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3667 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
3668 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
Manman Rend105e732012-10-16 19:01:37 +00003669 Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align");
Rafael Espindolae164c182011-08-02 22:33:37 +00003670 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003671
3672 uint64_t Offset =
Manman Rend105e732012-10-16 19:01:37 +00003673 llvm::RoundUpToAlignment(Size, 4);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003674 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00003675 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003676 "ap.next");
3677 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3678
Manman Ren97f81572012-10-16 19:18:39 +00003679 if (IsIndirect)
3680 Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP));
Manman Ren93371022012-10-16 19:51:48 +00003681 else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) {
Manman Rend105e732012-10-16 19:01:37 +00003682 // We can't directly cast ap.cur to pointer to a vector type, since ap.cur
3683 // may not be correctly aligned for the vector type. We create an aligned
3684 // temporary space and copy the content over from ap.cur to the temporary
3685 // space. This is necessary if the natural alignment of the type is greater
3686 // than the ABI alignment.
3687 llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
3688 CharUnits CharSize = getContext().getTypeSizeInChars(Ty);
3689 llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty),
3690 "var.align");
3691 llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
3692 llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy);
3693 Builder.CreateMemCpy(Dst, Src,
3694 llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()),
3695 TyAlign, false);
3696 Addr = AlignedTemp; //The content is in aligned location.
3697 }
3698 llvm::Type *PTy =
3699 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3700 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3701
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003702 return AddrTyped;
3703}
3704
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00003705namespace {
3706
Derek Schuff263366f2012-10-16 22:30:41 +00003707class NaClARMABIInfo : public ABIInfo {
3708 public:
3709 NaClARMABIInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3710 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, Kind) {}
3711 virtual void computeInfo(CGFunctionInfo &FI) const;
3712 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3713 CodeGenFunction &CGF) const;
3714 private:
3715 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
3716 ARMABIInfo NInfo; // Used for everything else.
3717};
3718
3719class NaClARMTargetCodeGenInfo : public TargetCodeGenInfo {
3720 public:
3721 NaClARMTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3722 : TargetCodeGenInfo(new NaClARMABIInfo(CGT, Kind)) {}
3723};
3724
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00003725}
3726
Derek Schuff263366f2012-10-16 22:30:41 +00003727void NaClARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
3728 if (FI.getASTCallingConvention() == CC_PnaclCall)
3729 PInfo.computeInfo(FI);
3730 else
3731 static_cast<const ABIInfo&>(NInfo).computeInfo(FI);
3732}
3733
3734llvm::Value *NaClARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3735 CodeGenFunction &CGF) const {
3736 // Always use the native convention; calling pnacl-style varargs functions
3737 // is unsupported.
3738 return static_cast<const ABIInfo&>(NInfo).EmitVAArg(VAListAddr, Ty, CGF);
3739}
3740
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003741//===----------------------------------------------------------------------===//
Tim Northoverc264e162013-01-31 12:13:10 +00003742// AArch64 ABI Implementation
3743//===----------------------------------------------------------------------===//
3744
3745namespace {
3746
3747class AArch64ABIInfo : public ABIInfo {
3748public:
3749 AArch64ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3750
3751private:
3752 // The AArch64 PCS is explicit about return types and argument types being
3753 // handled identically, so we don't need to draw a distinction between
3754 // Argument and Return classification.
3755 ABIArgInfo classifyGenericType(QualType Ty, int &FreeIntRegs,
3756 int &FreeVFPRegs) const;
3757
3758 ABIArgInfo tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded, bool IsInt,
3759 llvm::Type *DirectTy = 0) const;
3760
3761 virtual void computeInfo(CGFunctionInfo &FI) const;
3762
3763 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3764 CodeGenFunction &CGF) const;
3765};
3766
3767class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
3768public:
3769 AArch64TargetCodeGenInfo(CodeGenTypes &CGT)
3770 :TargetCodeGenInfo(new AArch64ABIInfo(CGT)) {}
3771
3772 const AArch64ABIInfo &getABIInfo() const {
3773 return static_cast<const AArch64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
3774 }
3775
3776 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3777 return 31;
3778 }
3779
3780 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3781 llvm::Value *Address) const {
3782 // 0-31 are x0-x30 and sp: 8 bytes each
3783 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
3784 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 31);
3785
3786 // 64-95 are v0-v31: 16 bytes each
3787 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
3788 AssignToArrayRange(CGF.Builder, Address, Sixteen8, 64, 95);
3789
3790 return false;
3791 }
3792
3793};
3794
3795}
3796
3797void AArch64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
3798 int FreeIntRegs = 8, FreeVFPRegs = 8;
3799
3800 FI.getReturnInfo() = classifyGenericType(FI.getReturnType(),
3801 FreeIntRegs, FreeVFPRegs);
3802
3803 FreeIntRegs = FreeVFPRegs = 8;
3804 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3805 it != ie; ++it) {
3806 it->info = classifyGenericType(it->type, FreeIntRegs, FreeVFPRegs);
3807
3808 }
3809}
3810
3811ABIArgInfo
3812AArch64ABIInfo::tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded,
3813 bool IsInt, llvm::Type *DirectTy) const {
3814 if (FreeRegs >= RegsNeeded) {
3815 FreeRegs -= RegsNeeded;
3816 return ABIArgInfo::getDirect(DirectTy);
3817 }
3818
3819 llvm::Type *Padding = 0;
3820
3821 // We need padding so that later arguments don't get filled in anyway. That
3822 // wouldn't happen if only ByVal arguments followed in the same category, but
3823 // a large structure will simply seem to be a pointer as far as LLVM is
3824 // concerned.
3825 if (FreeRegs > 0) {
3826 if (IsInt)
3827 Padding = llvm::Type::getInt64Ty(getVMContext());
3828 else
3829 Padding = llvm::Type::getFloatTy(getVMContext());
3830
3831 // Either [N x i64] or [N x float].
3832 Padding = llvm::ArrayType::get(Padding, FreeRegs);
3833 FreeRegs = 0;
3834 }
3835
3836 return ABIArgInfo::getIndirect(getContext().getTypeAlign(Ty) / 8,
3837 /*IsByVal=*/ true, /*Realign=*/ false,
3838 Padding);
3839}
3840
3841
3842ABIArgInfo AArch64ABIInfo::classifyGenericType(QualType Ty,
3843 int &FreeIntRegs,
3844 int &FreeVFPRegs) const {
3845 // Can only occurs for return, but harmless otherwise.
3846 if (Ty->isVoidType())
3847 return ABIArgInfo::getIgnore();
3848
3849 // Large vector types should be returned via memory. There's no such concept
3850 // in the ABI, but they'd be over 16 bytes anyway so no matter how they're
3851 // classified they'd go into memory (see B.3).
3852 if (Ty->isVectorType() && getContext().getTypeSize(Ty) > 128) {
3853 if (FreeIntRegs > 0)
3854 --FreeIntRegs;
3855 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3856 }
3857
3858 // All non-aggregate LLVM types have a concrete ABI representation so they can
3859 // be passed directly. After this block we're guaranteed to be in a
3860 // complicated case.
3861 if (!isAggregateTypeForABI(Ty)) {
3862 // Treat an enum type as its underlying type.
3863 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3864 Ty = EnumTy->getDecl()->getIntegerType();
3865
3866 if (Ty->isFloatingType() || Ty->isVectorType())
3867 return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ false);
3868
3869 assert(getContext().getTypeSize(Ty) <= 128 &&
3870 "unexpectedly large scalar type");
3871
3872 int RegsNeeded = getContext().getTypeSize(Ty) > 64 ? 2 : 1;
3873
3874 // If the type may need padding registers to ensure "alignment", we must be
3875 // careful when this is accounted for. Increasing the effective size covers
3876 // all cases.
3877 if (getContext().getTypeAlign(Ty) == 128)
3878 RegsNeeded += FreeIntRegs % 2 != 0;
3879
3880 return tryUseRegs(Ty, FreeIntRegs, RegsNeeded, /*IsInt=*/ true);
3881 }
3882
Mark Lacey23630722013-10-06 01:33:34 +00003883 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00003884 if (FreeIntRegs > 0 && RAA == CGCXXABI::RAA_Indirect)
Tim Northoverc264e162013-01-31 12:13:10 +00003885 --FreeIntRegs;
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00003886 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Tim Northoverc264e162013-01-31 12:13:10 +00003887 }
3888
3889 if (isEmptyRecord(getContext(), Ty, true)) {
3890 if (!getContext().getLangOpts().CPlusPlus) {
3891 // Empty structs outside C++ mode are a GNU extension, so no ABI can
3892 // possibly tell us what to do. It turns out (I believe) that GCC ignores
3893 // the object for parameter-passsing purposes.
3894 return ABIArgInfo::getIgnore();
3895 }
3896
3897 // The combination of C++98 9p5 (sizeof(struct) != 0) and the pseudocode
3898 // description of va_arg in the PCS require that an empty struct does
3899 // actually occupy space for parameter-passing. I'm hoping for a
3900 // clarification giving an explicit paragraph to point to in future.
3901 return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ true,
3902 llvm::Type::getInt8Ty(getVMContext()));
3903 }
3904
3905 // Homogeneous vector aggregates get passed in registers or on the stack.
3906 const Type *Base = 0;
3907 uint64_t NumMembers = 0;
3908 if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)) {
3909 assert(Base && "Base class should be set for homogeneous aggregate");
3910 // Homogeneous aggregates are passed and returned directly.
3911 return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ NumMembers,
3912 /*IsInt=*/ false);
3913 }
3914
3915 uint64_t Size = getContext().getTypeSize(Ty);
3916 if (Size <= 128) {
3917 // Small structs can use the same direct type whether they're in registers
3918 // or on the stack.
3919 llvm::Type *BaseTy;
3920 unsigned NumBases;
3921 int SizeInRegs = (Size + 63) / 64;
3922
3923 if (getContext().getTypeAlign(Ty) == 128) {
3924 BaseTy = llvm::Type::getIntNTy(getVMContext(), 128);
3925 NumBases = 1;
3926
3927 // If the type may need padding registers to ensure "alignment", we must
3928 // be careful when this is accounted for. Increasing the effective size
3929 // covers all cases.
3930 SizeInRegs += FreeIntRegs % 2 != 0;
3931 } else {
3932 BaseTy = llvm::Type::getInt64Ty(getVMContext());
3933 NumBases = SizeInRegs;
3934 }
3935 llvm::Type *DirectTy = llvm::ArrayType::get(BaseTy, NumBases);
3936
3937 return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ SizeInRegs,
3938 /*IsInt=*/ true, DirectTy);
3939 }
3940
3941 // If the aggregate is > 16 bytes, it's passed and returned indirectly. In
3942 // LLVM terms the return uses an "sret" pointer, but that's handled elsewhere.
3943 --FreeIntRegs;
3944 return ABIArgInfo::getIndirect(0, /* byVal = */ false);
3945}
3946
3947llvm::Value *AArch64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3948 CodeGenFunction &CGF) const {
3949 // The AArch64 va_list type and handling is specified in the Procedure Call
3950 // Standard, section B.4:
3951 //
3952 // struct {
3953 // void *__stack;
3954 // void *__gr_top;
3955 // void *__vr_top;
3956 // int __gr_offs;
3957 // int __vr_offs;
3958 // };
3959
3960 assert(!CGF.CGM.getDataLayout().isBigEndian()
3961 && "va_arg not implemented for big-endian AArch64");
3962
3963 int FreeIntRegs = 8, FreeVFPRegs = 8;
3964 Ty = CGF.getContext().getCanonicalType(Ty);
3965 ABIArgInfo AI = classifyGenericType(Ty, FreeIntRegs, FreeVFPRegs);
3966
3967 llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
3968 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
3969 llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
3970 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
3971
3972 llvm::Value *reg_offs_p = 0, *reg_offs = 0;
3973 int reg_top_index;
3974 int RegSize;
3975 if (FreeIntRegs < 8) {
3976 assert(FreeVFPRegs == 8 && "Arguments never split between int & VFP regs");
3977 // 3 is the field number of __gr_offs
3978 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p");
3979 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
3980 reg_top_index = 1; // field number for __gr_top
3981 RegSize = 8 * (8 - FreeIntRegs);
3982 } else {
3983 assert(FreeVFPRegs < 8 && "Argument must go in VFP or int regs");
3984 // 4 is the field number of __vr_offs.
3985 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p");
3986 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
3987 reg_top_index = 2; // field number for __vr_top
3988 RegSize = 16 * (8 - FreeVFPRegs);
3989 }
3990
3991 //=======================================
3992 // Find out where argument was passed
3993 //=======================================
3994
3995 // If reg_offs >= 0 we're already using the stack for this type of
3996 // argument. We don't want to keep updating reg_offs (in case it overflows,
3997 // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
3998 // whatever they get).
3999 llvm::Value *UsingStack = 0;
4000 UsingStack = CGF.Builder.CreateICmpSGE(reg_offs,
4001 llvm::ConstantInt::get(CGF.Int32Ty, 0));
4002
4003 CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
4004
4005 // Otherwise, at least some kind of argument could go in these registers, the
4006 // quesiton is whether this particular type is too big.
4007 CGF.EmitBlock(MaybeRegBlock);
4008
4009 // Integer arguments may need to correct register alignment (for example a
4010 // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
4011 // align __gr_offs to calculate the potential address.
4012 if (FreeIntRegs < 8 && AI.isDirect() && getContext().getTypeAlign(Ty) > 64) {
4013 int Align = getContext().getTypeAlign(Ty) / 8;
4014
4015 reg_offs = CGF.Builder.CreateAdd(reg_offs,
4016 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
4017 "align_regoffs");
4018 reg_offs = CGF.Builder.CreateAnd(reg_offs,
4019 llvm::ConstantInt::get(CGF.Int32Ty, -Align),
4020 "aligned_regoffs");
4021 }
4022
4023 // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
4024 llvm::Value *NewOffset = 0;
4025 NewOffset = CGF.Builder.CreateAdd(reg_offs,
4026 llvm::ConstantInt::get(CGF.Int32Ty, RegSize),
4027 "new_reg_offs");
4028 CGF.Builder.CreateStore(NewOffset, reg_offs_p);
4029
4030 // Now we're in a position to decide whether this argument really was in
4031 // registers or not.
4032 llvm::Value *InRegs = 0;
4033 InRegs = CGF.Builder.CreateICmpSLE(NewOffset,
4034 llvm::ConstantInt::get(CGF.Int32Ty, 0),
4035 "inreg");
4036
4037 CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
4038
4039 //=======================================
4040 // Argument was in registers
4041 //=======================================
4042
4043 // Now we emit the code for if the argument was originally passed in
4044 // registers. First start the appropriate block:
4045 CGF.EmitBlock(InRegBlock);
4046
4047 llvm::Value *reg_top_p = 0, *reg_top = 0;
4048 reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p");
4049 reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
4050 llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs);
4051 llvm::Value *RegAddr = 0;
4052 llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
4053
4054 if (!AI.isDirect()) {
4055 // If it's been passed indirectly (actually a struct), whatever we find from
4056 // stored registers or on the stack will actually be a struct **.
4057 MemTy = llvm::PointerType::getUnqual(MemTy);
4058 }
4059
4060 const Type *Base = 0;
4061 uint64_t NumMembers;
4062 if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)
4063 && NumMembers > 1) {
4064 // Homogeneous aggregates passed in registers will have their elements split
4065 // and stored 16-bytes apart regardless of size (they're notionally in qN,
4066 // qN+1, ...). We reload and store into a temporary local variable
4067 // contiguously.
4068 assert(AI.isDirect() && "Homogeneous aggregates should be passed directly");
4069 llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
4070 llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
4071 llvm::Value *Tmp = CGF.CreateTempAlloca(HFATy);
4072
4073 for (unsigned i = 0; i < NumMembers; ++i) {
4074 llvm::Value *BaseOffset = llvm::ConstantInt::get(CGF.Int32Ty, 16 * i);
4075 llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset);
4076 LoadAddr = CGF.Builder.CreateBitCast(LoadAddr,
4077 llvm::PointerType::getUnqual(BaseTy));
4078 llvm::Value *StoreAddr = CGF.Builder.CreateStructGEP(Tmp, i);
4079
4080 llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
4081 CGF.Builder.CreateStore(Elem, StoreAddr);
4082 }
4083
4084 RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy);
4085 } else {
4086 // Otherwise the object is contiguous in memory
4087 RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy);
4088 }
4089
4090 CGF.EmitBranch(ContBlock);
4091
4092 //=======================================
4093 // Argument was on the stack
4094 //=======================================
4095 CGF.EmitBlock(OnStackBlock);
4096
4097 llvm::Value *stack_p = 0, *OnStackAddr = 0;
4098 stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p");
4099 OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack");
4100
4101 // Again, stack arguments may need realigmnent. In this case both integer and
4102 // floating-point ones might be affected.
4103 if (AI.isDirect() && getContext().getTypeAlign(Ty) > 64) {
4104 int Align = getContext().getTypeAlign(Ty) / 8;
4105
4106 OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty);
4107
4108 OnStackAddr = CGF.Builder.CreateAdd(OnStackAddr,
4109 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
4110 "align_stack");
4111 OnStackAddr = CGF.Builder.CreateAnd(OnStackAddr,
4112 llvm::ConstantInt::get(CGF.Int64Ty, -Align),
4113 "align_stack");
4114
4115 OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy);
4116 }
4117
4118 uint64_t StackSize;
4119 if (AI.isDirect())
4120 StackSize = getContext().getTypeSize(Ty) / 8;
4121 else
4122 StackSize = 8;
4123
4124 // All stack slots are 8 bytes
4125 StackSize = llvm::RoundUpToAlignment(StackSize, 8);
4126
4127 llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize);
4128 llvm::Value *NewStack = CGF.Builder.CreateGEP(OnStackAddr, StackSizeC,
4129 "new_stack");
4130
4131 // Write the new value of __stack for the next call to va_arg
4132 CGF.Builder.CreateStore(NewStack, stack_p);
4133
4134 OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy);
4135
4136 CGF.EmitBranch(ContBlock);
4137
4138 //=======================================
4139 // Tidy up
4140 //=======================================
4141 CGF.EmitBlock(ContBlock);
4142
4143 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr");
4144 ResAddr->addIncoming(RegAddr, InRegBlock);
4145 ResAddr->addIncoming(OnStackAddr, OnStackBlock);
4146
4147 if (AI.isDirect())
4148 return ResAddr;
4149
4150 return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr");
4151}
4152
4153//===----------------------------------------------------------------------===//
Justin Holewinski2c585b92012-05-24 17:43:12 +00004154// NVPTX ABI Implementation
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004155//===----------------------------------------------------------------------===//
4156
4157namespace {
4158
Justin Holewinski2c585b92012-05-24 17:43:12 +00004159class NVPTXABIInfo : public ABIInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004160public:
Justin Holewinskidca8f332013-03-30 14:38:24 +00004161 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004162
4163 ABIArgInfo classifyReturnType(QualType RetTy) const;
4164 ABIArgInfo classifyArgumentType(QualType Ty) const;
4165
4166 virtual void computeInfo(CGFunctionInfo &FI) const;
4167 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4168 CodeGenFunction &CFG) const;
4169};
4170
Justin Holewinski2c585b92012-05-24 17:43:12 +00004171class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004172public:
Justin Holewinski2c585b92012-05-24 17:43:12 +00004173 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
4174 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
Justin Holewinski818eafb2011-10-05 17:58:44 +00004175
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004176 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4177 CodeGen::CodeGenModule &M) const;
Justin Holewinskidca8f332013-03-30 14:38:24 +00004178private:
4179 static void addKernelMetadata(llvm::Function *F);
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004180};
4181
Justin Holewinski2c585b92012-05-24 17:43:12 +00004182ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004183 if (RetTy->isVoidType())
4184 return ABIArgInfo::getIgnore();
4185 if (isAggregateTypeForABI(RetTy))
4186 return ABIArgInfo::getIndirect(0);
4187 return ABIArgInfo::getDirect();
4188}
4189
Justin Holewinski2c585b92012-05-24 17:43:12 +00004190ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004191 if (isAggregateTypeForABI(Ty))
4192 return ABIArgInfo::getIndirect(0);
4193
4194 return ABIArgInfo::getDirect();
4195}
4196
Justin Holewinski2c585b92012-05-24 17:43:12 +00004197void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004198 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4199 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4200 it != ie; ++it)
4201 it->info = classifyArgumentType(it->type);
4202
4203 // Always honor user-specified calling convention.
4204 if (FI.getCallingConvention() != llvm::CallingConv::C)
4205 return;
4206
John McCallbd7370a2013-02-28 19:01:20 +00004207 FI.setEffectiveCallingConvention(getRuntimeCC());
4208}
4209
Justin Holewinski2c585b92012-05-24 17:43:12 +00004210llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4211 CodeGenFunction &CFG) const {
4212 llvm_unreachable("NVPTX does not support varargs");
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004213}
4214
Justin Holewinski2c585b92012-05-24 17:43:12 +00004215void NVPTXTargetCodeGenInfo::
4216SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4217 CodeGen::CodeGenModule &M) const{
Justin Holewinski818eafb2011-10-05 17:58:44 +00004218 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4219 if (!FD) return;
4220
4221 llvm::Function *F = cast<llvm::Function>(GV);
4222
4223 // Perform special handling in OpenCL mode
David Blaikie4e4d0842012-03-11 07:00:24 +00004224 if (M.getLangOpts().OpenCL) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00004225 // Use OpenCL function attributes to check for kernel functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00004226 // By default, all functions are device functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00004227 if (FD->hasAttr<OpenCLKernelAttr>()) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00004228 // OpenCL __kernel functions get kernel metadata
4229 addKernelMetadata(F);
Justin Holewinski818eafb2011-10-05 17:58:44 +00004230 // And kernel functions are not subject to inlining
Bill Wendling72390b32012-12-20 19:27:06 +00004231 F->addFnAttr(llvm::Attribute::NoInline);
Justin Holewinski818eafb2011-10-05 17:58:44 +00004232 }
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004233 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00004234
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004235 // Perform special handling in CUDA mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00004236 if (M.getLangOpts().CUDA) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00004237 // CUDA __global__ functions get a kernel metadata entry. Since
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004238 // __global__ functions cannot be called from the device, we do not
4239 // need to set the noinline attribute.
4240 if (FD->getAttr<CUDAGlobalAttr>())
Justin Holewinskidca8f332013-03-30 14:38:24 +00004241 addKernelMetadata(F);
Justin Holewinski818eafb2011-10-05 17:58:44 +00004242 }
4243}
4244
Justin Holewinskidca8f332013-03-30 14:38:24 +00004245void NVPTXTargetCodeGenInfo::addKernelMetadata(llvm::Function *F) {
4246 llvm::Module *M = F->getParent();
4247 llvm::LLVMContext &Ctx = M->getContext();
4248
4249 // Get "nvvm.annotations" metadata node
4250 llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
4251
4252 // Create !{<func-ref>, metadata !"kernel", i32 1} node
4253 llvm::SmallVector<llvm::Value *, 3> MDVals;
4254 MDVals.push_back(F);
4255 MDVals.push_back(llvm::MDString::get(Ctx, "kernel"));
4256 MDVals.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), 1));
4257
4258 // Append metadata to nvvm.annotations
4259 MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
4260}
4261
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004262}
4263
4264//===----------------------------------------------------------------------===//
Ulrich Weigandb8409212013-05-06 16:26:41 +00004265// SystemZ ABI Implementation
4266//===----------------------------------------------------------------------===//
4267
4268namespace {
4269
4270class SystemZABIInfo : public ABIInfo {
4271public:
4272 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
4273
4274 bool isPromotableIntegerType(QualType Ty) const;
4275 bool isCompoundType(QualType Ty) const;
4276 bool isFPArgumentType(QualType Ty) const;
4277
4278 ABIArgInfo classifyReturnType(QualType RetTy) const;
4279 ABIArgInfo classifyArgumentType(QualType ArgTy) const;
4280
4281 virtual void computeInfo(CGFunctionInfo &FI) const {
4282 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4283 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4284 it != ie; ++it)
4285 it->info = classifyArgumentType(it->type);
4286 }
4287
4288 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4289 CodeGenFunction &CGF) const;
4290};
4291
4292class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
4293public:
4294 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
4295 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
4296};
4297
4298}
4299
4300bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
4301 // Treat an enum type as its underlying type.
4302 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4303 Ty = EnumTy->getDecl()->getIntegerType();
4304
4305 // Promotable integer types are required to be promoted by the ABI.
4306 if (Ty->isPromotableIntegerType())
4307 return true;
4308
4309 // 32-bit values must also be promoted.
4310 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4311 switch (BT->getKind()) {
4312 case BuiltinType::Int:
4313 case BuiltinType::UInt:
4314 return true;
4315 default:
4316 return false;
4317 }
4318 return false;
4319}
4320
4321bool SystemZABIInfo::isCompoundType(QualType Ty) const {
4322 return Ty->isAnyComplexType() || isAggregateTypeForABI(Ty);
4323}
4324
4325bool SystemZABIInfo::isFPArgumentType(QualType Ty) const {
4326 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4327 switch (BT->getKind()) {
4328 case BuiltinType::Float:
4329 case BuiltinType::Double:
4330 return true;
4331 default:
4332 return false;
4333 }
4334
4335 if (const RecordType *RT = Ty->getAsStructureType()) {
4336 const RecordDecl *RD = RT->getDecl();
4337 bool Found = false;
4338
4339 // If this is a C++ record, check the bases first.
4340 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
4341 for (CXXRecordDecl::base_class_const_iterator I = CXXRD->bases_begin(),
4342 E = CXXRD->bases_end(); I != E; ++I) {
4343 QualType Base = I->getType();
4344
4345 // Empty bases don't affect things either way.
4346 if (isEmptyRecord(getContext(), Base, true))
4347 continue;
4348
4349 if (Found)
4350 return false;
4351 Found = isFPArgumentType(Base);
4352 if (!Found)
4353 return false;
4354 }
4355
4356 // Check the fields.
4357 for (RecordDecl::field_iterator I = RD->field_begin(),
4358 E = RD->field_end(); I != E; ++I) {
4359 const FieldDecl *FD = *I;
4360
4361 // Empty bitfields don't affect things either way.
4362 // Unlike isSingleElementStruct(), empty structure and array fields
4363 // do count. So do anonymous bitfields that aren't zero-sized.
4364 if (FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
4365 return true;
4366
4367 // Unlike isSingleElementStruct(), arrays do not count.
4368 // Nested isFPArgumentType structures still do though.
4369 if (Found)
4370 return false;
4371 Found = isFPArgumentType(FD->getType());
4372 if (!Found)
4373 return false;
4374 }
4375
4376 // Unlike isSingleElementStruct(), trailing padding is allowed.
4377 // An 8-byte aligned struct s { float f; } is passed as a double.
4378 return Found;
4379 }
4380
4381 return false;
4382}
4383
4384llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4385 CodeGenFunction &CGF) const {
4386 // Assume that va_list type is correct; should be pointer to LLVM type:
4387 // struct {
4388 // i64 __gpr;
4389 // i64 __fpr;
4390 // i8 *__overflow_arg_area;
4391 // i8 *__reg_save_area;
4392 // };
4393
4394 // Every argument occupies 8 bytes and is passed by preference in either
4395 // GPRs or FPRs.
4396 Ty = CGF.getContext().getCanonicalType(Ty);
4397 ABIArgInfo AI = classifyArgumentType(Ty);
4398 bool InFPRs = isFPArgumentType(Ty);
4399
4400 llvm::Type *APTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
4401 bool IsIndirect = AI.isIndirect();
4402 unsigned UnpaddedBitSize;
4403 if (IsIndirect) {
4404 APTy = llvm::PointerType::getUnqual(APTy);
4405 UnpaddedBitSize = 64;
4406 } else
4407 UnpaddedBitSize = getContext().getTypeSize(Ty);
4408 unsigned PaddedBitSize = 64;
4409 assert((UnpaddedBitSize <= PaddedBitSize) && "Invalid argument size.");
4410
4411 unsigned PaddedSize = PaddedBitSize / 8;
4412 unsigned Padding = (PaddedBitSize - UnpaddedBitSize) / 8;
4413
4414 unsigned MaxRegs, RegCountField, RegSaveIndex, RegPadding;
4415 if (InFPRs) {
4416 MaxRegs = 4; // Maximum of 4 FPR arguments
4417 RegCountField = 1; // __fpr
4418 RegSaveIndex = 16; // save offset for f0
4419 RegPadding = 0; // floats are passed in the high bits of an FPR
4420 } else {
4421 MaxRegs = 5; // Maximum of 5 GPR arguments
4422 RegCountField = 0; // __gpr
4423 RegSaveIndex = 2; // save offset for r2
4424 RegPadding = Padding; // values are passed in the low bits of a GPR
4425 }
4426
4427 llvm::Value *RegCountPtr =
4428 CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr");
4429 llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count");
4430 llvm::Type *IndexTy = RegCount->getType();
4431 llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs);
4432 llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV,
4433 "fits_in_regs");
4434
4435 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
4436 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
4437 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
4438 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
4439
4440 // Emit code to load the value if it was passed in registers.
4441 CGF.EmitBlock(InRegBlock);
4442
4443 // Work out the address of an argument register.
4444 llvm::Value *PaddedSizeV = llvm::ConstantInt::get(IndexTy, PaddedSize);
4445 llvm::Value *ScaledRegCount =
4446 CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count");
4447 llvm::Value *RegBase =
4448 llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize + RegPadding);
4449 llvm::Value *RegOffset =
4450 CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset");
4451 llvm::Value *RegSaveAreaPtr =
4452 CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr");
4453 llvm::Value *RegSaveArea =
4454 CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area");
4455 llvm::Value *RawRegAddr =
4456 CGF.Builder.CreateGEP(RegSaveArea, RegOffset, "raw_reg_addr");
4457 llvm::Value *RegAddr =
4458 CGF.Builder.CreateBitCast(RawRegAddr, APTy, "reg_addr");
4459
4460 // Update the register count
4461 llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1);
4462 llvm::Value *NewRegCount =
4463 CGF.Builder.CreateAdd(RegCount, One, "reg_count");
4464 CGF.Builder.CreateStore(NewRegCount, RegCountPtr);
4465 CGF.EmitBranch(ContBlock);
4466
4467 // Emit code to load the value if it was passed in memory.
4468 CGF.EmitBlock(InMemBlock);
4469
4470 // Work out the address of a stack argument.
4471 llvm::Value *OverflowArgAreaPtr =
4472 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr");
4473 llvm::Value *OverflowArgArea =
4474 CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area");
4475 llvm::Value *PaddingV = llvm::ConstantInt::get(IndexTy, Padding);
4476 llvm::Value *RawMemAddr =
4477 CGF.Builder.CreateGEP(OverflowArgArea, PaddingV, "raw_mem_addr");
4478 llvm::Value *MemAddr =
4479 CGF.Builder.CreateBitCast(RawMemAddr, APTy, "mem_addr");
4480
4481 // Update overflow_arg_area_ptr pointer
4482 llvm::Value *NewOverflowArgArea =
4483 CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area");
4484 CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
4485 CGF.EmitBranch(ContBlock);
4486
4487 // Return the appropriate result.
4488 CGF.EmitBlock(ContBlock);
4489 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(APTy, 2, "va_arg.addr");
4490 ResAddr->addIncoming(RegAddr, InRegBlock);
4491 ResAddr->addIncoming(MemAddr, InMemBlock);
4492
4493 if (IsIndirect)
4494 return CGF.Builder.CreateLoad(ResAddr, "indirect_arg");
4495
4496 return ResAddr;
4497}
4498
John McCallb8b52972013-06-18 02:46:29 +00004499bool X86_32TargetCodeGenInfo::isStructReturnInRegABI(
4500 const llvm::Triple &Triple, const CodeGenOptions &Opts) {
4501 assert(Triple.getArch() == llvm::Triple::x86);
4502
4503 switch (Opts.getStructReturnConvention()) {
4504 case CodeGenOptions::SRCK_Default:
4505 break;
4506 case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return
4507 return false;
4508 case CodeGenOptions::SRCK_InRegs: // -freg-struct-return
4509 return true;
4510 }
4511
4512 if (Triple.isOSDarwin())
4513 return true;
4514
4515 switch (Triple.getOS()) {
4516 case llvm::Triple::Cygwin:
4517 case llvm::Triple::MinGW32:
4518 case llvm::Triple::AuroraUX:
4519 case llvm::Triple::DragonFly:
4520 case llvm::Triple::FreeBSD:
4521 case llvm::Triple::OpenBSD:
4522 case llvm::Triple::Bitrig:
4523 case llvm::Triple::Win32:
4524 return true;
4525 default:
4526 return false;
4527 }
4528}
Ulrich Weigandb8409212013-05-06 16:26:41 +00004529
4530ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
4531 if (RetTy->isVoidType())
4532 return ABIArgInfo::getIgnore();
4533 if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64)
4534 return ABIArgInfo::getIndirect(0);
4535 return (isPromotableIntegerType(RetTy) ?
4536 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4537}
4538
4539ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
4540 // Handle the generic C++ ABI.
Mark Lacey23630722013-10-06 01:33:34 +00004541 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Ulrich Weigandb8409212013-05-06 16:26:41 +00004542 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
4543
4544 // Integers and enums are extended to full register width.
4545 if (isPromotableIntegerType(Ty))
4546 return ABIArgInfo::getExtend();
4547
4548 // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly.
4549 uint64_t Size = getContext().getTypeSize(Ty);
4550 if (Size != 8 && Size != 16 && Size != 32 && Size != 64)
4551 return ABIArgInfo::getIndirect(0);
4552
4553 // Handle small structures.
4554 if (const RecordType *RT = Ty->getAs<RecordType>()) {
4555 // Structures with flexible arrays have variable length, so really
4556 // fail the size test above.
4557 const RecordDecl *RD = RT->getDecl();
4558 if (RD->hasFlexibleArrayMember())
4559 return ABIArgInfo::getIndirect(0);
4560
4561 // The structure is passed as an unextended integer, a float, or a double.
4562 llvm::Type *PassTy;
4563 if (isFPArgumentType(Ty)) {
4564 assert(Size == 32 || Size == 64);
4565 if (Size == 32)
4566 PassTy = llvm::Type::getFloatTy(getVMContext());
4567 else
4568 PassTy = llvm::Type::getDoubleTy(getVMContext());
4569 } else
4570 PassTy = llvm::IntegerType::get(getVMContext(), Size);
4571 return ABIArgInfo::getDirect(PassTy);
4572 }
4573
4574 // Non-structure compounds are passed indirectly.
4575 if (isCompoundType(Ty))
4576 return ABIArgInfo::getIndirect(0);
4577
4578 return ABIArgInfo::getDirect(0);
4579}
4580
4581//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004582// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004583//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004584
4585namespace {
4586
4587class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
4588public:
Chris Lattnerea044322010-07-29 02:01:43 +00004589 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
4590 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004591 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4592 CodeGen::CodeGenModule &M) const;
4593};
4594
4595}
4596
4597void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4598 llvm::GlobalValue *GV,
4599 CodeGen::CodeGenModule &M) const {
4600 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4601 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
4602 // Handle 'interrupt' attribute:
4603 llvm::Function *F = cast<llvm::Function>(GV);
4604
4605 // Step 1: Set ISR calling convention.
4606 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
4607
4608 // Step 2: Add attributes goodness.
Bill Wendling72390b32012-12-20 19:27:06 +00004609 F->addFnAttr(llvm::Attribute::NoInline);
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004610
4611 // Step 3: Emit ISR vector alias.
Anton Korobeynikovf419a852012-11-26 18:59:10 +00004612 unsigned Num = attr->getNumber() / 2;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004613 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Anton Korobeynikovf419a852012-11-26 18:59:10 +00004614 "__isr_" + Twine(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004615 GV, &M.getModule());
4616 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004617 }
4618}
4619
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004620//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00004621// MIPS ABI Implementation. This works for both little-endian and
4622// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004623//===----------------------------------------------------------------------===//
4624
John McCallaeeb7012010-05-27 06:19:26 +00004625namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00004626class MipsABIInfo : public ABIInfo {
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004627 bool IsO32;
Akira Hatanakac359f202012-07-03 19:24:06 +00004628 unsigned MinABIStackAlignInBytes, StackAlignInBytes;
4629 void CoerceToIntArgs(uint64_t TySize,
Craig Topper6b9240e2013-07-05 19:34:19 +00004630 SmallVectorImpl<llvm::Type *> &ArgList) const;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004631 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004632 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004633 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004634public:
Akira Hatanakab551dd32011-11-03 00:05:50 +00004635 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
Akira Hatanakac359f202012-07-03 19:24:06 +00004636 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
4637 StackAlignInBytes(IsO32 ? 8 : 16) {}
Akira Hatanaka619e8872011-06-02 00:09:17 +00004638
4639 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004640 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004641 virtual void computeInfo(CGFunctionInfo &FI) const;
4642 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4643 CodeGenFunction &CGF) const;
4644};
4645
John McCallaeeb7012010-05-27 06:19:26 +00004646class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanakae624fa02011-09-20 18:23:28 +00004647 unsigned SizeOfUnwindException;
John McCallaeeb7012010-05-27 06:19:26 +00004648public:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004649 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
4650 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
4651 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCallaeeb7012010-05-27 06:19:26 +00004652
4653 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
4654 return 29;
4655 }
4656
Reed Kotler7dfd1822013-01-16 17:10:28 +00004657 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4658 CodeGen::CodeGenModule &CGM) const {
Reed Kotlerad4b8b42013-03-13 20:40:30 +00004659 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4660 if (!FD) return;
Rafael Espindolad8e6d6d2013-03-19 14:32:23 +00004661 llvm::Function *Fn = cast<llvm::Function>(GV);
Reed Kotlerad4b8b42013-03-13 20:40:30 +00004662 if (FD->hasAttr<Mips16Attr>()) {
4663 Fn->addFnAttr("mips16");
4664 }
4665 else if (FD->hasAttr<NoMips16Attr>()) {
4666 Fn->addFnAttr("nomips16");
4667 }
Reed Kotler7dfd1822013-01-16 17:10:28 +00004668 }
Reed Kotlerad4b8b42013-03-13 20:40:30 +00004669
John McCallaeeb7012010-05-27 06:19:26 +00004670 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00004671 llvm::Value *Address) const;
John McCall49e34be2011-08-30 01:42:09 +00004672
4673 unsigned getSizeOfUnwindException() const {
Akira Hatanakae624fa02011-09-20 18:23:28 +00004674 return SizeOfUnwindException;
John McCall49e34be2011-08-30 01:42:09 +00004675 }
John McCallaeeb7012010-05-27 06:19:26 +00004676};
4677}
4678
Akira Hatanakac359f202012-07-03 19:24:06 +00004679void MipsABIInfo::CoerceToIntArgs(uint64_t TySize,
Craig Topper6b9240e2013-07-05 19:34:19 +00004680 SmallVectorImpl<llvm::Type *> &ArgList) const {
Akira Hatanakac359f202012-07-03 19:24:06 +00004681 llvm::IntegerType *IntTy =
4682 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004683
4684 // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
4685 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
4686 ArgList.push_back(IntTy);
4687
4688 // If necessary, add one more integer type to ArgList.
4689 unsigned R = TySize % (MinABIStackAlignInBytes * 8);
4690
4691 if (R)
4692 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004693}
4694
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004695// In N32/64, an aligned double precision floating point field is passed in
4696// a register.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004697llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
Akira Hatanakac359f202012-07-03 19:24:06 +00004698 SmallVector<llvm::Type*, 8> ArgList, IntArgList;
4699
4700 if (IsO32) {
4701 CoerceToIntArgs(TySize, ArgList);
4702 return llvm::StructType::get(getVMContext(), ArgList);
4703 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004704
Akira Hatanaka2afd23d2012-01-12 00:52:17 +00004705 if (Ty->isComplexType())
4706 return CGT.ConvertType(Ty);
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00004707
Akira Hatanakaa34e9212012-02-09 19:54:16 +00004708 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004709
Akira Hatanakac359f202012-07-03 19:24:06 +00004710 // Unions/vectors are passed in integer registers.
4711 if (!RT || !RT->isStructureOrClassType()) {
4712 CoerceToIntArgs(TySize, ArgList);
4713 return llvm::StructType::get(getVMContext(), ArgList);
4714 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004715
4716 const RecordDecl *RD = RT->getDecl();
4717 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004718 assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004719
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004720 uint64_t LastOffset = 0;
4721 unsigned idx = 0;
4722 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
4723
Akira Hatanakaa34e9212012-02-09 19:54:16 +00004724 // Iterate over fields in the struct/class and check if there are any aligned
4725 // double fields.
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004726 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
4727 i != e; ++i, ++idx) {
David Blaikie262bc182012-04-30 02:36:29 +00004728 const QualType Ty = i->getType();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004729 const BuiltinType *BT = Ty->getAs<BuiltinType>();
4730
4731 if (!BT || BT->getKind() != BuiltinType::Double)
4732 continue;
4733
4734 uint64_t Offset = Layout.getFieldOffset(idx);
4735 if (Offset % 64) // Ignore doubles that are not aligned.
4736 continue;
4737
4738 // Add ((Offset - LastOffset) / 64) args of type i64.
4739 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
4740 ArgList.push_back(I64);
4741
4742 // Add double type.
4743 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
4744 LastOffset = Offset + 64;
4745 }
4746
Akira Hatanakac359f202012-07-03 19:24:06 +00004747 CoerceToIntArgs(TySize - LastOffset, IntArgList);
4748 ArgList.append(IntArgList.begin(), IntArgList.end());
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004749
4750 return llvm::StructType::get(getVMContext(), ArgList);
4751}
4752
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004753llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004754 assert((Offset % MinABIStackAlignInBytes) == 0);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004755
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004756 if ((Align - 1) & Offset)
4757 return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
4758
4759 return 0;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004760}
Akira Hatanaka9659d592012-01-10 22:44:52 +00004761
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004762ABIArgInfo
4763MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004764 uint64_t OrigOffset = Offset;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004765 uint64_t TySize = getContext().getTypeSize(Ty);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004766 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004767
Akira Hatanakac359f202012-07-03 19:24:06 +00004768 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
4769 (uint64_t)StackAlignInBytes);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004770 Offset = llvm::RoundUpToAlignment(Offset, Align);
4771 Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004772
Akira Hatanakac359f202012-07-03 19:24:06 +00004773 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
Akira Hatanaka619e8872011-06-02 00:09:17 +00004774 // Ignore empty aggregates.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004775 if (TySize == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00004776 return ABIArgInfo::getIgnore();
4777
Mark Lacey23630722013-10-06 01:33:34 +00004778 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004779 Offset = OrigOffset + MinABIStackAlignInBytes;
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00004780 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004781 }
Akira Hatanaka511949b2011-08-01 18:09:58 +00004782
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004783 // If we have reached here, aggregates are passed directly by coercing to
4784 // another structure type. Padding is inserted if the offset of the
4785 // aggregate is unaligned.
4786 return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
4787 getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00004788 }
4789
4790 // Treat an enum type as its underlying type.
4791 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4792 Ty = EnumTy->getDecl()->getIntegerType();
4793
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004794 if (Ty->isPromotableIntegerType())
4795 return ABIArgInfo::getExtend();
4796
Akira Hatanaka4055cfc2013-01-24 21:47:33 +00004797 return ABIArgInfo::getDirect(0, 0,
4798 IsO32 ? 0 : getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00004799}
4800
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004801llvm::Type*
4802MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakada54ff32012-02-09 18:49:26 +00004803 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakac359f202012-07-03 19:24:06 +00004804 SmallVector<llvm::Type*, 8> RTList;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004805
Akira Hatanakada54ff32012-02-09 18:49:26 +00004806 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004807 const RecordDecl *RD = RT->getDecl();
Akira Hatanakada54ff32012-02-09 18:49:26 +00004808 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
4809 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004810
Akira Hatanakada54ff32012-02-09 18:49:26 +00004811 // N32/64 returns struct/classes in floating point registers if the
4812 // following conditions are met:
4813 // 1. The size of the struct/class is no larger than 128-bit.
4814 // 2. The struct/class has one or two fields all of which are floating
4815 // point types.
4816 // 3. The offset of the first field is zero (this follows what gcc does).
4817 //
4818 // Any other composite results are returned in integer registers.
4819 //
4820 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
4821 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
4822 for (; b != e; ++b) {
David Blaikie262bc182012-04-30 02:36:29 +00004823 const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004824
Akira Hatanakada54ff32012-02-09 18:49:26 +00004825 if (!BT || !BT->isFloatingPoint())
4826 break;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004827
David Blaikie262bc182012-04-30 02:36:29 +00004828 RTList.push_back(CGT.ConvertType(b->getType()));
Akira Hatanakada54ff32012-02-09 18:49:26 +00004829 }
4830
4831 if (b == e)
4832 return llvm::StructType::get(getVMContext(), RTList,
4833 RD->hasAttr<PackedAttr>());
4834
4835 RTList.clear();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004836 }
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004837 }
4838
Akira Hatanakac359f202012-07-03 19:24:06 +00004839 CoerceToIntArgs(Size, RTList);
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004840 return llvm::StructType::get(getVMContext(), RTList);
4841}
4842
Akira Hatanaka619e8872011-06-02 00:09:17 +00004843ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanakaa8536c02012-01-23 23:18:57 +00004844 uint64_t Size = getContext().getTypeSize(RetTy);
4845
4846 if (RetTy->isVoidType() || Size == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00004847 return ABIArgInfo::getIgnore();
4848
Akira Hatanaka8aeb1472012-05-11 21:01:17 +00004849 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
Mark Lacey23630722013-10-06 01:33:34 +00004850 if (isRecordReturnIndirect(RetTy, getCXXABI()))
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00004851 return ABIArgInfo::getIndirect(0);
4852
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004853 if (Size <= 128) {
4854 if (RetTy->isAnyComplexType())
4855 return ABIArgInfo::getDirect();
4856
Akira Hatanakac359f202012-07-03 19:24:06 +00004857 // O32 returns integer vectors in registers.
4858 if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())
4859 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
4860
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00004861 if (!IsO32)
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004862 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
4863 }
Akira Hatanaka619e8872011-06-02 00:09:17 +00004864
4865 return ABIArgInfo::getIndirect(0);
4866 }
4867
4868 // Treat an enum type as its underlying type.
4869 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4870 RetTy = EnumTy->getDecl()->getIntegerType();
4871
4872 return (RetTy->isPromotableIntegerType() ?
4873 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4874}
4875
4876void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanakacc662542012-01-12 01:10:09 +00004877 ABIArgInfo &RetInfo = FI.getReturnInfo();
4878 RetInfo = classifyReturnType(FI.getReturnType());
4879
4880 // Check if a pointer to an aggregate is passed as a hidden argument.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004881 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
Akira Hatanakacc662542012-01-12 01:10:09 +00004882
Akira Hatanaka619e8872011-06-02 00:09:17 +00004883 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4884 it != ie; ++it)
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004885 it->info = classifyArgumentType(it->type, Offset);
Akira Hatanaka619e8872011-06-02 00:09:17 +00004886}
4887
4888llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4889 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00004890 llvm::Type *BP = CGF.Int8PtrTy;
4891 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004892
4893 CGBuilderTy &Builder = CGF.Builder;
4894 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
4895 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004896 int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004897 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
4898 llvm::Value *AddrTyped;
John McCall64aa4b32013-04-16 22:48:15 +00004899 unsigned PtrWidth = getTarget().getPointerWidth(0);
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004900 llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004901
4902 if (TypeAlign > MinABIStackAlignInBytes) {
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004903 llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
4904 llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
4905 llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
4906 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004907 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
4908 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
4909 }
4910 else
4911 AddrTyped = Builder.CreateBitCast(Addr, PTy);
4912
4913 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004914 TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004915 uint64_t Offset =
4916 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
4917 llvm::Value *NextAddr =
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004918 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004919 "ap.next");
4920 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
4921
4922 return AddrTyped;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004923}
4924
John McCallaeeb7012010-05-27 06:19:26 +00004925bool
4926MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4927 llvm::Value *Address) const {
4928 // This information comes from gcc's implementation, which seems to
4929 // as canonical as it gets.
4930
John McCallaeeb7012010-05-27 06:19:26 +00004931 // Everything on MIPS is 4 bytes. Double-precision FP registers
4932 // are aliased to pairs of single-precision FP registers.
Chris Lattner8b418682012-02-07 00:39:47 +00004933 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCallaeeb7012010-05-27 06:19:26 +00004934
4935 // 0-31 are the general purpose registers, $0 - $31.
4936 // 32-63 are the floating-point registers, $f0 - $f31.
4937 // 64 and 65 are the multiply/divide registers, $hi and $lo.
4938 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattner8b418682012-02-07 00:39:47 +00004939 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCallaeeb7012010-05-27 06:19:26 +00004940
4941 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
4942 // They are one bit wide and ignored here.
4943
4944 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
4945 // (coprocessor 1 is the FP unit)
4946 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
4947 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
4948 // 176-181 are the DSP accumulator registers.
Chris Lattner8b418682012-02-07 00:39:47 +00004949 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCallaeeb7012010-05-27 06:19:26 +00004950 return false;
4951}
4952
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004953//===----------------------------------------------------------------------===//
4954// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
4955// Currently subclassed only to implement custom OpenCL C function attribute
4956// handling.
4957//===----------------------------------------------------------------------===//
4958
4959namespace {
4960
4961class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
4962public:
4963 TCETargetCodeGenInfo(CodeGenTypes &CGT)
4964 : DefaultTargetCodeGenInfo(CGT) {}
4965
4966 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4967 CodeGen::CodeGenModule &M) const;
4968};
4969
4970void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4971 llvm::GlobalValue *GV,
4972 CodeGen::CodeGenModule &M) const {
4973 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4974 if (!FD) return;
4975
4976 llvm::Function *F = cast<llvm::Function>(GV);
4977
David Blaikie4e4d0842012-03-11 07:00:24 +00004978 if (M.getLangOpts().OpenCL) {
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004979 if (FD->hasAttr<OpenCLKernelAttr>()) {
4980 // OpenCL C Kernel functions are not subject to inlining
Bill Wendling72390b32012-12-20 19:27:06 +00004981 F->addFnAttr(llvm::Attribute::NoInline);
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004982
4983 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
4984
4985 // Convert the reqd_work_group_size() attributes to metadata.
4986 llvm::LLVMContext &Context = F->getContext();
4987 llvm::NamedMDNode *OpenCLMetadata =
4988 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
4989
4990 SmallVector<llvm::Value*, 5> Operands;
4991 Operands.push_back(F);
4992
Chris Lattner8b418682012-02-07 00:39:47 +00004993 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4994 llvm::APInt(32,
4995 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
4996 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4997 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004998 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
Chris Lattner8b418682012-02-07 00:39:47 +00004999 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
5000 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00005001 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
5002
5003 // Add a boolean constant operand for "required" (true) or "hint" (false)
5004 // for implementing the work_group_size_hint attr later. Currently
5005 // always true as the hint is not yet implemented.
Chris Lattner8b418682012-02-07 00:39:47 +00005006 Operands.push_back(llvm::ConstantInt::getTrue(Context));
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00005007 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
5008 }
5009 }
5010 }
5011}
5012
5013}
John McCallaeeb7012010-05-27 06:19:26 +00005014
Tony Linthicum96319392011-12-12 21:14:55 +00005015//===----------------------------------------------------------------------===//
5016// Hexagon ABI Implementation
5017//===----------------------------------------------------------------------===//
5018
5019namespace {
5020
5021class HexagonABIInfo : public ABIInfo {
5022
5023
5024public:
5025 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
5026
5027private:
5028
5029 ABIArgInfo classifyReturnType(QualType RetTy) const;
5030 ABIArgInfo classifyArgumentType(QualType RetTy) const;
5031
5032 virtual void computeInfo(CGFunctionInfo &FI) const;
5033
5034 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5035 CodeGenFunction &CGF) const;
5036};
5037
5038class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
5039public:
5040 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
5041 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
5042
5043 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
5044 return 29;
5045 }
5046};
5047
5048}
5049
5050void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
5051 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
5052 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
5053 it != ie; ++it)
5054 it->info = classifyArgumentType(it->type);
5055}
5056
5057ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
5058 if (!isAggregateTypeForABI(Ty)) {
5059 // Treat an enum type as its underlying type.
5060 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5061 Ty = EnumTy->getDecl()->getIntegerType();
5062
5063 return (Ty->isPromotableIntegerType() ?
5064 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
5065 }
5066
5067 // Ignore empty records.
5068 if (isEmptyRecord(getContext(), Ty, true))
5069 return ABIArgInfo::getIgnore();
5070
Mark Lacey23630722013-10-06 01:33:34 +00005071 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00005072 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Tony Linthicum96319392011-12-12 21:14:55 +00005073
5074 uint64_t Size = getContext().getTypeSize(Ty);
5075 if (Size > 64)
5076 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
5077 // Pass in the smallest viable integer type.
5078 else if (Size > 32)
5079 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
5080 else if (Size > 16)
5081 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
5082 else if (Size > 8)
5083 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5084 else
5085 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
5086}
5087
5088ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
5089 if (RetTy->isVoidType())
5090 return ABIArgInfo::getIgnore();
5091
5092 // Large vector types should be returned via memory.
5093 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
5094 return ABIArgInfo::getIndirect(0);
5095
5096 if (!isAggregateTypeForABI(RetTy)) {
5097 // Treat an enum type as its underlying type.
5098 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5099 RetTy = EnumTy->getDecl()->getIntegerType();
5100
5101 return (RetTy->isPromotableIntegerType() ?
5102 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
5103 }
5104
5105 // Structures with either a non-trivial destructor or a non-trivial
5106 // copy constructor are always indirect.
Mark Lacey23630722013-10-06 01:33:34 +00005107 if (isRecordReturnIndirect(RetTy, getCXXABI()))
Tony Linthicum96319392011-12-12 21:14:55 +00005108 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
5109
5110 if (isEmptyRecord(getContext(), RetTy, true))
5111 return ABIArgInfo::getIgnore();
5112
5113 // Aggregates <= 8 bytes are returned in r0; other aggregates
5114 // are returned indirectly.
5115 uint64_t Size = getContext().getTypeSize(RetTy);
5116 if (Size <= 64) {
5117 // Return in the smallest viable integer type.
5118 if (Size <= 8)
5119 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
5120 if (Size <= 16)
5121 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5122 if (Size <= 32)
5123 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
5124 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
5125 }
5126
5127 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
5128}
5129
5130llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner8b418682012-02-07 00:39:47 +00005131 CodeGenFunction &CGF) const {
Tony Linthicum96319392011-12-12 21:14:55 +00005132 // FIXME: Need to handle alignment
Chris Lattner8b418682012-02-07 00:39:47 +00005133 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Tony Linthicum96319392011-12-12 21:14:55 +00005134
5135 CGBuilderTy &Builder = CGF.Builder;
5136 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
5137 "ap");
5138 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
5139 llvm::Type *PTy =
5140 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
5141 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
5142
5143 uint64_t Offset =
5144 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
5145 llvm::Value *NextAddr =
5146 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
5147 "ap.next");
5148 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
5149
5150 return AddrTyped;
5151}
5152
5153
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00005154//===----------------------------------------------------------------------===//
5155// SPARC v9 ABI Implementation.
5156// Based on the SPARC Compliance Definition version 2.4.1.
5157//
5158// Function arguments a mapped to a nominal "parameter array" and promoted to
5159// registers depending on their type. Each argument occupies 8 or 16 bytes in
5160// the array, structs larger than 16 bytes are passed indirectly.
5161//
5162// One case requires special care:
5163//
5164// struct mixed {
5165// int i;
5166// float f;
5167// };
5168//
5169// When a struct mixed is passed by value, it only occupies 8 bytes in the
5170// parameter array, but the int is passed in an integer register, and the float
5171// is passed in a floating point register. This is represented as two arguments
5172// with the LLVM IR inreg attribute:
5173//
5174// declare void f(i32 inreg %i, float inreg %f)
5175//
5176// The code generator will only allocate 4 bytes from the parameter array for
5177// the inreg arguments. All other arguments are allocated a multiple of 8
5178// bytes.
5179//
5180namespace {
5181class SparcV9ABIInfo : public ABIInfo {
5182public:
5183 SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
5184
5185private:
5186 ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const;
5187 virtual void computeInfo(CGFunctionInfo &FI) const;
5188 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5189 CodeGenFunction &CGF) const;
Jakob Stoklund Olesenfc782fb2013-05-28 04:57:37 +00005190
5191 // Coercion type builder for structs passed in registers. The coercion type
5192 // serves two purposes:
5193 //
5194 // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned'
5195 // in registers.
5196 // 2. Expose aligned floating point elements as first-level elements, so the
5197 // code generator knows to pass them in floating point registers.
5198 //
5199 // We also compute the InReg flag which indicates that the struct contains
5200 // aligned 32-bit floats.
5201 //
5202 struct CoerceBuilder {
5203 llvm::LLVMContext &Context;
5204 const llvm::DataLayout &DL;
5205 SmallVector<llvm::Type*, 8> Elems;
5206 uint64_t Size;
5207 bool InReg;
5208
5209 CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl)
5210 : Context(c), DL(dl), Size(0), InReg(false) {}
5211
5212 // Pad Elems with integers until Size is ToSize.
5213 void pad(uint64_t ToSize) {
5214 assert(ToSize >= Size && "Cannot remove elements");
5215 if (ToSize == Size)
5216 return;
5217
5218 // Finish the current 64-bit word.
5219 uint64_t Aligned = llvm::RoundUpToAlignment(Size, 64);
5220 if (Aligned > Size && Aligned <= ToSize) {
5221 Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size));
5222 Size = Aligned;
5223 }
5224
5225 // Add whole 64-bit words.
5226 while (Size + 64 <= ToSize) {
5227 Elems.push_back(llvm::Type::getInt64Ty(Context));
5228 Size += 64;
5229 }
5230
5231 // Final in-word padding.
5232 if (Size < ToSize) {
5233 Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size));
5234 Size = ToSize;
5235 }
5236 }
5237
5238 // Add a floating point element at Offset.
5239 void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) {
5240 // Unaligned floats are treated as integers.
5241 if (Offset % Bits)
5242 return;
5243 // The InReg flag is only required if there are any floats < 64 bits.
5244 if (Bits < 64)
5245 InReg = true;
5246 pad(Offset);
5247 Elems.push_back(Ty);
5248 Size = Offset + Bits;
5249 }
5250
5251 // Add a struct type to the coercion type, starting at Offset (in bits).
5252 void addStruct(uint64_t Offset, llvm::StructType *StrTy) {
5253 const llvm::StructLayout *Layout = DL.getStructLayout(StrTy);
5254 for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) {
5255 llvm::Type *ElemTy = StrTy->getElementType(i);
5256 uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i);
5257 switch (ElemTy->getTypeID()) {
5258 case llvm::Type::StructTyID:
5259 addStruct(ElemOffset, cast<llvm::StructType>(ElemTy));
5260 break;
5261 case llvm::Type::FloatTyID:
5262 addFloat(ElemOffset, ElemTy, 32);
5263 break;
5264 case llvm::Type::DoubleTyID:
5265 addFloat(ElemOffset, ElemTy, 64);
5266 break;
5267 case llvm::Type::FP128TyID:
5268 addFloat(ElemOffset, ElemTy, 128);
5269 break;
5270 case llvm::Type::PointerTyID:
5271 if (ElemOffset % 64 == 0) {
5272 pad(ElemOffset);
5273 Elems.push_back(ElemTy);
5274 Size += 64;
5275 }
5276 break;
5277 default:
5278 break;
5279 }
5280 }
5281 }
5282
5283 // Check if Ty is a usable substitute for the coercion type.
5284 bool isUsableType(llvm::StructType *Ty) const {
5285 if (Ty->getNumElements() != Elems.size())
5286 return false;
5287 for (unsigned i = 0, e = Elems.size(); i != e; ++i)
5288 if (Elems[i] != Ty->getElementType(i))
5289 return false;
5290 return true;
5291 }
5292
5293 // Get the coercion type as a literal struct type.
5294 llvm::Type *getType() const {
5295 if (Elems.size() == 1)
5296 return Elems.front();
5297 else
5298 return llvm::StructType::get(Context, Elems);
5299 }
5300 };
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00005301};
5302} // end anonymous namespace
5303
5304ABIArgInfo
5305SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const {
5306 if (Ty->isVoidType())
5307 return ABIArgInfo::getIgnore();
5308
5309 uint64_t Size = getContext().getTypeSize(Ty);
5310
5311 // Anything too big to fit in registers is passed with an explicit indirect
5312 // pointer / sret pointer.
5313 if (Size > SizeLimit)
5314 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
5315
5316 // Treat an enum type as its underlying type.
5317 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5318 Ty = EnumTy->getDecl()->getIntegerType();
5319
5320 // Integer types smaller than a register are extended.
5321 if (Size < 64 && Ty->isIntegerType())
5322 return ABIArgInfo::getExtend();
5323
5324 // Other non-aggregates go in registers.
5325 if (!isAggregateTypeForABI(Ty))
5326 return ABIArgInfo::getDirect();
5327
5328 // This is a small aggregate type that should be passed in registers.
Jakob Stoklund Olesenfc782fb2013-05-28 04:57:37 +00005329 // Build a coercion type from the LLVM struct type.
5330 llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty));
5331 if (!StrTy)
5332 return ABIArgInfo::getDirect();
5333
5334 CoerceBuilder CB(getVMContext(), getDataLayout());
5335 CB.addStruct(0, StrTy);
5336 CB.pad(llvm::RoundUpToAlignment(CB.DL.getTypeSizeInBits(StrTy), 64));
5337
5338 // Try to use the original type for coercion.
5339 llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();
5340
5341 if (CB.InReg)
5342 return ABIArgInfo::getDirectInReg(CoerceTy);
5343 else
5344 return ABIArgInfo::getDirect(CoerceTy);
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00005345}
5346
5347llvm::Value *SparcV9ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5348 CodeGenFunction &CGF) const {
Jakob Stoklund Olesena4b56d32013-06-05 03:00:18 +00005349 ABIArgInfo AI = classifyType(Ty, 16 * 8);
5350 llvm::Type *ArgTy = CGT.ConvertType(Ty);
5351 if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
5352 AI.setCoerceToType(ArgTy);
5353
5354 llvm::Type *BPP = CGF.Int8PtrPtrTy;
5355 CGBuilderTy &Builder = CGF.Builder;
5356 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
5357 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
5358 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
5359 llvm::Value *ArgAddr;
5360 unsigned Stride;
5361
5362 switch (AI.getKind()) {
5363 case ABIArgInfo::Expand:
5364 llvm_unreachable("Unsupported ABI kind for va_arg");
5365
5366 case ABIArgInfo::Extend:
5367 Stride = 8;
5368 ArgAddr = Builder
5369 .CreateConstGEP1_32(Addr, 8 - getDataLayout().getTypeAllocSize(ArgTy),
5370 "extend");
5371 break;
5372
5373 case ABIArgInfo::Direct:
5374 Stride = getDataLayout().getTypeAllocSize(AI.getCoerceToType());
5375 ArgAddr = Addr;
5376 break;
5377
5378 case ABIArgInfo::Indirect:
5379 Stride = 8;
5380 ArgAddr = Builder.CreateBitCast(Addr,
5381 llvm::PointerType::getUnqual(ArgPtrTy),
5382 "indirect");
5383 ArgAddr = Builder.CreateLoad(ArgAddr, "indirect.arg");
5384 break;
5385
5386 case ABIArgInfo::Ignore:
5387 return llvm::UndefValue::get(ArgPtrTy);
5388 }
5389
5390 // Update VAList.
5391 Addr = Builder.CreateConstGEP1_32(Addr, Stride, "ap.next");
5392 Builder.CreateStore(Addr, VAListAddrAsBPP);
5393
5394 return Builder.CreatePointerCast(ArgAddr, ArgPtrTy, "arg.addr");
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00005395}
5396
5397void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const {
5398 FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8);
5399 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
5400 it != ie; ++it)
5401 it->info = classifyType(it->type, 16 * 8);
5402}
5403
5404namespace {
5405class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo {
5406public:
5407 SparcV9TargetCodeGenInfo(CodeGenTypes &CGT)
5408 : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {}
5409};
5410} // end anonymous namespace
5411
5412
Robert Lytton5f15f4d2013-08-13 09:43:10 +00005413//===----------------------------------------------------------------------===//
5414// Xcore ABI Implementation
5415//===----------------------------------------------------------------------===//
5416namespace {
Robert Lytton276c2892013-08-19 09:46:39 +00005417class XCoreABIInfo : public DefaultABIInfo {
5418public:
5419 XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
5420 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5421 CodeGenFunction &CGF) const;
5422};
5423
Robert Lytton5f15f4d2013-08-13 09:43:10 +00005424class XcoreTargetCodeGenInfo : public TargetCodeGenInfo {
5425public:
5426 XcoreTargetCodeGenInfo(CodeGenTypes &CGT)
Robert Lytton276c2892013-08-19 09:46:39 +00005427 :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {}
Robert Lytton5f15f4d2013-08-13 09:43:10 +00005428};
5429} // end anonymous namespace
5430
Robert Lytton276c2892013-08-19 09:46:39 +00005431llvm::Value *XCoreABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5432 CodeGenFunction &CGF) const {
5433 ABIArgInfo AI = classifyArgumentType(Ty);
5434 CGBuilderTy &Builder = CGF.Builder;
5435 llvm::Type *ArgTy = CGT.ConvertType(Ty);
5436 if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
5437 AI.setCoerceToType(ArgTy);
5438
5439 // handle the VAList
5440 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr,
5441 CGF.Int8PtrPtrTy);
5442 llvm::Value *AP = Builder.CreateLoad(VAListAddrAsBPP);
5443 llvm::Value *APN = Builder.CreateConstGEP1_32(AP, 4);
5444 Builder.CreateStore(APN, VAListAddrAsBPP);
5445
5446 // handle the argument
5447 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
5448 switch (AI.getKind()) {
Robert Lytton276c2892013-08-19 09:46:39 +00005449 case ABIArgInfo::Expand:
5450 llvm_unreachable("Unsupported ABI kind for va_arg");
5451 case ABIArgInfo::Ignore:
5452 return llvm::UndefValue::get(ArgPtrTy);
5453 case ABIArgInfo::Extend:
5454 case ABIArgInfo::Direct:
5455 return Builder.CreatePointerCast(AP, ArgPtrTy);
5456 case ABIArgInfo::Indirect:
5457 llvm::Value *ArgAddr;
5458 ArgAddr = Builder.CreateBitCast(AP, llvm::PointerType::getUnqual(ArgPtrTy));
5459 ArgAddr = Builder.CreateLoad(ArgAddr);
5460 return Builder.CreatePointerCast(ArgAddr, ArgPtrTy);
5461 }
Alexey Samsonovacfea842013-08-19 13:07:12 +00005462 llvm_unreachable("Unknown ABI kind");
Robert Lytton276c2892013-08-19 09:46:39 +00005463}
Robert Lytton5f15f4d2013-08-13 09:43:10 +00005464
5465//===----------------------------------------------------------------------===//
5466// Driver code
5467//===----------------------------------------------------------------------===//
5468
Chris Lattnerea044322010-07-29 02:01:43 +00005469const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005470 if (TheTargetCodeGenInfo)
5471 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005472
John McCall64aa4b32013-04-16 22:48:15 +00005473 const llvm::Triple &Triple = getTarget().getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00005474 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00005475 default:
Chris Lattnerea044322010-07-29 02:01:43 +00005476 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00005477
Derek Schuff9ed63f82012-09-06 17:37:28 +00005478 case llvm::Triple::le32:
5479 return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
John McCallaeeb7012010-05-27 06:19:26 +00005480 case llvm::Triple::mips:
5481 case llvm::Triple::mipsel:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00005482 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCallaeeb7012010-05-27 06:19:26 +00005483
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00005484 case llvm::Triple::mips64:
5485 case llvm::Triple::mips64el:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00005486 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00005487
Tim Northoverc264e162013-01-31 12:13:10 +00005488 case llvm::Triple::aarch64:
5489 return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types));
5490
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00005491 case llvm::Triple::arm:
5492 case llvm::Triple::thumb:
Sandeep Patel34c1af82011-04-05 00:23:47 +00005493 {
5494 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
John McCall64aa4b32013-04-16 22:48:15 +00005495 if (strcmp(getTarget().getABI(), "apcs-gnu") == 0)
Sandeep Patel34c1af82011-04-05 00:23:47 +00005496 Kind = ARMABIInfo::APCS;
David Tweedb16abb12012-10-25 13:33:01 +00005497 else if (CodeGenOpts.FloatABI == "hard" ||
John McCall64aa4b32013-04-16 22:48:15 +00005498 (CodeGenOpts.FloatABI != "soft" &&
5499 Triple.getEnvironment() == llvm::Triple::GNUEABIHF))
Sandeep Patel34c1af82011-04-05 00:23:47 +00005500 Kind = ARMABIInfo::AAPCS_VFP;
5501
Derek Schuff263366f2012-10-16 22:30:41 +00005502 switch (Triple.getOS()) {
Eli Bendersky441d9f72012-12-04 18:38:10 +00005503 case llvm::Triple::NaCl:
Derek Schuff263366f2012-10-16 22:30:41 +00005504 return *(TheTargetCodeGenInfo =
5505 new NaClARMTargetCodeGenInfo(Types, Kind));
5506 default:
5507 return *(TheTargetCodeGenInfo =
5508 new ARMTargetCodeGenInfo(Types, Kind));
5509 }
Sandeep Patel34c1af82011-04-05 00:23:47 +00005510 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00005511
John McCallec853ba2010-03-11 00:10:12 +00005512 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00005513 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
Roman Divacky0fbc4b92012-05-09 18:22:46 +00005514 case llvm::Triple::ppc64:
Bill Schmidt2fc107f2012-10-03 19:18:57 +00005515 if (Triple.isOSBinFormatELF())
5516 return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types));
5517 else
5518 return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
Bill Schmidtea7fb0c2013-07-26 01:36:11 +00005519 case llvm::Triple::ppc64le:
5520 assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!");
5521 return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00005522
Peter Collingbourneedb66f32012-05-20 23:28:41 +00005523 case llvm::Triple::nvptx:
5524 case llvm::Triple::nvptx64:
Justin Holewinski2c585b92012-05-24 17:43:12 +00005525 return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types));
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005526
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005527 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00005528 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00005529
Ulrich Weigandb8409212013-05-06 16:26:41 +00005530 case llvm::Triple::systemz:
5531 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
5532
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00005533 case llvm::Triple::tce:
5534 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
5535
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00005536 case llvm::Triple::x86: {
John McCallb8b52972013-06-18 02:46:29 +00005537 bool IsDarwinVectorABI = Triple.isOSDarwin();
5538 bool IsSmallStructInRegABI =
5539 X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
5540 bool IsWin32FloatStructABI = (Triple.getOS() == llvm::Triple::Win32);
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00005541
John McCallb8b52972013-06-18 02:46:29 +00005542 if (Triple.getOS() == llvm::Triple::Win32) {
Eli Friedman55fc7e22012-01-25 22:46:34 +00005543 return *(TheTargetCodeGenInfo =
Reid Kleckner3190ca92013-05-08 13:44:39 +00005544 new WinX86_32TargetCodeGenInfo(Types,
John McCallb8b52972013-06-18 02:46:29 +00005545 IsDarwinVectorABI, IsSmallStructInRegABI,
5546 IsWin32FloatStructABI,
Reid Kleckner3190ca92013-05-08 13:44:39 +00005547 CodeGenOpts.NumRegisterParameters));
John McCallb8b52972013-06-18 02:46:29 +00005548 } else {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005549 return *(TheTargetCodeGenInfo =
John McCallb8b52972013-06-18 02:46:29 +00005550 new X86_32TargetCodeGenInfo(Types,
5551 IsDarwinVectorABI, IsSmallStructInRegABI,
5552 IsWin32FloatStructABI,
Rafael Espindolab48280b2012-07-31 02:44:24 +00005553 CodeGenOpts.NumRegisterParameters));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005554 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00005555 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005556
Eli Friedmanee1ad992011-12-02 00:11:43 +00005557 case llvm::Triple::x86_64: {
John McCall64aa4b32013-04-16 22:48:15 +00005558 bool HasAVX = strcmp(getTarget().getABI(), "avx") == 0;
Eli Friedmanee1ad992011-12-02 00:11:43 +00005559
Chris Lattnerf13721d2010-08-31 16:44:54 +00005560 switch (Triple.getOS()) {
5561 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00005562 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00005563 case llvm::Triple::Cygwin:
5564 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
Eli Bendersky441d9f72012-12-04 18:38:10 +00005565 case llvm::Triple::NaCl:
John McCall64aa4b32013-04-16 22:48:15 +00005566 return *(TheTargetCodeGenInfo = new NaClX86_64TargetCodeGenInfo(Types,
5567 HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00005568 default:
Eli Friedmanee1ad992011-12-02 00:11:43 +00005569 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
5570 HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00005571 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00005572 }
Tony Linthicum96319392011-12-12 21:14:55 +00005573 case llvm::Triple::hexagon:
5574 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00005575 case llvm::Triple::sparcv9:
5576 return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types));
Robert Lytton5f15f4d2013-08-13 09:43:10 +00005577 case llvm::Triple::xcore:
5578 return *(TheTargetCodeGenInfo = new XcoreTargetCodeGenInfo(Types));
5579
Eli Friedmanee1ad992011-12-02 00:11:43 +00005580 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005581}