blob: 4fa0c3bbdfcd982a399e7710d9135d771312994f [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
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +000047static bool isRecordReturnIndirect(const RecordType *RT, CodeGen::CodeGenTypes &CGT) {
48 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
49 if (!RD)
50 return false;
51 return CGT.CGM.getCXXABI().isReturnTypeIndirect(RD);
52}
53
54
55static bool isRecordReturnIndirect(QualType T, CodeGen::CodeGenTypes &CGT) {
56 const RecordType *RT = T->getAs<RecordType>();
57 if (!RT)
58 return false;
59 return isRecordReturnIndirect(RT, CGT);
60}
61
62static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT,
63 CodeGen::CodeGenTypes &CGT) {
64 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
65 if (!RD)
66 return CGCXXABI::RAA_Default;
67 return CGT.CGM.getCXXABI().getRecordArgABI(RD);
68}
69
70static CGCXXABI::RecordArgABI getRecordArgABI(QualType T,
71 CodeGen::CodeGenTypes &CGT) {
72 const RecordType *RT = T->getAs<RecordType>();
73 if (!RT)
74 return CGCXXABI::RAA_Default;
75 return getRecordArgABI(RT, CGT);
76}
77
Chris Lattnerea044322010-07-29 02:01:43 +000078ASTContext &ABIInfo::getContext() const {
79 return CGT.getContext();
80}
81
82llvm::LLVMContext &ABIInfo::getVMContext() const {
83 return CGT.getLLVMContext();
84}
85
Micah Villmow25a6a842012-10-08 16:25:52 +000086const llvm::DataLayout &ABIInfo::getDataLayout() const {
87 return CGT.getDataLayout();
Chris Lattnerea044322010-07-29 02:01:43 +000088}
89
John McCall64aa4b32013-04-16 22:48:15 +000090const TargetInfo &ABIInfo::getTarget() const {
91 return CGT.getTarget();
92}
Chris Lattnerea044322010-07-29 02:01:43 +000093
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000094void ABIArgInfo::dump() const {
Chris Lattner5f9e2722011-07-23 10:55:15 +000095 raw_ostream &OS = llvm::errs();
Daniel Dunbar28df7a52009-12-03 09:13:49 +000096 OS << "(ABIArgInfo Kind=";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000097 switch (TheKind) {
98 case Direct:
Chris Lattner800588f2010-07-29 06:26:06 +000099 OS << "Direct Type=";
Chris Lattner2acc6e32011-07-18 04:24:23 +0000100 if (llvm::Type *Ty = getCoerceToType())
Chris Lattner800588f2010-07-29 06:26:06 +0000101 Ty->print(OS);
102 else
103 OS << "null";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000104 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000105 case Extend:
Daniel Dunbar28df7a52009-12-03 09:13:49 +0000106 OS << "Extend";
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000107 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000108 case Ignore:
Daniel Dunbar28df7a52009-12-03 09:13:49 +0000109 OS << "Ignore";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000110 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000111 case Indirect:
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000112 OS << "Indirect Align=" << getIndirectAlign()
Joerg Sonnenbergere9b5d772011-07-15 18:23:44 +0000113 << " ByVal=" << getIndirectByVal()
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000114 << " Realign=" << getIndirectRealign();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000115 break;
116 case Expand:
Daniel Dunbar28df7a52009-12-03 09:13:49 +0000117 OS << "Expand";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000118 break;
119 }
Daniel Dunbar28df7a52009-12-03 09:13:49 +0000120 OS << ")\n";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000121}
122
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000123TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
124
John McCall49e34be2011-08-30 01:42:09 +0000125// If someone can figure out a general rule for this, that would be great.
126// It's probably just doomed to be platform-dependent, though.
127unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
128 // Verified for:
129 // x86-64 FreeBSD, Linux, Darwin
130 // x86-32 FreeBSD, Linux, Darwin
131 // PowerPC Linux, Darwin
132 // ARM Darwin (*not* EABI)
Tim Northoverc264e162013-01-31 12:13:10 +0000133 // AArch64 Linux
John McCall49e34be2011-08-30 01:42:09 +0000134 return 32;
135}
136
John McCallde5d3c72012-02-17 03:33:10 +0000137bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
138 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +0000139 // The following conventions are known to require this to be false:
140 // x86_stdcall
141 // MIPS
142 // For everything else, we just prefer false unless we opt out.
143 return false;
144}
145
Reid Kleckner3190ca92013-05-08 13:44:39 +0000146void
147TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib,
148 llvm::SmallString<24> &Opt) const {
149 // This assumes the user is passing a library name like "rt" instead of a
150 // filename like "librt.a/so", and that they don't care whether it's static or
151 // dynamic.
152 Opt = "-l";
153 Opt += Lib;
154}
155
Daniel Dunbar98303b92009-09-13 08:03:58 +0000156static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000157
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000158/// isEmptyField - Return true iff a the field is "empty", that is it
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000159/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +0000160static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
161 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000162 if (FD->isUnnamedBitfield())
163 return true;
164
165 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000166
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000167 // Constant arrays of empty records count as empty, strip them off.
168 // Constant arrays of zero length always count as empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000169 if (AllowArrays)
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000170 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
171 if (AT->getSize() == 0)
172 return true;
Daniel Dunbar98303b92009-09-13 08:03:58 +0000173 FT = AT->getElementType();
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000174 }
Daniel Dunbar98303b92009-09-13 08:03:58 +0000175
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000176 const RecordType *RT = FT->getAs<RecordType>();
177 if (!RT)
178 return false;
179
180 // C++ record fields are never empty, at least in the Itanium ABI.
181 //
182 // FIXME: We should use a predicate for whether this behavior is true in the
183 // current ABI.
184 if (isa<CXXRecordDecl>(RT->getDecl()))
185 return false;
186
Daniel Dunbar98303b92009-09-13 08:03:58 +0000187 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000188}
189
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000190/// isEmptyRecord - Return true iff a structure contains only empty
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000191/// fields. Note that a structure with a flexible array member is not
192/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000193static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000194 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000195 if (!RT)
196 return 0;
197 const RecordDecl *RD = RT->getDecl();
198 if (RD->hasFlexibleArrayMember())
199 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000200
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000201 // If this is a C++ record, check the bases first.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000202 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000203 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
204 e = CXXRD->bases_end(); i != e; ++i)
205 if (!isEmptyRecord(Context, i->getType(), true))
206 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000207
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000208 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
209 i != e; ++i)
David Blaikie581deb32012-06-06 20:45:41 +0000210 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000211 return false;
212 return true;
213}
214
215/// isSingleElementStruct - Determine if a structure is a "single
216/// element struct", i.e. it has exactly one non-empty field or
217/// exactly one field which is itself a single element
218/// struct. Structures with flexible array members are never
219/// considered single element structs.
220///
221/// \return The field declaration for the single non-empty field, if
222/// it exists.
223static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
224 const RecordType *RT = T->getAsStructureType();
225 if (!RT)
226 return 0;
227
228 const RecordDecl *RD = RT->getDecl();
229 if (RD->hasFlexibleArrayMember())
230 return 0;
231
232 const Type *Found = 0;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000233
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000234 // If this is a C++ record, check the bases first.
235 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
236 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
237 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000238 // Ignore empty records.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000239 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000240 continue;
241
242 // If we already found an element then this isn't a single-element struct.
243 if (Found)
244 return 0;
245
246 // If this is non-empty and not a single element struct, the composite
247 // cannot be a single element struct.
248 Found = isSingleElementStruct(i->getType(), Context);
249 if (!Found)
250 return 0;
251 }
252 }
253
254 // Check for single element.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000255 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
256 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000257 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000258 QualType FT = FD->getType();
259
260 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000261 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000262 continue;
263
264 // If we already found an element then this isn't a single-element
265 // struct.
266 if (Found)
267 return 0;
268
269 // Treat single element arrays as the element.
270 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
271 if (AT->getSize().getZExtValue() != 1)
272 break;
273 FT = AT->getElementType();
274 }
275
John McCalld608cdb2010-08-22 10:59:02 +0000276 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000277 Found = FT.getTypePtr();
278 } else {
279 Found = isSingleElementStruct(FT, Context);
280 if (!Found)
281 return 0;
282 }
283 }
284
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000285 // We don't consider a struct a single-element struct if it has
286 // padding beyond the element type.
287 if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
288 return 0;
289
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000290 return Found;
291}
292
293static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Eli Friedmandb748a32012-11-29 23:21:04 +0000294 // Treat complex types as the element type.
295 if (const ComplexType *CTy = Ty->getAs<ComplexType>())
296 Ty = CTy->getElementType();
297
298 // Check for a type which we know has a simple scalar argument-passing
299 // convention without any padding. (We're specifically looking for 32
300 // and 64-bit integer and integer-equivalents, float, and double.)
Daniel Dunbara1842d32010-05-14 03:40:53 +0000301 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Eli Friedmandb748a32012-11-29 23:21:04 +0000302 !Ty->isEnumeralType() && !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000303 return false;
304
305 uint64_t Size = Context.getTypeSize(Ty);
306 return Size == 32 || Size == 64;
307}
308
Daniel Dunbar53012f42009-11-09 01:33:53 +0000309/// canExpandIndirectArgument - Test whether an argument type which is to be
310/// passed indirectly (on the stack) would have the equivalent layout if it was
311/// expanded into separate arguments. If so, we prefer to do the latter to avoid
312/// inhibiting optimizations.
313///
314// FIXME: This predicate is missing many cases, currently it just follows
315// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
316// should probably make this smarter, or better yet make the LLVM backend
317// capable of handling it.
318static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
319 // We can only expand structure types.
320 const RecordType *RT = Ty->getAs<RecordType>();
321 if (!RT)
322 return false;
323
324 // We can only expand (C) structures.
325 //
326 // FIXME: This needs to be generalized to handle classes as well.
327 const RecordDecl *RD = RT->getDecl();
328 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
329 return false;
330
Eli Friedman506d4e32011-11-18 01:32:26 +0000331 uint64_t Size = 0;
332
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000333 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
334 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000335 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000336
337 if (!is32Or64BitBasicType(FD->getType(), Context))
338 return false;
339
340 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
341 // how to expand them yet, and the predicate for telling if a bitfield still
342 // counts as "basic" is more complicated than what we were doing previously.
343 if (FD->isBitField())
344 return false;
Eli Friedman506d4e32011-11-18 01:32:26 +0000345
346 Size += Context.getTypeSize(FD->getType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000347 }
348
Eli Friedman506d4e32011-11-18 01:32:26 +0000349 // Make sure there are not any holes in the struct.
350 if (Size != Context.getTypeSize(Ty))
351 return false;
352
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000353 return true;
354}
355
356namespace {
357/// DefaultABIInfo - The default implementation for ABI specific
358/// details. This implementation provides information which results in
359/// self-consistent and sensible LLVM IR generation, but does not
360/// conform to any particular ABI.
361class DefaultABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +0000362public:
363 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000364
Chris Lattnera3c109b2010-07-29 02:16:43 +0000365 ABIArgInfo classifyReturnType(QualType RetTy) const;
366 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000367
Chris Lattneree5dcd02010-07-29 02:31:05 +0000368 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000369 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000370 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
371 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000372 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000373 }
374
375 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
376 CodeGenFunction &CGF) const;
377};
378
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000379class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
380public:
Chris Lattnerea044322010-07-29 02:01:43 +0000381 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
382 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000383};
384
385llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
386 CodeGenFunction &CGF) const {
387 return 0;
388}
389
Chris Lattnera3c109b2010-07-29 02:16:43 +0000390ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
Jan Wen Voung90306932011-11-03 00:59:44 +0000391 if (isAggregateTypeForABI(Ty)) {
392 // Records with non trivial destructors/constructors should not be passed
393 // by value.
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000394 if (isRecordReturnIndirect(Ty, CGT))
Jan Wen Voung90306932011-11-03 00:59:44 +0000395 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
396
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000397 return ABIArgInfo::getIndirect(0);
Jan Wen Voung90306932011-11-03 00:59:44 +0000398 }
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000399
Chris Lattnera14db752010-03-11 18:19:55 +0000400 // Treat an enum type as its underlying type.
401 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
402 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000403
Chris Lattnera14db752010-03-11 18:19:55 +0000404 return (Ty->isPromotableIntegerType() ?
405 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000406}
407
Bob Wilson0024f942011-01-10 23:54:17 +0000408ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
409 if (RetTy->isVoidType())
410 return ABIArgInfo::getIgnore();
411
412 if (isAggregateTypeForABI(RetTy))
413 return ABIArgInfo::getIndirect(0);
414
415 // Treat an enum type as its underlying type.
416 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
417 RetTy = EnumTy->getDecl()->getIntegerType();
418
419 return (RetTy->isPromotableIntegerType() ?
420 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
421}
422
Derek Schuff9ed63f82012-09-06 17:37:28 +0000423//===----------------------------------------------------------------------===//
424// le32/PNaCl bitcode ABI Implementation
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000425//
426// This is a simplified version of the x86_32 ABI. Arguments and return values
427// are always passed on the stack.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000428//===----------------------------------------------------------------------===//
429
430class PNaClABIInfo : public ABIInfo {
431 public:
432 PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
433
434 ABIArgInfo classifyReturnType(QualType RetTy) const;
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000435 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Derek Schuff9ed63f82012-09-06 17:37:28 +0000436
437 virtual void computeInfo(CGFunctionInfo &FI) const;
438 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
439 CodeGenFunction &CGF) const;
440};
441
442class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
443 public:
444 PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
445 : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {}
446};
447
448void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
449 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
450
Derek Schuff9ed63f82012-09-06 17:37:28 +0000451 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
452 it != ie; ++it)
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000453 it->info = classifyArgumentType(it->type);
Derek Schuff9ed63f82012-09-06 17:37:28 +0000454 }
455
456llvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
457 CodeGenFunction &CGF) const {
458 return 0;
459}
460
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000461/// \brief Classify argument of given type \p Ty.
462ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const {
Derek Schuff9ed63f82012-09-06 17:37:28 +0000463 if (isAggregateTypeForABI(Ty)) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000464 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
465 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Derek Schuff9ed63f82012-09-06 17:37:28 +0000466 return ABIArgInfo::getIndirect(0);
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000467 } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
468 // Treat an enum type as its underlying type.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000469 Ty = EnumTy->getDecl()->getIntegerType();
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000470 } else if (Ty->isFloatingType()) {
471 // Floating-point types don't go inreg.
472 return ABIArgInfo::getDirect();
Derek Schuff9ed63f82012-09-06 17:37:28 +0000473 }
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000474
475 return (Ty->isPromotableIntegerType() ?
476 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Derek Schuff9ed63f82012-09-06 17:37:28 +0000477}
478
479ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
480 if (RetTy->isVoidType())
481 return ABIArgInfo::getIgnore();
482
Eli Benderskye45dfd12013-04-04 22:49:35 +0000483 // In the PNaCl ABI we always return records/structures on the stack.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000484 if (isAggregateTypeForABI(RetTy))
485 return ABIArgInfo::getIndirect(0);
486
487 // Treat an enum type as its underlying type.
488 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
489 RetTy = EnumTy->getDecl()->getIntegerType();
490
491 return (RetTy->isPromotableIntegerType() ?
492 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
493}
494
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000495/// IsX86_MMXType - Return true if this is an MMX type.
496bool IsX86_MMXType(llvm::Type *IRType) {
497 // 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 +0000498 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
499 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
500 IRType->getScalarSizeInBits() != 64;
501}
502
Jay Foadef6de3d2011-07-11 09:56:20 +0000503static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000504 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000505 llvm::Type* Ty) {
Bill Wendling0507be62011-03-07 22:47:14 +0000506 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy())
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000507 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
508 return Ty;
509}
510
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000511//===----------------------------------------------------------------------===//
512// X86-32 ABI Implementation
513//===----------------------------------------------------------------------===//
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000514
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000515/// X86_32ABIInfo - The X86-32 ABI information.
516class X86_32ABIInfo : public ABIInfo {
Rafael Espindolab48280b2012-07-31 02:44:24 +0000517 enum Class {
518 Integer,
519 Float
520 };
521
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000522 static const unsigned MinABIStackAlignInBytes = 4;
523
David Chisnall1e4249c2009-08-17 23:08:21 +0000524 bool IsDarwinVectorABI;
525 bool IsSmallStructInRegABI;
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000526 bool IsWin32StructABI;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000527 unsigned DefaultNumRegisterParameters;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000528
529 static bool isRegisterSize(unsigned Size) {
530 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
531 }
532
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000533 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context,
534 unsigned callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000535
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000536 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
537 /// such that the argument will be passed in memory.
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000538 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal,
539 unsigned &FreeRegs) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000540
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000541 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbare59d8582010-09-16 20:42:06 +0000542 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000543
Rafael Espindolab48280b2012-07-31 02:44:24 +0000544 Class classify(QualType Ty) const;
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000545 ABIArgInfo classifyReturnType(QualType RetTy,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000546 unsigned callingConvention) const;
Rafael Espindolab6932692012-10-24 01:58:58 +0000547 ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &FreeRegs,
548 bool IsFastCall) const;
549 bool shouldUseInReg(QualType Ty, unsigned &FreeRegs,
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000550 bool IsFastCall, bool &NeedsPadding) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000551
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000552public:
553
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000554 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000555 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
556 CodeGenFunction &CGF) const;
557
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000558 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool w,
Rafael Espindolab48280b2012-07-31 02:44:24 +0000559 unsigned r)
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000560 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000561 IsWin32StructABI(w), DefaultNumRegisterParameters(r) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000562};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000563
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000564class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
565public:
Eli Friedman55fc7e22012-01-25 22:46:34 +0000566 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000567 bool d, bool p, bool w, unsigned r)
568 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, w, r)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000569
570 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
571 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000572
573 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
574 // Darwin uses different dwarf register numbers for EH.
John McCall64aa4b32013-04-16 22:48:15 +0000575 if (CGM.getTarget().getTriple().isOSDarwin()) return 5;
John McCall6374c332010-03-06 00:35:14 +0000576 return 4;
577 }
578
579 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
580 llvm::Value *Address) const;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000581
Jay Foadef6de3d2011-07-11 09:56:20 +0000582 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000583 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000584 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000585 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
586 }
587
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000588};
589
590}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000591
592/// shouldReturnTypeInRegister - Determine if the given type should be
593/// passed in a register (for the Darwin ABI).
594bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000595 ASTContext &Context,
596 unsigned callingConvention) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000597 uint64_t Size = Context.getTypeSize(Ty);
598
599 // Type must be register sized.
600 if (!isRegisterSize(Size))
601 return false;
602
603 if (Ty->isVectorType()) {
604 // 64- and 128- bit vectors inside structures are not returned in
605 // registers.
606 if (Size == 64 || Size == 128)
607 return false;
608
609 return true;
610 }
611
Daniel Dunbar77115232010-05-15 00:00:30 +0000612 // If this is a builtin, pointer, enum, complex type, member pointer, or
613 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000614 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000615 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000616 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000617 return true;
618
619 // Arrays are treated like records.
620 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000621 return shouldReturnTypeInRegister(AT->getElementType(), Context,
622 callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000623
624 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000625 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000626 if (!RT) return false;
627
Anders Carlssona8874232010-01-27 03:25:19 +0000628 // FIXME: Traverse bases here too.
629
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000630 // For thiscall conventions, structures will never be returned in
631 // a register. This is for compatibility with the MSVC ABI
632 if (callingConvention == llvm::CallingConv::X86_ThisCall &&
633 RT->isStructureType()) {
634 return false;
635 }
636
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000637 // Structure types are passed in register if all fields would be
638 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000639 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
640 e = RT->getDecl()->field_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000641 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000642
643 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000644 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000645 continue;
646
647 // Check fields recursively.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000648 if (!shouldReturnTypeInRegister(FD->getType(), Context,
649 callingConvention))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000650 return false;
651 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000652 return true;
653}
654
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000655ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
656 unsigned callingConvention) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000657 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000658 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000659
Chris Lattnera3c109b2010-07-29 02:16:43 +0000660 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000661 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000662 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000663 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000664
665 // 128-bit vectors are a special case; they are returned in
666 // registers and we need to make sure to pick a type the LLVM
667 // backend will like.
668 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +0000669 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000670 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000671
672 // Always return in register if it fits in a general purpose
673 // register, or if it is 64 bits and has a single element.
674 if ((Size == 8 || Size == 16 || Size == 32) ||
675 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +0000676 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000677 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000678
679 return ABIArgInfo::getIndirect(0);
680 }
681
682 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000683 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000684
John McCalld608cdb2010-08-22 10:59:02 +0000685 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000686 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000687 if (isRecordReturnIndirect(RT, CGT))
Anders Carlsson40092972009-10-20 22:07:59 +0000688 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000689
Anders Carlsson40092972009-10-20 22:07:59 +0000690 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000691 if (RT->getDecl()->hasFlexibleArrayMember())
692 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000693 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000694
David Chisnall1e4249c2009-08-17 23:08:21 +0000695 // If specified, structs and unions are always indirect.
696 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000697 return ABIArgInfo::getIndirect(0);
698
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000699 // Small structures which are register sized are generally returned
700 // in a register.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000701 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(),
702 callingConvention)) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000703 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000704
705 // As a special-case, if the struct is a "single-element" struct, and
706 // the field is of type "float" or "double", return it in a
Eli Friedman55fc7e22012-01-25 22:46:34 +0000707 // floating-point register. (MSVC does not apply this special case.)
708 // We apply a similar transformation for pointer types to improve the
709 // quality of the generated IR.
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000710 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000711 if ((!IsWin32StructABI && SeltTy->isRealFloatingType())
Eli Friedman55fc7e22012-01-25 22:46:34 +0000712 || SeltTy->hasPointerRepresentation())
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000713 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
714
715 // FIXME: We should be able to narrow this integer in cases with dead
716 // padding.
Chris Lattner800588f2010-07-29 06:26:06 +0000717 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000718 }
719
720 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000721 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000722
Chris Lattnera3c109b2010-07-29 02:16:43 +0000723 // Treat an enum type as its underlying type.
724 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
725 RetTy = EnumTy->getDecl()->getIntegerType();
726
727 return (RetTy->isPromotableIntegerType() ?
728 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000729}
730
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000731static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
732 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
733}
734
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000735static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
736 const RecordType *RT = Ty->getAs<RecordType>();
737 if (!RT)
738 return 0;
739 const RecordDecl *RD = RT->getDecl();
740
741 // If this is a C++ record, check the bases first.
742 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
743 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
744 e = CXXRD->bases_end(); i != e; ++i)
745 if (!isRecordWithSSEVectorType(Context, i->getType()))
746 return false;
747
748 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
749 i != e; ++i) {
750 QualType FT = i->getType();
751
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000752 if (isSSEVectorType(Context, FT))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000753 return true;
754
755 if (isRecordWithSSEVectorType(Context, FT))
756 return true;
757 }
758
759 return false;
760}
761
Daniel Dunbare59d8582010-09-16 20:42:06 +0000762unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
763 unsigned Align) const {
764 // Otherwise, if the alignment is less than or equal to the minimum ABI
765 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000766 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +0000767 return 0; // Use default alignment.
768
769 // On non-Darwin, the stack type alignment is always 4.
770 if (!IsDarwinVectorABI) {
771 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000772 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +0000773 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000774
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000775 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000776 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
777 isRecordWithSSEVectorType(getContext(), Ty)))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000778 return 16;
779
780 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000781}
782
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000783ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
784 unsigned &FreeRegs) const {
785 if (!ByVal) {
786 if (FreeRegs) {
787 --FreeRegs; // Non byval indirects just use one pointer.
788 return ABIArgInfo::getIndirectInReg(0, false);
789 }
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000790 return ABIArgInfo::getIndirect(0, false);
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000791 }
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000792
Daniel Dunbare59d8582010-09-16 20:42:06 +0000793 // Compute the byval alignment.
794 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
795 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
796 if (StackAlign == 0)
Chris Lattnerde92d732011-05-22 23:35:00 +0000797 return ABIArgInfo::getIndirect(4);
Daniel Dunbare59d8582010-09-16 20:42:06 +0000798
799 // If the stack alignment is less than the type alignment, realign the
800 // argument.
801 if (StackAlign < TypeAlign)
802 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
803 /*Realign=*/true);
804
805 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000806}
807
Rafael Espindolab48280b2012-07-31 02:44:24 +0000808X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
809 const Type *T = isSingleElementStruct(Ty, getContext());
810 if (!T)
811 T = Ty.getTypePtr();
812
813 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
814 BuiltinType::Kind K = BT->getKind();
815 if (K == BuiltinType::Float || K == BuiltinType::Double)
816 return Float;
817 }
818 return Integer;
819}
820
Rafael Espindolab6932692012-10-24 01:58:58 +0000821bool X86_32ABIInfo::shouldUseInReg(QualType Ty, unsigned &FreeRegs,
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000822 bool IsFastCall, bool &NeedsPadding) const {
823 NeedsPadding = false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000824 Class C = classify(Ty);
825 if (C == Float)
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000826 return false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000827
Rafael Espindolab6932692012-10-24 01:58:58 +0000828 unsigned Size = getContext().getTypeSize(Ty);
829 unsigned SizeInRegs = (Size + 31) / 32;
Rafael Espindola5f14fcb2012-10-23 02:04:01 +0000830
831 if (SizeInRegs == 0)
832 return false;
833
Rafael Espindolab48280b2012-07-31 02:44:24 +0000834 if (SizeInRegs > FreeRegs) {
835 FreeRegs = 0;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000836 return false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000837 }
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000838
Rafael Espindolab48280b2012-07-31 02:44:24 +0000839 FreeRegs -= SizeInRegs;
Rafael Espindolab6932692012-10-24 01:58:58 +0000840
841 if (IsFastCall) {
842 if (Size > 32)
843 return false;
844
845 if (Ty->isIntegralOrEnumerationType())
846 return true;
847
848 if (Ty->isPointerType())
849 return true;
850
851 if (Ty->isReferenceType())
852 return true;
853
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000854 if (FreeRegs)
855 NeedsPadding = true;
856
Rafael Espindolab6932692012-10-24 01:58:58 +0000857 return false;
858 }
859
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000860 return true;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000861}
862
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000863ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Rafael Espindolab6932692012-10-24 01:58:58 +0000864 unsigned &FreeRegs,
865 bool IsFastCall) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000866 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000867 if (isAggregateTypeForABI(Ty)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000868 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000869 if (IsWin32StructABI)
870 return getIndirectResult(Ty, true, FreeRegs);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000871
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000872 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, CGT))
873 return getIndirectResult(Ty, RAA == CGCXXABI::RAA_DirectInMemory, FreeRegs);
874
875 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000876 if (RT->getDecl()->hasFlexibleArrayMember())
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000877 return getIndirectResult(Ty, true, FreeRegs);
Anders Carlssona8874232010-01-27 03:25:19 +0000878 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000879
Eli Friedman5a4d3522011-11-18 00:28:11 +0000880 // Ignore empty structs/unions.
Eli Friedman5a1ac892011-11-18 04:01:36 +0000881 if (isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000882 return ABIArgInfo::getIgnore();
883
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000884 llvm::LLVMContext &LLVMContext = getVMContext();
885 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
886 bool NeedsPadding;
887 if (shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding)) {
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000888 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000889 SmallVector<llvm::Type*, 3> Elements;
890 for (unsigned I = 0; I < SizeInRegs; ++I)
891 Elements.push_back(Int32);
892 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
893 return ABIArgInfo::getDirectInReg(Result);
894 }
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000895 llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : 0;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000896
Daniel Dunbar53012f42009-11-09 01:33:53 +0000897 // Expand small (<= 128-bit) record types when we know that the stack layout
898 // of those arguments will match the struct. This is important because the
899 // LLVM backend isn't smart enough to remove byval, which inhibits many
900 // optimizations.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000901 if (getContext().getTypeSize(Ty) <= 4*32 &&
902 canExpandIndirectArgument(Ty, getContext()))
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000903 return ABIArgInfo::getExpandWithPadding(IsFastCall, PaddingType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000904
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000905 return getIndirectResult(Ty, true, FreeRegs);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000906 }
907
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000908 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +0000909 // On Darwin, some vectors are passed in memory, we handle this by passing
910 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000911 if (IsDarwinVectorABI) {
912 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000913 if ((Size == 8 || Size == 16 || Size == 32) ||
914 (Size == 64 && VT->getNumElements() == 1))
915 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
916 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000917 }
Bill Wendlingbb465d72010-10-18 03:41:31 +0000918
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000919 if (IsX86_MMXType(CGT.ConvertType(Ty)))
920 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64));
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000921
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000922 return ABIArgInfo::getDirect();
923 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000924
925
Chris Lattnera3c109b2010-07-29 02:16:43 +0000926 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
927 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000928
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000929 bool NeedsPadding;
930 bool InReg = shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding);
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000931
932 if (Ty->isPromotableIntegerType()) {
933 if (InReg)
934 return ABIArgInfo::getExtendInReg();
935 return ABIArgInfo::getExtend();
936 }
937 if (InReg)
938 return ABIArgInfo::getDirectInReg();
939 return ABIArgInfo::getDirect();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000940}
941
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000942void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
943 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
944 FI.getCallingConvention());
Rafael Espindolab48280b2012-07-31 02:44:24 +0000945
Rafael Espindolab6932692012-10-24 01:58:58 +0000946 unsigned CC = FI.getCallingConvention();
947 bool IsFastCall = CC == llvm::CallingConv::X86_FastCall;
948 unsigned FreeRegs;
949 if (IsFastCall)
950 FreeRegs = 2;
951 else if (FI.getHasRegParm())
952 FreeRegs = FI.getRegParm();
953 else
954 FreeRegs = DefaultNumRegisterParameters;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000955
956 // If the return value is indirect, then the hidden argument is consuming one
957 // integer register.
958 if (FI.getReturnInfo().isIndirect() && FreeRegs) {
959 --FreeRegs;
960 ABIArgInfo &Old = FI.getReturnInfo();
961 Old = ABIArgInfo::getIndirectInReg(Old.getIndirectAlign(),
962 Old.getIndirectByVal(),
963 Old.getIndirectRealign());
964 }
965
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000966 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
967 it != ie; ++it)
Rafael Espindolab6932692012-10-24 01:58:58 +0000968 it->info = classifyArgumentType(it->type, FreeRegs, IsFastCall);
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000969}
970
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000971llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
972 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +0000973 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000974
975 CGBuilderTy &Builder = CGF.Builder;
976 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
977 "ap");
978 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Eli Friedman7b1fb812011-11-18 02:12:09 +0000979
980 // Compute if the address needs to be aligned
981 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
982 Align = getTypeStackAlignInBytes(Ty, Align);
983 Align = std::max(Align, 4U);
984 if (Align > 4) {
985 // addr = (addr + align - 1) & -align;
986 llvm::Value *Offset =
987 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
988 Addr = CGF.Builder.CreateGEP(Addr, Offset);
989 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
990 CGF.Int32Ty);
991 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
992 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
993 Addr->getType(),
994 "ap.cur.aligned");
995 }
996
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000997 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000998 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000999 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1000
1001 uint64_t Offset =
Eli Friedman7b1fb812011-11-18 02:12:09 +00001002 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001003 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00001004 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001005 "ap.next");
1006 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1007
1008 return AddrTyped;
1009}
1010
Charles Davis74f72932010-02-13 15:54:06 +00001011void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
1012 llvm::GlobalValue *GV,
1013 CodeGen::CodeGenModule &CGM) const {
1014 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1015 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
1016 // Get the LLVM function.
1017 llvm::Function *Fn = cast<llvm::Function>(GV);
1018
1019 // Now add the 'alignstack' attribute with a value of 16.
Bill Wendling0d583392012-10-15 20:36:26 +00001020 llvm::AttrBuilder B;
Bill Wendlinge91e9ec2012-10-14 03:28:14 +00001021 B.addStackAlignmentAttr(16);
Bill Wendling909b6de2013-01-23 00:21:06 +00001022 Fn->addAttributes(llvm::AttributeSet::FunctionIndex,
1023 llvm::AttributeSet::get(CGM.getLLVMContext(),
1024 llvm::AttributeSet::FunctionIndex,
1025 B));
Charles Davis74f72932010-02-13 15:54:06 +00001026 }
1027 }
1028}
1029
John McCall6374c332010-03-06 00:35:14 +00001030bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
1031 CodeGen::CodeGenFunction &CGF,
1032 llvm::Value *Address) const {
1033 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCall6374c332010-03-06 00:35:14 +00001034
Chris Lattner8b418682012-02-07 00:39:47 +00001035 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001036
John McCall6374c332010-03-06 00:35:14 +00001037 // 0-7 are the eight integer registers; the order is different
1038 // on Darwin (for EH), but the range is the same.
1039 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +00001040 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +00001041
John McCall64aa4b32013-04-16 22:48:15 +00001042 if (CGF.CGM.getTarget().getTriple().isOSDarwin()) {
John McCall6374c332010-03-06 00:35:14 +00001043 // 12-16 are st(0..4). Not sure why we stop at 4.
1044 // These have size 16, which is sizeof(long double) on
1045 // platforms with 8-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001046 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCallaeeb7012010-05-27 06:19:26 +00001047 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001048
John McCall6374c332010-03-06 00:35:14 +00001049 } else {
1050 // 9 is %eflags, which doesn't get a size on Darwin for some
1051 // reason.
1052 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
1053
1054 // 11-16 are st(0..5). Not sure why we stop at 5.
1055 // These have size 12, which is sizeof(long double) on
1056 // platforms with 4-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001057 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCallaeeb7012010-05-27 06:19:26 +00001058 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1059 }
John McCall6374c332010-03-06 00:35:14 +00001060
1061 return false;
1062}
1063
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001064//===----------------------------------------------------------------------===//
1065// X86-64 ABI Implementation
1066//===----------------------------------------------------------------------===//
1067
1068
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001069namespace {
1070/// X86_64ABIInfo - The X86_64 ABI information.
1071class X86_64ABIInfo : public ABIInfo {
1072 enum Class {
1073 Integer = 0,
1074 SSE,
1075 SSEUp,
1076 X87,
1077 X87Up,
1078 ComplexX87,
1079 NoClass,
1080 Memory
1081 };
1082
1083 /// merge - Implement the X86_64 ABI merging algorithm.
1084 ///
1085 /// Merge an accumulating classification \arg Accum with a field
1086 /// classification \arg Field.
1087 ///
1088 /// \param Accum - The accumulating classification. This should
1089 /// always be either NoClass or the result of a previous merge
1090 /// call. In addition, this should never be Memory (the caller
1091 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001092 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001093
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001094 /// postMerge - Implement the X86_64 ABI post merging algorithm.
1095 ///
1096 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
1097 /// final MEMORY or SSE classes when necessary.
1098 ///
1099 /// \param AggregateSize - The size of the current aggregate in
1100 /// the classification process.
1101 ///
1102 /// \param Lo - The classification for the parts of the type
1103 /// residing in the low word of the containing object.
1104 ///
1105 /// \param Hi - The classification for the parts of the type
1106 /// residing in the higher words of the containing object.
1107 ///
1108 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
1109
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001110 /// classify - Determine the x86_64 register classes in which the
1111 /// given type T should be passed.
1112 ///
1113 /// \param Lo - The classification for the parts of the type
1114 /// residing in the low word of the containing object.
1115 ///
1116 /// \param Hi - The classification for the parts of the type
1117 /// residing in the high word of the containing object.
1118 ///
1119 /// \param OffsetBase - The bit offset of this type in the
1120 /// containing object. Some parameters are classified different
1121 /// depending on whether they straddle an eightbyte boundary.
1122 ///
1123 /// If a word is unused its result will be NoClass; if a type should
1124 /// be passed in Memory then at least the classification of \arg Lo
1125 /// will be Memory.
1126 ///
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001127 /// The \arg Lo class will be NoClass iff the argument is ignored.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001128 ///
1129 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
1130 /// also be ComplexX87.
Chris Lattner9c254f02010-06-29 06:01:59 +00001131 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001132
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001133 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001134 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
1135 unsigned IROffset, QualType SourceTy,
1136 unsigned SourceOffset) const;
1137 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
1138 unsigned IROffset, QualType SourceTy,
1139 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001140
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001141 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001142 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +00001143 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001144
1145 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001146 /// such that the argument will be passed in memory.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001147 ///
1148 /// \param freeIntRegs - The number of free integer registers remaining
1149 /// available.
1150 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001151
Chris Lattnera3c109b2010-07-29 02:16:43 +00001152 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001153
Bill Wendlingbb465d72010-10-18 03:41:31 +00001154 ABIArgInfo classifyArgumentType(QualType Ty,
Daniel Dunbaredfac032012-03-10 01:03:58 +00001155 unsigned freeIntRegs,
Bill Wendlingbb465d72010-10-18 03:41:31 +00001156 unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +00001157 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001158
Eli Friedmanee1ad992011-12-02 00:11:43 +00001159 bool IsIllegalVectorType(QualType Ty) const;
1160
John McCall67a57732011-04-21 01:20:55 +00001161 /// The 0.98 ABI revision clarified a lot of ambiguities,
1162 /// unfortunately in ways that were not always consistent with
1163 /// certain previous compilers. In particular, platforms which
1164 /// required strict binary compatibility with older versions of GCC
1165 /// may need to exempt themselves.
1166 bool honorsRevision0_98() const {
John McCall64aa4b32013-04-16 22:48:15 +00001167 return !getTarget().getTriple().isOSDarwin();
John McCall67a57732011-04-21 01:20:55 +00001168 }
1169
Eli Friedmanee1ad992011-12-02 00:11:43 +00001170 bool HasAVX;
Derek Schuffbabaf312012-10-11 15:52:22 +00001171 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
1172 // 64-bit hardware.
1173 bool Has64BitPointers;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001174
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001175public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001176 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
Derek Schuffbabaf312012-10-11 15:52:22 +00001177 ABIInfo(CGT), HasAVX(hasavx),
Derek Schuff90da80c2012-10-11 18:21:13 +00001178 Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001179 }
Chris Lattner9c254f02010-06-29 06:01:59 +00001180
John McCallde5d3c72012-02-17 03:33:10 +00001181 bool isPassedUsingAVXType(QualType type) const {
1182 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00001183 // The freeIntRegs argument doesn't matter here.
1184 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE);
John McCallde5d3c72012-02-17 03:33:10 +00001185 if (info.isDirect()) {
1186 llvm::Type *ty = info.getCoerceToType();
1187 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
1188 return (vectorTy->getBitWidth() > 128);
1189 }
1190 return false;
1191 }
1192
Chris Lattneree5dcd02010-07-29 02:31:05 +00001193 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001194
1195 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1196 CodeGenFunction &CGF) const;
1197};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001198
Chris Lattnerf13721d2010-08-31 16:44:54 +00001199/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001200class WinX86_64ABIInfo : public ABIInfo {
1201
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00001202 ABIArgInfo classify(QualType Ty, bool IsReturnType) const;
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001203
Chris Lattnerf13721d2010-08-31 16:44:54 +00001204public:
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001205 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
1206
1207 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattnerf13721d2010-08-31 16:44:54 +00001208
1209 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1210 CodeGenFunction &CGF) const;
1211};
1212
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001213class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1214public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001215 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
Derek Schuffbabaf312012-10-11 15:52:22 +00001216 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
John McCall6374c332010-03-06 00:35:14 +00001217
John McCallde5d3c72012-02-17 03:33:10 +00001218 const X86_64ABIInfo &getABIInfo() const {
1219 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
1220 }
1221
John McCall6374c332010-03-06 00:35:14 +00001222 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1223 return 7;
1224 }
1225
1226 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1227 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001228 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001229
John McCallaeeb7012010-05-27 06:19:26 +00001230 // 0-15 are the 16 integer registers.
1231 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001232 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +00001233 return false;
1234 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001235
Jay Foadef6de3d2011-07-11 09:56:20 +00001236 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001237 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +00001238 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001239 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1240 }
1241
John McCallde5d3c72012-02-17 03:33:10 +00001242 bool isNoProtoCallVariadic(const CallArgList &args,
1243 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +00001244 // The default CC on x86-64 sets %al to the number of SSA
1245 // registers used, and GCC sets this when calling an unprototyped
Eli Friedman3ed79032011-12-01 04:53:19 +00001246 // function, so we override the default behavior. However, don't do
Eli Friedman68805fe2011-12-06 03:08:26 +00001247 // that when AVX types are involved: the ABI explicitly states it is
1248 // undefined, and it doesn't work in practice because of how the ABI
1249 // defines varargs anyway.
John McCallde5d3c72012-02-17 03:33:10 +00001250 if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) {
Eli Friedman3ed79032011-12-01 04:53:19 +00001251 bool HasAVXType = false;
John McCallde5d3c72012-02-17 03:33:10 +00001252 for (CallArgList::const_iterator
1253 it = args.begin(), ie = args.end(); it != ie; ++it) {
1254 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1255 HasAVXType = true;
1256 break;
Eli Friedman3ed79032011-12-01 04:53:19 +00001257 }
1258 }
John McCallde5d3c72012-02-17 03:33:10 +00001259
Eli Friedman3ed79032011-12-01 04:53:19 +00001260 if (!HasAVXType)
1261 return true;
1262 }
John McCall01f151e2011-09-21 08:08:30 +00001263
John McCallde5d3c72012-02-17 03:33:10 +00001264 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
John McCall01f151e2011-09-21 08:08:30 +00001265 }
1266
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001267};
1268
Aaron Ballman89735b92013-05-24 15:06:56 +00001269static std::string qualifyWindowsLibrary(llvm::StringRef Lib) {
1270 // If the argument does not end in .lib, automatically add the suffix. This
1271 // matches the behavior of MSVC.
1272 std::string ArgStr = Lib;
1273 if (Lib.size() <= 4 ||
1274 Lib.substr(Lib.size() - 4).compare_lower(".lib") != 0) {
1275 ArgStr += ".lib";
1276 }
1277 return ArgStr;
1278}
1279
Reid Kleckner3190ca92013-05-08 13:44:39 +00001280class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo {
1281public:
1282 WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned RegParms)
1283 : X86_32TargetCodeGenInfo(CGT, false, true, true, RegParms) {}
1284
1285 void getDependentLibraryOption(llvm::StringRef Lib,
1286 llvm::SmallString<24> &Opt) const {
1287 Opt = "/DEFAULTLIB:";
Aaron Ballman89735b92013-05-24 15:06:56 +00001288 Opt += qualifyWindowsLibrary(Lib);
Reid Kleckner3190ca92013-05-08 13:44:39 +00001289 }
1290};
1291
Chris Lattnerf13721d2010-08-31 16:44:54 +00001292class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1293public:
1294 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1295 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1296
1297 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1298 return 7;
1299 }
1300
1301 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1302 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001303 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001304
Chris Lattnerf13721d2010-08-31 16:44:54 +00001305 // 0-15 are the 16 integer registers.
1306 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001307 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattnerf13721d2010-08-31 16:44:54 +00001308 return false;
1309 }
Reid Kleckner3190ca92013-05-08 13:44:39 +00001310
1311 void getDependentLibraryOption(llvm::StringRef Lib,
1312 llvm::SmallString<24> &Opt) const {
1313 Opt = "/DEFAULTLIB:";
Aaron Ballman89735b92013-05-24 15:06:56 +00001314 Opt += qualifyWindowsLibrary(Lib);
Reid Kleckner3190ca92013-05-08 13:44:39 +00001315 }
Chris Lattnerf13721d2010-08-31 16:44:54 +00001316};
1317
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001318}
1319
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001320void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1321 Class &Hi) const {
1322 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1323 //
1324 // (a) If one of the classes is Memory, the whole argument is passed in
1325 // memory.
1326 //
1327 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1328 // memory.
1329 //
1330 // (c) If the size of the aggregate exceeds two eightbytes and the first
1331 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1332 // argument is passed in memory. NOTE: This is necessary to keep the
1333 // ABI working for processors that don't support the __m256 type.
1334 //
1335 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1336 //
1337 // Some of these are enforced by the merging logic. Others can arise
1338 // only with unions; for example:
1339 // union { _Complex double; unsigned; }
1340 //
1341 // Note that clauses (b) and (c) were added in 0.98.
1342 //
1343 if (Hi == Memory)
1344 Lo = Memory;
1345 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1346 Lo = Memory;
1347 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1348 Lo = Memory;
1349 if (Hi == SSEUp && Lo != SSE)
1350 Hi = SSE;
1351}
1352
Chris Lattner1090a9b2010-06-28 21:43:59 +00001353X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001354 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1355 // classified recursively so that always two fields are
1356 // considered. The resulting class is calculated according to
1357 // the classes of the fields in the eightbyte:
1358 //
1359 // (a) If both classes are equal, this is the resulting class.
1360 //
1361 // (b) If one of the classes is NO_CLASS, the resulting class is
1362 // the other class.
1363 //
1364 // (c) If one of the classes is MEMORY, the result is the MEMORY
1365 // class.
1366 //
1367 // (d) If one of the classes is INTEGER, the result is the
1368 // INTEGER.
1369 //
1370 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1371 // MEMORY is used as class.
1372 //
1373 // (f) Otherwise class SSE is used.
1374
1375 // Accum should never be memory (we should have returned) or
1376 // ComplexX87 (because this cannot be passed in a structure).
1377 assert((Accum != Memory && Accum != ComplexX87) &&
1378 "Invalid accumulated classification during merge.");
1379 if (Accum == Field || Field == NoClass)
1380 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001381 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001382 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001383 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001384 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001385 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001386 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001387 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1388 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001389 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001390 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001391}
1392
Chris Lattnerbcaedae2010-06-30 19:14:05 +00001393void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001394 Class &Lo, Class &Hi) const {
1395 // FIXME: This code can be simplified by introducing a simple value class for
1396 // Class pairs with appropriate constructor methods for the various
1397 // situations.
1398
1399 // FIXME: Some of the split computations are wrong; unaligned vectors
1400 // shouldn't be passed in registers for example, so there is no chance they
1401 // can straddle an eightbyte. Verify & simplify.
1402
1403 Lo = Hi = NoClass;
1404
1405 Class &Current = OffsetBase < 64 ? Lo : Hi;
1406 Current = Memory;
1407
John McCall183700f2009-09-21 23:43:11 +00001408 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001409 BuiltinType::Kind k = BT->getKind();
1410
1411 if (k == BuiltinType::Void) {
1412 Current = NoClass;
1413 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1414 Lo = Integer;
1415 Hi = Integer;
1416 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1417 Current = Integer;
Derek Schuff7da46f92012-10-11 16:55:58 +00001418 } else if ((k == BuiltinType::Float || k == BuiltinType::Double) ||
1419 (k == BuiltinType::LongDouble &&
John McCall64aa4b32013-04-16 22:48:15 +00001420 getTarget().getTriple().getOS() == llvm::Triple::NaCl)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001421 Current = SSE;
1422 } else if (k == BuiltinType::LongDouble) {
1423 Lo = X87;
1424 Hi = X87Up;
1425 }
1426 // FIXME: _Decimal32 and _Decimal64 are SSE.
1427 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001428 return;
1429 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001430
Chris Lattner1090a9b2010-06-28 21:43:59 +00001431 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001432 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +00001433 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001434 return;
1435 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001436
Chris Lattner1090a9b2010-06-28 21:43:59 +00001437 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001438 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001439 return;
1440 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001441
Chris Lattner1090a9b2010-06-28 21:43:59 +00001442 if (Ty->isMemberPointerType()) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001443 if (Ty->isMemberFunctionPointerType() && Has64BitPointers)
Daniel Dunbar67d438d2010-05-15 00:00:37 +00001444 Lo = Hi = Integer;
1445 else
1446 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001447 return;
1448 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001449
Chris Lattner1090a9b2010-06-28 21:43:59 +00001450 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001451 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001452 if (Size == 32) {
1453 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1454 // float> as integer.
1455 Current = Integer;
1456
1457 // If this type crosses an eightbyte boundary, it should be
1458 // split.
1459 uint64_t EB_Real = (OffsetBase) / 64;
1460 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1461 if (EB_Real != EB_Imag)
1462 Hi = Lo;
1463 } else if (Size == 64) {
1464 // gcc passes <1 x double> in memory. :(
1465 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1466 return;
1467
1468 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001469 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001470 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1471 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1472 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001473 Current = Integer;
1474 else
1475 Current = SSE;
1476
1477 // If this type crosses an eightbyte boundary, it should be
1478 // split.
1479 if (OffsetBase && OffsetBase != 64)
1480 Hi = Lo;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001481 } else if (Size == 128 || (HasAVX && Size == 256)) {
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001482 // Arguments of 256-bits are split into four eightbyte chunks. The
1483 // least significant one belongs to class SSE and all the others to class
1484 // SSEUP. The original Lo and Hi design considers that types can't be
1485 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1486 // This design isn't correct for 256-bits, but since there're no cases
1487 // where the upper parts would need to be inspected, avoid adding
1488 // complexity and just consider Hi to match the 64-256 part.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001489 Lo = SSE;
1490 Hi = SSEUp;
1491 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001492 return;
1493 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001494
Chris Lattner1090a9b2010-06-28 21:43:59 +00001495 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001496 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001497
Chris Lattnerea044322010-07-29 02:01:43 +00001498 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001499 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001500 if (Size <= 64)
1501 Current = Integer;
1502 else if (Size <= 128)
1503 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001504 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001505 Current = SSE;
Derek Schuff7da46f92012-10-11 16:55:58 +00001506 else if (ET == getContext().DoubleTy ||
1507 (ET == getContext().LongDoubleTy &&
John McCall64aa4b32013-04-16 22:48:15 +00001508 getTarget().getTriple().getOS() == llvm::Triple::NaCl))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001509 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001510 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001511 Current = ComplexX87;
1512
1513 // If this complex type crosses an eightbyte boundary then it
1514 // should be split.
1515 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001516 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001517 if (Hi == NoClass && EB_Real != EB_Imag)
1518 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001519
Chris Lattner1090a9b2010-06-28 21:43:59 +00001520 return;
1521 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001522
Chris Lattnerea044322010-07-29 02:01:43 +00001523 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001524 // Arrays are treated like structures.
1525
Chris Lattnerea044322010-07-29 02:01:43 +00001526 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001527
1528 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001529 // than four eightbytes, ..., it has class MEMORY.
1530 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001531 return;
1532
1533 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1534 // fields, it has class MEMORY.
1535 //
1536 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001537 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001538 return;
1539
1540 // Otherwise implement simplified merge. We could be smarter about
1541 // this, but it isn't worth it and would be harder to verify.
1542 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001543 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001544 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes089d8922011-07-12 01:27:38 +00001545
1546 // The only case a 256-bit wide vector could be used is when the array
1547 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1548 // to work for sizes wider than 128, early check and fallback to memory.
1549 if (Size > 128 && EltSize != 256)
1550 return;
1551
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001552 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1553 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001554 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001555 Lo = merge(Lo, FieldLo);
1556 Hi = merge(Hi, FieldHi);
1557 if (Lo == Memory || Hi == Memory)
1558 break;
1559 }
1560
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001561 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001562 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001563 return;
1564 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001565
Chris Lattner1090a9b2010-06-28 21:43:59 +00001566 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001567 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001568
1569 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001570 // than four eightbytes, ..., it has class MEMORY.
1571 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001572 return;
1573
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001574 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1575 // copy constructor or a non-trivial destructor, it is passed by invisible
1576 // reference.
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00001577 if (getRecordArgABI(RT, CGT))
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001578 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001579
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001580 const RecordDecl *RD = RT->getDecl();
1581
1582 // Assume variable sized types are passed in memory.
1583 if (RD->hasFlexibleArrayMember())
1584 return;
1585
Chris Lattnerea044322010-07-29 02:01:43 +00001586 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001587
1588 // Reset Lo class, this will be recomputed.
1589 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001590
1591 // If this is a C++ record, classify the bases first.
1592 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1593 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1594 e = CXXRD->bases_end(); i != e; ++i) {
1595 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1596 "Unexpected base class!");
1597 const CXXRecordDecl *Base =
1598 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1599
1600 // Classify this field.
1601 //
1602 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1603 // single eightbyte, each is classified separately. Each eightbyte gets
1604 // initialized to class NO_CLASS.
1605 Class FieldLo, FieldHi;
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001606 uint64_t Offset =
1607 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
Chris Lattner9c254f02010-06-29 06:01:59 +00001608 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001609 Lo = merge(Lo, FieldLo);
1610 Hi = merge(Hi, FieldHi);
1611 if (Lo == Memory || Hi == Memory)
1612 break;
1613 }
1614 }
1615
1616 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001617 unsigned idx = 0;
Bruno Cardoso Lopes548e4782011-07-12 22:30:58 +00001618 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001619 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001620 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1621 bool BitField = i->isBitField();
1622
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001623 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1624 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001625 //
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001626 // The only case a 256-bit wide vector could be used is when the struct
1627 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1628 // to work for sizes wider than 128, early check and fallback to memory.
1629 //
1630 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1631 Lo = Memory;
1632 return;
1633 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001634 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001635 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001636 Lo = Memory;
1637 return;
1638 }
1639
1640 // Classify this field.
1641 //
1642 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1643 // exceeds a single eightbyte, each is classified
1644 // separately. Each eightbyte gets initialized to class
1645 // NO_CLASS.
1646 Class FieldLo, FieldHi;
1647
1648 // Bit-fields require special handling, they do not force the
1649 // structure to be passed in memory even if unaligned, and
1650 // therefore they can straddle an eightbyte.
1651 if (BitField) {
1652 // Ignore padding bit-fields.
1653 if (i->isUnnamedBitfield())
1654 continue;
1655
1656 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001657 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001658
1659 uint64_t EB_Lo = Offset / 64;
1660 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1661 FieldLo = FieldHi = NoClass;
1662 if (EB_Lo) {
1663 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1664 FieldLo = NoClass;
1665 FieldHi = Integer;
1666 } else {
1667 FieldLo = Integer;
1668 FieldHi = EB_Hi ? Integer : NoClass;
1669 }
1670 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001671 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001672 Lo = merge(Lo, FieldLo);
1673 Hi = merge(Hi, FieldHi);
1674 if (Lo == Memory || Hi == Memory)
1675 break;
1676 }
1677
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001678 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001679 }
1680}
1681
Chris Lattner9c254f02010-06-29 06:01:59 +00001682ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001683 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1684 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001685 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001686 // Treat an enum type as its underlying type.
1687 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1688 Ty = EnumTy->getDecl()->getIntegerType();
1689
1690 return (Ty->isPromotableIntegerType() ?
1691 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1692 }
1693
1694 return ABIArgInfo::getIndirect(0);
1695}
1696
Eli Friedmanee1ad992011-12-02 00:11:43 +00001697bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1698 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1699 uint64_t Size = getContext().getTypeSize(VecTy);
1700 unsigned LargestVector = HasAVX ? 256 : 128;
1701 if (Size <= 64 || Size > LargestVector)
1702 return true;
1703 }
1704
1705 return false;
1706}
1707
Daniel Dunbaredfac032012-03-10 01:03:58 +00001708ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1709 unsigned freeIntRegs) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001710 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1711 // place naturally.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001712 //
1713 // This assumption is optimistic, as there could be free registers available
1714 // when we need to pass this argument in memory, and LLVM could try to pass
1715 // the argument in the free register. This does not seem to happen currently,
1716 // but this code would be much safer if we could mark the argument with
1717 // 'onstack'. See PR12193.
Eli Friedmanee1ad992011-12-02 00:11:43 +00001718 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001719 // Treat an enum type as its underlying type.
1720 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1721 Ty = EnumTy->getDecl()->getIntegerType();
1722
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001723 return (Ty->isPromotableIntegerType() ?
1724 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001725 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001726
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00001727 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
1728 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001729
Chris Lattner855d2272011-05-22 23:21:23 +00001730 // Compute the byval alignment. We specify the alignment of the byval in all
1731 // cases so that the mid-level optimizer knows the alignment of the byval.
1732 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
Daniel Dunbaredfac032012-03-10 01:03:58 +00001733
1734 // Attempt to avoid passing indirect results using byval when possible. This
1735 // is important for good codegen.
1736 //
1737 // We do this by coercing the value into a scalar type which the backend can
1738 // handle naturally (i.e., without using byval).
1739 //
1740 // For simplicity, we currently only do this when we have exhausted all of the
1741 // free integer registers. Doing this when there are free integer registers
1742 // would require more care, as we would have to ensure that the coerced value
1743 // did not claim the unused register. That would require either reording the
1744 // arguments to the function (so that any subsequent inreg values came first),
1745 // or only doing this optimization when there were no following arguments that
1746 // might be inreg.
1747 //
1748 // We currently expect it to be rare (particularly in well written code) for
1749 // arguments to be passed on the stack when there are still free integer
1750 // registers available (this would typically imply large structs being passed
1751 // by value), so this seems like a fair tradeoff for now.
1752 //
1753 // We can revisit this if the backend grows support for 'onstack' parameter
1754 // attributes. See PR12193.
1755 if (freeIntRegs == 0) {
1756 uint64_t Size = getContext().getTypeSize(Ty);
1757
1758 // If this type fits in an eightbyte, coerce it into the matching integral
1759 // type, which will end up on the stack (with alignment 8).
1760 if (Align == 8 && Size <= 64)
1761 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1762 Size));
1763 }
1764
Chris Lattner855d2272011-05-22 23:21:23 +00001765 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001766}
1767
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001768/// GetByteVectorType - The ABI specifies that a value should be passed in an
1769/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner0f408f52010-07-29 04:56:46 +00001770/// vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001771llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001772 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001773
Chris Lattner15842bd2010-07-29 05:02:29 +00001774 // Wrapper structs that just contain vectors are passed just like vectors,
1775 // strip them off if present.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001776 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner15842bd2010-07-29 05:02:29 +00001777 while (STy && STy->getNumElements() == 1) {
1778 IRType = STy->getElementType(0);
1779 STy = dyn_cast<llvm::StructType>(IRType);
1780 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001781
Bruno Cardoso Lopes528a8c72011-07-08 22:57:35 +00001782 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001783 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1784 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001785 unsigned BitWidth = VT->getBitWidth();
Tanya Lattnerce275672011-11-28 23:18:11 +00001786 if ((BitWidth >= 128 && BitWidth <= 256) &&
Chris Lattner0f408f52010-07-29 04:56:46 +00001787 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1788 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1789 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1790 EltTy->isIntegerTy(128)))
1791 return VT;
1792 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001793
Chris Lattner0f408f52010-07-29 04:56:46 +00001794 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1795}
1796
Chris Lattnere2962be2010-07-29 07:30:00 +00001797/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1798/// is known to either be off the end of the specified type or being in
1799/// alignment padding. The user type specified is known to be at most 128 bits
1800/// in size, and have passed through X86_64ABIInfo::classify with a successful
1801/// classification that put one of the two halves in the INTEGER class.
1802///
1803/// It is conservatively correct to return false.
1804static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1805 unsigned EndBit, ASTContext &Context) {
1806 // If the bytes being queried are off the end of the type, there is no user
1807 // data hiding here. This handles analysis of builtins, vectors and other
1808 // types that don't contain interesting padding.
1809 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1810 if (TySize <= StartBit)
1811 return true;
1812
Chris Lattner021c3a32010-07-29 07:43:55 +00001813 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1814 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1815 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1816
1817 // Check each element to see if the element overlaps with the queried range.
1818 for (unsigned i = 0; i != NumElts; ++i) {
1819 // If the element is after the span we care about, then we're done..
1820 unsigned EltOffset = i*EltSize;
1821 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001822
Chris Lattner021c3a32010-07-29 07:43:55 +00001823 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1824 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1825 EndBit-EltOffset, Context))
1826 return false;
1827 }
1828 // If it overlaps no elements, then it is safe to process as padding.
1829 return true;
1830 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001831
Chris Lattnere2962be2010-07-29 07:30:00 +00001832 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1833 const RecordDecl *RD = RT->getDecl();
1834 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001835
Chris Lattnere2962be2010-07-29 07:30:00 +00001836 // If this is a C++ record, check the bases first.
1837 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1838 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1839 e = CXXRD->bases_end(); i != e; ++i) {
1840 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1841 "Unexpected base class!");
1842 const CXXRecordDecl *Base =
1843 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001844
Chris Lattnere2962be2010-07-29 07:30:00 +00001845 // If the base is after the span we care about, ignore it.
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001846 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
Chris Lattnere2962be2010-07-29 07:30:00 +00001847 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001848
Chris Lattnere2962be2010-07-29 07:30:00 +00001849 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1850 if (!BitsContainNoUserData(i->getType(), BaseStart,
1851 EndBit-BaseOffset, Context))
1852 return false;
1853 }
1854 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001855
Chris Lattnere2962be2010-07-29 07:30:00 +00001856 // Verify that no field has data that overlaps the region of interest. Yes
1857 // this could be sped up a lot by being smarter about queried fields,
1858 // however we're only looking at structs up to 16 bytes, so we don't care
1859 // much.
1860 unsigned idx = 0;
1861 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1862 i != e; ++i, ++idx) {
1863 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001864
Chris Lattnere2962be2010-07-29 07:30:00 +00001865 // If we found a field after the region we care about, then we're done.
1866 if (FieldOffset >= EndBit) break;
1867
1868 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1869 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1870 Context))
1871 return false;
1872 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001873
Chris Lattnere2962be2010-07-29 07:30:00 +00001874 // If nothing in this record overlapped the area of interest, then we're
1875 // clean.
1876 return true;
1877 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001878
Chris Lattnere2962be2010-07-29 07:30:00 +00001879 return false;
1880}
1881
Chris Lattner0b362002010-07-29 18:39:32 +00001882/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1883/// float member at the specified offset. For example, {int,{float}} has a
1884/// float at offset 4. It is conservatively correct for this routine to return
1885/// false.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001886static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Micah Villmow25a6a842012-10-08 16:25:52 +00001887 const llvm::DataLayout &TD) {
Chris Lattner0b362002010-07-29 18:39:32 +00001888 // Base case if we find a float.
1889 if (IROffset == 0 && IRType->isFloatTy())
1890 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001891
Chris Lattner0b362002010-07-29 18:39:32 +00001892 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001893 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner0b362002010-07-29 18:39:32 +00001894 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1895 unsigned Elt = SL->getElementContainingOffset(IROffset);
1896 IROffset -= SL->getElementOffset(Elt);
1897 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1898 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001899
Chris Lattner0b362002010-07-29 18:39:32 +00001900 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001901 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1902 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner0b362002010-07-29 18:39:32 +00001903 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1904 IROffset -= IROffset/EltSize*EltSize;
1905 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1906 }
1907
1908 return false;
1909}
1910
Chris Lattnerf47c9442010-07-29 18:13:09 +00001911
1912/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1913/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001914llvm::Type *X86_64ABIInfo::
1915GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00001916 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001917 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001918 // pass as float if the last 4 bytes is just padding. This happens for
1919 // structs that contain 3 floats.
1920 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1921 SourceOffset*8+64, getContext()))
1922 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001923
Chris Lattner0b362002010-07-29 18:39:32 +00001924 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1925 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1926 // case.
Micah Villmow25a6a842012-10-08 16:25:52 +00001927 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
1928 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001929 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001930
Chris Lattnerf47c9442010-07-29 18:13:09 +00001931 return llvm::Type::getDoubleTy(getVMContext());
1932}
1933
1934
Chris Lattner0d2656d2010-07-29 17:40:35 +00001935/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1936/// an 8-byte GPR. This means that we either have a scalar or we are talking
1937/// about the high or low part of an up-to-16-byte struct. This routine picks
1938/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001939/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1940/// etc).
1941///
1942/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1943/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1944/// the 8-byte value references. PrefType may be null.
1945///
1946/// SourceTy is the source level type for the entire argument. SourceOffset is
1947/// an offset into this that we're processing (which is always either 0 or 8).
1948///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001949llvm::Type *X86_64ABIInfo::
1950GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00001951 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001952 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1953 // returning an 8-byte unit starting with it. See if we can safely use it.
1954 if (IROffset == 0) {
1955 // Pointers and int64's always fill the 8-byte unit.
Derek Schuffbabaf312012-10-11 15:52:22 +00001956 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
1957 IRType->isIntegerTy(64))
Chris Lattnere2962be2010-07-29 07:30:00 +00001958 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001959
Chris Lattnere2962be2010-07-29 07:30:00 +00001960 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1961 // goodness in the source type is just tail padding. This is allowed to
1962 // kick in for struct {double,int} on the int, but not on
1963 // struct{double,int,int} because we wouldn't return the second int. We
1964 // have to do this analysis on the source type because we can't depend on
1965 // unions being lowered a specific way etc.
1966 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
Derek Schuffbabaf312012-10-11 15:52:22 +00001967 IRType->isIntegerTy(32) ||
1968 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
1969 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
1970 cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001971
Chris Lattnere2962be2010-07-29 07:30:00 +00001972 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1973 SourceOffset*8+64, getContext()))
1974 return IRType;
1975 }
1976 }
Chris Lattner49382de2010-07-28 22:44:07 +00001977
Chris Lattner2acc6e32011-07-18 04:24:23 +00001978 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001979 // If this is a struct, recurse into the field at the specified offset.
Micah Villmow25a6a842012-10-08 16:25:52 +00001980 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001981 if (IROffset < SL->getSizeInBytes()) {
1982 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1983 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001984
Chris Lattner0d2656d2010-07-29 17:40:35 +00001985 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1986 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001987 }
Chris Lattner49382de2010-07-28 22:44:07 +00001988 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001989
Chris Lattner2acc6e32011-07-18 04:24:23 +00001990 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001991 llvm::Type *EltTy = ATy->getElementType();
Micah Villmow25a6a842012-10-08 16:25:52 +00001992 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
Chris Lattner021c3a32010-07-29 07:43:55 +00001993 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00001994 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1995 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00001996 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001997
Chris Lattner49382de2010-07-28 22:44:07 +00001998 // Okay, we don't have any better idea of what to pass, so we pass this in an
1999 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00002000 unsigned TySizeInBytes =
2001 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00002002
Chris Lattner9e45a3d2010-07-29 17:34:39 +00002003 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002004
Chris Lattner49382de2010-07-28 22:44:07 +00002005 // It is always safe to classify this as an integer type up to i64 that
2006 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00002007 return llvm::IntegerType::get(getVMContext(),
2008 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00002009}
2010
Chris Lattner66e7b682010-09-01 00:50:20 +00002011
2012/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
2013/// be used as elements of a two register pair to pass or return, return a
2014/// first class aggregate to represent them. For example, if the low part of
2015/// a by-value argument should be passed as i32* and the high part as float,
2016/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002017static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00002018GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Micah Villmow25a6a842012-10-08 16:25:52 +00002019 const llvm::DataLayout &TD) {
Chris Lattner66e7b682010-09-01 00:50:20 +00002020 // In order to correctly satisfy the ABI, we need to the high part to start
2021 // at offset 8. If the high and low parts we inferred are both 4-byte types
2022 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
2023 // the second element at offset 8. Check for this:
2024 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
2025 unsigned HiAlign = TD.getABITypeAlignment(Hi);
Micah Villmow25a6a842012-10-08 16:25:52 +00002026 unsigned HiStart = llvm::DataLayout::RoundUpAlignment(LoSize, HiAlign);
Chris Lattner66e7b682010-09-01 00:50:20 +00002027 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002028
Chris Lattner66e7b682010-09-01 00:50:20 +00002029 // To handle this, we have to increase the size of the low part so that the
2030 // second element will start at an 8 byte offset. We can't increase the size
2031 // of the second element because it might make us access off the end of the
2032 // struct.
2033 if (HiStart != 8) {
2034 // There are only two sorts of types the ABI generation code can produce for
2035 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
2036 // Promote these to a larger type.
2037 if (Lo->isFloatTy())
2038 Lo = llvm::Type::getDoubleTy(Lo->getContext());
2039 else {
2040 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
2041 Lo = llvm::Type::getInt64Ty(Lo->getContext());
2042 }
2043 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002044
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002045 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002046
2047
Chris Lattner66e7b682010-09-01 00:50:20 +00002048 // Verify that the second element is at an 8-byte offset.
2049 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
2050 "Invalid x86-64 argument pair!");
2051 return Result;
2052}
2053
Chris Lattner519f68c2010-07-28 23:06:14 +00002054ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00002055classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00002056 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
2057 // classification algorithm.
2058 X86_64ABIInfo::Class Lo, Hi;
2059 classify(RetTy, 0, Lo, Hi);
2060
2061 // Check some invariants.
2062 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002063 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2064
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002065 llvm::Type *ResType = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002066 switch (Lo) {
2067 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002068 if (Hi == NoClass)
2069 return ABIArgInfo::getIgnore();
2070 // If the low part is just padding, it takes no register, leave ResType
2071 // null.
2072 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2073 "Unknown missing lo part");
2074 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002075
2076 case SSEUp:
2077 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002078 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002079
2080 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
2081 // hidden argument.
2082 case Memory:
2083 return getIndirectReturnResult(RetTy);
2084
2085 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
2086 // available register of the sequence %rax, %rdx is used.
2087 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002088 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002089
Chris Lattnereb518b42010-07-29 21:42:50 +00002090 // If we have a sign or zero extended integer, make sure to return Extend
2091 // so that the parameter gets the right LLVM IR attributes.
2092 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2093 // Treat an enum type as its underlying type.
2094 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2095 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002096
Chris Lattnereb518b42010-07-29 21:42:50 +00002097 if (RetTy->isIntegralOrEnumerationType() &&
2098 RetTy->isPromotableIntegerType())
2099 return ABIArgInfo::getExtend();
2100 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002101 break;
2102
2103 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
2104 // available SSE register of the sequence %xmm0, %xmm1 is used.
2105 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002106 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00002107 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002108
2109 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
2110 // returned on the X87 stack in %st0 as 80-bit x87 number.
2111 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00002112 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00002113 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002114
2115 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
2116 // part of the value is returned in %st0 and the imaginary part in
2117 // %st1.
2118 case ComplexX87:
2119 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00002120 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00002121 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00002122 NULL);
2123 break;
2124 }
2125
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002126 llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002127 switch (Hi) {
2128 // Memory was handled previously and X87 should
2129 // never occur as a hi class.
2130 case Memory:
2131 case X87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002132 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002133
2134 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00002135 case NoClass:
2136 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002137
Chris Lattner3db4dde2010-09-01 00:20:33 +00002138 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002139 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002140 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2141 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002142 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00002143 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002144 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002145 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2146 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002147 break;
2148
2149 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002150 // is passed in the next available eightbyte chunk if the last used
2151 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00002152 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002153 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00002154 case SSEUp:
2155 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002156 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00002157 break;
2158
2159 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
2160 // returned together with the previous X87 value in %st0.
2161 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002162 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00002163 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002164 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00002165 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00002166 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002167 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002168 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2169 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00002170 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002171 break;
2172 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002173
Chris Lattner3db4dde2010-09-01 00:20:33 +00002174 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00002175 // known to pass in the high eightbyte of the result. We do this by forming a
2176 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00002177 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002178 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Chris Lattner519f68c2010-07-28 23:06:14 +00002179
Chris Lattnereb518b42010-07-29 21:42:50 +00002180 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00002181}
2182
Daniel Dunbaredfac032012-03-10 01:03:58 +00002183ABIArgInfo X86_64ABIInfo::classifyArgumentType(
2184 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE)
2185 const
2186{
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002187 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00002188 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002189
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002190 // Check some invariants.
2191 // FIXME: Enforce these by construction.
2192 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002193 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2194
2195 neededInt = 0;
2196 neededSSE = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002197 llvm::Type *ResType = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002198 switch (Lo) {
2199 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002200 if (Hi == NoClass)
2201 return ABIArgInfo::getIgnore();
2202 // If the low part is just padding, it takes no register, leave ResType
2203 // null.
2204 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2205 "Unknown missing lo part");
2206 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002207
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002208 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
2209 // on the stack.
2210 case Memory:
2211
2212 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
2213 // COMPLEX_X87, it is passed in memory.
2214 case X87:
2215 case ComplexX87:
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002216 if (getRecordArgABI(Ty, CGT) == CGCXXABI::RAA_Indirect)
Eli Friedmanded137f2011-06-29 07:04:55 +00002217 ++neededInt;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002218 return getIndirectResult(Ty, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002219
2220 case SSEUp:
2221 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002222 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002223
2224 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
2225 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
2226 // and %r9 is used.
2227 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00002228 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002229
Chris Lattner49382de2010-07-28 22:44:07 +00002230 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002231 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00002232
2233 // If we have a sign or zero extended integer, make sure to return Extend
2234 // so that the parameter gets the right LLVM IR attributes.
2235 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2236 // Treat an enum type as its underlying type.
2237 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2238 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002239
Chris Lattnereb518b42010-07-29 21:42:50 +00002240 if (Ty->isIntegralOrEnumerationType() &&
2241 Ty->isPromotableIntegerType())
2242 return ABIArgInfo::getExtend();
2243 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002244
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002245 break;
2246
2247 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
2248 // available SSE register is used, the registers are taken in the
2249 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00002250 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002251 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00002252 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00002253 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002254 break;
2255 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00002256 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002257
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002258 llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002259 switch (Hi) {
2260 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002261 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002262 // which is passed in memory.
2263 case Memory:
2264 case X87:
2265 case ComplexX87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002266 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002267
2268 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002269
Chris Lattner645406a2010-09-01 00:24:35 +00002270 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002271 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00002272 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002273 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002274
Chris Lattner645406a2010-09-01 00:24:35 +00002275 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2276 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002277 break;
2278
2279 // X87Up generally doesn't occur here (long double is passed in
2280 // memory), except in situations involving unions.
2281 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00002282 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002283 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002284
Chris Lattner645406a2010-09-01 00:24:35 +00002285 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2286 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00002287
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002288 ++neededSSE;
2289 break;
2290
2291 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
2292 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002293 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002294 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00002295 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002296 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002297 break;
2298 }
2299
Chris Lattner645406a2010-09-01 00:24:35 +00002300 // If a high part was specified, merge it together with the low part. It is
2301 // known to pass in the high eightbyte of the result. We do this by forming a
2302 // first class struct aggregate with the high and low part: {low, high}
2303 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002304 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002305
Chris Lattnereb518b42010-07-29 21:42:50 +00002306 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002307}
2308
Chris Lattneree5dcd02010-07-29 02:31:05 +00002309void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002310
Chris Lattnera3c109b2010-07-29 02:16:43 +00002311 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002312
2313 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00002314 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002315
2316 // If the return value is indirect, then the hidden argument is consuming one
2317 // integer register.
2318 if (FI.getReturnInfo().isIndirect())
2319 --freeIntRegs;
2320
2321 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2322 // get assigned (in left-to-right order) for passing as follows...
2323 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2324 it != ie; ++it) {
Bill Wendling99aaae82010-10-18 23:51:38 +00002325 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002326 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
2327 neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002328
2329 // AMD64-ABI 3.2.3p3: If there are no registers available for any
2330 // eightbyte of an argument, the whole argument is passed on the
2331 // stack. If registers have already been assigned for some
2332 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00002333 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002334 freeIntRegs -= neededInt;
2335 freeSSERegs -= neededSSE;
2336 } else {
Daniel Dunbaredfac032012-03-10 01:03:58 +00002337 it->info = getIndirectResult(it->type, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002338 }
2339 }
2340}
2341
2342static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2343 QualType Ty,
2344 CodeGenFunction &CGF) {
2345 llvm::Value *overflow_arg_area_p =
2346 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2347 llvm::Value *overflow_arg_area =
2348 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2349
2350 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2351 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedman8d2fe422011-11-18 02:44:19 +00002352 // It isn't stated explicitly in the standard, but in practice we use
2353 // alignment greater than 16 where necessary.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002354 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2355 if (Align > 8) {
Eli Friedman8d2fe422011-11-18 02:44:19 +00002356 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
Owen Anderson0032b272009-08-13 21:57:51 +00002357 llvm::Value *Offset =
Eli Friedman8d2fe422011-11-18 02:44:19 +00002358 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002359 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2360 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00002361 CGF.Int64Ty);
Eli Friedman8d2fe422011-11-18 02:44:19 +00002362 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002363 overflow_arg_area =
2364 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2365 overflow_arg_area->getType(),
2366 "overflow_arg_area.align");
2367 }
2368
2369 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002370 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002371 llvm::Value *Res =
2372 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002373 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002374
2375 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2376 // l->overflow_arg_area + sizeof(type).
2377 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2378 // an 8 byte boundary.
2379
2380 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00002381 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00002382 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002383 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2384 "overflow_arg_area.next");
2385 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2386
2387 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2388 return Res;
2389}
2390
2391llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2392 CodeGenFunction &CGF) const {
2393 // Assume that va_list type is correct; should be pointer to LLVM type:
2394 // struct {
2395 // i32 gp_offset;
2396 // i32 fp_offset;
2397 // i8* overflow_arg_area;
2398 // i8* reg_save_area;
2399 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00002400 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002401
Chris Lattnera14db752010-03-11 18:19:55 +00002402 Ty = CGF.getContext().getCanonicalType(Ty);
Daniel Dunbaredfac032012-03-10 01:03:58 +00002403 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002404
2405 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2406 // in the registers. If not go to step 7.
2407 if (!neededInt && !neededSSE)
2408 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2409
2410 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2411 // general purpose registers needed to pass type and num_fp to hold
2412 // the number of floating point registers needed.
2413
2414 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2415 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2416 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2417 //
2418 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2419 // register save space).
2420
2421 llvm::Value *InRegs = 0;
2422 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2423 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2424 if (neededInt) {
2425 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2426 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002427 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2428 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002429 }
2430
2431 if (neededSSE) {
2432 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2433 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2434 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00002435 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2436 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002437 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2438 }
2439
2440 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2441 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2442 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2443 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2444
2445 // Emit code to load the value if it was passed in registers.
2446
2447 CGF.EmitBlock(InRegBlock);
2448
2449 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2450 // an offset of l->gp_offset and/or l->fp_offset. This may require
2451 // copying to a temporary location in case the parameter is passed
2452 // in different register classes or requires an alignment greater
2453 // than 8 for general purpose registers and 16 for XMM registers.
2454 //
2455 // FIXME: This really results in shameful code when we end up needing to
2456 // collect arguments from different places; often what should result in a
2457 // simple assembling of a structure from scattered addresses has many more
2458 // loads than necessary. Can we clean this up?
Chris Lattner2acc6e32011-07-18 04:24:23 +00002459 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002460 llvm::Value *RegAddr =
2461 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2462 "reg_save_area");
2463 if (neededInt && neededSSE) {
2464 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002465 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002466 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002467 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2468 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002469 llvm::Type *TyLo = ST->getElementType(0);
2470 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002471 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002472 "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002473 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2474 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002475 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2476 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002477 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2478 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002479 llvm::Value *V =
2480 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2481 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2482 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2483 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2484
Owen Andersona1cf15f2009-07-14 23:10:40 +00002485 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002486 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002487 } else if (neededInt) {
2488 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2489 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002490 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002491 } else if (neededSSE == 1) {
2492 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2493 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2494 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002495 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002496 assert(neededSSE == 2 && "Invalid number of needed registers!");
2497 // SSE registers are spaced 16 bytes apart in the register save
2498 // area, we need to collect the two eightbytes together.
2499 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002500 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner8b418682012-02-07 00:39:47 +00002501 llvm::Type *DoubleTy = CGF.DoubleTy;
Chris Lattner2acc6e32011-07-18 04:24:23 +00002502 llvm::Type *DblPtrTy =
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002503 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002504 llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002505 DoubleTy, NULL);
2506 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2507 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2508 DblPtrTy));
2509 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2510 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2511 DblPtrTy));
2512 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2513 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2514 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002515 }
2516
2517 // AMD64-ABI 3.5.7p5: Step 5. Set:
2518 // l->gp_offset = l->gp_offset + num_gp * 8
2519 // l->fp_offset = l->fp_offset + num_fp * 16.
2520 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002521 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002522 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2523 gp_offset_p);
2524 }
2525 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002526 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002527 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2528 fp_offset_p);
2529 }
2530 CGF.EmitBranch(ContBlock);
2531
2532 // Emit code to load the value if it was passed in memory.
2533
2534 CGF.EmitBlock(InMemBlock);
2535 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2536
2537 // Return the appropriate result.
2538
2539 CGF.EmitBlock(ContBlock);
Jay Foadbbf3bac2011-03-30 11:28:58 +00002540 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002541 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002542 ResAddr->addIncoming(RegAddr, InRegBlock);
2543 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002544 return ResAddr;
2545}
2546
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002547ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, bool IsReturnType) const {
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002548
2549 if (Ty->isVoidType())
2550 return ABIArgInfo::getIgnore();
2551
2552 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2553 Ty = EnumTy->getDecl()->getIntegerType();
2554
2555 uint64_t Size = getContext().getTypeSize(Ty);
2556
2557 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002558 if (IsReturnType) {
2559 if (isRecordReturnIndirect(RT, CGT))
2560 return ABIArgInfo::getIndirect(0, false);
2561 } else {
2562 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, CGT))
2563 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
2564 }
2565
2566 if (RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002567 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2568
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002569 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
John McCall64aa4b32013-04-16 22:48:15 +00002570 if (Size == 128 && getTarget().getTriple().getOS() == llvm::Triple::MinGW32)
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002571 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2572 Size));
2573
2574 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2575 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2576 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002577 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002578 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2579 Size));
2580
2581 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2582 }
2583
2584 if (Ty->isPromotableIntegerType())
2585 return ABIArgInfo::getExtend();
2586
2587 return ABIArgInfo::getDirect();
2588}
2589
2590void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2591
2592 QualType RetTy = FI.getReturnType();
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002593 FI.getReturnInfo() = classify(RetTy, true);
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002594
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002595 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2596 it != ie; ++it)
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002597 it->info = classify(it->type, false);
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002598}
2599
Chris Lattnerf13721d2010-08-31 16:44:54 +00002600llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2601 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002602 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002603
Chris Lattnerf13721d2010-08-31 16:44:54 +00002604 CGBuilderTy &Builder = CGF.Builder;
2605 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2606 "ap");
2607 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2608 llvm::Type *PTy =
2609 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2610 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2611
2612 uint64_t Offset =
2613 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2614 llvm::Value *NextAddr =
2615 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2616 "ap.next");
2617 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2618
2619 return AddrTyped;
2620}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002621
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00002622namespace {
2623
Derek Schuff263366f2012-10-16 22:30:41 +00002624class NaClX86_64ABIInfo : public ABIInfo {
2625 public:
2626 NaClX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2627 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, HasAVX) {}
2628 virtual void computeInfo(CGFunctionInfo &FI) const;
2629 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2630 CodeGenFunction &CGF) const;
2631 private:
2632 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
2633 X86_64ABIInfo NInfo; // Used for everything else.
2634};
2635
2636class NaClX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2637 public:
2638 NaClX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2639 : TargetCodeGenInfo(new NaClX86_64ABIInfo(CGT, HasAVX)) {}
2640};
2641
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00002642}
2643
Derek Schuff263366f2012-10-16 22:30:41 +00002644void NaClX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2645 if (FI.getASTCallingConvention() == CC_PnaclCall)
2646 PInfo.computeInfo(FI);
2647 else
2648 NInfo.computeInfo(FI);
2649}
2650
2651llvm::Value *NaClX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2652 CodeGenFunction &CGF) const {
2653 // Always use the native convention; calling pnacl-style varargs functions
2654 // is unuspported.
2655 return NInfo.EmitVAArg(VAListAddr, Ty, CGF);
2656}
2657
2658
John McCallec853ba2010-03-11 00:10:12 +00002659// PowerPC-32
2660
2661namespace {
2662class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2663public:
Chris Lattnerea044322010-07-29 02:01:43 +00002664 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002665
John McCallec853ba2010-03-11 00:10:12 +00002666 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2667 // This is recovered from gcc output.
2668 return 1; // r1 is the dedicated stack pointer
2669 }
2670
2671 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002672 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002673};
2674
2675}
2676
2677bool
2678PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2679 llvm::Value *Address) const {
2680 // This is calculated from the LLVM and GCC tables and verified
2681 // against gcc output. AFAIK all ABIs use the same encoding.
2682
2683 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallec853ba2010-03-11 00:10:12 +00002684
Chris Lattner8b418682012-02-07 00:39:47 +00002685 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallec853ba2010-03-11 00:10:12 +00002686 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2687 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2688 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2689
2690 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002691 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002692
2693 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002694 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002695
2696 // 64-76 are various 4-byte special-purpose registers:
2697 // 64: mq
2698 // 65: lr
2699 // 66: ctr
2700 // 67: ap
2701 // 68-75 cr0-7
2702 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002703 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002704
2705 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002706 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002707
2708 // 109: vrsave
2709 // 110: vscr
2710 // 111: spe_acc
2711 // 112: spefscr
2712 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002713 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002714
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002715 return false;
John McCallec853ba2010-03-11 00:10:12 +00002716}
2717
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002718// PowerPC-64
2719
2720namespace {
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002721/// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
2722class PPC64_SVR4_ABIInfo : public DefaultABIInfo {
2723
2724public:
2725 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
2726
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002727 bool isPromotableTypeForABI(QualType Ty) const;
2728
2729 ABIArgInfo classifyReturnType(QualType RetTy) const;
2730 ABIArgInfo classifyArgumentType(QualType Ty) const;
2731
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00002732 // TODO: We can add more logic to computeInfo to improve performance.
2733 // Example: For aggregate arguments that fit in a register, we could
2734 // use getDirectInReg (as is done below for structs containing a single
2735 // floating-point value) to avoid pushing them to memory on function
2736 // entry. This would require changing the logic in PPCISelLowering
2737 // when lowering the parameters in the caller and args in the callee.
2738 virtual void computeInfo(CGFunctionInfo &FI) const {
2739 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2740 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2741 it != ie; ++it) {
2742 // We rely on the default argument classification for the most part.
2743 // One exception: An aggregate containing a single floating-point
2744 // item must be passed in a register if one is available.
2745 const Type *T = isSingleElementStruct(it->type, getContext());
2746 if (T) {
2747 const BuiltinType *BT = T->getAs<BuiltinType>();
2748 if (BT && BT->isFloatingPoint()) {
2749 QualType QT(T, 0);
2750 it->info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
2751 continue;
2752 }
2753 }
2754 it->info = classifyArgumentType(it->type);
2755 }
2756 }
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002757
2758 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr,
2759 QualType Ty,
2760 CodeGenFunction &CGF) const;
2761};
2762
2763class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
2764public:
2765 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT)
2766 : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT)) {}
2767
2768 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2769 // This is recovered from gcc output.
2770 return 1; // r1 is the dedicated stack pointer
2771 }
2772
2773 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2774 llvm::Value *Address) const;
2775};
2776
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002777class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2778public:
2779 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
2780
2781 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2782 // This is recovered from gcc output.
2783 return 1; // r1 is the dedicated stack pointer
2784 }
2785
2786 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2787 llvm::Value *Address) const;
2788};
2789
2790}
2791
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002792// Return true if the ABI requires Ty to be passed sign- or zero-
2793// extended to 64 bits.
2794bool
2795PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
2796 // Treat an enum type as its underlying type.
2797 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2798 Ty = EnumTy->getDecl()->getIntegerType();
2799
2800 // Promotable integer types are required to be promoted by the ABI.
2801 if (Ty->isPromotableIntegerType())
2802 return true;
2803
2804 // In addition to the usual promotable integer types, we also need to
2805 // extend all 32-bit types, since the ABI requires promotion to 64 bits.
2806 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2807 switch (BT->getKind()) {
2808 case BuiltinType::Int:
2809 case BuiltinType::UInt:
2810 return true;
2811 default:
2812 break;
2813 }
2814
2815 return false;
2816}
2817
2818ABIArgInfo
2819PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
Bill Schmidtc9715fc2012-11-27 02:46:43 +00002820 if (Ty->isAnyComplexType())
2821 return ABIArgInfo::getDirect();
2822
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002823 if (isAggregateTypeForABI(Ty)) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002824 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
2825 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002826
2827 return ABIArgInfo::getIndirect(0);
2828 }
2829
2830 return (isPromotableTypeForABI(Ty) ?
2831 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2832}
2833
2834ABIArgInfo
2835PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
2836 if (RetTy->isVoidType())
2837 return ABIArgInfo::getIgnore();
2838
Bill Schmidt9e6111a2012-12-17 04:20:17 +00002839 if (RetTy->isAnyComplexType())
2840 return ABIArgInfo::getDirect();
2841
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002842 if (isAggregateTypeForABI(RetTy))
2843 return ABIArgInfo::getIndirect(0);
2844
2845 return (isPromotableTypeForABI(RetTy) ?
2846 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2847}
2848
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002849// Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
2850llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr,
2851 QualType Ty,
2852 CodeGenFunction &CGF) const {
2853 llvm::Type *BP = CGF.Int8PtrTy;
2854 llvm::Type *BPP = CGF.Int8PtrPtrTy;
2855
2856 CGBuilderTy &Builder = CGF.Builder;
2857 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
2858 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2859
Bill Schmidt19f8e852013-01-14 17:45:36 +00002860 // Update the va_list pointer. The pointer should be bumped by the
2861 // size of the object. We can trust getTypeSize() except for a complex
2862 // type whose base type is smaller than a doubleword. For these, the
2863 // size of the object is 16 bytes; see below for further explanation.
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002864 unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8;
Bill Schmidt19f8e852013-01-14 17:45:36 +00002865 QualType BaseTy;
2866 unsigned CplxBaseSize = 0;
2867
2868 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
2869 BaseTy = CTy->getElementType();
2870 CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8;
2871 if (CplxBaseSize < 8)
2872 SizeInBytes = 16;
2873 }
2874
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002875 unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8);
2876 llvm::Value *NextAddr =
2877 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset),
2878 "ap.next");
2879 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2880
Bill Schmidt19f8e852013-01-14 17:45:36 +00002881 // If we have a complex type and the base type is smaller than 8 bytes,
2882 // the ABI calls for the real and imaginary parts to be right-adjusted
2883 // in separate doublewords. However, Clang expects us to produce a
2884 // pointer to a structure with the two parts packed tightly. So generate
2885 // loads of the real and imaginary parts relative to the va_list pointer,
2886 // and store them to a temporary structure.
2887 if (CplxBaseSize && CplxBaseSize < 8) {
2888 llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2889 llvm::Value *ImagAddr = RealAddr;
2890 RealAddr = Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize));
2891 ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize));
2892 llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy));
2893 RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy);
2894 ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy);
2895 llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal");
2896 llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag");
2897 llvm::Value *Ptr = CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty),
2898 "vacplx");
2899 llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, ".real");
2900 llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, ".imag");
2901 Builder.CreateStore(Real, RealPtr, false);
2902 Builder.CreateStore(Imag, ImagPtr, false);
2903 return Ptr;
2904 }
2905
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002906 // If the argument is smaller than 8 bytes, it is right-adjusted in
2907 // its doubleword slot. Adjust the pointer to pick it up from the
2908 // correct offset.
2909 if (SizeInBytes < 8) {
2910 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2911 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes));
2912 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2913 }
2914
2915 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2916 return Builder.CreateBitCast(Addr, PTy);
2917}
2918
2919static bool
2920PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2921 llvm::Value *Address) {
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002922 // This is calculated from the LLVM and GCC tables and verified
2923 // against gcc output. AFAIK all ABIs use the same encoding.
2924
2925 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2926
2927 llvm::IntegerType *i8 = CGF.Int8Ty;
2928 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2929 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2930 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2931
2932 // 0-31: r0-31, the 8-byte general-purpose registers
2933 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
2934
2935 // 32-63: fp0-31, the 8-byte floating-point registers
2936 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
2937
2938 // 64-76 are various 4-byte special-purpose registers:
2939 // 64: mq
2940 // 65: lr
2941 // 66: ctr
2942 // 67: ap
2943 // 68-75 cr0-7
2944 // 76: xer
2945 AssignToArrayRange(Builder, Address, Four8, 64, 76);
2946
2947 // 77-108: v0-31, the 16-byte vector registers
2948 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
2949
2950 // 109: vrsave
2951 // 110: vscr
2952 // 111: spe_acc
2953 // 112: spefscr
2954 // 113: sfp
2955 AssignToArrayRange(Builder, Address, Four8, 109, 113);
2956
2957 return false;
2958}
John McCallec853ba2010-03-11 00:10:12 +00002959
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002960bool
2961PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
2962 CodeGen::CodeGenFunction &CGF,
2963 llvm::Value *Address) const {
2964
2965 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
2966}
2967
2968bool
2969PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2970 llvm::Value *Address) const {
2971
2972 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
2973}
2974
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002975//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002976// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002977//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002978
2979namespace {
2980
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002981class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002982public:
2983 enum ABIKind {
2984 APCS = 0,
2985 AAPCS = 1,
2986 AAPCS_VFP
2987 };
2988
2989private:
2990 ABIKind Kind;
2991
2992public:
John McCallbd7370a2013-02-28 19:01:20 +00002993 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {
2994 setRuntimeCC();
2995 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002996
John McCall49e34be2011-08-30 01:42:09 +00002997 bool isEABI() const {
John McCall64aa4b32013-04-16 22:48:15 +00002998 StringRef Env = getTarget().getTriple().getEnvironmentName();
Logan Chien94a71422012-09-02 09:30:11 +00002999 return (Env == "gnueabi" || Env == "eabi" ||
3000 Env == "android" || Env == "androideabi");
John McCall49e34be2011-08-30 01:42:09 +00003001 }
3002
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003003private:
3004 ABIKind getABIKind() const { return Kind; }
3005
Chris Lattnera3c109b2010-07-29 02:16:43 +00003006 ABIArgInfo classifyReturnType(QualType RetTy) const;
Manman Ren710c5172012-10-31 19:02:26 +00003007 ABIArgInfo classifyArgumentType(QualType RetTy, int *VFPRegs,
3008 unsigned &AllocatedVFP,
Manman Renb3fa55f2012-10-30 23:21:41 +00003009 bool &IsHA) const;
Manman Ren97f81572012-10-16 19:18:39 +00003010 bool isIllegalVectorType(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003011
Chris Lattneree5dcd02010-07-29 02:31:05 +00003012 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003013
3014 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3015 CodeGenFunction &CGF) const;
John McCallbd7370a2013-02-28 19:01:20 +00003016
3017 llvm::CallingConv::ID getLLVMDefaultCC() const;
3018 llvm::CallingConv::ID getABIDefaultCC() const;
3019 void setRuntimeCC();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003020};
3021
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003022class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
3023public:
Chris Lattnerea044322010-07-29 02:01:43 +00003024 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
3025 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00003026
John McCall49e34be2011-08-30 01:42:09 +00003027 const ARMABIInfo &getABIInfo() const {
3028 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
3029 }
3030
John McCall6374c332010-03-06 00:35:14 +00003031 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3032 return 13;
3033 }
Roman Divacky09345d12011-05-18 19:36:54 +00003034
Chris Lattner5f9e2722011-07-23 10:55:15 +00003035 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCallf85e1932011-06-15 23:02:42 +00003036 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
3037 }
3038
Roman Divacky09345d12011-05-18 19:36:54 +00003039 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3040 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003041 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divacky09345d12011-05-18 19:36:54 +00003042
3043 // 0-15 are the 16 integer registers.
Chris Lattner8b418682012-02-07 00:39:47 +00003044 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divacky09345d12011-05-18 19:36:54 +00003045 return false;
3046 }
John McCall49e34be2011-08-30 01:42:09 +00003047
3048 unsigned getSizeOfUnwindException() const {
3049 if (getABIInfo().isEABI()) return 88;
3050 return TargetCodeGenInfo::getSizeOfUnwindException();
3051 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003052};
3053
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003054}
3055
Chris Lattneree5dcd02010-07-29 02:31:05 +00003056void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Manman Renb3fa55f2012-10-30 23:21:41 +00003057 // To correctly handle Homogeneous Aggregate, we need to keep track of the
Manman Ren710c5172012-10-31 19:02:26 +00003058 // VFP registers allocated so far.
Manman Renb3fa55f2012-10-30 23:21:41 +00003059 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
3060 // VFP registers of the appropriate type unallocated then the argument is
3061 // allocated to the lowest-numbered sequence of such registers.
3062 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
3063 // unallocated are marked as unavailable.
3064 unsigned AllocatedVFP = 0;
Manman Ren710c5172012-10-31 19:02:26 +00003065 int VFPRegs[16] = { 0 };
Chris Lattnera3c109b2010-07-29 02:16:43 +00003066 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003067 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Manman Renb3fa55f2012-10-30 23:21:41 +00003068 it != ie; ++it) {
3069 unsigned PreAllocation = AllocatedVFP;
3070 bool IsHA = false;
3071 // 6.1.2.3 There is one VFP co-processor register class using registers
3072 // s0-s15 (d0-d7) for passing arguments.
3073 const unsigned NumVFPs = 16;
Manman Ren710c5172012-10-31 19:02:26 +00003074 it->info = classifyArgumentType(it->type, VFPRegs, AllocatedVFP, IsHA);
Manman Renb3fa55f2012-10-30 23:21:41 +00003075 // If we do not have enough VFP registers for the HA, any VFP registers
3076 // that are unallocated are marked as unavailable. To achieve this, we add
3077 // padding of (NumVFPs - PreAllocation) floats.
3078 if (IsHA && AllocatedVFP > NumVFPs && PreAllocation < NumVFPs) {
3079 llvm::Type *PaddingTy = llvm::ArrayType::get(
3080 llvm::Type::getFloatTy(getVMContext()), NumVFPs - PreAllocation);
3081 it->info = ABIArgInfo::getExpandWithPadding(false, PaddingTy);
3082 }
3083 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003084
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003085 // Always honor user-specified calling convention.
3086 if (FI.getCallingConvention() != llvm::CallingConv::C)
3087 return;
3088
John McCallbd7370a2013-02-28 19:01:20 +00003089 llvm::CallingConv::ID cc = getRuntimeCC();
3090 if (cc != llvm::CallingConv::C)
3091 FI.setEffectiveCallingConvention(cc);
3092}
Rafael Espindola25117ab2010-06-16 16:13:39 +00003093
John McCallbd7370a2013-02-28 19:01:20 +00003094/// Return the default calling convention that LLVM will use.
3095llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
3096 // The default calling convention that LLVM will infer.
John McCall64aa4b32013-04-16 22:48:15 +00003097 if (getTarget().getTriple().getEnvironmentName()=="gnueabihf")
John McCallbd7370a2013-02-28 19:01:20 +00003098 return llvm::CallingConv::ARM_AAPCS_VFP;
3099 else if (isEABI())
3100 return llvm::CallingConv::ARM_AAPCS;
3101 else
3102 return llvm::CallingConv::ARM_APCS;
3103}
3104
3105/// Return the calling convention that our ABI would like us to use
3106/// as the C calling convention.
3107llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003108 switch (getABIKind()) {
John McCallbd7370a2013-02-28 19:01:20 +00003109 case APCS: return llvm::CallingConv::ARM_APCS;
3110 case AAPCS: return llvm::CallingConv::ARM_AAPCS;
3111 case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003112 }
John McCallbd7370a2013-02-28 19:01:20 +00003113 llvm_unreachable("bad ABI kind");
3114}
3115
3116void ARMABIInfo::setRuntimeCC() {
3117 assert(getRuntimeCC() == llvm::CallingConv::C);
3118
3119 // Don't muddy up the IR with a ton of explicit annotations if
3120 // they'd just match what LLVM will infer from the triple.
3121 llvm::CallingConv::ID abiCC = getABIDefaultCC();
3122 if (abiCC != getLLVMDefaultCC())
3123 RuntimeCC = abiCC;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003124}
3125
Bob Wilson194f06a2011-08-03 05:58:22 +00003126/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
3127/// aggregate. If HAMembers is non-null, the number of base elements
3128/// contained in the type is returned through it; this is used for the
3129/// recursive calls that check aggregate component types.
3130static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
3131 ASTContext &Context,
3132 uint64_t *HAMembers = 0) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003133 uint64_t Members = 0;
Bob Wilson194f06a2011-08-03 05:58:22 +00003134 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
3135 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
3136 return false;
3137 Members *= AT->getSize().getZExtValue();
3138 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
3139 const RecordDecl *RD = RT->getDecl();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003140 if (RD->hasFlexibleArrayMember())
Bob Wilson194f06a2011-08-03 05:58:22 +00003141 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003142
Bob Wilson194f06a2011-08-03 05:58:22 +00003143 Members = 0;
3144 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3145 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00003146 const FieldDecl *FD = *i;
Bob Wilson194f06a2011-08-03 05:58:22 +00003147 uint64_t FldMembers;
3148 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
3149 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003150
3151 Members = (RD->isUnion() ?
3152 std::max(Members, FldMembers) : Members + FldMembers);
Bob Wilson194f06a2011-08-03 05:58:22 +00003153 }
3154 } else {
3155 Members = 1;
3156 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
3157 Members = 2;
3158 Ty = CT->getElementType();
3159 }
3160
3161 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
3162 // double, or 64-bit or 128-bit vectors.
3163 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3164 if (BT->getKind() != BuiltinType::Float &&
Tim Northoveradfa45f2012-07-20 22:29:29 +00003165 BT->getKind() != BuiltinType::Double &&
3166 BT->getKind() != BuiltinType::LongDouble)
Bob Wilson194f06a2011-08-03 05:58:22 +00003167 return false;
3168 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
3169 unsigned VecSize = Context.getTypeSize(VT);
3170 if (VecSize != 64 && VecSize != 128)
3171 return false;
3172 } else {
3173 return false;
3174 }
3175
3176 // The base type must be the same for all members. Vector types of the
3177 // same total size are treated as being equivalent here.
3178 const Type *TyPtr = Ty.getTypePtr();
3179 if (!Base)
3180 Base = TyPtr;
3181 if (Base != TyPtr &&
3182 (!Base->isVectorType() || !TyPtr->isVectorType() ||
3183 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
3184 return false;
3185 }
3186
3187 // Homogeneous Aggregates can have at most 4 members of the base type.
3188 if (HAMembers)
3189 *HAMembers = Members;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003190
3191 return (Members > 0 && Members <= 4);
Bob Wilson194f06a2011-08-03 05:58:22 +00003192}
3193
Manman Ren710c5172012-10-31 19:02:26 +00003194/// markAllocatedVFPs - update VFPRegs according to the alignment and
3195/// number of VFP registers (unit is S register) requested.
3196static void markAllocatedVFPs(int *VFPRegs, unsigned &AllocatedVFP,
3197 unsigned Alignment,
3198 unsigned NumRequired) {
3199 // Early Exit.
3200 if (AllocatedVFP >= 16)
3201 return;
3202 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
3203 // VFP registers of the appropriate type unallocated then the argument is
3204 // allocated to the lowest-numbered sequence of such registers.
3205 for (unsigned I = 0; I < 16; I += Alignment) {
3206 bool FoundSlot = true;
3207 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3208 if (J >= 16 || VFPRegs[J]) {
3209 FoundSlot = false;
3210 break;
3211 }
3212 if (FoundSlot) {
3213 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3214 VFPRegs[J] = 1;
3215 AllocatedVFP += NumRequired;
3216 return;
3217 }
3218 }
3219 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
3220 // unallocated are marked as unavailable.
3221 for (unsigned I = 0; I < 16; I++)
3222 VFPRegs[I] = 1;
3223 AllocatedVFP = 17; // We do not have enough VFP registers.
3224}
3225
3226ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, int *VFPRegs,
3227 unsigned &AllocatedVFP,
Manman Renb3fa55f2012-10-30 23:21:41 +00003228 bool &IsHA) const {
3229 // We update number of allocated VFPs according to
3230 // 6.1.2.1 The following argument types are VFP CPRCs:
3231 // A single-precision floating-point type (including promoted
3232 // half-precision types); A double-precision floating-point type;
3233 // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
3234 // with a Base Type of a single- or double-precision floating-point type,
3235 // 64-bit containerized vectors or 128-bit containerized vectors with one
3236 // to four Elements.
3237
Manman Ren97f81572012-10-16 19:18:39 +00003238 // Handle illegal vector types here.
3239 if (isIllegalVectorType(Ty)) {
3240 uint64_t Size = getContext().getTypeSize(Ty);
3241 if (Size <= 32) {
3242 llvm::Type *ResType =
3243 llvm::Type::getInt32Ty(getVMContext());
3244 return ABIArgInfo::getDirect(ResType);
3245 }
3246 if (Size == 64) {
3247 llvm::Type *ResType = llvm::VectorType::get(
3248 llvm::Type::getInt32Ty(getVMContext()), 2);
Manman Ren710c5172012-10-31 19:02:26 +00003249 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
Manman Ren97f81572012-10-16 19:18:39 +00003250 return ABIArgInfo::getDirect(ResType);
3251 }
3252 if (Size == 128) {
3253 llvm::Type *ResType = llvm::VectorType::get(
3254 llvm::Type::getInt32Ty(getVMContext()), 4);
Manman Ren710c5172012-10-31 19:02:26 +00003255 markAllocatedVFPs(VFPRegs, AllocatedVFP, 4, 4);
Manman Ren97f81572012-10-16 19:18:39 +00003256 return ABIArgInfo::getDirect(ResType);
3257 }
3258 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3259 }
Manman Ren710c5172012-10-31 19:02:26 +00003260 // Update VFPRegs for legal vector types.
Manman Renb3fa55f2012-10-30 23:21:41 +00003261 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3262 uint64_t Size = getContext().getTypeSize(VT);
3263 // Size of a legal vector should be power of 2 and above 64.
Manman Ren710c5172012-10-31 19:02:26 +00003264 markAllocatedVFPs(VFPRegs, AllocatedVFP, Size >= 128 ? 4 : 2, Size / 32);
Manman Renb3fa55f2012-10-30 23:21:41 +00003265 }
Manman Ren710c5172012-10-31 19:02:26 +00003266 // Update VFPRegs for floating point types.
Manman Renb3fa55f2012-10-30 23:21:41 +00003267 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3268 if (BT->getKind() == BuiltinType::Half ||
3269 BT->getKind() == BuiltinType::Float)
Manman Ren710c5172012-10-31 19:02:26 +00003270 markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, 1);
Manman Renb3fa55f2012-10-30 23:21:41 +00003271 if (BT->getKind() == BuiltinType::Double ||
Manman Ren710c5172012-10-31 19:02:26 +00003272 BT->getKind() == BuiltinType::LongDouble)
3273 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
Manman Renb3fa55f2012-10-30 23:21:41 +00003274 }
Manman Ren97f81572012-10-16 19:18:39 +00003275
John McCalld608cdb2010-08-22 10:59:02 +00003276 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003277 // Treat an enum type as its underlying type.
3278 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3279 Ty = EnumTy->getDecl()->getIntegerType();
3280
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003281 return (Ty->isPromotableIntegerType() ?
3282 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003283 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003284
Daniel Dunbar42025572009-09-14 21:54:03 +00003285 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003286 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00003287 return ABIArgInfo::getIgnore();
3288
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00003289 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
3290 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003291
Bob Wilson194f06a2011-08-03 05:58:22 +00003292 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
Manman Renb3fa55f2012-10-30 23:21:41 +00003293 // Homogeneous Aggregates need to be expanded when we can fit the aggregate
3294 // into VFP registers.
Bob Wilson194f06a2011-08-03 05:58:22 +00003295 const Type *Base = 0;
Manman Renb3fa55f2012-10-30 23:21:41 +00003296 uint64_t Members = 0;
3297 if (isHomogeneousAggregate(Ty, Base, getContext(), &Members)) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003298 assert(Base && "Base class should be set for homogeneous aggregate");
Manman Renb3fa55f2012-10-30 23:21:41 +00003299 // Base can be a floating-point or a vector.
3300 if (Base->isVectorType()) {
3301 // ElementSize is in number of floats.
3302 unsigned ElementSize = getContext().getTypeSize(Base) == 64 ? 2 : 4;
Manman Rencb489dd2012-11-06 19:05:29 +00003303 markAllocatedVFPs(VFPRegs, AllocatedVFP, ElementSize,
3304 Members * ElementSize);
Manman Renb3fa55f2012-10-30 23:21:41 +00003305 } else if (Base->isSpecificBuiltinType(BuiltinType::Float))
Manman Ren710c5172012-10-31 19:02:26 +00003306 markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, Members);
Manman Renb3fa55f2012-10-30 23:21:41 +00003307 else {
3308 assert(Base->isSpecificBuiltinType(BuiltinType::Double) ||
3309 Base->isSpecificBuiltinType(BuiltinType::LongDouble));
Manman Ren710c5172012-10-31 19:02:26 +00003310 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, Members * 2);
Manman Renb3fa55f2012-10-30 23:21:41 +00003311 }
3312 IsHA = true;
Bob Wilson194f06a2011-08-03 05:58:22 +00003313 return ABIArgInfo::getExpand();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003314 }
Bob Wilson194f06a2011-08-03 05:58:22 +00003315 }
3316
Manman Ren634b3d22012-08-13 21:23:55 +00003317 // Support byval for ARM.
Manman Rencb489dd2012-11-06 19:05:29 +00003318 // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
3319 // most 8-byte. We realign the indirect argument if type alignment is bigger
3320 // than ABI alignment.
Manman Renfd1ba912012-11-05 22:42:46 +00003321 uint64_t ABIAlign = 4;
3322 uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8;
3323 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3324 getABIKind() == ARMABIInfo::AAPCS)
3325 ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
Manman Ren885ad692012-11-06 04:58:01 +00003326 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
3327 return ABIArgInfo::getIndirect(0, /*ByVal=*/true,
Manman Rencb489dd2012-11-06 19:05:29 +00003328 /*Realign=*/TyAlign > ABIAlign);
Eli Friedman79f30982012-08-09 00:31:40 +00003329 }
3330
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00003331 // Otherwise, pass by coercing to a structure of the appropriate size.
Chris Lattner2acc6e32011-07-18 04:24:23 +00003332 llvm::Type* ElemTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003333 unsigned SizeRegs;
Eli Friedman79f30982012-08-09 00:31:40 +00003334 // FIXME: Try to match the types of the arguments more accurately where
3335 // we can.
3336 if (getContext().getTypeAlign(Ty) <= 32) {
Bob Wilson53fc1a62011-08-01 23:39:04 +00003337 ElemTy = llvm::Type::getInt32Ty(getVMContext());
3338 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Manman Ren78eb76e2012-06-25 22:04:00 +00003339 } else {
Manman Ren78eb76e2012-06-25 22:04:00 +00003340 ElemTy = llvm::Type::getInt64Ty(getVMContext());
3341 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastings67d097e2011-04-27 17:24:02 +00003342 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003343
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003344 llvm::Type *STy =
Chris Lattner7650d952011-06-18 22:49:11 +00003345 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003346 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003347}
3348
Chris Lattnera3c109b2010-07-29 02:16:43 +00003349static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00003350 llvm::LLVMContext &VMContext) {
3351 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
3352 // is called integer-like if its size is less than or equal to one word, and
3353 // the offset of each of its addressable sub-fields is zero.
3354
3355 uint64_t Size = Context.getTypeSize(Ty);
3356
3357 // Check that the type fits in a word.
3358 if (Size > 32)
3359 return false;
3360
3361 // FIXME: Handle vector types!
3362 if (Ty->isVectorType())
3363 return false;
3364
Daniel Dunbarb0d58192009-09-14 02:20:34 +00003365 // Float types are never treated as "integer like".
3366 if (Ty->isRealFloatingType())
3367 return false;
3368
Daniel Dunbar98303b92009-09-13 08:03:58 +00003369 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00003370 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00003371 return true;
3372
Daniel Dunbar45815812010-02-01 23:31:26 +00003373 // Small complex integer types are "integer like".
3374 if (const ComplexType *CT = Ty->getAs<ComplexType>())
3375 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003376
3377 // Single element and zero sized arrays should be allowed, by the definition
3378 // above, but they are not.
3379
3380 // Otherwise, it must be a record type.
3381 const RecordType *RT = Ty->getAs<RecordType>();
3382 if (!RT) return false;
3383
3384 // Ignore records with flexible arrays.
3385 const RecordDecl *RD = RT->getDecl();
3386 if (RD->hasFlexibleArrayMember())
3387 return false;
3388
3389 // Check that all sub-fields are at offset 0, and are themselves "integer
3390 // like".
3391 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3392
3393 bool HadField = false;
3394 unsigned idx = 0;
3395 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3396 i != e; ++i, ++idx) {
David Blaikie581deb32012-06-06 20:45:41 +00003397 const FieldDecl *FD = *i;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003398
Daniel Dunbar679855a2010-01-29 03:22:29 +00003399 // Bit-fields are not addressable, we only need to verify they are "integer
3400 // like". We still have to disallow a subsequent non-bitfield, for example:
3401 // struct { int : 0; int x }
3402 // is non-integer like according to gcc.
3403 if (FD->isBitField()) {
3404 if (!RD->isUnion())
3405 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003406
Daniel Dunbar679855a2010-01-29 03:22:29 +00003407 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3408 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003409
Daniel Dunbar679855a2010-01-29 03:22:29 +00003410 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003411 }
3412
Daniel Dunbar679855a2010-01-29 03:22:29 +00003413 // Check if this field is at offset 0.
3414 if (Layout.getFieldOffset(idx) != 0)
3415 return false;
3416
Daniel Dunbar98303b92009-09-13 08:03:58 +00003417 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3418 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003419
Daniel Dunbar679855a2010-01-29 03:22:29 +00003420 // Only allow at most one field in a structure. This doesn't match the
3421 // wording above, but follows gcc in situations with a field following an
3422 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00003423 if (!RD->isUnion()) {
3424 if (HadField)
3425 return false;
3426
3427 HadField = true;
3428 }
3429 }
3430
3431 return true;
3432}
3433
Chris Lattnera3c109b2010-07-29 02:16:43 +00003434ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003435 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003436 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00003437
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00003438 // Large vector types should be returned via memory.
3439 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
3440 return ABIArgInfo::getIndirect(0);
3441
John McCalld608cdb2010-08-22 10:59:02 +00003442 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003443 // Treat an enum type as its underlying type.
3444 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3445 RetTy = EnumTy->getDecl()->getIntegerType();
3446
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003447 return (RetTy->isPromotableIntegerType() ?
3448 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003449 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003450
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003451 // Structures with either a non-trivial destructor or a non-trivial
3452 // copy constructor are always indirect.
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00003453 if (isRecordReturnIndirect(RetTy, CGT))
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003454 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3455
Daniel Dunbar98303b92009-09-13 08:03:58 +00003456 // Are we following APCS?
3457 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00003458 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00003459 return ABIArgInfo::getIgnore();
3460
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003461 // Complex types are all returned as packed integers.
3462 //
3463 // FIXME: Consider using 2 x vector types if the back end handles them
3464 // correctly.
3465 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00003466 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00003467 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003468
Daniel Dunbar98303b92009-09-13 08:03:58 +00003469 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003470 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003471 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003472 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003473 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003474 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003475 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003476 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3477 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003478 }
3479
3480 // Otherwise return in memory.
3481 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003482 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003483
3484 // Otherwise this is an AAPCS variant.
3485
Chris Lattnera3c109b2010-07-29 02:16:43 +00003486 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00003487 return ABIArgInfo::getIgnore();
3488
Bob Wilson3b694fa2011-11-02 04:51:36 +00003489 // Check for homogeneous aggregates with AAPCS-VFP.
3490 if (getABIKind() == AAPCS_VFP) {
3491 const Type *Base = 0;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003492 if (isHomogeneousAggregate(RetTy, Base, getContext())) {
3493 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson3b694fa2011-11-02 04:51:36 +00003494 // Homogeneous Aggregates are returned directly.
3495 return ABIArgInfo::getDirect();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003496 }
Bob Wilson3b694fa2011-11-02 04:51:36 +00003497 }
3498
Daniel Dunbar98303b92009-09-13 08:03:58 +00003499 // Aggregates <= 4 bytes are returned in r0; other aggregates
3500 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003501 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00003502 if (Size <= 32) {
3503 // Return in the smallest viable integer type.
3504 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003505 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003506 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003507 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3508 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003509 }
3510
Daniel Dunbar98303b92009-09-13 08:03:58 +00003511 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003512}
3513
Manman Ren97f81572012-10-16 19:18:39 +00003514/// isIllegalVector - check whether Ty is an illegal vector type.
3515bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
3516 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3517 // Check whether VT is legal.
3518 unsigned NumElements = VT->getNumElements();
3519 uint64_t Size = getContext().getTypeSize(VT);
3520 // NumElements should be power of 2.
3521 if ((NumElements & (NumElements - 1)) != 0)
3522 return true;
3523 // Size should be greater than 32 bits.
3524 return Size <= 32;
3525 }
3526 return false;
3527}
3528
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003529llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00003530 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003531 llvm::Type *BP = CGF.Int8PtrTy;
3532 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003533
3534 CGBuilderTy &Builder = CGF.Builder;
Chris Lattner8b418682012-02-07 00:39:47 +00003535 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003536 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Manman Rend105e732012-10-16 19:01:37 +00003537
3538 uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8;
Rafael Espindolae164c182011-08-02 22:33:37 +00003539 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
Manman Ren97f81572012-10-16 19:18:39 +00003540 bool IsIndirect = false;
Manman Rend105e732012-10-16 19:01:37 +00003541
3542 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
3543 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
Manman Ren93371022012-10-16 19:51:48 +00003544 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3545 getABIKind() == ARMABIInfo::AAPCS)
3546 TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
3547 else
3548 TyAlign = 4;
Manman Ren97f81572012-10-16 19:18:39 +00003549 // Use indirect if size of the illegal vector is bigger than 16 bytes.
3550 if (isIllegalVectorType(Ty) && Size > 16) {
3551 IsIndirect = true;
3552 Size = 4;
3553 TyAlign = 4;
3554 }
Manman Rend105e732012-10-16 19:01:37 +00003555
3556 // Handle address alignment for ABI alignment > 4 bytes.
Rafael Espindolae164c182011-08-02 22:33:37 +00003557 if (TyAlign > 4) {
3558 assert((TyAlign & (TyAlign - 1)) == 0 &&
3559 "Alignment is not power of 2!");
3560 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3561 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
3562 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
Manman Rend105e732012-10-16 19:01:37 +00003563 Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align");
Rafael Espindolae164c182011-08-02 22:33:37 +00003564 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003565
3566 uint64_t Offset =
Manman Rend105e732012-10-16 19:01:37 +00003567 llvm::RoundUpToAlignment(Size, 4);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003568 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00003569 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003570 "ap.next");
3571 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3572
Manman Ren97f81572012-10-16 19:18:39 +00003573 if (IsIndirect)
3574 Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP));
Manman Ren93371022012-10-16 19:51:48 +00003575 else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) {
Manman Rend105e732012-10-16 19:01:37 +00003576 // We can't directly cast ap.cur to pointer to a vector type, since ap.cur
3577 // may not be correctly aligned for the vector type. We create an aligned
3578 // temporary space and copy the content over from ap.cur to the temporary
3579 // space. This is necessary if the natural alignment of the type is greater
3580 // than the ABI alignment.
3581 llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
3582 CharUnits CharSize = getContext().getTypeSizeInChars(Ty);
3583 llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty),
3584 "var.align");
3585 llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
3586 llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy);
3587 Builder.CreateMemCpy(Dst, Src,
3588 llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()),
3589 TyAlign, false);
3590 Addr = AlignedTemp; //The content is in aligned location.
3591 }
3592 llvm::Type *PTy =
3593 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3594 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3595
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003596 return AddrTyped;
3597}
3598
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00003599namespace {
3600
Derek Schuff263366f2012-10-16 22:30:41 +00003601class NaClARMABIInfo : public ABIInfo {
3602 public:
3603 NaClARMABIInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3604 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, Kind) {}
3605 virtual void computeInfo(CGFunctionInfo &FI) const;
3606 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3607 CodeGenFunction &CGF) const;
3608 private:
3609 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
3610 ARMABIInfo NInfo; // Used for everything else.
3611};
3612
3613class NaClARMTargetCodeGenInfo : public TargetCodeGenInfo {
3614 public:
3615 NaClARMTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3616 : TargetCodeGenInfo(new NaClARMABIInfo(CGT, Kind)) {}
3617};
3618
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00003619}
3620
Derek Schuff263366f2012-10-16 22:30:41 +00003621void NaClARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
3622 if (FI.getASTCallingConvention() == CC_PnaclCall)
3623 PInfo.computeInfo(FI);
3624 else
3625 static_cast<const ABIInfo&>(NInfo).computeInfo(FI);
3626}
3627
3628llvm::Value *NaClARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3629 CodeGenFunction &CGF) const {
3630 // Always use the native convention; calling pnacl-style varargs functions
3631 // is unsupported.
3632 return static_cast<const ABIInfo&>(NInfo).EmitVAArg(VAListAddr, Ty, CGF);
3633}
3634
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003635//===----------------------------------------------------------------------===//
Tim Northoverc264e162013-01-31 12:13:10 +00003636// AArch64 ABI Implementation
3637//===----------------------------------------------------------------------===//
3638
3639namespace {
3640
3641class AArch64ABIInfo : public ABIInfo {
3642public:
3643 AArch64ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3644
3645private:
3646 // The AArch64 PCS is explicit about return types and argument types being
3647 // handled identically, so we don't need to draw a distinction between
3648 // Argument and Return classification.
3649 ABIArgInfo classifyGenericType(QualType Ty, int &FreeIntRegs,
3650 int &FreeVFPRegs) const;
3651
3652 ABIArgInfo tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded, bool IsInt,
3653 llvm::Type *DirectTy = 0) const;
3654
3655 virtual void computeInfo(CGFunctionInfo &FI) const;
3656
3657 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3658 CodeGenFunction &CGF) const;
3659};
3660
3661class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
3662public:
3663 AArch64TargetCodeGenInfo(CodeGenTypes &CGT)
3664 :TargetCodeGenInfo(new AArch64ABIInfo(CGT)) {}
3665
3666 const AArch64ABIInfo &getABIInfo() const {
3667 return static_cast<const AArch64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
3668 }
3669
3670 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3671 return 31;
3672 }
3673
3674 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3675 llvm::Value *Address) const {
3676 // 0-31 are x0-x30 and sp: 8 bytes each
3677 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
3678 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 31);
3679
3680 // 64-95 are v0-v31: 16 bytes each
3681 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
3682 AssignToArrayRange(CGF.Builder, Address, Sixteen8, 64, 95);
3683
3684 return false;
3685 }
3686
3687};
3688
3689}
3690
3691void AArch64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
3692 int FreeIntRegs = 8, FreeVFPRegs = 8;
3693
3694 FI.getReturnInfo() = classifyGenericType(FI.getReturnType(),
3695 FreeIntRegs, FreeVFPRegs);
3696
3697 FreeIntRegs = FreeVFPRegs = 8;
3698 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3699 it != ie; ++it) {
3700 it->info = classifyGenericType(it->type, FreeIntRegs, FreeVFPRegs);
3701
3702 }
3703}
3704
3705ABIArgInfo
3706AArch64ABIInfo::tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded,
3707 bool IsInt, llvm::Type *DirectTy) const {
3708 if (FreeRegs >= RegsNeeded) {
3709 FreeRegs -= RegsNeeded;
3710 return ABIArgInfo::getDirect(DirectTy);
3711 }
3712
3713 llvm::Type *Padding = 0;
3714
3715 // We need padding so that later arguments don't get filled in anyway. That
3716 // wouldn't happen if only ByVal arguments followed in the same category, but
3717 // a large structure will simply seem to be a pointer as far as LLVM is
3718 // concerned.
3719 if (FreeRegs > 0) {
3720 if (IsInt)
3721 Padding = llvm::Type::getInt64Ty(getVMContext());
3722 else
3723 Padding = llvm::Type::getFloatTy(getVMContext());
3724
3725 // Either [N x i64] or [N x float].
3726 Padding = llvm::ArrayType::get(Padding, FreeRegs);
3727 FreeRegs = 0;
3728 }
3729
3730 return ABIArgInfo::getIndirect(getContext().getTypeAlign(Ty) / 8,
3731 /*IsByVal=*/ true, /*Realign=*/ false,
3732 Padding);
3733}
3734
3735
3736ABIArgInfo AArch64ABIInfo::classifyGenericType(QualType Ty,
3737 int &FreeIntRegs,
3738 int &FreeVFPRegs) const {
3739 // Can only occurs for return, but harmless otherwise.
3740 if (Ty->isVoidType())
3741 return ABIArgInfo::getIgnore();
3742
3743 // Large vector types should be returned via memory. There's no such concept
3744 // in the ABI, but they'd be over 16 bytes anyway so no matter how they're
3745 // classified they'd go into memory (see B.3).
3746 if (Ty->isVectorType() && getContext().getTypeSize(Ty) > 128) {
3747 if (FreeIntRegs > 0)
3748 --FreeIntRegs;
3749 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3750 }
3751
3752 // All non-aggregate LLVM types have a concrete ABI representation so they can
3753 // be passed directly. After this block we're guaranteed to be in a
3754 // complicated case.
3755 if (!isAggregateTypeForABI(Ty)) {
3756 // Treat an enum type as its underlying type.
3757 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3758 Ty = EnumTy->getDecl()->getIntegerType();
3759
3760 if (Ty->isFloatingType() || Ty->isVectorType())
3761 return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ false);
3762
3763 assert(getContext().getTypeSize(Ty) <= 128 &&
3764 "unexpectedly large scalar type");
3765
3766 int RegsNeeded = getContext().getTypeSize(Ty) > 64 ? 2 : 1;
3767
3768 // If the type may need padding registers to ensure "alignment", we must be
3769 // careful when this is accounted for. Increasing the effective size covers
3770 // all cases.
3771 if (getContext().getTypeAlign(Ty) == 128)
3772 RegsNeeded += FreeIntRegs % 2 != 0;
3773
3774 return tryUseRegs(Ty, FreeIntRegs, RegsNeeded, /*IsInt=*/ true);
3775 }
3776
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00003777 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) {
3778 if (FreeIntRegs > 0 && RAA == CGCXXABI::RAA_Indirect)
Tim Northoverc264e162013-01-31 12:13:10 +00003779 --FreeIntRegs;
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00003780 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Tim Northoverc264e162013-01-31 12:13:10 +00003781 }
3782
3783 if (isEmptyRecord(getContext(), Ty, true)) {
3784 if (!getContext().getLangOpts().CPlusPlus) {
3785 // Empty structs outside C++ mode are a GNU extension, so no ABI can
3786 // possibly tell us what to do. It turns out (I believe) that GCC ignores
3787 // the object for parameter-passsing purposes.
3788 return ABIArgInfo::getIgnore();
3789 }
3790
3791 // The combination of C++98 9p5 (sizeof(struct) != 0) and the pseudocode
3792 // description of va_arg in the PCS require that an empty struct does
3793 // actually occupy space for parameter-passing. I'm hoping for a
3794 // clarification giving an explicit paragraph to point to in future.
3795 return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ true,
3796 llvm::Type::getInt8Ty(getVMContext()));
3797 }
3798
3799 // Homogeneous vector aggregates get passed in registers or on the stack.
3800 const Type *Base = 0;
3801 uint64_t NumMembers = 0;
3802 if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)) {
3803 assert(Base && "Base class should be set for homogeneous aggregate");
3804 // Homogeneous aggregates are passed and returned directly.
3805 return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ NumMembers,
3806 /*IsInt=*/ false);
3807 }
3808
3809 uint64_t Size = getContext().getTypeSize(Ty);
3810 if (Size <= 128) {
3811 // Small structs can use the same direct type whether they're in registers
3812 // or on the stack.
3813 llvm::Type *BaseTy;
3814 unsigned NumBases;
3815 int SizeInRegs = (Size + 63) / 64;
3816
3817 if (getContext().getTypeAlign(Ty) == 128) {
3818 BaseTy = llvm::Type::getIntNTy(getVMContext(), 128);
3819 NumBases = 1;
3820
3821 // If the type may need padding registers to ensure "alignment", we must
3822 // be careful when this is accounted for. Increasing the effective size
3823 // covers all cases.
3824 SizeInRegs += FreeIntRegs % 2 != 0;
3825 } else {
3826 BaseTy = llvm::Type::getInt64Ty(getVMContext());
3827 NumBases = SizeInRegs;
3828 }
3829 llvm::Type *DirectTy = llvm::ArrayType::get(BaseTy, NumBases);
3830
3831 return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ SizeInRegs,
3832 /*IsInt=*/ true, DirectTy);
3833 }
3834
3835 // If the aggregate is > 16 bytes, it's passed and returned indirectly. In
3836 // LLVM terms the return uses an "sret" pointer, but that's handled elsewhere.
3837 --FreeIntRegs;
3838 return ABIArgInfo::getIndirect(0, /* byVal = */ false);
3839}
3840
3841llvm::Value *AArch64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3842 CodeGenFunction &CGF) const {
3843 // The AArch64 va_list type and handling is specified in the Procedure Call
3844 // Standard, section B.4:
3845 //
3846 // struct {
3847 // void *__stack;
3848 // void *__gr_top;
3849 // void *__vr_top;
3850 // int __gr_offs;
3851 // int __vr_offs;
3852 // };
3853
3854 assert(!CGF.CGM.getDataLayout().isBigEndian()
3855 && "va_arg not implemented for big-endian AArch64");
3856
3857 int FreeIntRegs = 8, FreeVFPRegs = 8;
3858 Ty = CGF.getContext().getCanonicalType(Ty);
3859 ABIArgInfo AI = classifyGenericType(Ty, FreeIntRegs, FreeVFPRegs);
3860
3861 llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
3862 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
3863 llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
3864 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
3865
3866 llvm::Value *reg_offs_p = 0, *reg_offs = 0;
3867 int reg_top_index;
3868 int RegSize;
3869 if (FreeIntRegs < 8) {
3870 assert(FreeVFPRegs == 8 && "Arguments never split between int & VFP regs");
3871 // 3 is the field number of __gr_offs
3872 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p");
3873 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
3874 reg_top_index = 1; // field number for __gr_top
3875 RegSize = 8 * (8 - FreeIntRegs);
3876 } else {
3877 assert(FreeVFPRegs < 8 && "Argument must go in VFP or int regs");
3878 // 4 is the field number of __vr_offs.
3879 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p");
3880 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
3881 reg_top_index = 2; // field number for __vr_top
3882 RegSize = 16 * (8 - FreeVFPRegs);
3883 }
3884
3885 //=======================================
3886 // Find out where argument was passed
3887 //=======================================
3888
3889 // If reg_offs >= 0 we're already using the stack for this type of
3890 // argument. We don't want to keep updating reg_offs (in case it overflows,
3891 // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
3892 // whatever they get).
3893 llvm::Value *UsingStack = 0;
3894 UsingStack = CGF.Builder.CreateICmpSGE(reg_offs,
3895 llvm::ConstantInt::get(CGF.Int32Ty, 0));
3896
3897 CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
3898
3899 // Otherwise, at least some kind of argument could go in these registers, the
3900 // quesiton is whether this particular type is too big.
3901 CGF.EmitBlock(MaybeRegBlock);
3902
3903 // Integer arguments may need to correct register alignment (for example a
3904 // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
3905 // align __gr_offs to calculate the potential address.
3906 if (FreeIntRegs < 8 && AI.isDirect() && getContext().getTypeAlign(Ty) > 64) {
3907 int Align = getContext().getTypeAlign(Ty) / 8;
3908
3909 reg_offs = CGF.Builder.CreateAdd(reg_offs,
3910 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
3911 "align_regoffs");
3912 reg_offs = CGF.Builder.CreateAnd(reg_offs,
3913 llvm::ConstantInt::get(CGF.Int32Ty, -Align),
3914 "aligned_regoffs");
3915 }
3916
3917 // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
3918 llvm::Value *NewOffset = 0;
3919 NewOffset = CGF.Builder.CreateAdd(reg_offs,
3920 llvm::ConstantInt::get(CGF.Int32Ty, RegSize),
3921 "new_reg_offs");
3922 CGF.Builder.CreateStore(NewOffset, reg_offs_p);
3923
3924 // Now we're in a position to decide whether this argument really was in
3925 // registers or not.
3926 llvm::Value *InRegs = 0;
3927 InRegs = CGF.Builder.CreateICmpSLE(NewOffset,
3928 llvm::ConstantInt::get(CGF.Int32Ty, 0),
3929 "inreg");
3930
3931 CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
3932
3933 //=======================================
3934 // Argument was in registers
3935 //=======================================
3936
3937 // Now we emit the code for if the argument was originally passed in
3938 // registers. First start the appropriate block:
3939 CGF.EmitBlock(InRegBlock);
3940
3941 llvm::Value *reg_top_p = 0, *reg_top = 0;
3942 reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p");
3943 reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
3944 llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs);
3945 llvm::Value *RegAddr = 0;
3946 llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
3947
3948 if (!AI.isDirect()) {
3949 // If it's been passed indirectly (actually a struct), whatever we find from
3950 // stored registers or on the stack will actually be a struct **.
3951 MemTy = llvm::PointerType::getUnqual(MemTy);
3952 }
3953
3954 const Type *Base = 0;
3955 uint64_t NumMembers;
3956 if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)
3957 && NumMembers > 1) {
3958 // Homogeneous aggregates passed in registers will have their elements split
3959 // and stored 16-bytes apart regardless of size (they're notionally in qN,
3960 // qN+1, ...). We reload and store into a temporary local variable
3961 // contiguously.
3962 assert(AI.isDirect() && "Homogeneous aggregates should be passed directly");
3963 llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
3964 llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
3965 llvm::Value *Tmp = CGF.CreateTempAlloca(HFATy);
3966
3967 for (unsigned i = 0; i < NumMembers; ++i) {
3968 llvm::Value *BaseOffset = llvm::ConstantInt::get(CGF.Int32Ty, 16 * i);
3969 llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset);
3970 LoadAddr = CGF.Builder.CreateBitCast(LoadAddr,
3971 llvm::PointerType::getUnqual(BaseTy));
3972 llvm::Value *StoreAddr = CGF.Builder.CreateStructGEP(Tmp, i);
3973
3974 llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
3975 CGF.Builder.CreateStore(Elem, StoreAddr);
3976 }
3977
3978 RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy);
3979 } else {
3980 // Otherwise the object is contiguous in memory
3981 RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy);
3982 }
3983
3984 CGF.EmitBranch(ContBlock);
3985
3986 //=======================================
3987 // Argument was on the stack
3988 //=======================================
3989 CGF.EmitBlock(OnStackBlock);
3990
3991 llvm::Value *stack_p = 0, *OnStackAddr = 0;
3992 stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p");
3993 OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack");
3994
3995 // Again, stack arguments may need realigmnent. In this case both integer and
3996 // floating-point ones might be affected.
3997 if (AI.isDirect() && getContext().getTypeAlign(Ty) > 64) {
3998 int Align = getContext().getTypeAlign(Ty) / 8;
3999
4000 OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty);
4001
4002 OnStackAddr = CGF.Builder.CreateAdd(OnStackAddr,
4003 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
4004 "align_stack");
4005 OnStackAddr = CGF.Builder.CreateAnd(OnStackAddr,
4006 llvm::ConstantInt::get(CGF.Int64Ty, -Align),
4007 "align_stack");
4008
4009 OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy);
4010 }
4011
4012 uint64_t StackSize;
4013 if (AI.isDirect())
4014 StackSize = getContext().getTypeSize(Ty) / 8;
4015 else
4016 StackSize = 8;
4017
4018 // All stack slots are 8 bytes
4019 StackSize = llvm::RoundUpToAlignment(StackSize, 8);
4020
4021 llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize);
4022 llvm::Value *NewStack = CGF.Builder.CreateGEP(OnStackAddr, StackSizeC,
4023 "new_stack");
4024
4025 // Write the new value of __stack for the next call to va_arg
4026 CGF.Builder.CreateStore(NewStack, stack_p);
4027
4028 OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy);
4029
4030 CGF.EmitBranch(ContBlock);
4031
4032 //=======================================
4033 // Tidy up
4034 //=======================================
4035 CGF.EmitBlock(ContBlock);
4036
4037 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr");
4038 ResAddr->addIncoming(RegAddr, InRegBlock);
4039 ResAddr->addIncoming(OnStackAddr, OnStackBlock);
4040
4041 if (AI.isDirect())
4042 return ResAddr;
4043
4044 return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr");
4045}
4046
4047//===----------------------------------------------------------------------===//
Justin Holewinski2c585b92012-05-24 17:43:12 +00004048// NVPTX ABI Implementation
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004049//===----------------------------------------------------------------------===//
4050
4051namespace {
4052
Justin Holewinski2c585b92012-05-24 17:43:12 +00004053class NVPTXABIInfo : public ABIInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004054public:
Justin Holewinskidca8f332013-03-30 14:38:24 +00004055 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004056
4057 ABIArgInfo classifyReturnType(QualType RetTy) const;
4058 ABIArgInfo classifyArgumentType(QualType Ty) const;
4059
4060 virtual void computeInfo(CGFunctionInfo &FI) const;
4061 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4062 CodeGenFunction &CFG) const;
4063};
4064
Justin Holewinski2c585b92012-05-24 17:43:12 +00004065class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004066public:
Justin Holewinski2c585b92012-05-24 17:43:12 +00004067 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
4068 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
Justin Holewinski818eafb2011-10-05 17:58:44 +00004069
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004070 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4071 CodeGen::CodeGenModule &M) const;
Justin Holewinskidca8f332013-03-30 14:38:24 +00004072private:
4073 static void addKernelMetadata(llvm::Function *F);
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004074};
4075
Justin Holewinski2c585b92012-05-24 17:43:12 +00004076ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004077 if (RetTy->isVoidType())
4078 return ABIArgInfo::getIgnore();
4079 if (isAggregateTypeForABI(RetTy))
4080 return ABIArgInfo::getIndirect(0);
4081 return ABIArgInfo::getDirect();
4082}
4083
Justin Holewinski2c585b92012-05-24 17:43:12 +00004084ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004085 if (isAggregateTypeForABI(Ty))
4086 return ABIArgInfo::getIndirect(0);
4087
4088 return ABIArgInfo::getDirect();
4089}
4090
Justin Holewinski2c585b92012-05-24 17:43:12 +00004091void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004092 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4093 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4094 it != ie; ++it)
4095 it->info = classifyArgumentType(it->type);
4096
4097 // Always honor user-specified calling convention.
4098 if (FI.getCallingConvention() != llvm::CallingConv::C)
4099 return;
4100
John McCallbd7370a2013-02-28 19:01:20 +00004101 FI.setEffectiveCallingConvention(getRuntimeCC());
4102}
4103
Justin Holewinski2c585b92012-05-24 17:43:12 +00004104llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4105 CodeGenFunction &CFG) const {
4106 llvm_unreachable("NVPTX does not support varargs");
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004107}
4108
Justin Holewinski2c585b92012-05-24 17:43:12 +00004109void NVPTXTargetCodeGenInfo::
4110SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4111 CodeGen::CodeGenModule &M) const{
Justin Holewinski818eafb2011-10-05 17:58:44 +00004112 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4113 if (!FD) return;
4114
4115 llvm::Function *F = cast<llvm::Function>(GV);
4116
4117 // Perform special handling in OpenCL mode
David Blaikie4e4d0842012-03-11 07:00:24 +00004118 if (M.getLangOpts().OpenCL) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00004119 // Use OpenCL function attributes to check for kernel functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00004120 // By default, all functions are device functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00004121 if (FD->hasAttr<OpenCLKernelAttr>()) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00004122 // OpenCL __kernel functions get kernel metadata
4123 addKernelMetadata(F);
Justin Holewinski818eafb2011-10-05 17:58:44 +00004124 // And kernel functions are not subject to inlining
Bill Wendling72390b32012-12-20 19:27:06 +00004125 F->addFnAttr(llvm::Attribute::NoInline);
Justin Holewinski818eafb2011-10-05 17:58:44 +00004126 }
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004127 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00004128
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004129 // Perform special handling in CUDA mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00004130 if (M.getLangOpts().CUDA) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00004131 // CUDA __global__ functions get a kernel metadata entry. Since
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004132 // __global__ functions cannot be called from the device, we do not
4133 // need to set the noinline attribute.
4134 if (FD->getAttr<CUDAGlobalAttr>())
Justin Holewinskidca8f332013-03-30 14:38:24 +00004135 addKernelMetadata(F);
Justin Holewinski818eafb2011-10-05 17:58:44 +00004136 }
4137}
4138
Justin Holewinskidca8f332013-03-30 14:38:24 +00004139void NVPTXTargetCodeGenInfo::addKernelMetadata(llvm::Function *F) {
4140 llvm::Module *M = F->getParent();
4141 llvm::LLVMContext &Ctx = M->getContext();
4142
4143 // Get "nvvm.annotations" metadata node
4144 llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
4145
4146 // Create !{<func-ref>, metadata !"kernel", i32 1} node
4147 llvm::SmallVector<llvm::Value *, 3> MDVals;
4148 MDVals.push_back(F);
4149 MDVals.push_back(llvm::MDString::get(Ctx, "kernel"));
4150 MDVals.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), 1));
4151
4152 // Append metadata to nvvm.annotations
4153 MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
4154}
4155
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004156}
4157
4158//===----------------------------------------------------------------------===//
Ulrich Weigandb8409212013-05-06 16:26:41 +00004159// SystemZ ABI Implementation
4160//===----------------------------------------------------------------------===//
4161
4162namespace {
4163
4164class SystemZABIInfo : public ABIInfo {
4165public:
4166 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
4167
4168 bool isPromotableIntegerType(QualType Ty) const;
4169 bool isCompoundType(QualType Ty) const;
4170 bool isFPArgumentType(QualType Ty) const;
4171
4172 ABIArgInfo classifyReturnType(QualType RetTy) const;
4173 ABIArgInfo classifyArgumentType(QualType ArgTy) const;
4174
4175 virtual void computeInfo(CGFunctionInfo &FI) const {
4176 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4177 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4178 it != ie; ++it)
4179 it->info = classifyArgumentType(it->type);
4180 }
4181
4182 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4183 CodeGenFunction &CGF) const;
4184};
4185
4186class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
4187public:
4188 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
4189 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
4190};
4191
4192}
4193
4194bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
4195 // Treat an enum type as its underlying type.
4196 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4197 Ty = EnumTy->getDecl()->getIntegerType();
4198
4199 // Promotable integer types are required to be promoted by the ABI.
4200 if (Ty->isPromotableIntegerType())
4201 return true;
4202
4203 // 32-bit values must also be promoted.
4204 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4205 switch (BT->getKind()) {
4206 case BuiltinType::Int:
4207 case BuiltinType::UInt:
4208 return true;
4209 default:
4210 return false;
4211 }
4212 return false;
4213}
4214
4215bool SystemZABIInfo::isCompoundType(QualType Ty) const {
4216 return Ty->isAnyComplexType() || isAggregateTypeForABI(Ty);
4217}
4218
4219bool SystemZABIInfo::isFPArgumentType(QualType Ty) const {
4220 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4221 switch (BT->getKind()) {
4222 case BuiltinType::Float:
4223 case BuiltinType::Double:
4224 return true;
4225 default:
4226 return false;
4227 }
4228
4229 if (const RecordType *RT = Ty->getAsStructureType()) {
4230 const RecordDecl *RD = RT->getDecl();
4231 bool Found = false;
4232
4233 // If this is a C++ record, check the bases first.
4234 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
4235 for (CXXRecordDecl::base_class_const_iterator I = CXXRD->bases_begin(),
4236 E = CXXRD->bases_end(); I != E; ++I) {
4237 QualType Base = I->getType();
4238
4239 // Empty bases don't affect things either way.
4240 if (isEmptyRecord(getContext(), Base, true))
4241 continue;
4242
4243 if (Found)
4244 return false;
4245 Found = isFPArgumentType(Base);
4246 if (!Found)
4247 return false;
4248 }
4249
4250 // Check the fields.
4251 for (RecordDecl::field_iterator I = RD->field_begin(),
4252 E = RD->field_end(); I != E; ++I) {
4253 const FieldDecl *FD = *I;
4254
4255 // Empty bitfields don't affect things either way.
4256 // Unlike isSingleElementStruct(), empty structure and array fields
4257 // do count. So do anonymous bitfields that aren't zero-sized.
4258 if (FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
4259 return true;
4260
4261 // Unlike isSingleElementStruct(), arrays do not count.
4262 // Nested isFPArgumentType structures still do though.
4263 if (Found)
4264 return false;
4265 Found = isFPArgumentType(FD->getType());
4266 if (!Found)
4267 return false;
4268 }
4269
4270 // Unlike isSingleElementStruct(), trailing padding is allowed.
4271 // An 8-byte aligned struct s { float f; } is passed as a double.
4272 return Found;
4273 }
4274
4275 return false;
4276}
4277
4278llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4279 CodeGenFunction &CGF) const {
4280 // Assume that va_list type is correct; should be pointer to LLVM type:
4281 // struct {
4282 // i64 __gpr;
4283 // i64 __fpr;
4284 // i8 *__overflow_arg_area;
4285 // i8 *__reg_save_area;
4286 // };
4287
4288 // Every argument occupies 8 bytes and is passed by preference in either
4289 // GPRs or FPRs.
4290 Ty = CGF.getContext().getCanonicalType(Ty);
4291 ABIArgInfo AI = classifyArgumentType(Ty);
4292 bool InFPRs = isFPArgumentType(Ty);
4293
4294 llvm::Type *APTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
4295 bool IsIndirect = AI.isIndirect();
4296 unsigned UnpaddedBitSize;
4297 if (IsIndirect) {
4298 APTy = llvm::PointerType::getUnqual(APTy);
4299 UnpaddedBitSize = 64;
4300 } else
4301 UnpaddedBitSize = getContext().getTypeSize(Ty);
4302 unsigned PaddedBitSize = 64;
4303 assert((UnpaddedBitSize <= PaddedBitSize) && "Invalid argument size.");
4304
4305 unsigned PaddedSize = PaddedBitSize / 8;
4306 unsigned Padding = (PaddedBitSize - UnpaddedBitSize) / 8;
4307
4308 unsigned MaxRegs, RegCountField, RegSaveIndex, RegPadding;
4309 if (InFPRs) {
4310 MaxRegs = 4; // Maximum of 4 FPR arguments
4311 RegCountField = 1; // __fpr
4312 RegSaveIndex = 16; // save offset for f0
4313 RegPadding = 0; // floats are passed in the high bits of an FPR
4314 } else {
4315 MaxRegs = 5; // Maximum of 5 GPR arguments
4316 RegCountField = 0; // __gpr
4317 RegSaveIndex = 2; // save offset for r2
4318 RegPadding = Padding; // values are passed in the low bits of a GPR
4319 }
4320
4321 llvm::Value *RegCountPtr =
4322 CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr");
4323 llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count");
4324 llvm::Type *IndexTy = RegCount->getType();
4325 llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs);
4326 llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV,
4327 "fits_in_regs");
4328
4329 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
4330 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
4331 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
4332 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
4333
4334 // Emit code to load the value if it was passed in registers.
4335 CGF.EmitBlock(InRegBlock);
4336
4337 // Work out the address of an argument register.
4338 llvm::Value *PaddedSizeV = llvm::ConstantInt::get(IndexTy, PaddedSize);
4339 llvm::Value *ScaledRegCount =
4340 CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count");
4341 llvm::Value *RegBase =
4342 llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize + RegPadding);
4343 llvm::Value *RegOffset =
4344 CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset");
4345 llvm::Value *RegSaveAreaPtr =
4346 CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr");
4347 llvm::Value *RegSaveArea =
4348 CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area");
4349 llvm::Value *RawRegAddr =
4350 CGF.Builder.CreateGEP(RegSaveArea, RegOffset, "raw_reg_addr");
4351 llvm::Value *RegAddr =
4352 CGF.Builder.CreateBitCast(RawRegAddr, APTy, "reg_addr");
4353
4354 // Update the register count
4355 llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1);
4356 llvm::Value *NewRegCount =
4357 CGF.Builder.CreateAdd(RegCount, One, "reg_count");
4358 CGF.Builder.CreateStore(NewRegCount, RegCountPtr);
4359 CGF.EmitBranch(ContBlock);
4360
4361 // Emit code to load the value if it was passed in memory.
4362 CGF.EmitBlock(InMemBlock);
4363
4364 // Work out the address of a stack argument.
4365 llvm::Value *OverflowArgAreaPtr =
4366 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr");
4367 llvm::Value *OverflowArgArea =
4368 CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area");
4369 llvm::Value *PaddingV = llvm::ConstantInt::get(IndexTy, Padding);
4370 llvm::Value *RawMemAddr =
4371 CGF.Builder.CreateGEP(OverflowArgArea, PaddingV, "raw_mem_addr");
4372 llvm::Value *MemAddr =
4373 CGF.Builder.CreateBitCast(RawMemAddr, APTy, "mem_addr");
4374
4375 // Update overflow_arg_area_ptr pointer
4376 llvm::Value *NewOverflowArgArea =
4377 CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area");
4378 CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
4379 CGF.EmitBranch(ContBlock);
4380
4381 // Return the appropriate result.
4382 CGF.EmitBlock(ContBlock);
4383 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(APTy, 2, "va_arg.addr");
4384 ResAddr->addIncoming(RegAddr, InRegBlock);
4385 ResAddr->addIncoming(MemAddr, InMemBlock);
4386
4387 if (IsIndirect)
4388 return CGF.Builder.CreateLoad(ResAddr, "indirect_arg");
4389
4390 return ResAddr;
4391}
4392
4393
4394ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
4395 if (RetTy->isVoidType())
4396 return ABIArgInfo::getIgnore();
4397 if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64)
4398 return ABIArgInfo::getIndirect(0);
4399 return (isPromotableIntegerType(RetTy) ?
4400 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4401}
4402
4403ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
4404 // Handle the generic C++ ABI.
4405 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
4406 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
4407
4408 // Integers and enums are extended to full register width.
4409 if (isPromotableIntegerType(Ty))
4410 return ABIArgInfo::getExtend();
4411
4412 // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly.
4413 uint64_t Size = getContext().getTypeSize(Ty);
4414 if (Size != 8 && Size != 16 && Size != 32 && Size != 64)
4415 return ABIArgInfo::getIndirect(0);
4416
4417 // Handle small structures.
4418 if (const RecordType *RT = Ty->getAs<RecordType>()) {
4419 // Structures with flexible arrays have variable length, so really
4420 // fail the size test above.
4421 const RecordDecl *RD = RT->getDecl();
4422 if (RD->hasFlexibleArrayMember())
4423 return ABIArgInfo::getIndirect(0);
4424
4425 // The structure is passed as an unextended integer, a float, or a double.
4426 llvm::Type *PassTy;
4427 if (isFPArgumentType(Ty)) {
4428 assert(Size == 32 || Size == 64);
4429 if (Size == 32)
4430 PassTy = llvm::Type::getFloatTy(getVMContext());
4431 else
4432 PassTy = llvm::Type::getDoubleTy(getVMContext());
4433 } else
4434 PassTy = llvm::IntegerType::get(getVMContext(), Size);
4435 return ABIArgInfo::getDirect(PassTy);
4436 }
4437
4438 // Non-structure compounds are passed indirectly.
4439 if (isCompoundType(Ty))
4440 return ABIArgInfo::getIndirect(0);
4441
4442 return ABIArgInfo::getDirect(0);
4443}
4444
4445//===----------------------------------------------------------------------===//
Wesley Peck276fdf42010-12-19 19:57:51 +00004446// MBlaze ABI Implementation
4447//===----------------------------------------------------------------------===//
4448
4449namespace {
4450
4451class MBlazeABIInfo : public ABIInfo {
4452public:
4453 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
4454
4455 bool isPromotableIntegerType(QualType Ty) const;
4456
4457 ABIArgInfo classifyReturnType(QualType RetTy) const;
4458 ABIArgInfo classifyArgumentType(QualType RetTy) const;
4459
4460 virtual void computeInfo(CGFunctionInfo &FI) const {
4461 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4462 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4463 it != ie; ++it)
4464 it->info = classifyArgumentType(it->type);
4465 }
4466
4467 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4468 CodeGenFunction &CGF) const;
4469};
4470
4471class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
4472public:
4473 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
4474 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
4475 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4476 CodeGen::CodeGenModule &M) const;
4477};
4478
4479}
4480
4481bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
4482 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
4483 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4484 switch (BT->getKind()) {
4485 case BuiltinType::Bool:
4486 case BuiltinType::Char_S:
4487 case BuiltinType::Char_U:
4488 case BuiltinType::SChar:
4489 case BuiltinType::UChar:
4490 case BuiltinType::Short:
4491 case BuiltinType::UShort:
4492 return true;
4493 default:
4494 return false;
4495 }
4496 return false;
4497}
4498
4499llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4500 CodeGenFunction &CGF) const {
4501 // FIXME: Implement
4502 return 0;
4503}
4504
4505
4506ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
4507 if (RetTy->isVoidType())
4508 return ABIArgInfo::getIgnore();
4509 if (isAggregateTypeForABI(RetTy))
4510 return ABIArgInfo::getIndirect(0);
4511
4512 return (isPromotableIntegerType(RetTy) ?
4513 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4514}
4515
4516ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
4517 if (isAggregateTypeForABI(Ty))
4518 return ABIArgInfo::getIndirect(0);
4519
4520 return (isPromotableIntegerType(Ty) ?
4521 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4522}
4523
4524void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4525 llvm::GlobalValue *GV,
4526 CodeGen::CodeGenModule &M)
4527 const {
4528 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4529 if (!FD) return;
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00004530
Wesley Peck276fdf42010-12-19 19:57:51 +00004531 llvm::CallingConv::ID CC = llvm::CallingConv::C;
4532 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
4533 CC = llvm::CallingConv::MBLAZE_INTR;
4534 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
4535 CC = llvm::CallingConv::MBLAZE_SVOL;
4536
4537 if (CC != llvm::CallingConv::C) {
4538 // Handle 'interrupt_handler' attribute:
4539 llvm::Function *F = cast<llvm::Function>(GV);
4540
4541 // Step 1: Set ISR calling convention.
4542 F->setCallingConv(CC);
4543
4544 // Step 2: Add attributes goodness.
Bill Wendling72390b32012-12-20 19:27:06 +00004545 F->addFnAttr(llvm::Attribute::NoInline);
Wesley Peck276fdf42010-12-19 19:57:51 +00004546 }
4547
4548 // Step 3: Emit _interrupt_handler alias.
4549 if (CC == llvm::CallingConv::MBLAZE_INTR)
4550 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
4551 "_interrupt_handler", GV, &M.getModule());
4552}
4553
4554
4555//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004556// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004557//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004558
4559namespace {
4560
4561class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
4562public:
Chris Lattnerea044322010-07-29 02:01:43 +00004563 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
4564 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004565 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4566 CodeGen::CodeGenModule &M) const;
4567};
4568
4569}
4570
4571void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4572 llvm::GlobalValue *GV,
4573 CodeGen::CodeGenModule &M) const {
4574 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4575 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
4576 // Handle 'interrupt' attribute:
4577 llvm::Function *F = cast<llvm::Function>(GV);
4578
4579 // Step 1: Set ISR calling convention.
4580 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
4581
4582 // Step 2: Add attributes goodness.
Bill Wendling72390b32012-12-20 19:27:06 +00004583 F->addFnAttr(llvm::Attribute::NoInline);
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004584
4585 // Step 3: Emit ISR vector alias.
Anton Korobeynikovf419a852012-11-26 18:59:10 +00004586 unsigned Num = attr->getNumber() / 2;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004587 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Anton Korobeynikovf419a852012-11-26 18:59:10 +00004588 "__isr_" + Twine(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004589 GV, &M.getModule());
4590 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004591 }
4592}
4593
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004594//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00004595// MIPS ABI Implementation. This works for both little-endian and
4596// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004597//===----------------------------------------------------------------------===//
4598
John McCallaeeb7012010-05-27 06:19:26 +00004599namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00004600class MipsABIInfo : public ABIInfo {
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004601 bool IsO32;
Akira Hatanakac359f202012-07-03 19:24:06 +00004602 unsigned MinABIStackAlignInBytes, StackAlignInBytes;
4603 void CoerceToIntArgs(uint64_t TySize,
4604 SmallVector<llvm::Type*, 8> &ArgList) const;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004605 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004606 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004607 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004608public:
Akira Hatanakab551dd32011-11-03 00:05:50 +00004609 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
Akira Hatanakac359f202012-07-03 19:24:06 +00004610 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
4611 StackAlignInBytes(IsO32 ? 8 : 16) {}
Akira Hatanaka619e8872011-06-02 00:09:17 +00004612
4613 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004614 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004615 virtual void computeInfo(CGFunctionInfo &FI) const;
4616 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4617 CodeGenFunction &CGF) const;
4618};
4619
John McCallaeeb7012010-05-27 06:19:26 +00004620class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanakae624fa02011-09-20 18:23:28 +00004621 unsigned SizeOfUnwindException;
John McCallaeeb7012010-05-27 06:19:26 +00004622public:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004623 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
4624 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
4625 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCallaeeb7012010-05-27 06:19:26 +00004626
4627 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
4628 return 29;
4629 }
4630
Reed Kotler7dfd1822013-01-16 17:10:28 +00004631 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4632 CodeGen::CodeGenModule &CGM) const {
Reed Kotlerad4b8b42013-03-13 20:40:30 +00004633 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4634 if (!FD) return;
Rafael Espindolad8e6d6d2013-03-19 14:32:23 +00004635 llvm::Function *Fn = cast<llvm::Function>(GV);
Reed Kotlerad4b8b42013-03-13 20:40:30 +00004636 if (FD->hasAttr<Mips16Attr>()) {
4637 Fn->addFnAttr("mips16");
4638 }
4639 else if (FD->hasAttr<NoMips16Attr>()) {
4640 Fn->addFnAttr("nomips16");
4641 }
Reed Kotler7dfd1822013-01-16 17:10:28 +00004642 }
Reed Kotlerad4b8b42013-03-13 20:40:30 +00004643
John McCallaeeb7012010-05-27 06:19:26 +00004644 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00004645 llvm::Value *Address) const;
John McCall49e34be2011-08-30 01:42:09 +00004646
4647 unsigned getSizeOfUnwindException() const {
Akira Hatanakae624fa02011-09-20 18:23:28 +00004648 return SizeOfUnwindException;
John McCall49e34be2011-08-30 01:42:09 +00004649 }
John McCallaeeb7012010-05-27 06:19:26 +00004650};
4651}
4652
Akira Hatanakac359f202012-07-03 19:24:06 +00004653void MipsABIInfo::CoerceToIntArgs(uint64_t TySize,
4654 SmallVector<llvm::Type*, 8> &ArgList) const {
4655 llvm::IntegerType *IntTy =
4656 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004657
4658 // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
4659 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
4660 ArgList.push_back(IntTy);
4661
4662 // If necessary, add one more integer type to ArgList.
4663 unsigned R = TySize % (MinABIStackAlignInBytes * 8);
4664
4665 if (R)
4666 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004667}
4668
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004669// In N32/64, an aligned double precision floating point field is passed in
4670// a register.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004671llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
Akira Hatanakac359f202012-07-03 19:24:06 +00004672 SmallVector<llvm::Type*, 8> ArgList, IntArgList;
4673
4674 if (IsO32) {
4675 CoerceToIntArgs(TySize, ArgList);
4676 return llvm::StructType::get(getVMContext(), ArgList);
4677 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004678
Akira Hatanaka2afd23d2012-01-12 00:52:17 +00004679 if (Ty->isComplexType())
4680 return CGT.ConvertType(Ty);
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00004681
Akira Hatanakaa34e9212012-02-09 19:54:16 +00004682 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004683
Akira Hatanakac359f202012-07-03 19:24:06 +00004684 // Unions/vectors are passed in integer registers.
4685 if (!RT || !RT->isStructureOrClassType()) {
4686 CoerceToIntArgs(TySize, ArgList);
4687 return llvm::StructType::get(getVMContext(), ArgList);
4688 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004689
4690 const RecordDecl *RD = RT->getDecl();
4691 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004692 assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004693
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004694 uint64_t LastOffset = 0;
4695 unsigned idx = 0;
4696 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
4697
Akira Hatanakaa34e9212012-02-09 19:54:16 +00004698 // Iterate over fields in the struct/class and check if there are any aligned
4699 // double fields.
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004700 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
4701 i != e; ++i, ++idx) {
David Blaikie262bc182012-04-30 02:36:29 +00004702 const QualType Ty = i->getType();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004703 const BuiltinType *BT = Ty->getAs<BuiltinType>();
4704
4705 if (!BT || BT->getKind() != BuiltinType::Double)
4706 continue;
4707
4708 uint64_t Offset = Layout.getFieldOffset(idx);
4709 if (Offset % 64) // Ignore doubles that are not aligned.
4710 continue;
4711
4712 // Add ((Offset - LastOffset) / 64) args of type i64.
4713 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
4714 ArgList.push_back(I64);
4715
4716 // Add double type.
4717 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
4718 LastOffset = Offset + 64;
4719 }
4720
Akira Hatanakac359f202012-07-03 19:24:06 +00004721 CoerceToIntArgs(TySize - LastOffset, IntArgList);
4722 ArgList.append(IntArgList.begin(), IntArgList.end());
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004723
4724 return llvm::StructType::get(getVMContext(), ArgList);
4725}
4726
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004727llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004728 assert((Offset % MinABIStackAlignInBytes) == 0);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004729
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004730 if ((Align - 1) & Offset)
4731 return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
4732
4733 return 0;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004734}
Akira Hatanaka9659d592012-01-10 22:44:52 +00004735
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004736ABIArgInfo
4737MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004738 uint64_t OrigOffset = Offset;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004739 uint64_t TySize = getContext().getTypeSize(Ty);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004740 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004741
Akira Hatanakac359f202012-07-03 19:24:06 +00004742 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
4743 (uint64_t)StackAlignInBytes);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004744 Offset = llvm::RoundUpToAlignment(Offset, Align);
4745 Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004746
Akira Hatanakac359f202012-07-03 19:24:06 +00004747 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
Akira Hatanaka619e8872011-06-02 00:09:17 +00004748 // Ignore empty aggregates.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004749 if (TySize == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00004750 return ABIArgInfo::getIgnore();
4751
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00004752 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004753 Offset = OrigOffset + MinABIStackAlignInBytes;
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00004754 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004755 }
Akira Hatanaka511949b2011-08-01 18:09:58 +00004756
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004757 // If we have reached here, aggregates are passed directly by coercing to
4758 // another structure type. Padding is inserted if the offset of the
4759 // aggregate is unaligned.
4760 return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
4761 getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00004762 }
4763
4764 // Treat an enum type as its underlying type.
4765 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4766 Ty = EnumTy->getDecl()->getIntegerType();
4767
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004768 if (Ty->isPromotableIntegerType())
4769 return ABIArgInfo::getExtend();
4770
Akira Hatanaka4055cfc2013-01-24 21:47:33 +00004771 return ABIArgInfo::getDirect(0, 0,
4772 IsO32 ? 0 : getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00004773}
4774
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004775llvm::Type*
4776MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakada54ff32012-02-09 18:49:26 +00004777 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakac359f202012-07-03 19:24:06 +00004778 SmallVector<llvm::Type*, 8> RTList;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004779
Akira Hatanakada54ff32012-02-09 18:49:26 +00004780 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004781 const RecordDecl *RD = RT->getDecl();
Akira Hatanakada54ff32012-02-09 18:49:26 +00004782 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
4783 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004784
Akira Hatanakada54ff32012-02-09 18:49:26 +00004785 // N32/64 returns struct/classes in floating point registers if the
4786 // following conditions are met:
4787 // 1. The size of the struct/class is no larger than 128-bit.
4788 // 2. The struct/class has one or two fields all of which are floating
4789 // point types.
4790 // 3. The offset of the first field is zero (this follows what gcc does).
4791 //
4792 // Any other composite results are returned in integer registers.
4793 //
4794 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
4795 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
4796 for (; b != e; ++b) {
David Blaikie262bc182012-04-30 02:36:29 +00004797 const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004798
Akira Hatanakada54ff32012-02-09 18:49:26 +00004799 if (!BT || !BT->isFloatingPoint())
4800 break;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004801
David Blaikie262bc182012-04-30 02:36:29 +00004802 RTList.push_back(CGT.ConvertType(b->getType()));
Akira Hatanakada54ff32012-02-09 18:49:26 +00004803 }
4804
4805 if (b == e)
4806 return llvm::StructType::get(getVMContext(), RTList,
4807 RD->hasAttr<PackedAttr>());
4808
4809 RTList.clear();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004810 }
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004811 }
4812
Akira Hatanakac359f202012-07-03 19:24:06 +00004813 CoerceToIntArgs(Size, RTList);
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004814 return llvm::StructType::get(getVMContext(), RTList);
4815}
4816
Akira Hatanaka619e8872011-06-02 00:09:17 +00004817ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanakaa8536c02012-01-23 23:18:57 +00004818 uint64_t Size = getContext().getTypeSize(RetTy);
4819
4820 if (RetTy->isVoidType() || Size == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00004821 return ABIArgInfo::getIgnore();
4822
Akira Hatanaka8aeb1472012-05-11 21:01:17 +00004823 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00004824 if (isRecordReturnIndirect(RetTy, CGT))
4825 return ABIArgInfo::getIndirect(0);
4826
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004827 if (Size <= 128) {
4828 if (RetTy->isAnyComplexType())
4829 return ABIArgInfo::getDirect();
4830
Akira Hatanakac359f202012-07-03 19:24:06 +00004831 // O32 returns integer vectors in registers.
4832 if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())
4833 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
4834
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00004835 if (!IsO32)
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004836 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
4837 }
Akira Hatanaka619e8872011-06-02 00:09:17 +00004838
4839 return ABIArgInfo::getIndirect(0);
4840 }
4841
4842 // Treat an enum type as its underlying type.
4843 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4844 RetTy = EnumTy->getDecl()->getIntegerType();
4845
4846 return (RetTy->isPromotableIntegerType() ?
4847 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4848}
4849
4850void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanakacc662542012-01-12 01:10:09 +00004851 ABIArgInfo &RetInfo = FI.getReturnInfo();
4852 RetInfo = classifyReturnType(FI.getReturnType());
4853
4854 // Check if a pointer to an aggregate is passed as a hidden argument.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004855 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
Akira Hatanakacc662542012-01-12 01:10:09 +00004856
Akira Hatanaka619e8872011-06-02 00:09:17 +00004857 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4858 it != ie; ++it)
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004859 it->info = classifyArgumentType(it->type, Offset);
Akira Hatanaka619e8872011-06-02 00:09:17 +00004860}
4861
4862llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4863 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00004864 llvm::Type *BP = CGF.Int8PtrTy;
4865 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004866
4867 CGBuilderTy &Builder = CGF.Builder;
4868 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
4869 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004870 int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004871 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
4872 llvm::Value *AddrTyped;
John McCall64aa4b32013-04-16 22:48:15 +00004873 unsigned PtrWidth = getTarget().getPointerWidth(0);
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004874 llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004875
4876 if (TypeAlign > MinABIStackAlignInBytes) {
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004877 llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
4878 llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
4879 llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
4880 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004881 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
4882 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
4883 }
4884 else
4885 AddrTyped = Builder.CreateBitCast(Addr, PTy);
4886
4887 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004888 TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004889 uint64_t Offset =
4890 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
4891 llvm::Value *NextAddr =
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004892 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004893 "ap.next");
4894 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
4895
4896 return AddrTyped;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004897}
4898
John McCallaeeb7012010-05-27 06:19:26 +00004899bool
4900MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4901 llvm::Value *Address) const {
4902 // This information comes from gcc's implementation, which seems to
4903 // as canonical as it gets.
4904
John McCallaeeb7012010-05-27 06:19:26 +00004905 // Everything on MIPS is 4 bytes. Double-precision FP registers
4906 // are aliased to pairs of single-precision FP registers.
Chris Lattner8b418682012-02-07 00:39:47 +00004907 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCallaeeb7012010-05-27 06:19:26 +00004908
4909 // 0-31 are the general purpose registers, $0 - $31.
4910 // 32-63 are the floating-point registers, $f0 - $f31.
4911 // 64 and 65 are the multiply/divide registers, $hi and $lo.
4912 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattner8b418682012-02-07 00:39:47 +00004913 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCallaeeb7012010-05-27 06:19:26 +00004914
4915 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
4916 // They are one bit wide and ignored here.
4917
4918 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
4919 // (coprocessor 1 is the FP unit)
4920 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
4921 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
4922 // 176-181 are the DSP accumulator registers.
Chris Lattner8b418682012-02-07 00:39:47 +00004923 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCallaeeb7012010-05-27 06:19:26 +00004924 return false;
4925}
4926
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004927//===----------------------------------------------------------------------===//
4928// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
4929// Currently subclassed only to implement custom OpenCL C function attribute
4930// handling.
4931//===----------------------------------------------------------------------===//
4932
4933namespace {
4934
4935class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
4936public:
4937 TCETargetCodeGenInfo(CodeGenTypes &CGT)
4938 : DefaultTargetCodeGenInfo(CGT) {}
4939
4940 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4941 CodeGen::CodeGenModule &M) const;
4942};
4943
4944void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4945 llvm::GlobalValue *GV,
4946 CodeGen::CodeGenModule &M) const {
4947 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4948 if (!FD) return;
4949
4950 llvm::Function *F = cast<llvm::Function>(GV);
4951
David Blaikie4e4d0842012-03-11 07:00:24 +00004952 if (M.getLangOpts().OpenCL) {
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004953 if (FD->hasAttr<OpenCLKernelAttr>()) {
4954 // OpenCL C Kernel functions are not subject to inlining
Bill Wendling72390b32012-12-20 19:27:06 +00004955 F->addFnAttr(llvm::Attribute::NoInline);
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004956
4957 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
4958
4959 // Convert the reqd_work_group_size() attributes to metadata.
4960 llvm::LLVMContext &Context = F->getContext();
4961 llvm::NamedMDNode *OpenCLMetadata =
4962 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
4963
4964 SmallVector<llvm::Value*, 5> Operands;
4965 Operands.push_back(F);
4966
Chris Lattner8b418682012-02-07 00:39:47 +00004967 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4968 llvm::APInt(32,
4969 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
4970 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4971 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004972 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
Chris Lattner8b418682012-02-07 00:39:47 +00004973 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4974 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004975 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
4976
4977 // Add a boolean constant operand for "required" (true) or "hint" (false)
4978 // for implementing the work_group_size_hint attr later. Currently
4979 // always true as the hint is not yet implemented.
Chris Lattner8b418682012-02-07 00:39:47 +00004980 Operands.push_back(llvm::ConstantInt::getTrue(Context));
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004981 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
4982 }
4983 }
4984 }
4985}
4986
4987}
John McCallaeeb7012010-05-27 06:19:26 +00004988
Tony Linthicum96319392011-12-12 21:14:55 +00004989//===----------------------------------------------------------------------===//
4990// Hexagon ABI Implementation
4991//===----------------------------------------------------------------------===//
4992
4993namespace {
4994
4995class HexagonABIInfo : public ABIInfo {
4996
4997
4998public:
4999 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
5000
5001private:
5002
5003 ABIArgInfo classifyReturnType(QualType RetTy) const;
5004 ABIArgInfo classifyArgumentType(QualType RetTy) const;
5005
5006 virtual void computeInfo(CGFunctionInfo &FI) const;
5007
5008 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5009 CodeGenFunction &CGF) const;
5010};
5011
5012class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
5013public:
5014 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
5015 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
5016
5017 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
5018 return 29;
5019 }
5020};
5021
5022}
5023
5024void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
5025 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
5026 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
5027 it != ie; ++it)
5028 it->info = classifyArgumentType(it->type);
5029}
5030
5031ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
5032 if (!isAggregateTypeForABI(Ty)) {
5033 // Treat an enum type as its underlying type.
5034 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5035 Ty = EnumTy->getDecl()->getIntegerType();
5036
5037 return (Ty->isPromotableIntegerType() ?
5038 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
5039 }
5040
5041 // Ignore empty records.
5042 if (isEmptyRecord(getContext(), Ty, true))
5043 return ABIArgInfo::getIgnore();
5044
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00005045 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
5046 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Tony Linthicum96319392011-12-12 21:14:55 +00005047
5048 uint64_t Size = getContext().getTypeSize(Ty);
5049 if (Size > 64)
5050 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
5051 // Pass in the smallest viable integer type.
5052 else if (Size > 32)
5053 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
5054 else if (Size > 16)
5055 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
5056 else if (Size > 8)
5057 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5058 else
5059 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
5060}
5061
5062ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
5063 if (RetTy->isVoidType())
5064 return ABIArgInfo::getIgnore();
5065
5066 // Large vector types should be returned via memory.
5067 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
5068 return ABIArgInfo::getIndirect(0);
5069
5070 if (!isAggregateTypeForABI(RetTy)) {
5071 // Treat an enum type as its underlying type.
5072 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5073 RetTy = EnumTy->getDecl()->getIntegerType();
5074
5075 return (RetTy->isPromotableIntegerType() ?
5076 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
5077 }
5078
5079 // Structures with either a non-trivial destructor or a non-trivial
5080 // copy constructor are always indirect.
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00005081 if (isRecordReturnIndirect(RetTy, CGT))
Tony Linthicum96319392011-12-12 21:14:55 +00005082 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
5083
5084 if (isEmptyRecord(getContext(), RetTy, true))
5085 return ABIArgInfo::getIgnore();
5086
5087 // Aggregates <= 8 bytes are returned in r0; other aggregates
5088 // are returned indirectly.
5089 uint64_t Size = getContext().getTypeSize(RetTy);
5090 if (Size <= 64) {
5091 // Return in the smallest viable integer type.
5092 if (Size <= 8)
5093 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
5094 if (Size <= 16)
5095 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5096 if (Size <= 32)
5097 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
5098 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
5099 }
5100
5101 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
5102}
5103
5104llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner8b418682012-02-07 00:39:47 +00005105 CodeGenFunction &CGF) const {
Tony Linthicum96319392011-12-12 21:14:55 +00005106 // FIXME: Need to handle alignment
Chris Lattner8b418682012-02-07 00:39:47 +00005107 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Tony Linthicum96319392011-12-12 21:14:55 +00005108
5109 CGBuilderTy &Builder = CGF.Builder;
5110 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
5111 "ap");
5112 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
5113 llvm::Type *PTy =
5114 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
5115 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
5116
5117 uint64_t Offset =
5118 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
5119 llvm::Value *NextAddr =
5120 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
5121 "ap.next");
5122 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
5123
5124 return AddrTyped;
5125}
5126
5127
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00005128//===----------------------------------------------------------------------===//
5129// SPARC v9 ABI Implementation.
5130// Based on the SPARC Compliance Definition version 2.4.1.
5131//
5132// Function arguments a mapped to a nominal "parameter array" and promoted to
5133// registers depending on their type. Each argument occupies 8 or 16 bytes in
5134// the array, structs larger than 16 bytes are passed indirectly.
5135//
5136// One case requires special care:
5137//
5138// struct mixed {
5139// int i;
5140// float f;
5141// };
5142//
5143// When a struct mixed is passed by value, it only occupies 8 bytes in the
5144// parameter array, but the int is passed in an integer register, and the float
5145// is passed in a floating point register. This is represented as two arguments
5146// with the LLVM IR inreg attribute:
5147//
5148// declare void f(i32 inreg %i, float inreg %f)
5149//
5150// The code generator will only allocate 4 bytes from the parameter array for
5151// the inreg arguments. All other arguments are allocated a multiple of 8
5152// bytes.
5153//
5154namespace {
5155class SparcV9ABIInfo : public ABIInfo {
5156public:
5157 SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
5158
5159private:
5160 ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const;
5161 virtual void computeInfo(CGFunctionInfo &FI) const;
5162 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5163 CodeGenFunction &CGF) const;
Jakob Stoklund Olesenfc782fb2013-05-28 04:57:37 +00005164
5165 // Coercion type builder for structs passed in registers. The coercion type
5166 // serves two purposes:
5167 //
5168 // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned'
5169 // in registers.
5170 // 2. Expose aligned floating point elements as first-level elements, so the
5171 // code generator knows to pass them in floating point registers.
5172 //
5173 // We also compute the InReg flag which indicates that the struct contains
5174 // aligned 32-bit floats.
5175 //
5176 struct CoerceBuilder {
5177 llvm::LLVMContext &Context;
5178 const llvm::DataLayout &DL;
5179 SmallVector<llvm::Type*, 8> Elems;
5180 uint64_t Size;
5181 bool InReg;
5182
5183 CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl)
5184 : Context(c), DL(dl), Size(0), InReg(false) {}
5185
5186 // Pad Elems with integers until Size is ToSize.
5187 void pad(uint64_t ToSize) {
5188 assert(ToSize >= Size && "Cannot remove elements");
5189 if (ToSize == Size)
5190 return;
5191
5192 // Finish the current 64-bit word.
5193 uint64_t Aligned = llvm::RoundUpToAlignment(Size, 64);
5194 if (Aligned > Size && Aligned <= ToSize) {
5195 Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size));
5196 Size = Aligned;
5197 }
5198
5199 // Add whole 64-bit words.
5200 while (Size + 64 <= ToSize) {
5201 Elems.push_back(llvm::Type::getInt64Ty(Context));
5202 Size += 64;
5203 }
5204
5205 // Final in-word padding.
5206 if (Size < ToSize) {
5207 Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size));
5208 Size = ToSize;
5209 }
5210 }
5211
5212 // Add a floating point element at Offset.
5213 void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) {
5214 // Unaligned floats are treated as integers.
5215 if (Offset % Bits)
5216 return;
5217 // The InReg flag is only required if there are any floats < 64 bits.
5218 if (Bits < 64)
5219 InReg = true;
5220 pad(Offset);
5221 Elems.push_back(Ty);
5222 Size = Offset + Bits;
5223 }
5224
5225 // Add a struct type to the coercion type, starting at Offset (in bits).
5226 void addStruct(uint64_t Offset, llvm::StructType *StrTy) {
5227 const llvm::StructLayout *Layout = DL.getStructLayout(StrTy);
5228 for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) {
5229 llvm::Type *ElemTy = StrTy->getElementType(i);
5230 uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i);
5231 switch (ElemTy->getTypeID()) {
5232 case llvm::Type::StructTyID:
5233 addStruct(ElemOffset, cast<llvm::StructType>(ElemTy));
5234 break;
5235 case llvm::Type::FloatTyID:
5236 addFloat(ElemOffset, ElemTy, 32);
5237 break;
5238 case llvm::Type::DoubleTyID:
5239 addFloat(ElemOffset, ElemTy, 64);
5240 break;
5241 case llvm::Type::FP128TyID:
5242 addFloat(ElemOffset, ElemTy, 128);
5243 break;
5244 case llvm::Type::PointerTyID:
5245 if (ElemOffset % 64 == 0) {
5246 pad(ElemOffset);
5247 Elems.push_back(ElemTy);
5248 Size += 64;
5249 }
5250 break;
5251 default:
5252 break;
5253 }
5254 }
5255 }
5256
5257 // Check if Ty is a usable substitute for the coercion type.
5258 bool isUsableType(llvm::StructType *Ty) const {
5259 if (Ty->getNumElements() != Elems.size())
5260 return false;
5261 for (unsigned i = 0, e = Elems.size(); i != e; ++i)
5262 if (Elems[i] != Ty->getElementType(i))
5263 return false;
5264 return true;
5265 }
5266
5267 // Get the coercion type as a literal struct type.
5268 llvm::Type *getType() const {
5269 if (Elems.size() == 1)
5270 return Elems.front();
5271 else
5272 return llvm::StructType::get(Context, Elems);
5273 }
5274 };
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00005275};
5276} // end anonymous namespace
5277
5278ABIArgInfo
5279SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const {
5280 if (Ty->isVoidType())
5281 return ABIArgInfo::getIgnore();
5282
5283 uint64_t Size = getContext().getTypeSize(Ty);
5284
5285 // Anything too big to fit in registers is passed with an explicit indirect
5286 // pointer / sret pointer.
5287 if (Size > SizeLimit)
5288 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
5289
5290 // Treat an enum type as its underlying type.
5291 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5292 Ty = EnumTy->getDecl()->getIntegerType();
5293
5294 // Integer types smaller than a register are extended.
5295 if (Size < 64 && Ty->isIntegerType())
5296 return ABIArgInfo::getExtend();
5297
5298 // Other non-aggregates go in registers.
5299 if (!isAggregateTypeForABI(Ty))
5300 return ABIArgInfo::getDirect();
5301
5302 // This is a small aggregate type that should be passed in registers.
Jakob Stoklund Olesenfc782fb2013-05-28 04:57:37 +00005303 // Build a coercion type from the LLVM struct type.
5304 llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty));
5305 if (!StrTy)
5306 return ABIArgInfo::getDirect();
5307
5308 CoerceBuilder CB(getVMContext(), getDataLayout());
5309 CB.addStruct(0, StrTy);
5310 CB.pad(llvm::RoundUpToAlignment(CB.DL.getTypeSizeInBits(StrTy), 64));
5311
5312 // Try to use the original type for coercion.
5313 llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();
5314
5315 if (CB.InReg)
5316 return ABIArgInfo::getDirectInReg(CoerceTy);
5317 else
5318 return ABIArgInfo::getDirect(CoerceTy);
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00005319}
5320
5321llvm::Value *SparcV9ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5322 CodeGenFunction &CGF) const {
5323 // FIXME: Implement with va_arg.
5324 return 0;
5325}
5326
5327void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const {
5328 FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8);
5329 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
5330 it != ie; ++it)
5331 it->info = classifyType(it->type, 16 * 8);
5332}
5333
5334namespace {
5335class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo {
5336public:
5337 SparcV9TargetCodeGenInfo(CodeGenTypes &CGT)
5338 : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {}
5339};
5340} // end anonymous namespace
5341
5342
Chris Lattnerea044322010-07-29 02:01:43 +00005343const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005344 if (TheTargetCodeGenInfo)
5345 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005346
John McCall64aa4b32013-04-16 22:48:15 +00005347 const llvm::Triple &Triple = getTarget().getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00005348 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00005349 default:
Chris Lattnerea044322010-07-29 02:01:43 +00005350 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00005351
Derek Schuff9ed63f82012-09-06 17:37:28 +00005352 case llvm::Triple::le32:
5353 return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
John McCallaeeb7012010-05-27 06:19:26 +00005354 case llvm::Triple::mips:
5355 case llvm::Triple::mipsel:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00005356 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCallaeeb7012010-05-27 06:19:26 +00005357
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00005358 case llvm::Triple::mips64:
5359 case llvm::Triple::mips64el:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00005360 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00005361
Tim Northoverc264e162013-01-31 12:13:10 +00005362 case llvm::Triple::aarch64:
5363 return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types));
5364
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00005365 case llvm::Triple::arm:
5366 case llvm::Triple::thumb:
Sandeep Patel34c1af82011-04-05 00:23:47 +00005367 {
5368 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
John McCall64aa4b32013-04-16 22:48:15 +00005369 if (strcmp(getTarget().getABI(), "apcs-gnu") == 0)
Sandeep Patel34c1af82011-04-05 00:23:47 +00005370 Kind = ARMABIInfo::APCS;
David Tweedb16abb12012-10-25 13:33:01 +00005371 else if (CodeGenOpts.FloatABI == "hard" ||
John McCall64aa4b32013-04-16 22:48:15 +00005372 (CodeGenOpts.FloatABI != "soft" &&
5373 Triple.getEnvironment() == llvm::Triple::GNUEABIHF))
Sandeep Patel34c1af82011-04-05 00:23:47 +00005374 Kind = ARMABIInfo::AAPCS_VFP;
5375
Derek Schuff263366f2012-10-16 22:30:41 +00005376 switch (Triple.getOS()) {
Eli Bendersky441d9f72012-12-04 18:38:10 +00005377 case llvm::Triple::NaCl:
Derek Schuff263366f2012-10-16 22:30:41 +00005378 return *(TheTargetCodeGenInfo =
5379 new NaClARMTargetCodeGenInfo(Types, Kind));
5380 default:
5381 return *(TheTargetCodeGenInfo =
5382 new ARMTargetCodeGenInfo(Types, Kind));
5383 }
Sandeep Patel34c1af82011-04-05 00:23:47 +00005384 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00005385
John McCallec853ba2010-03-11 00:10:12 +00005386 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00005387 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
Roman Divacky0fbc4b92012-05-09 18:22:46 +00005388 case llvm::Triple::ppc64:
Bill Schmidt2fc107f2012-10-03 19:18:57 +00005389 if (Triple.isOSBinFormatELF())
5390 return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types));
5391 else
5392 return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00005393
Peter Collingbourneedb66f32012-05-20 23:28:41 +00005394 case llvm::Triple::nvptx:
5395 case llvm::Triple::nvptx64:
Justin Holewinski2c585b92012-05-24 17:43:12 +00005396 return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types));
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005397
Wesley Peck276fdf42010-12-19 19:57:51 +00005398 case llvm::Triple::mblaze:
5399 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
5400
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005401 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00005402 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00005403
Ulrich Weigandb8409212013-05-06 16:26:41 +00005404 case llvm::Triple::systemz:
5405 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
5406
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00005407 case llvm::Triple::tce:
5408 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
5409
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00005410 case llvm::Triple::x86: {
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00005411 if (Triple.isOSDarwin())
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005412 return *(TheTargetCodeGenInfo =
Chad Rosier1f1df1f2013-03-25 21:00:27 +00005413 new X86_32TargetCodeGenInfo(Types, true, true, false,
Rafael Espindolab48280b2012-07-31 02:44:24 +00005414 CodeGenOpts.NumRegisterParameters));
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00005415
5416 switch (Triple.getOS()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00005417 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00005418 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00005419 case llvm::Triple::AuroraUX:
5420 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00005421 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00005422 case llvm::Triple::OpenBSD:
Eli Friedman42f74f22012-08-08 23:57:20 +00005423 case llvm::Triple::Bitrig:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005424 return *(TheTargetCodeGenInfo =
Chad Rosier1f1df1f2013-03-25 21:00:27 +00005425 new X86_32TargetCodeGenInfo(Types, false, true, false,
Rafael Espindolab48280b2012-07-31 02:44:24 +00005426 CodeGenOpts.NumRegisterParameters));
Eli Friedman55fc7e22012-01-25 22:46:34 +00005427
5428 case llvm::Triple::Win32:
5429 return *(TheTargetCodeGenInfo =
Reid Kleckner3190ca92013-05-08 13:44:39 +00005430 new WinX86_32TargetCodeGenInfo(Types,
5431 CodeGenOpts.NumRegisterParameters));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00005432
5433 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005434 return *(TheTargetCodeGenInfo =
Chad Rosier1f1df1f2013-03-25 21:00:27 +00005435 new X86_32TargetCodeGenInfo(Types, false, false, false,
Rafael Espindolab48280b2012-07-31 02:44:24 +00005436 CodeGenOpts.NumRegisterParameters));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005437 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00005438 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005439
Eli Friedmanee1ad992011-12-02 00:11:43 +00005440 case llvm::Triple::x86_64: {
John McCall64aa4b32013-04-16 22:48:15 +00005441 bool HasAVX = strcmp(getTarget().getABI(), "avx") == 0;
Eli Friedmanee1ad992011-12-02 00:11:43 +00005442
Chris Lattnerf13721d2010-08-31 16:44:54 +00005443 switch (Triple.getOS()) {
5444 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00005445 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00005446 case llvm::Triple::Cygwin:
5447 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
Eli Bendersky441d9f72012-12-04 18:38:10 +00005448 case llvm::Triple::NaCl:
John McCall64aa4b32013-04-16 22:48:15 +00005449 return *(TheTargetCodeGenInfo = new NaClX86_64TargetCodeGenInfo(Types,
5450 HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00005451 default:
Eli Friedmanee1ad992011-12-02 00:11:43 +00005452 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
5453 HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00005454 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00005455 }
Tony Linthicum96319392011-12-12 21:14:55 +00005456 case llvm::Triple::hexagon:
5457 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00005458 case llvm::Triple::sparcv9:
5459 return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types));
Eli Friedmanee1ad992011-12-02 00:11:43 +00005460 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005461}