blob: 3e7f7fedba68e9750f3d82e3b22e9a3264637995 [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) {
Tim Northover1bea6532013-06-07 00:04:50 +0000506 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) {
507 if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) {
508 // Invalid MMX constraint
509 return 0;
510 }
511
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000512 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
Tim Northover1bea6532013-06-07 00:04:50 +0000513 }
514
515 // No operation needed
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000516 return Ty;
517}
518
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000519//===----------------------------------------------------------------------===//
520// X86-32 ABI Implementation
521//===----------------------------------------------------------------------===//
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000522
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000523/// X86_32ABIInfo - The X86-32 ABI information.
524class X86_32ABIInfo : public ABIInfo {
Rafael Espindolab48280b2012-07-31 02:44:24 +0000525 enum Class {
526 Integer,
527 Float
528 };
529
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000530 static const unsigned MinABIStackAlignInBytes = 4;
531
David Chisnall1e4249c2009-08-17 23:08:21 +0000532 bool IsDarwinVectorABI;
533 bool IsSmallStructInRegABI;
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000534 bool IsWin32StructABI;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000535 unsigned DefaultNumRegisterParameters;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000536
537 static bool isRegisterSize(unsigned Size) {
538 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
539 }
540
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000541 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context,
542 unsigned callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000543
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000544 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
545 /// such that the argument will be passed in memory.
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000546 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal,
547 unsigned &FreeRegs) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000548
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000549 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbare59d8582010-09-16 20:42:06 +0000550 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000551
Rafael Espindolab48280b2012-07-31 02:44:24 +0000552 Class classify(QualType Ty) const;
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000553 ABIArgInfo classifyReturnType(QualType RetTy,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000554 unsigned callingConvention) const;
Rafael Espindolab6932692012-10-24 01:58:58 +0000555 ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &FreeRegs,
556 bool IsFastCall) const;
557 bool shouldUseInReg(QualType Ty, unsigned &FreeRegs,
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000558 bool IsFastCall, bool &NeedsPadding) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000559
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000560public:
561
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000562 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000563 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
564 CodeGenFunction &CGF) const;
565
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000566 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool w,
Rafael Espindolab48280b2012-07-31 02:44:24 +0000567 unsigned r)
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000568 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000569 IsWin32StructABI(w), DefaultNumRegisterParameters(r) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000570};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000571
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000572class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
573public:
Eli Friedman55fc7e22012-01-25 22:46:34 +0000574 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000575 bool d, bool p, bool w, unsigned r)
576 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, w, r)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000577
John McCallb8b52972013-06-18 02:46:29 +0000578 static bool isStructReturnInRegABI(
579 const llvm::Triple &Triple, const CodeGenOptions &Opts);
580
Charles Davis74f72932010-02-13 15:54:06 +0000581 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
582 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000583
584 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
585 // Darwin uses different dwarf register numbers for EH.
John McCall64aa4b32013-04-16 22:48:15 +0000586 if (CGM.getTarget().getTriple().isOSDarwin()) return 5;
John McCall6374c332010-03-06 00:35:14 +0000587 return 4;
588 }
589
590 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
591 llvm::Value *Address) const;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000592
Jay Foadef6de3d2011-07-11 09:56:20 +0000593 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000594 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000595 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000596 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
597 }
598
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000599};
600
601}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000602
603/// shouldReturnTypeInRegister - Determine if the given type should be
604/// passed in a register (for the Darwin ABI).
605bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000606 ASTContext &Context,
607 unsigned callingConvention) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000608 uint64_t Size = Context.getTypeSize(Ty);
609
610 // Type must be register sized.
611 if (!isRegisterSize(Size))
612 return false;
613
614 if (Ty->isVectorType()) {
615 // 64- and 128- bit vectors inside structures are not returned in
616 // registers.
617 if (Size == 64 || Size == 128)
618 return false;
619
620 return true;
621 }
622
Daniel Dunbar77115232010-05-15 00:00:30 +0000623 // If this is a builtin, pointer, enum, complex type, member pointer, or
624 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000625 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000626 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000627 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000628 return true;
629
630 // Arrays are treated like records.
631 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000632 return shouldReturnTypeInRegister(AT->getElementType(), Context,
633 callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000634
635 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000636 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000637 if (!RT) return false;
638
Anders Carlssona8874232010-01-27 03:25:19 +0000639 // FIXME: Traverse bases here too.
640
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000641 // For thiscall conventions, structures will never be returned in
642 // a register. This is for compatibility with the MSVC ABI
643 if (callingConvention == llvm::CallingConv::X86_ThisCall &&
644 RT->isStructureType()) {
645 return false;
646 }
647
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000648 // Structure types are passed in register if all fields would be
649 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000650 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
651 e = RT->getDecl()->field_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000652 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000653
654 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000655 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000656 continue;
657
658 // Check fields recursively.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000659 if (!shouldReturnTypeInRegister(FD->getType(), Context,
660 callingConvention))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000661 return false;
662 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000663 return true;
664}
665
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000666ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
667 unsigned callingConvention) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000668 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000669 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000670
Chris Lattnera3c109b2010-07-29 02:16:43 +0000671 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000672 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000673 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000674 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000675
676 // 128-bit vectors are a special case; they are returned in
677 // registers and we need to make sure to pick a type the LLVM
678 // backend will like.
679 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +0000680 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000681 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000682
683 // Always return in register if it fits in a general purpose
684 // register, or if it is 64 bits and has a single element.
685 if ((Size == 8 || Size == 16 || Size == 32) ||
686 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +0000687 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000688 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000689
690 return ABIArgInfo::getIndirect(0);
691 }
692
693 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000694 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000695
John McCalld608cdb2010-08-22 10:59:02 +0000696 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000697 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000698 if (isRecordReturnIndirect(RT, CGT))
Anders Carlsson40092972009-10-20 22:07:59 +0000699 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000700
Anders Carlsson40092972009-10-20 22:07:59 +0000701 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000702 if (RT->getDecl()->hasFlexibleArrayMember())
703 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000704 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000705
David Chisnall1e4249c2009-08-17 23:08:21 +0000706 // If specified, structs and unions are always indirect.
707 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000708 return ABIArgInfo::getIndirect(0);
709
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000710 // Small structures which are register sized are generally returned
711 // in a register.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000712 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(),
713 callingConvention)) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000714 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000715
716 // As a special-case, if the struct is a "single-element" struct, and
717 // the field is of type "float" or "double", return it in a
Eli Friedman55fc7e22012-01-25 22:46:34 +0000718 // floating-point register. (MSVC does not apply this special case.)
719 // We apply a similar transformation for pointer types to improve the
720 // quality of the generated IR.
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000721 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000722 if ((!IsWin32StructABI && SeltTy->isRealFloatingType())
Eli Friedman55fc7e22012-01-25 22:46:34 +0000723 || SeltTy->hasPointerRepresentation())
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000724 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
725
726 // FIXME: We should be able to narrow this integer in cases with dead
727 // padding.
Chris Lattner800588f2010-07-29 06:26:06 +0000728 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000729 }
730
731 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000732 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000733
Chris Lattnera3c109b2010-07-29 02:16:43 +0000734 // Treat an enum type as its underlying type.
735 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
736 RetTy = EnumTy->getDecl()->getIntegerType();
737
738 return (RetTy->isPromotableIntegerType() ?
739 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000740}
741
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000742static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
743 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
744}
745
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000746static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
747 const RecordType *RT = Ty->getAs<RecordType>();
748 if (!RT)
749 return 0;
750 const RecordDecl *RD = RT->getDecl();
751
752 // If this is a C++ record, check the bases first.
753 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
754 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
755 e = CXXRD->bases_end(); i != e; ++i)
756 if (!isRecordWithSSEVectorType(Context, i->getType()))
757 return false;
758
759 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
760 i != e; ++i) {
761 QualType FT = i->getType();
762
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000763 if (isSSEVectorType(Context, FT))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000764 return true;
765
766 if (isRecordWithSSEVectorType(Context, FT))
767 return true;
768 }
769
770 return false;
771}
772
Daniel Dunbare59d8582010-09-16 20:42:06 +0000773unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
774 unsigned Align) const {
775 // Otherwise, if the alignment is less than or equal to the minimum ABI
776 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000777 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +0000778 return 0; // Use default alignment.
779
780 // On non-Darwin, the stack type alignment is always 4.
781 if (!IsDarwinVectorABI) {
782 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000783 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +0000784 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000785
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000786 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000787 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
788 isRecordWithSSEVectorType(getContext(), Ty)))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000789 return 16;
790
791 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000792}
793
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000794ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
795 unsigned &FreeRegs) const {
796 if (!ByVal) {
797 if (FreeRegs) {
798 --FreeRegs; // Non byval indirects just use one pointer.
799 return ABIArgInfo::getIndirectInReg(0, false);
800 }
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000801 return ABIArgInfo::getIndirect(0, false);
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000802 }
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000803
Daniel Dunbare59d8582010-09-16 20:42:06 +0000804 // Compute the byval alignment.
805 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
806 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
807 if (StackAlign == 0)
Chris Lattnerde92d732011-05-22 23:35:00 +0000808 return ABIArgInfo::getIndirect(4);
Daniel Dunbare59d8582010-09-16 20:42:06 +0000809
810 // If the stack alignment is less than the type alignment, realign the
811 // argument.
812 if (StackAlign < TypeAlign)
813 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
814 /*Realign=*/true);
815
816 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000817}
818
Rafael Espindolab48280b2012-07-31 02:44:24 +0000819X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
820 const Type *T = isSingleElementStruct(Ty, getContext());
821 if (!T)
822 T = Ty.getTypePtr();
823
824 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
825 BuiltinType::Kind K = BT->getKind();
826 if (K == BuiltinType::Float || K == BuiltinType::Double)
827 return Float;
828 }
829 return Integer;
830}
831
Rafael Espindolab6932692012-10-24 01:58:58 +0000832bool X86_32ABIInfo::shouldUseInReg(QualType Ty, unsigned &FreeRegs,
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000833 bool IsFastCall, bool &NeedsPadding) const {
834 NeedsPadding = false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000835 Class C = classify(Ty);
836 if (C == Float)
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000837 return false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000838
Rafael Espindolab6932692012-10-24 01:58:58 +0000839 unsigned Size = getContext().getTypeSize(Ty);
840 unsigned SizeInRegs = (Size + 31) / 32;
Rafael Espindola5f14fcb2012-10-23 02:04:01 +0000841
842 if (SizeInRegs == 0)
843 return false;
844
Rafael Espindolab48280b2012-07-31 02:44:24 +0000845 if (SizeInRegs > FreeRegs) {
846 FreeRegs = 0;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000847 return false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000848 }
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000849
Rafael Espindolab48280b2012-07-31 02:44:24 +0000850 FreeRegs -= SizeInRegs;
Rafael Espindolab6932692012-10-24 01:58:58 +0000851
852 if (IsFastCall) {
853 if (Size > 32)
854 return false;
855
856 if (Ty->isIntegralOrEnumerationType())
857 return true;
858
859 if (Ty->isPointerType())
860 return true;
861
862 if (Ty->isReferenceType())
863 return true;
864
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000865 if (FreeRegs)
866 NeedsPadding = true;
867
Rafael Espindolab6932692012-10-24 01:58:58 +0000868 return false;
869 }
870
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000871 return true;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000872}
873
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000874ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Rafael Espindolab6932692012-10-24 01:58:58 +0000875 unsigned &FreeRegs,
876 bool IsFastCall) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000877 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000878 if (isAggregateTypeForABI(Ty)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000879 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000880 if (IsWin32StructABI)
881 return getIndirectResult(Ty, true, FreeRegs);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000882
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000883 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, CGT))
884 return getIndirectResult(Ty, RAA == CGCXXABI::RAA_DirectInMemory, FreeRegs);
885
886 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000887 if (RT->getDecl()->hasFlexibleArrayMember())
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000888 return getIndirectResult(Ty, true, FreeRegs);
Anders Carlssona8874232010-01-27 03:25:19 +0000889 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000890
Eli Friedman5a4d3522011-11-18 00:28:11 +0000891 // Ignore empty structs/unions.
Eli Friedman5a1ac892011-11-18 04:01:36 +0000892 if (isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000893 return ABIArgInfo::getIgnore();
894
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000895 llvm::LLVMContext &LLVMContext = getVMContext();
896 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
897 bool NeedsPadding;
898 if (shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding)) {
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000899 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Craig Topperb9bad792013-07-08 04:47:18 +0000900 SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32);
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000901 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
902 return ABIArgInfo::getDirectInReg(Result);
903 }
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000904 llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : 0;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000905
Daniel Dunbar53012f42009-11-09 01:33:53 +0000906 // Expand small (<= 128-bit) record types when we know that the stack layout
907 // of those arguments will match the struct. This is important because the
908 // LLVM backend isn't smart enough to remove byval, which inhibits many
909 // optimizations.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000910 if (getContext().getTypeSize(Ty) <= 4*32 &&
911 canExpandIndirectArgument(Ty, getContext()))
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000912 return ABIArgInfo::getExpandWithPadding(IsFastCall, PaddingType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000913
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000914 return getIndirectResult(Ty, true, FreeRegs);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000915 }
916
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000917 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +0000918 // On Darwin, some vectors are passed in memory, we handle this by passing
919 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000920 if (IsDarwinVectorABI) {
921 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000922 if ((Size == 8 || Size == 16 || Size == 32) ||
923 (Size == 64 && VT->getNumElements() == 1))
924 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
925 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000926 }
Bill Wendlingbb465d72010-10-18 03:41:31 +0000927
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000928 if (IsX86_MMXType(CGT.ConvertType(Ty)))
929 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64));
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000930
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000931 return ABIArgInfo::getDirect();
932 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000933
934
Chris Lattnera3c109b2010-07-29 02:16:43 +0000935 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
936 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000937
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000938 bool NeedsPadding;
939 bool InReg = shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding);
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000940
941 if (Ty->isPromotableIntegerType()) {
942 if (InReg)
943 return ABIArgInfo::getExtendInReg();
944 return ABIArgInfo::getExtend();
945 }
946 if (InReg)
947 return ABIArgInfo::getDirectInReg();
948 return ABIArgInfo::getDirect();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000949}
950
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000951void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
952 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
953 FI.getCallingConvention());
Rafael Espindolab48280b2012-07-31 02:44:24 +0000954
Rafael Espindolab6932692012-10-24 01:58:58 +0000955 unsigned CC = FI.getCallingConvention();
956 bool IsFastCall = CC == llvm::CallingConv::X86_FastCall;
957 unsigned FreeRegs;
958 if (IsFastCall)
959 FreeRegs = 2;
960 else if (FI.getHasRegParm())
961 FreeRegs = FI.getRegParm();
962 else
963 FreeRegs = DefaultNumRegisterParameters;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000964
965 // If the return value is indirect, then the hidden argument is consuming one
966 // integer register.
967 if (FI.getReturnInfo().isIndirect() && FreeRegs) {
968 --FreeRegs;
969 ABIArgInfo &Old = FI.getReturnInfo();
970 Old = ABIArgInfo::getIndirectInReg(Old.getIndirectAlign(),
971 Old.getIndirectByVal(),
972 Old.getIndirectRealign());
973 }
974
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000975 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
976 it != ie; ++it)
Rafael Espindolab6932692012-10-24 01:58:58 +0000977 it->info = classifyArgumentType(it->type, FreeRegs, IsFastCall);
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000978}
979
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000980llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
981 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +0000982 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000983
984 CGBuilderTy &Builder = CGF.Builder;
985 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
986 "ap");
987 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Eli Friedman7b1fb812011-11-18 02:12:09 +0000988
989 // Compute if the address needs to be aligned
990 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
991 Align = getTypeStackAlignInBytes(Ty, Align);
992 Align = std::max(Align, 4U);
993 if (Align > 4) {
994 // addr = (addr + align - 1) & -align;
995 llvm::Value *Offset =
996 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
997 Addr = CGF.Builder.CreateGEP(Addr, Offset);
998 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
999 CGF.Int32Ty);
1000 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
1001 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1002 Addr->getType(),
1003 "ap.cur.aligned");
1004 }
1005
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001006 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00001007 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001008 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1009
1010 uint64_t Offset =
Eli Friedman7b1fb812011-11-18 02:12:09 +00001011 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001012 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00001013 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001014 "ap.next");
1015 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1016
1017 return AddrTyped;
1018}
1019
Charles Davis74f72932010-02-13 15:54:06 +00001020void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
1021 llvm::GlobalValue *GV,
1022 CodeGen::CodeGenModule &CGM) const {
1023 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1024 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
1025 // Get the LLVM function.
1026 llvm::Function *Fn = cast<llvm::Function>(GV);
1027
1028 // Now add the 'alignstack' attribute with a value of 16.
Bill Wendling0d583392012-10-15 20:36:26 +00001029 llvm::AttrBuilder B;
Bill Wendlinge91e9ec2012-10-14 03:28:14 +00001030 B.addStackAlignmentAttr(16);
Bill Wendling909b6de2013-01-23 00:21:06 +00001031 Fn->addAttributes(llvm::AttributeSet::FunctionIndex,
1032 llvm::AttributeSet::get(CGM.getLLVMContext(),
1033 llvm::AttributeSet::FunctionIndex,
1034 B));
Charles Davis74f72932010-02-13 15:54:06 +00001035 }
1036 }
1037}
1038
John McCall6374c332010-03-06 00:35:14 +00001039bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
1040 CodeGen::CodeGenFunction &CGF,
1041 llvm::Value *Address) const {
1042 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCall6374c332010-03-06 00:35:14 +00001043
Chris Lattner8b418682012-02-07 00:39:47 +00001044 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001045
John McCall6374c332010-03-06 00:35:14 +00001046 // 0-7 are the eight integer registers; the order is different
1047 // on Darwin (for EH), but the range is the same.
1048 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +00001049 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +00001050
John McCall64aa4b32013-04-16 22:48:15 +00001051 if (CGF.CGM.getTarget().getTriple().isOSDarwin()) {
John McCall6374c332010-03-06 00:35:14 +00001052 // 12-16 are st(0..4). Not sure why we stop at 4.
1053 // These have size 16, which is sizeof(long double) on
1054 // platforms with 8-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001055 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCallaeeb7012010-05-27 06:19:26 +00001056 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001057
John McCall6374c332010-03-06 00:35:14 +00001058 } else {
1059 // 9 is %eflags, which doesn't get a size on Darwin for some
1060 // reason.
1061 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
1062
1063 // 11-16 are st(0..5). Not sure why we stop at 5.
1064 // These have size 12, which is sizeof(long double) on
1065 // platforms with 4-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001066 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCallaeeb7012010-05-27 06:19:26 +00001067 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1068 }
John McCall6374c332010-03-06 00:35:14 +00001069
1070 return false;
1071}
1072
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001073//===----------------------------------------------------------------------===//
1074// X86-64 ABI Implementation
1075//===----------------------------------------------------------------------===//
1076
1077
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001078namespace {
1079/// X86_64ABIInfo - The X86_64 ABI information.
1080class X86_64ABIInfo : public ABIInfo {
1081 enum Class {
1082 Integer = 0,
1083 SSE,
1084 SSEUp,
1085 X87,
1086 X87Up,
1087 ComplexX87,
1088 NoClass,
1089 Memory
1090 };
1091
1092 /// merge - Implement the X86_64 ABI merging algorithm.
1093 ///
1094 /// Merge an accumulating classification \arg Accum with a field
1095 /// classification \arg Field.
1096 ///
1097 /// \param Accum - The accumulating classification. This should
1098 /// always be either NoClass or the result of a previous merge
1099 /// call. In addition, this should never be Memory (the caller
1100 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001101 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001102
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001103 /// postMerge - Implement the X86_64 ABI post merging algorithm.
1104 ///
1105 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
1106 /// final MEMORY or SSE classes when necessary.
1107 ///
1108 /// \param AggregateSize - The size of the current aggregate in
1109 /// the classification process.
1110 ///
1111 /// \param Lo - The classification for the parts of the type
1112 /// residing in the low word of the containing object.
1113 ///
1114 /// \param Hi - The classification for the parts of the type
1115 /// residing in the higher words of the containing object.
1116 ///
1117 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
1118
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001119 /// classify - Determine the x86_64 register classes in which the
1120 /// given type T should be passed.
1121 ///
1122 /// \param Lo - The classification for the parts of the type
1123 /// residing in the low word of the containing object.
1124 ///
1125 /// \param Hi - The classification for the parts of the type
1126 /// residing in the high word of the containing object.
1127 ///
1128 /// \param OffsetBase - The bit offset of this type in the
1129 /// containing object. Some parameters are classified different
1130 /// depending on whether they straddle an eightbyte boundary.
1131 ///
Eli Friedman7a1b5862013-06-12 00:13:45 +00001132 /// \param isNamedArg - Whether the argument in question is a "named"
1133 /// argument, as used in AMD64-ABI 3.5.7.
1134 ///
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001135 /// If a word is unused its result will be NoClass; if a type should
1136 /// be passed in Memory then at least the classification of \arg Lo
1137 /// will be Memory.
1138 ///
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001139 /// The \arg Lo class will be NoClass iff the argument is ignored.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001140 ///
1141 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
1142 /// also be ComplexX87.
Eli Friedman7a1b5862013-06-12 00:13:45 +00001143 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi,
1144 bool isNamedArg) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001145
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001146 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001147 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
1148 unsigned IROffset, QualType SourceTy,
1149 unsigned SourceOffset) const;
1150 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
1151 unsigned IROffset, QualType SourceTy,
1152 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001153
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001154 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001155 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +00001156 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001157
1158 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001159 /// such that the argument will be passed in memory.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001160 ///
1161 /// \param freeIntRegs - The number of free integer registers remaining
1162 /// available.
1163 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001164
Chris Lattnera3c109b2010-07-29 02:16:43 +00001165 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001166
Bill Wendlingbb465d72010-10-18 03:41:31 +00001167 ABIArgInfo classifyArgumentType(QualType Ty,
Daniel Dunbaredfac032012-03-10 01:03:58 +00001168 unsigned freeIntRegs,
Bill Wendlingbb465d72010-10-18 03:41:31 +00001169 unsigned &neededInt,
Eli Friedman7a1b5862013-06-12 00:13:45 +00001170 unsigned &neededSSE,
1171 bool isNamedArg) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001172
Eli Friedmanee1ad992011-12-02 00:11:43 +00001173 bool IsIllegalVectorType(QualType Ty) const;
1174
John McCall67a57732011-04-21 01:20:55 +00001175 /// The 0.98 ABI revision clarified a lot of ambiguities,
1176 /// unfortunately in ways that were not always consistent with
1177 /// certain previous compilers. In particular, platforms which
1178 /// required strict binary compatibility with older versions of GCC
1179 /// may need to exempt themselves.
1180 bool honorsRevision0_98() const {
John McCall64aa4b32013-04-16 22:48:15 +00001181 return !getTarget().getTriple().isOSDarwin();
John McCall67a57732011-04-21 01:20:55 +00001182 }
1183
Eli Friedmanee1ad992011-12-02 00:11:43 +00001184 bool HasAVX;
Derek Schuffbabaf312012-10-11 15:52:22 +00001185 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
1186 // 64-bit hardware.
1187 bool Has64BitPointers;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001188
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001189public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001190 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
Derek Schuffbabaf312012-10-11 15:52:22 +00001191 ABIInfo(CGT), HasAVX(hasavx),
Derek Schuff90da80c2012-10-11 18:21:13 +00001192 Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001193 }
Chris Lattner9c254f02010-06-29 06:01:59 +00001194
John McCallde5d3c72012-02-17 03:33:10 +00001195 bool isPassedUsingAVXType(QualType type) const {
1196 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00001197 // The freeIntRegs argument doesn't matter here.
Eli Friedman7a1b5862013-06-12 00:13:45 +00001198 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE,
1199 /*isNamedArg*/true);
John McCallde5d3c72012-02-17 03:33:10 +00001200 if (info.isDirect()) {
1201 llvm::Type *ty = info.getCoerceToType();
1202 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
1203 return (vectorTy->getBitWidth() > 128);
1204 }
1205 return false;
1206 }
1207
Chris Lattneree5dcd02010-07-29 02:31:05 +00001208 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001209
1210 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1211 CodeGenFunction &CGF) const;
1212};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001213
Chris Lattnerf13721d2010-08-31 16:44:54 +00001214/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001215class WinX86_64ABIInfo : public ABIInfo {
1216
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00001217 ABIArgInfo classify(QualType Ty, bool IsReturnType) const;
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001218
Chris Lattnerf13721d2010-08-31 16:44:54 +00001219public:
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001220 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
1221
1222 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattnerf13721d2010-08-31 16:44:54 +00001223
1224 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1225 CodeGenFunction &CGF) const;
1226};
1227
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001228class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1229public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001230 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
Derek Schuffbabaf312012-10-11 15:52:22 +00001231 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
John McCall6374c332010-03-06 00:35:14 +00001232
John McCallde5d3c72012-02-17 03:33:10 +00001233 const X86_64ABIInfo &getABIInfo() const {
1234 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
1235 }
1236
John McCall6374c332010-03-06 00:35:14 +00001237 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1238 return 7;
1239 }
1240
1241 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1242 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001243 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001244
John McCallaeeb7012010-05-27 06:19:26 +00001245 // 0-15 are the 16 integer registers.
1246 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001247 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +00001248 return false;
1249 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001250
Jay Foadef6de3d2011-07-11 09:56:20 +00001251 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001252 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +00001253 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001254 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1255 }
1256
John McCallde5d3c72012-02-17 03:33:10 +00001257 bool isNoProtoCallVariadic(const CallArgList &args,
1258 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +00001259 // The default CC on x86-64 sets %al to the number of SSA
1260 // registers used, and GCC sets this when calling an unprototyped
Eli Friedman3ed79032011-12-01 04:53:19 +00001261 // function, so we override the default behavior. However, don't do
Eli Friedman68805fe2011-12-06 03:08:26 +00001262 // that when AVX types are involved: the ABI explicitly states it is
1263 // undefined, and it doesn't work in practice because of how the ABI
1264 // defines varargs anyway.
John McCallde5d3c72012-02-17 03:33:10 +00001265 if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) {
Eli Friedman3ed79032011-12-01 04:53:19 +00001266 bool HasAVXType = false;
John McCallde5d3c72012-02-17 03:33:10 +00001267 for (CallArgList::const_iterator
1268 it = args.begin(), ie = args.end(); it != ie; ++it) {
1269 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1270 HasAVXType = true;
1271 break;
Eli Friedman3ed79032011-12-01 04:53:19 +00001272 }
1273 }
John McCallde5d3c72012-02-17 03:33:10 +00001274
Eli Friedman3ed79032011-12-01 04:53:19 +00001275 if (!HasAVXType)
1276 return true;
1277 }
John McCall01f151e2011-09-21 08:08:30 +00001278
John McCallde5d3c72012-02-17 03:33:10 +00001279 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
John McCall01f151e2011-09-21 08:08:30 +00001280 }
1281
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001282};
1283
Aaron Ballman89735b92013-05-24 15:06:56 +00001284static std::string qualifyWindowsLibrary(llvm::StringRef Lib) {
1285 // If the argument does not end in .lib, automatically add the suffix. This
1286 // matches the behavior of MSVC.
1287 std::string ArgStr = Lib;
1288 if (Lib.size() <= 4 ||
1289 Lib.substr(Lib.size() - 4).compare_lower(".lib") != 0) {
1290 ArgStr += ".lib";
1291 }
1292 return ArgStr;
1293}
1294
Reid Kleckner3190ca92013-05-08 13:44:39 +00001295class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo {
1296public:
John McCallb8b52972013-06-18 02:46:29 +00001297 WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
1298 bool d, bool p, bool w, unsigned RegParms)
1299 : X86_32TargetCodeGenInfo(CGT, d, p, w, RegParms) {}
Reid Kleckner3190ca92013-05-08 13:44:39 +00001300
1301 void getDependentLibraryOption(llvm::StringRef Lib,
1302 llvm::SmallString<24> &Opt) const {
1303 Opt = "/DEFAULTLIB:";
Aaron Ballman89735b92013-05-24 15:06:56 +00001304 Opt += qualifyWindowsLibrary(Lib);
Reid Kleckner3190ca92013-05-08 13:44:39 +00001305 }
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00001306
1307 void getDetectMismatchOption(llvm::StringRef Name,
1308 llvm::StringRef Value,
1309 llvm::SmallString<32> &Opt) const {
Eli Friedman572ac322013-06-07 22:42:22 +00001310 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00001311 }
Reid Kleckner3190ca92013-05-08 13:44:39 +00001312};
1313
Chris Lattnerf13721d2010-08-31 16:44:54 +00001314class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1315public:
1316 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1317 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1318
1319 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1320 return 7;
1321 }
1322
1323 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1324 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001325 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001326
Chris Lattnerf13721d2010-08-31 16:44:54 +00001327 // 0-15 are the 16 integer registers.
1328 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001329 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattnerf13721d2010-08-31 16:44:54 +00001330 return false;
1331 }
Reid Kleckner3190ca92013-05-08 13:44:39 +00001332
1333 void getDependentLibraryOption(llvm::StringRef Lib,
1334 llvm::SmallString<24> &Opt) const {
1335 Opt = "/DEFAULTLIB:";
Aaron Ballman89735b92013-05-24 15:06:56 +00001336 Opt += qualifyWindowsLibrary(Lib);
Reid Kleckner3190ca92013-05-08 13:44:39 +00001337 }
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00001338
1339 void getDetectMismatchOption(llvm::StringRef Name,
1340 llvm::StringRef Value,
1341 llvm::SmallString<32> &Opt) const {
Eli Friedman572ac322013-06-07 22:42:22 +00001342 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00001343 }
Chris Lattnerf13721d2010-08-31 16:44:54 +00001344};
1345
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001346}
1347
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001348void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1349 Class &Hi) const {
1350 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1351 //
1352 // (a) If one of the classes is Memory, the whole argument is passed in
1353 // memory.
1354 //
1355 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1356 // memory.
1357 //
1358 // (c) If the size of the aggregate exceeds two eightbytes and the first
1359 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1360 // argument is passed in memory. NOTE: This is necessary to keep the
1361 // ABI working for processors that don't support the __m256 type.
1362 //
1363 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1364 //
1365 // Some of these are enforced by the merging logic. Others can arise
1366 // only with unions; for example:
1367 // union { _Complex double; unsigned; }
1368 //
1369 // Note that clauses (b) and (c) were added in 0.98.
1370 //
1371 if (Hi == Memory)
1372 Lo = Memory;
1373 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1374 Lo = Memory;
1375 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1376 Lo = Memory;
1377 if (Hi == SSEUp && Lo != SSE)
1378 Hi = SSE;
1379}
1380
Chris Lattner1090a9b2010-06-28 21:43:59 +00001381X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001382 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1383 // classified recursively so that always two fields are
1384 // considered. The resulting class is calculated according to
1385 // the classes of the fields in the eightbyte:
1386 //
1387 // (a) If both classes are equal, this is the resulting class.
1388 //
1389 // (b) If one of the classes is NO_CLASS, the resulting class is
1390 // the other class.
1391 //
1392 // (c) If one of the classes is MEMORY, the result is the MEMORY
1393 // class.
1394 //
1395 // (d) If one of the classes is INTEGER, the result is the
1396 // INTEGER.
1397 //
1398 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1399 // MEMORY is used as class.
1400 //
1401 // (f) Otherwise class SSE is used.
1402
1403 // Accum should never be memory (we should have returned) or
1404 // ComplexX87 (because this cannot be passed in a structure).
1405 assert((Accum != Memory && Accum != ComplexX87) &&
1406 "Invalid accumulated classification during merge.");
1407 if (Accum == Field || Field == NoClass)
1408 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001409 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001410 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001411 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001412 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001413 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001414 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001415 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1416 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001417 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001418 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001419}
1420
Chris Lattnerbcaedae2010-06-30 19:14:05 +00001421void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Eli Friedman7a1b5862013-06-12 00:13:45 +00001422 Class &Lo, Class &Hi, bool isNamedArg) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001423 // FIXME: This code can be simplified by introducing a simple value class for
1424 // Class pairs with appropriate constructor methods for the various
1425 // situations.
1426
1427 // FIXME: Some of the split computations are wrong; unaligned vectors
1428 // shouldn't be passed in registers for example, so there is no chance they
1429 // can straddle an eightbyte. Verify & simplify.
1430
1431 Lo = Hi = NoClass;
1432
1433 Class &Current = OffsetBase < 64 ? Lo : Hi;
1434 Current = Memory;
1435
John McCall183700f2009-09-21 23:43:11 +00001436 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001437 BuiltinType::Kind k = BT->getKind();
1438
1439 if (k == BuiltinType::Void) {
1440 Current = NoClass;
1441 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1442 Lo = Integer;
1443 Hi = Integer;
1444 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1445 Current = Integer;
Derek Schuff7da46f92012-10-11 16:55:58 +00001446 } else if ((k == BuiltinType::Float || k == BuiltinType::Double) ||
1447 (k == BuiltinType::LongDouble &&
John McCall64aa4b32013-04-16 22:48:15 +00001448 getTarget().getTriple().getOS() == llvm::Triple::NaCl)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001449 Current = SSE;
1450 } else if (k == BuiltinType::LongDouble) {
1451 Lo = X87;
1452 Hi = X87Up;
1453 }
1454 // FIXME: _Decimal32 and _Decimal64 are SSE.
1455 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001456 return;
1457 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001458
Chris Lattner1090a9b2010-06-28 21:43:59 +00001459 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001460 // Classify the underlying integer type.
Eli Friedman7a1b5862013-06-12 00:13:45 +00001461 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001462 return;
1463 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001464
Chris Lattner1090a9b2010-06-28 21:43:59 +00001465 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001466 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001467 return;
1468 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001469
Chris Lattner1090a9b2010-06-28 21:43:59 +00001470 if (Ty->isMemberPointerType()) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001471 if (Ty->isMemberFunctionPointerType() && Has64BitPointers)
Daniel Dunbar67d438d2010-05-15 00:00:37 +00001472 Lo = Hi = Integer;
1473 else
1474 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001475 return;
1476 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001477
Chris Lattner1090a9b2010-06-28 21:43:59 +00001478 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001479 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001480 if (Size == 32) {
1481 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1482 // float> as integer.
1483 Current = Integer;
1484
1485 // If this type crosses an eightbyte boundary, it should be
1486 // split.
1487 uint64_t EB_Real = (OffsetBase) / 64;
1488 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1489 if (EB_Real != EB_Imag)
1490 Hi = Lo;
1491 } else if (Size == 64) {
1492 // gcc passes <1 x double> in memory. :(
1493 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1494 return;
1495
1496 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001497 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001498 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1499 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1500 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001501 Current = Integer;
1502 else
1503 Current = SSE;
1504
1505 // If this type crosses an eightbyte boundary, it should be
1506 // split.
1507 if (OffsetBase && OffsetBase != 64)
1508 Hi = Lo;
Eli Friedman7a1b5862013-06-12 00:13:45 +00001509 } else if (Size == 128 || (HasAVX && isNamedArg && Size == 256)) {
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001510 // Arguments of 256-bits are split into four eightbyte chunks. The
1511 // least significant one belongs to class SSE and all the others to class
1512 // SSEUP. The original Lo and Hi design considers that types can't be
1513 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1514 // This design isn't correct for 256-bits, but since there're no cases
1515 // where the upper parts would need to be inspected, avoid adding
1516 // complexity and just consider Hi to match the 64-256 part.
Eli Friedman7a1b5862013-06-12 00:13:45 +00001517 //
1518 // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in
1519 // registers if they are "named", i.e. not part of the "..." of a
1520 // variadic function.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001521 Lo = SSE;
1522 Hi = SSEUp;
1523 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001524 return;
1525 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001526
Chris Lattner1090a9b2010-06-28 21:43:59 +00001527 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001528 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001529
Chris Lattnerea044322010-07-29 02:01:43 +00001530 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001531 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001532 if (Size <= 64)
1533 Current = Integer;
1534 else if (Size <= 128)
1535 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001536 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001537 Current = SSE;
Derek Schuff7da46f92012-10-11 16:55:58 +00001538 else if (ET == getContext().DoubleTy ||
1539 (ET == getContext().LongDoubleTy &&
John McCall64aa4b32013-04-16 22:48:15 +00001540 getTarget().getTriple().getOS() == llvm::Triple::NaCl))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001541 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001542 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001543 Current = ComplexX87;
1544
1545 // If this complex type crosses an eightbyte boundary then it
1546 // should be split.
1547 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001548 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001549 if (Hi == NoClass && EB_Real != EB_Imag)
1550 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001551
Chris Lattner1090a9b2010-06-28 21:43:59 +00001552 return;
1553 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001554
Chris Lattnerea044322010-07-29 02:01:43 +00001555 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001556 // Arrays are treated like structures.
1557
Chris Lattnerea044322010-07-29 02:01:43 +00001558 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001559
1560 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001561 // than four eightbytes, ..., it has class MEMORY.
1562 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001563 return;
1564
1565 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1566 // fields, it has class MEMORY.
1567 //
1568 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001569 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001570 return;
1571
1572 // Otherwise implement simplified merge. We could be smarter about
1573 // this, but it isn't worth it and would be harder to verify.
1574 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001575 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001576 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes089d8922011-07-12 01:27:38 +00001577
1578 // The only case a 256-bit wide vector could be used is when the array
1579 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1580 // to work for sizes wider than 128, early check and fallback to memory.
1581 if (Size > 128 && EltSize != 256)
1582 return;
1583
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001584 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1585 Class FieldLo, FieldHi;
Eli Friedman7a1b5862013-06-12 00:13:45 +00001586 classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001587 Lo = merge(Lo, FieldLo);
1588 Hi = merge(Hi, FieldHi);
1589 if (Lo == Memory || Hi == Memory)
1590 break;
1591 }
1592
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001593 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001594 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001595 return;
1596 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001597
Chris Lattner1090a9b2010-06-28 21:43:59 +00001598 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001599 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001600
1601 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001602 // than four eightbytes, ..., it has class MEMORY.
1603 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001604 return;
1605
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001606 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1607 // copy constructor or a non-trivial destructor, it is passed by invisible
1608 // reference.
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00001609 if (getRecordArgABI(RT, CGT))
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001610 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001611
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001612 const RecordDecl *RD = RT->getDecl();
1613
1614 // Assume variable sized types are passed in memory.
1615 if (RD->hasFlexibleArrayMember())
1616 return;
1617
Chris Lattnerea044322010-07-29 02:01:43 +00001618 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001619
1620 // Reset Lo class, this will be recomputed.
1621 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001622
1623 // If this is a C++ record, classify the bases first.
1624 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1625 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1626 e = CXXRD->bases_end(); i != e; ++i) {
1627 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1628 "Unexpected base class!");
1629 const CXXRecordDecl *Base =
1630 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1631
1632 // Classify this field.
1633 //
1634 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1635 // single eightbyte, each is classified separately. Each eightbyte gets
1636 // initialized to class NO_CLASS.
1637 Class FieldLo, FieldHi;
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001638 uint64_t Offset =
1639 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
Eli Friedman7a1b5862013-06-12 00:13:45 +00001640 classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001641 Lo = merge(Lo, FieldLo);
1642 Hi = merge(Hi, FieldHi);
1643 if (Lo == Memory || Hi == Memory)
1644 break;
1645 }
1646 }
1647
1648 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001649 unsigned idx = 0;
Bruno Cardoso Lopes548e4782011-07-12 22:30:58 +00001650 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001651 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001652 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1653 bool BitField = i->isBitField();
1654
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001655 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1656 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001657 //
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001658 // The only case a 256-bit wide vector could be used is when the struct
1659 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1660 // to work for sizes wider than 128, early check and fallback to memory.
1661 //
1662 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1663 Lo = Memory;
1664 return;
1665 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001666 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001667 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001668 Lo = Memory;
1669 return;
1670 }
1671
1672 // Classify this field.
1673 //
1674 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1675 // exceeds a single eightbyte, each is classified
1676 // separately. Each eightbyte gets initialized to class
1677 // NO_CLASS.
1678 Class FieldLo, FieldHi;
1679
1680 // Bit-fields require special handling, they do not force the
1681 // structure to be passed in memory even if unaligned, and
1682 // therefore they can straddle an eightbyte.
1683 if (BitField) {
1684 // Ignore padding bit-fields.
1685 if (i->isUnnamedBitfield())
1686 continue;
1687
1688 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001689 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001690
1691 uint64_t EB_Lo = Offset / 64;
1692 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1693 FieldLo = FieldHi = NoClass;
1694 if (EB_Lo) {
1695 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1696 FieldLo = NoClass;
1697 FieldHi = Integer;
1698 } else {
1699 FieldLo = Integer;
1700 FieldHi = EB_Hi ? Integer : NoClass;
1701 }
1702 } else
Eli Friedman7a1b5862013-06-12 00:13:45 +00001703 classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001704 Lo = merge(Lo, FieldLo);
1705 Hi = merge(Hi, FieldHi);
1706 if (Lo == Memory || Hi == Memory)
1707 break;
1708 }
1709
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001710 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001711 }
1712}
1713
Chris Lattner9c254f02010-06-29 06:01:59 +00001714ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001715 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1716 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001717 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001718 // Treat an enum type as its underlying type.
1719 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1720 Ty = EnumTy->getDecl()->getIntegerType();
1721
1722 return (Ty->isPromotableIntegerType() ?
1723 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1724 }
1725
1726 return ABIArgInfo::getIndirect(0);
1727}
1728
Eli Friedmanee1ad992011-12-02 00:11:43 +00001729bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1730 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1731 uint64_t Size = getContext().getTypeSize(VecTy);
1732 unsigned LargestVector = HasAVX ? 256 : 128;
1733 if (Size <= 64 || Size > LargestVector)
1734 return true;
1735 }
1736
1737 return false;
1738}
1739
Daniel Dunbaredfac032012-03-10 01:03:58 +00001740ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1741 unsigned freeIntRegs) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001742 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1743 // place naturally.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001744 //
1745 // This assumption is optimistic, as there could be free registers available
1746 // when we need to pass this argument in memory, and LLVM could try to pass
1747 // the argument in the free register. This does not seem to happen currently,
1748 // but this code would be much safer if we could mark the argument with
1749 // 'onstack'. See PR12193.
Eli Friedmanee1ad992011-12-02 00:11:43 +00001750 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001751 // Treat an enum type as its underlying type.
1752 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1753 Ty = EnumTy->getDecl()->getIntegerType();
1754
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001755 return (Ty->isPromotableIntegerType() ?
1756 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001757 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001758
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00001759 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
1760 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001761
Chris Lattner855d2272011-05-22 23:21:23 +00001762 // Compute the byval alignment. We specify the alignment of the byval in all
1763 // cases so that the mid-level optimizer knows the alignment of the byval.
1764 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
Daniel Dunbaredfac032012-03-10 01:03:58 +00001765
1766 // Attempt to avoid passing indirect results using byval when possible. This
1767 // is important for good codegen.
1768 //
1769 // We do this by coercing the value into a scalar type which the backend can
1770 // handle naturally (i.e., without using byval).
1771 //
1772 // For simplicity, we currently only do this when we have exhausted all of the
1773 // free integer registers. Doing this when there are free integer registers
1774 // would require more care, as we would have to ensure that the coerced value
1775 // did not claim the unused register. That would require either reording the
1776 // arguments to the function (so that any subsequent inreg values came first),
1777 // or only doing this optimization when there were no following arguments that
1778 // might be inreg.
1779 //
1780 // We currently expect it to be rare (particularly in well written code) for
1781 // arguments to be passed on the stack when there are still free integer
1782 // registers available (this would typically imply large structs being passed
1783 // by value), so this seems like a fair tradeoff for now.
1784 //
1785 // We can revisit this if the backend grows support for 'onstack' parameter
1786 // attributes. See PR12193.
1787 if (freeIntRegs == 0) {
1788 uint64_t Size = getContext().getTypeSize(Ty);
1789
1790 // If this type fits in an eightbyte, coerce it into the matching integral
1791 // type, which will end up on the stack (with alignment 8).
1792 if (Align == 8 && Size <= 64)
1793 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1794 Size));
1795 }
1796
Chris Lattner855d2272011-05-22 23:21:23 +00001797 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001798}
1799
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001800/// GetByteVectorType - The ABI specifies that a value should be passed in an
1801/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner0f408f52010-07-29 04:56:46 +00001802/// vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001803llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001804 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001805
Chris Lattner15842bd2010-07-29 05:02:29 +00001806 // Wrapper structs that just contain vectors are passed just like vectors,
1807 // strip them off if present.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001808 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner15842bd2010-07-29 05:02:29 +00001809 while (STy && STy->getNumElements() == 1) {
1810 IRType = STy->getElementType(0);
1811 STy = dyn_cast<llvm::StructType>(IRType);
1812 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001813
Bruno Cardoso Lopes528a8c72011-07-08 22:57:35 +00001814 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001815 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1816 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001817 unsigned BitWidth = VT->getBitWidth();
Tanya Lattnerce275672011-11-28 23:18:11 +00001818 if ((BitWidth >= 128 && BitWidth <= 256) &&
Chris Lattner0f408f52010-07-29 04:56:46 +00001819 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1820 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1821 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1822 EltTy->isIntegerTy(128)))
1823 return VT;
1824 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001825
Chris Lattner0f408f52010-07-29 04:56:46 +00001826 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1827}
1828
Chris Lattnere2962be2010-07-29 07:30:00 +00001829/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1830/// is known to either be off the end of the specified type or being in
1831/// alignment padding. The user type specified is known to be at most 128 bits
1832/// in size, and have passed through X86_64ABIInfo::classify with a successful
1833/// classification that put one of the two halves in the INTEGER class.
1834///
1835/// It is conservatively correct to return false.
1836static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1837 unsigned EndBit, ASTContext &Context) {
1838 // If the bytes being queried are off the end of the type, there is no user
1839 // data hiding here. This handles analysis of builtins, vectors and other
1840 // types that don't contain interesting padding.
1841 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1842 if (TySize <= StartBit)
1843 return true;
1844
Chris Lattner021c3a32010-07-29 07:43:55 +00001845 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1846 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1847 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1848
1849 // Check each element to see if the element overlaps with the queried range.
1850 for (unsigned i = 0; i != NumElts; ++i) {
1851 // If the element is after the span we care about, then we're done..
1852 unsigned EltOffset = i*EltSize;
1853 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001854
Chris Lattner021c3a32010-07-29 07:43:55 +00001855 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1856 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1857 EndBit-EltOffset, Context))
1858 return false;
1859 }
1860 // If it overlaps no elements, then it is safe to process as padding.
1861 return true;
1862 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001863
Chris Lattnere2962be2010-07-29 07:30:00 +00001864 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1865 const RecordDecl *RD = RT->getDecl();
1866 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001867
Chris Lattnere2962be2010-07-29 07:30:00 +00001868 // If this is a C++ record, check the bases first.
1869 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1870 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1871 e = CXXRD->bases_end(); i != e; ++i) {
1872 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1873 "Unexpected base class!");
1874 const CXXRecordDecl *Base =
1875 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001876
Chris Lattnere2962be2010-07-29 07:30:00 +00001877 // If the base is after the span we care about, ignore it.
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001878 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
Chris Lattnere2962be2010-07-29 07:30:00 +00001879 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001880
Chris Lattnere2962be2010-07-29 07:30:00 +00001881 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1882 if (!BitsContainNoUserData(i->getType(), BaseStart,
1883 EndBit-BaseOffset, Context))
1884 return false;
1885 }
1886 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001887
Chris Lattnere2962be2010-07-29 07:30:00 +00001888 // Verify that no field has data that overlaps the region of interest. Yes
1889 // this could be sped up a lot by being smarter about queried fields,
1890 // however we're only looking at structs up to 16 bytes, so we don't care
1891 // much.
1892 unsigned idx = 0;
1893 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1894 i != e; ++i, ++idx) {
1895 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001896
Chris Lattnere2962be2010-07-29 07:30:00 +00001897 // If we found a field after the region we care about, then we're done.
1898 if (FieldOffset >= EndBit) break;
1899
1900 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1901 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1902 Context))
1903 return false;
1904 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001905
Chris Lattnere2962be2010-07-29 07:30:00 +00001906 // If nothing in this record overlapped the area of interest, then we're
1907 // clean.
1908 return true;
1909 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001910
Chris Lattnere2962be2010-07-29 07:30:00 +00001911 return false;
1912}
1913
Chris Lattner0b362002010-07-29 18:39:32 +00001914/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1915/// float member at the specified offset. For example, {int,{float}} has a
1916/// float at offset 4. It is conservatively correct for this routine to return
1917/// false.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001918static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Micah Villmow25a6a842012-10-08 16:25:52 +00001919 const llvm::DataLayout &TD) {
Chris Lattner0b362002010-07-29 18:39:32 +00001920 // Base case if we find a float.
1921 if (IROffset == 0 && IRType->isFloatTy())
1922 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001923
Chris Lattner0b362002010-07-29 18:39:32 +00001924 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001925 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner0b362002010-07-29 18:39:32 +00001926 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1927 unsigned Elt = SL->getElementContainingOffset(IROffset);
1928 IROffset -= SL->getElementOffset(Elt);
1929 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1930 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001931
Chris Lattner0b362002010-07-29 18:39:32 +00001932 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001933 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1934 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner0b362002010-07-29 18:39:32 +00001935 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1936 IROffset -= IROffset/EltSize*EltSize;
1937 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1938 }
1939
1940 return false;
1941}
1942
Chris Lattnerf47c9442010-07-29 18:13:09 +00001943
1944/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1945/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001946llvm::Type *X86_64ABIInfo::
1947GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00001948 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001949 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001950 // pass as float if the last 4 bytes is just padding. This happens for
1951 // structs that contain 3 floats.
1952 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1953 SourceOffset*8+64, getContext()))
1954 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001955
Chris Lattner0b362002010-07-29 18:39:32 +00001956 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1957 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1958 // case.
Micah Villmow25a6a842012-10-08 16:25:52 +00001959 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
1960 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001961 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001962
Chris Lattnerf47c9442010-07-29 18:13:09 +00001963 return llvm::Type::getDoubleTy(getVMContext());
1964}
1965
1966
Chris Lattner0d2656d2010-07-29 17:40:35 +00001967/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1968/// an 8-byte GPR. This means that we either have a scalar or we are talking
1969/// about the high or low part of an up-to-16-byte struct. This routine picks
1970/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001971/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1972/// etc).
1973///
1974/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1975/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1976/// the 8-byte value references. PrefType may be null.
1977///
1978/// SourceTy is the source level type for the entire argument. SourceOffset is
1979/// an offset into this that we're processing (which is always either 0 or 8).
1980///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001981llvm::Type *X86_64ABIInfo::
1982GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00001983 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001984 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1985 // returning an 8-byte unit starting with it. See if we can safely use it.
1986 if (IROffset == 0) {
1987 // Pointers and int64's always fill the 8-byte unit.
Derek Schuffbabaf312012-10-11 15:52:22 +00001988 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
1989 IRType->isIntegerTy(64))
Chris Lattnere2962be2010-07-29 07:30:00 +00001990 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001991
Chris Lattnere2962be2010-07-29 07:30:00 +00001992 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1993 // goodness in the source type is just tail padding. This is allowed to
1994 // kick in for struct {double,int} on the int, but not on
1995 // struct{double,int,int} because we wouldn't return the second int. We
1996 // have to do this analysis on the source type because we can't depend on
1997 // unions being lowered a specific way etc.
1998 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
Derek Schuffbabaf312012-10-11 15:52:22 +00001999 IRType->isIntegerTy(32) ||
2000 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
2001 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
2002 cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002003
Chris Lattnere2962be2010-07-29 07:30:00 +00002004 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
2005 SourceOffset*8+64, getContext()))
2006 return IRType;
2007 }
2008 }
Chris Lattner49382de2010-07-28 22:44:07 +00002009
Chris Lattner2acc6e32011-07-18 04:24:23 +00002010 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00002011 // If this is a struct, recurse into the field at the specified offset.
Micah Villmow25a6a842012-10-08 16:25:52 +00002012 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00002013 if (IROffset < SL->getSizeInBytes()) {
2014 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
2015 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002016
Chris Lattner0d2656d2010-07-29 17:40:35 +00002017 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
2018 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002019 }
Chris Lattner49382de2010-07-28 22:44:07 +00002020 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002021
Chris Lattner2acc6e32011-07-18 04:24:23 +00002022 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002023 llvm::Type *EltTy = ATy->getElementType();
Micah Villmow25a6a842012-10-08 16:25:52 +00002024 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
Chris Lattner021c3a32010-07-29 07:43:55 +00002025 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00002026 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
2027 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00002028 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002029
Chris Lattner49382de2010-07-28 22:44:07 +00002030 // Okay, we don't have any better idea of what to pass, so we pass this in an
2031 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00002032 unsigned TySizeInBytes =
2033 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00002034
Chris Lattner9e45a3d2010-07-29 17:34:39 +00002035 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002036
Chris Lattner49382de2010-07-28 22:44:07 +00002037 // It is always safe to classify this as an integer type up to i64 that
2038 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00002039 return llvm::IntegerType::get(getVMContext(),
2040 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00002041}
2042
Chris Lattner66e7b682010-09-01 00:50:20 +00002043
2044/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
2045/// be used as elements of a two register pair to pass or return, return a
2046/// first class aggregate to represent them. For example, if the low part of
2047/// a by-value argument should be passed as i32* and the high part as float,
2048/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002049static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00002050GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Micah Villmow25a6a842012-10-08 16:25:52 +00002051 const llvm::DataLayout &TD) {
Chris Lattner66e7b682010-09-01 00:50:20 +00002052 // In order to correctly satisfy the ABI, we need to the high part to start
2053 // at offset 8. If the high and low parts we inferred are both 4-byte types
2054 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
2055 // the second element at offset 8. Check for this:
2056 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
2057 unsigned HiAlign = TD.getABITypeAlignment(Hi);
Micah Villmow25a6a842012-10-08 16:25:52 +00002058 unsigned HiStart = llvm::DataLayout::RoundUpAlignment(LoSize, HiAlign);
Chris Lattner66e7b682010-09-01 00:50:20 +00002059 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002060
Chris Lattner66e7b682010-09-01 00:50:20 +00002061 // To handle this, we have to increase the size of the low part so that the
2062 // second element will start at an 8 byte offset. We can't increase the size
2063 // of the second element because it might make us access off the end of the
2064 // struct.
2065 if (HiStart != 8) {
2066 // There are only two sorts of types the ABI generation code can produce for
2067 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
2068 // Promote these to a larger type.
2069 if (Lo->isFloatTy())
2070 Lo = llvm::Type::getDoubleTy(Lo->getContext());
2071 else {
2072 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
2073 Lo = llvm::Type::getInt64Ty(Lo->getContext());
2074 }
2075 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002076
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002077 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002078
2079
Chris Lattner66e7b682010-09-01 00:50:20 +00002080 // Verify that the second element is at an 8-byte offset.
2081 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
2082 "Invalid x86-64 argument pair!");
2083 return Result;
2084}
2085
Chris Lattner519f68c2010-07-28 23:06:14 +00002086ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00002087classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00002088 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
2089 // classification algorithm.
2090 X86_64ABIInfo::Class Lo, Hi;
Eli Friedman7a1b5862013-06-12 00:13:45 +00002091 classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true);
Chris Lattner519f68c2010-07-28 23:06:14 +00002092
2093 // Check some invariants.
2094 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002095 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2096
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002097 llvm::Type *ResType = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002098 switch (Lo) {
2099 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002100 if (Hi == NoClass)
2101 return ABIArgInfo::getIgnore();
2102 // If the low part is just padding, it takes no register, leave ResType
2103 // null.
2104 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2105 "Unknown missing lo part");
2106 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002107
2108 case SSEUp:
2109 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002110 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002111
2112 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
2113 // hidden argument.
2114 case Memory:
2115 return getIndirectReturnResult(RetTy);
2116
2117 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
2118 // available register of the sequence %rax, %rdx is used.
2119 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002120 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002121
Chris Lattnereb518b42010-07-29 21:42:50 +00002122 // If we have a sign or zero extended integer, make sure to return Extend
2123 // so that the parameter gets the right LLVM IR attributes.
2124 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2125 // Treat an enum type as its underlying type.
2126 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2127 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002128
Chris Lattnereb518b42010-07-29 21:42:50 +00002129 if (RetTy->isIntegralOrEnumerationType() &&
2130 RetTy->isPromotableIntegerType())
2131 return ABIArgInfo::getExtend();
2132 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002133 break;
2134
2135 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
2136 // available SSE register of the sequence %xmm0, %xmm1 is used.
2137 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002138 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00002139 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002140
2141 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
2142 // returned on the X87 stack in %st0 as 80-bit x87 number.
2143 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00002144 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00002145 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002146
2147 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
2148 // part of the value is returned in %st0 and the imaginary part in
2149 // %st1.
2150 case ComplexX87:
2151 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00002152 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00002153 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00002154 NULL);
2155 break;
2156 }
2157
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002158 llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002159 switch (Hi) {
2160 // Memory was handled previously and X87 should
2161 // never occur as a hi class.
2162 case Memory:
2163 case X87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002164 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002165
2166 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00002167 case NoClass:
2168 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002169
Chris Lattner3db4dde2010-09-01 00:20:33 +00002170 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002171 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002172 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2173 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002174 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00002175 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002176 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002177 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2178 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002179 break;
2180
2181 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002182 // is passed in the next available eightbyte chunk if the last used
2183 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00002184 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002185 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00002186 case SSEUp:
2187 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002188 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00002189 break;
2190
2191 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
2192 // returned together with the previous X87 value in %st0.
2193 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002194 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00002195 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002196 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00002197 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00002198 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002199 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002200 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2201 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00002202 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002203 break;
2204 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002205
Chris Lattner3db4dde2010-09-01 00:20:33 +00002206 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00002207 // known to pass in the high eightbyte of the result. We do this by forming a
2208 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00002209 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002210 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Chris Lattner519f68c2010-07-28 23:06:14 +00002211
Chris Lattnereb518b42010-07-29 21:42:50 +00002212 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00002213}
2214
Daniel Dunbaredfac032012-03-10 01:03:58 +00002215ABIArgInfo X86_64ABIInfo::classifyArgumentType(
Eli Friedman7a1b5862013-06-12 00:13:45 +00002216 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE,
2217 bool isNamedArg)
Daniel Dunbaredfac032012-03-10 01:03:58 +00002218 const
2219{
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002220 X86_64ABIInfo::Class Lo, Hi;
Eli Friedman7a1b5862013-06-12 00:13:45 +00002221 classify(Ty, 0, Lo, Hi, isNamedArg);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002222
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002223 // Check some invariants.
2224 // FIXME: Enforce these by construction.
2225 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002226 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2227
2228 neededInt = 0;
2229 neededSSE = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002230 llvm::Type *ResType = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002231 switch (Lo) {
2232 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002233 if (Hi == NoClass)
2234 return ABIArgInfo::getIgnore();
2235 // If the low part is just padding, it takes no register, leave ResType
2236 // null.
2237 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2238 "Unknown missing lo part");
2239 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002240
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002241 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
2242 // on the stack.
2243 case Memory:
2244
2245 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
2246 // COMPLEX_X87, it is passed in memory.
2247 case X87:
2248 case ComplexX87:
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002249 if (getRecordArgABI(Ty, CGT) == CGCXXABI::RAA_Indirect)
Eli Friedmanded137f2011-06-29 07:04:55 +00002250 ++neededInt;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002251 return getIndirectResult(Ty, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002252
2253 case SSEUp:
2254 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002255 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002256
2257 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
2258 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
2259 // and %r9 is used.
2260 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00002261 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002262
Chris Lattner49382de2010-07-28 22:44:07 +00002263 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002264 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00002265
2266 // If we have a sign or zero extended integer, make sure to return Extend
2267 // so that the parameter gets the right LLVM IR attributes.
2268 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2269 // Treat an enum type as its underlying type.
2270 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2271 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002272
Chris Lattnereb518b42010-07-29 21:42:50 +00002273 if (Ty->isIntegralOrEnumerationType() &&
2274 Ty->isPromotableIntegerType())
2275 return ABIArgInfo::getExtend();
2276 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002277
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002278 break;
2279
2280 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
2281 // available SSE register is used, the registers are taken in the
2282 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00002283 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002284 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00002285 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00002286 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002287 break;
2288 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00002289 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002290
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002291 llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002292 switch (Hi) {
2293 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002294 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002295 // which is passed in memory.
2296 case Memory:
2297 case X87:
2298 case ComplexX87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002299 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002300
2301 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002302
Chris Lattner645406a2010-09-01 00:24:35 +00002303 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002304 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00002305 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002306 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002307
Chris Lattner645406a2010-09-01 00:24:35 +00002308 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2309 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002310 break;
2311
2312 // X87Up generally doesn't occur here (long double is passed in
2313 // memory), except in situations involving unions.
2314 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00002315 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002316 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002317
Chris Lattner645406a2010-09-01 00:24:35 +00002318 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2319 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00002320
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002321 ++neededSSE;
2322 break;
2323
2324 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
2325 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002326 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002327 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00002328 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002329 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002330 break;
2331 }
2332
Chris Lattner645406a2010-09-01 00:24:35 +00002333 // If a high part was specified, merge it together with the low part. It is
2334 // known to pass in the high eightbyte of the result. We do this by forming a
2335 // first class struct aggregate with the high and low part: {low, high}
2336 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002337 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002338
Chris Lattnereb518b42010-07-29 21:42:50 +00002339 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002340}
2341
Chris Lattneree5dcd02010-07-29 02:31:05 +00002342void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002343
Chris Lattnera3c109b2010-07-29 02:16:43 +00002344 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002345
2346 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00002347 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002348
2349 // If the return value is indirect, then the hidden argument is consuming one
2350 // integer register.
2351 if (FI.getReturnInfo().isIndirect())
2352 --freeIntRegs;
2353
Eli Friedman7a1b5862013-06-12 00:13:45 +00002354 bool isVariadic = FI.isVariadic();
2355 unsigned numRequiredArgs = 0;
2356 if (isVariadic)
2357 numRequiredArgs = FI.getRequiredArgs().getNumRequiredArgs();
2358
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002359 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2360 // get assigned (in left-to-right order) for passing as follows...
2361 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2362 it != ie; ++it) {
Eli Friedman7a1b5862013-06-12 00:13:45 +00002363 bool isNamedArg = true;
2364 if (isVariadic)
Aaron Ballmaneba7d2f2013-06-12 15:03:45 +00002365 isNamedArg = (it - FI.arg_begin()) <
2366 static_cast<signed>(numRequiredArgs);
Eli Friedman7a1b5862013-06-12 00:13:45 +00002367
Bill Wendling99aaae82010-10-18 23:51:38 +00002368 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002369 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
Eli Friedman7a1b5862013-06-12 00:13:45 +00002370 neededSSE, isNamedArg);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002371
2372 // AMD64-ABI 3.2.3p3: If there are no registers available for any
2373 // eightbyte of an argument, the whole argument is passed on the
2374 // stack. If registers have already been assigned for some
2375 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00002376 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002377 freeIntRegs -= neededInt;
2378 freeSSERegs -= neededSSE;
2379 } else {
Daniel Dunbaredfac032012-03-10 01:03:58 +00002380 it->info = getIndirectResult(it->type, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002381 }
2382 }
2383}
2384
2385static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2386 QualType Ty,
2387 CodeGenFunction &CGF) {
2388 llvm::Value *overflow_arg_area_p =
2389 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2390 llvm::Value *overflow_arg_area =
2391 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2392
2393 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2394 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedman8d2fe422011-11-18 02:44:19 +00002395 // It isn't stated explicitly in the standard, but in practice we use
2396 // alignment greater than 16 where necessary.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002397 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2398 if (Align > 8) {
Eli Friedman8d2fe422011-11-18 02:44:19 +00002399 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
Owen Anderson0032b272009-08-13 21:57:51 +00002400 llvm::Value *Offset =
Eli Friedman8d2fe422011-11-18 02:44:19 +00002401 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002402 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2403 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00002404 CGF.Int64Ty);
Eli Friedman8d2fe422011-11-18 02:44:19 +00002405 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002406 overflow_arg_area =
2407 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2408 overflow_arg_area->getType(),
2409 "overflow_arg_area.align");
2410 }
2411
2412 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002413 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002414 llvm::Value *Res =
2415 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002416 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002417
2418 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2419 // l->overflow_arg_area + sizeof(type).
2420 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2421 // an 8 byte boundary.
2422
2423 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00002424 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00002425 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002426 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2427 "overflow_arg_area.next");
2428 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2429
2430 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2431 return Res;
2432}
2433
2434llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2435 CodeGenFunction &CGF) const {
2436 // Assume that va_list type is correct; should be pointer to LLVM type:
2437 // struct {
2438 // i32 gp_offset;
2439 // i32 fp_offset;
2440 // i8* overflow_arg_area;
2441 // i8* reg_save_area;
2442 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00002443 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002444
Chris Lattnera14db752010-03-11 18:19:55 +00002445 Ty = CGF.getContext().getCanonicalType(Ty);
Eli Friedman7a1b5862013-06-12 00:13:45 +00002446 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE,
2447 /*isNamedArg*/false);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002448
2449 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2450 // in the registers. If not go to step 7.
2451 if (!neededInt && !neededSSE)
2452 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2453
2454 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2455 // general purpose registers needed to pass type and num_fp to hold
2456 // the number of floating point registers needed.
2457
2458 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2459 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2460 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2461 //
2462 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2463 // register save space).
2464
2465 llvm::Value *InRegs = 0;
2466 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2467 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2468 if (neededInt) {
2469 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2470 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002471 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2472 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002473 }
2474
2475 if (neededSSE) {
2476 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2477 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2478 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00002479 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2480 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002481 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2482 }
2483
2484 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2485 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2486 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2487 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2488
2489 // Emit code to load the value if it was passed in registers.
2490
2491 CGF.EmitBlock(InRegBlock);
2492
2493 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2494 // an offset of l->gp_offset and/or l->fp_offset. This may require
2495 // copying to a temporary location in case the parameter is passed
2496 // in different register classes or requires an alignment greater
2497 // than 8 for general purpose registers and 16 for XMM registers.
2498 //
2499 // FIXME: This really results in shameful code when we end up needing to
2500 // collect arguments from different places; often what should result in a
2501 // simple assembling of a structure from scattered addresses has many more
2502 // loads than necessary. Can we clean this up?
Chris Lattner2acc6e32011-07-18 04:24:23 +00002503 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002504 llvm::Value *RegAddr =
2505 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2506 "reg_save_area");
2507 if (neededInt && neededSSE) {
2508 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002509 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002510 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Eli Friedmaneeb00622013-06-07 23:20:55 +00002511 llvm::Value *Tmp = CGF.CreateMemTemp(Ty);
2512 Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002513 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002514 llvm::Type *TyLo = ST->getElementType(0);
2515 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002516 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002517 "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002518 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2519 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002520 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2521 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002522 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2523 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002524 llvm::Value *V =
2525 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2526 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2527 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2528 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2529
Owen Andersona1cf15f2009-07-14 23:10:40 +00002530 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002531 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002532 } else if (neededInt) {
2533 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2534 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002535 llvm::PointerType::getUnqual(LTy));
Eli Friedmaneeb00622013-06-07 23:20:55 +00002536
2537 // Copy to a temporary if necessary to ensure the appropriate alignment.
2538 std::pair<CharUnits, CharUnits> SizeAlign =
2539 CGF.getContext().getTypeInfoInChars(Ty);
2540 uint64_t TySize = SizeAlign.first.getQuantity();
2541 unsigned TyAlign = SizeAlign.second.getQuantity();
2542 if (TyAlign > 8) {
Eli Friedmaneeb00622013-06-07 23:20:55 +00002543 llvm::Value *Tmp = CGF.CreateMemTemp(Ty);
2544 CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, 8, false);
2545 RegAddr = Tmp;
2546 }
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002547 } else if (neededSSE == 1) {
2548 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2549 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2550 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002551 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002552 assert(neededSSE == 2 && "Invalid number of needed registers!");
2553 // SSE registers are spaced 16 bytes apart in the register save
2554 // area, we need to collect the two eightbytes together.
2555 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002556 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner8b418682012-02-07 00:39:47 +00002557 llvm::Type *DoubleTy = CGF.DoubleTy;
Chris Lattner2acc6e32011-07-18 04:24:23 +00002558 llvm::Type *DblPtrTy =
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002559 llvm::PointerType::getUnqual(DoubleTy);
Eli Friedmaneeb00622013-06-07 23:20:55 +00002560 llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, NULL);
2561 llvm::Value *V, *Tmp = CGF.CreateMemTemp(Ty);
2562 Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo());
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002563 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2564 DblPtrTy));
2565 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2566 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2567 DblPtrTy));
2568 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2569 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2570 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002571 }
2572
2573 // AMD64-ABI 3.5.7p5: Step 5. Set:
2574 // l->gp_offset = l->gp_offset + num_gp * 8
2575 // l->fp_offset = l->fp_offset + num_fp * 16.
2576 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002577 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002578 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2579 gp_offset_p);
2580 }
2581 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002582 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002583 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2584 fp_offset_p);
2585 }
2586 CGF.EmitBranch(ContBlock);
2587
2588 // Emit code to load the value if it was passed in memory.
2589
2590 CGF.EmitBlock(InMemBlock);
2591 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2592
2593 // Return the appropriate result.
2594
2595 CGF.EmitBlock(ContBlock);
Jay Foadbbf3bac2011-03-30 11:28:58 +00002596 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002597 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002598 ResAddr->addIncoming(RegAddr, InRegBlock);
2599 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002600 return ResAddr;
2601}
2602
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002603ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, bool IsReturnType) const {
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002604
2605 if (Ty->isVoidType())
2606 return ABIArgInfo::getIgnore();
2607
2608 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2609 Ty = EnumTy->getDecl()->getIntegerType();
2610
2611 uint64_t Size = getContext().getTypeSize(Ty);
2612
2613 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002614 if (IsReturnType) {
2615 if (isRecordReturnIndirect(RT, CGT))
2616 return ABIArgInfo::getIndirect(0, false);
2617 } else {
2618 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, CGT))
2619 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
2620 }
2621
2622 if (RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002623 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2624
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002625 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
John McCall64aa4b32013-04-16 22:48:15 +00002626 if (Size == 128 && getTarget().getTriple().getOS() == llvm::Triple::MinGW32)
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002627 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2628 Size));
2629
2630 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2631 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2632 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002633 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002634 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2635 Size));
2636
2637 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2638 }
2639
2640 if (Ty->isPromotableIntegerType())
2641 return ABIArgInfo::getExtend();
2642
2643 return ABIArgInfo::getDirect();
2644}
2645
2646void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2647
2648 QualType RetTy = FI.getReturnType();
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002649 FI.getReturnInfo() = classify(RetTy, true);
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002650
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002651 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2652 it != ie; ++it)
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002653 it->info = classify(it->type, false);
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002654}
2655
Chris Lattnerf13721d2010-08-31 16:44:54 +00002656llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2657 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002658 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002659
Chris Lattnerf13721d2010-08-31 16:44:54 +00002660 CGBuilderTy &Builder = CGF.Builder;
2661 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2662 "ap");
2663 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2664 llvm::Type *PTy =
2665 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2666 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2667
2668 uint64_t Offset =
2669 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2670 llvm::Value *NextAddr =
2671 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2672 "ap.next");
2673 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2674
2675 return AddrTyped;
2676}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002677
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00002678namespace {
2679
Derek Schuff263366f2012-10-16 22:30:41 +00002680class NaClX86_64ABIInfo : public ABIInfo {
2681 public:
2682 NaClX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2683 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, HasAVX) {}
2684 virtual void computeInfo(CGFunctionInfo &FI) const;
2685 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2686 CodeGenFunction &CGF) const;
2687 private:
2688 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
2689 X86_64ABIInfo NInfo; // Used for everything else.
2690};
2691
2692class NaClX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2693 public:
2694 NaClX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2695 : TargetCodeGenInfo(new NaClX86_64ABIInfo(CGT, HasAVX)) {}
2696};
2697
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00002698}
2699
Derek Schuff263366f2012-10-16 22:30:41 +00002700void NaClX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2701 if (FI.getASTCallingConvention() == CC_PnaclCall)
2702 PInfo.computeInfo(FI);
2703 else
2704 NInfo.computeInfo(FI);
2705}
2706
2707llvm::Value *NaClX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2708 CodeGenFunction &CGF) const {
2709 // Always use the native convention; calling pnacl-style varargs functions
2710 // is unuspported.
2711 return NInfo.EmitVAArg(VAListAddr, Ty, CGF);
2712}
2713
2714
John McCallec853ba2010-03-11 00:10:12 +00002715// PowerPC-32
2716
2717namespace {
2718class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2719public:
Chris Lattnerea044322010-07-29 02:01:43 +00002720 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002721
John McCallec853ba2010-03-11 00:10:12 +00002722 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2723 // This is recovered from gcc output.
2724 return 1; // r1 is the dedicated stack pointer
2725 }
2726
2727 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002728 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002729};
2730
2731}
2732
2733bool
2734PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2735 llvm::Value *Address) const {
2736 // This is calculated from the LLVM and GCC tables and verified
2737 // against gcc output. AFAIK all ABIs use the same encoding.
2738
2739 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallec853ba2010-03-11 00:10:12 +00002740
Chris Lattner8b418682012-02-07 00:39:47 +00002741 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallec853ba2010-03-11 00:10:12 +00002742 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2743 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2744 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2745
2746 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002747 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002748
2749 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002750 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002751
2752 // 64-76 are various 4-byte special-purpose registers:
2753 // 64: mq
2754 // 65: lr
2755 // 66: ctr
2756 // 67: ap
2757 // 68-75 cr0-7
2758 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002759 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002760
2761 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002762 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002763
2764 // 109: vrsave
2765 // 110: vscr
2766 // 111: spe_acc
2767 // 112: spefscr
2768 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002769 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002770
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002771 return false;
John McCallec853ba2010-03-11 00:10:12 +00002772}
2773
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002774// PowerPC-64
2775
2776namespace {
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002777/// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
2778class PPC64_SVR4_ABIInfo : public DefaultABIInfo {
2779
2780public:
2781 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
2782
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002783 bool isPromotableTypeForABI(QualType Ty) const;
2784
2785 ABIArgInfo classifyReturnType(QualType RetTy) const;
2786 ABIArgInfo classifyArgumentType(QualType Ty) const;
2787
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00002788 // TODO: We can add more logic to computeInfo to improve performance.
2789 // Example: For aggregate arguments that fit in a register, we could
2790 // use getDirectInReg (as is done below for structs containing a single
2791 // floating-point value) to avoid pushing them to memory on function
2792 // entry. This would require changing the logic in PPCISelLowering
2793 // when lowering the parameters in the caller and args in the callee.
2794 virtual void computeInfo(CGFunctionInfo &FI) const {
2795 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2796 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2797 it != ie; ++it) {
2798 // We rely on the default argument classification for the most part.
2799 // One exception: An aggregate containing a single floating-point
Bill Schmidtb1993102013-07-23 22:15:57 +00002800 // or vector item must be passed in a register if one is available.
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00002801 const Type *T = isSingleElementStruct(it->type, getContext());
2802 if (T) {
2803 const BuiltinType *BT = T->getAs<BuiltinType>();
Bill Schmidtb1993102013-07-23 22:15:57 +00002804 if (T->isVectorType() || (BT && BT->isFloatingPoint())) {
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00002805 QualType QT(T, 0);
2806 it->info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
2807 continue;
2808 }
2809 }
2810 it->info = classifyArgumentType(it->type);
2811 }
2812 }
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002813
2814 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr,
2815 QualType Ty,
2816 CodeGenFunction &CGF) const;
2817};
2818
2819class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
2820public:
2821 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT)
2822 : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT)) {}
2823
2824 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2825 // This is recovered from gcc output.
2826 return 1; // r1 is the dedicated stack pointer
2827 }
2828
2829 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2830 llvm::Value *Address) const;
2831};
2832
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002833class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2834public:
2835 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
2836
2837 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2838 // This is recovered from gcc output.
2839 return 1; // r1 is the dedicated stack pointer
2840 }
2841
2842 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2843 llvm::Value *Address) const;
2844};
2845
2846}
2847
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002848// Return true if the ABI requires Ty to be passed sign- or zero-
2849// extended to 64 bits.
2850bool
2851PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
2852 // Treat an enum type as its underlying type.
2853 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2854 Ty = EnumTy->getDecl()->getIntegerType();
2855
2856 // Promotable integer types are required to be promoted by the ABI.
2857 if (Ty->isPromotableIntegerType())
2858 return true;
2859
2860 // In addition to the usual promotable integer types, we also need to
2861 // extend all 32-bit types, since the ABI requires promotion to 64 bits.
2862 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2863 switch (BT->getKind()) {
2864 case BuiltinType::Int:
2865 case BuiltinType::UInt:
2866 return true;
2867 default:
2868 break;
2869 }
2870
2871 return false;
2872}
2873
2874ABIArgInfo
2875PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
Bill Schmidtc9715fc2012-11-27 02:46:43 +00002876 if (Ty->isAnyComplexType())
2877 return ABIArgInfo::getDirect();
2878
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002879 if (isAggregateTypeForABI(Ty)) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002880 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
2881 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002882
2883 return ABIArgInfo::getIndirect(0);
2884 }
2885
2886 return (isPromotableTypeForABI(Ty) ?
2887 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2888}
2889
2890ABIArgInfo
2891PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
2892 if (RetTy->isVoidType())
2893 return ABIArgInfo::getIgnore();
2894
Bill Schmidt9e6111a2012-12-17 04:20:17 +00002895 if (RetTy->isAnyComplexType())
2896 return ABIArgInfo::getDirect();
2897
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002898 if (isAggregateTypeForABI(RetTy))
2899 return ABIArgInfo::getIndirect(0);
2900
2901 return (isPromotableTypeForABI(RetTy) ?
2902 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2903}
2904
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002905// Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
2906llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr,
2907 QualType Ty,
2908 CodeGenFunction &CGF) const {
2909 llvm::Type *BP = CGF.Int8PtrTy;
2910 llvm::Type *BPP = CGF.Int8PtrPtrTy;
2911
2912 CGBuilderTy &Builder = CGF.Builder;
2913 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
2914 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2915
Bill Schmidt19f8e852013-01-14 17:45:36 +00002916 // Update the va_list pointer. The pointer should be bumped by the
2917 // size of the object. We can trust getTypeSize() except for a complex
2918 // type whose base type is smaller than a doubleword. For these, the
2919 // size of the object is 16 bytes; see below for further explanation.
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002920 unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8;
Bill Schmidt19f8e852013-01-14 17:45:36 +00002921 QualType BaseTy;
2922 unsigned CplxBaseSize = 0;
2923
2924 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
2925 BaseTy = CTy->getElementType();
2926 CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8;
2927 if (CplxBaseSize < 8)
2928 SizeInBytes = 16;
2929 }
2930
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002931 unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8);
2932 llvm::Value *NextAddr =
2933 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset),
2934 "ap.next");
2935 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2936
Bill Schmidt19f8e852013-01-14 17:45:36 +00002937 // If we have a complex type and the base type is smaller than 8 bytes,
2938 // the ABI calls for the real and imaginary parts to be right-adjusted
2939 // in separate doublewords. However, Clang expects us to produce a
2940 // pointer to a structure with the two parts packed tightly. So generate
2941 // loads of the real and imaginary parts relative to the va_list pointer,
2942 // and store them to a temporary structure.
2943 if (CplxBaseSize && CplxBaseSize < 8) {
2944 llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2945 llvm::Value *ImagAddr = RealAddr;
2946 RealAddr = Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize));
2947 ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize));
2948 llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy));
2949 RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy);
2950 ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy);
2951 llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal");
2952 llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag");
2953 llvm::Value *Ptr = CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty),
2954 "vacplx");
2955 llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, ".real");
2956 llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, ".imag");
2957 Builder.CreateStore(Real, RealPtr, false);
2958 Builder.CreateStore(Imag, ImagPtr, false);
2959 return Ptr;
2960 }
2961
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002962 // If the argument is smaller than 8 bytes, it is right-adjusted in
2963 // its doubleword slot. Adjust the pointer to pick it up from the
2964 // correct offset.
2965 if (SizeInBytes < 8) {
2966 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2967 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes));
2968 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2969 }
2970
2971 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2972 return Builder.CreateBitCast(Addr, PTy);
2973}
2974
2975static bool
2976PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2977 llvm::Value *Address) {
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002978 // This is calculated from the LLVM and GCC tables and verified
2979 // against gcc output. AFAIK all ABIs use the same encoding.
2980
2981 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2982
2983 llvm::IntegerType *i8 = CGF.Int8Ty;
2984 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2985 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2986 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2987
2988 // 0-31: r0-31, the 8-byte general-purpose registers
2989 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
2990
2991 // 32-63: fp0-31, the 8-byte floating-point registers
2992 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
2993
2994 // 64-76 are various 4-byte special-purpose registers:
2995 // 64: mq
2996 // 65: lr
2997 // 66: ctr
2998 // 67: ap
2999 // 68-75 cr0-7
3000 // 76: xer
3001 AssignToArrayRange(Builder, Address, Four8, 64, 76);
3002
3003 // 77-108: v0-31, the 16-byte vector registers
3004 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
3005
3006 // 109: vrsave
3007 // 110: vscr
3008 // 111: spe_acc
3009 // 112: spefscr
3010 // 113: sfp
3011 AssignToArrayRange(Builder, Address, Four8, 109, 113);
3012
3013 return false;
3014}
John McCallec853ba2010-03-11 00:10:12 +00003015
Bill Schmidt2fc107f2012-10-03 19:18:57 +00003016bool
3017PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
3018 CodeGen::CodeGenFunction &CGF,
3019 llvm::Value *Address) const {
3020
3021 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
3022}
3023
3024bool
3025PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3026 llvm::Value *Address) const {
3027
3028 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
3029}
3030
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003031//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003032// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003033//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003034
3035namespace {
3036
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003037class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003038public:
3039 enum ABIKind {
3040 APCS = 0,
3041 AAPCS = 1,
3042 AAPCS_VFP
3043 };
3044
3045private:
3046 ABIKind Kind;
3047
3048public:
John McCallbd7370a2013-02-28 19:01:20 +00003049 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {
3050 setRuntimeCC();
3051 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003052
John McCall49e34be2011-08-30 01:42:09 +00003053 bool isEABI() const {
John McCall64aa4b32013-04-16 22:48:15 +00003054 StringRef Env = getTarget().getTriple().getEnvironmentName();
Logan Chien94a71422012-09-02 09:30:11 +00003055 return (Env == "gnueabi" || Env == "eabi" ||
3056 Env == "android" || Env == "androideabi");
John McCall49e34be2011-08-30 01:42:09 +00003057 }
3058
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003059private:
3060 ABIKind getABIKind() const { return Kind; }
3061
Chris Lattnera3c109b2010-07-29 02:16:43 +00003062 ABIArgInfo classifyReturnType(QualType RetTy) const;
Manman Ren710c5172012-10-31 19:02:26 +00003063 ABIArgInfo classifyArgumentType(QualType RetTy, int *VFPRegs,
3064 unsigned &AllocatedVFP,
Manman Renb3fa55f2012-10-30 23:21:41 +00003065 bool &IsHA) const;
Manman Ren97f81572012-10-16 19:18:39 +00003066 bool isIllegalVectorType(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003067
Chris Lattneree5dcd02010-07-29 02:31:05 +00003068 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003069
3070 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3071 CodeGenFunction &CGF) const;
John McCallbd7370a2013-02-28 19:01:20 +00003072
3073 llvm::CallingConv::ID getLLVMDefaultCC() const;
3074 llvm::CallingConv::ID getABIDefaultCC() const;
3075 void setRuntimeCC();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003076};
3077
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003078class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
3079public:
Chris Lattnerea044322010-07-29 02:01:43 +00003080 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
3081 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00003082
John McCall49e34be2011-08-30 01:42:09 +00003083 const ARMABIInfo &getABIInfo() const {
3084 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
3085 }
3086
John McCall6374c332010-03-06 00:35:14 +00003087 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3088 return 13;
3089 }
Roman Divacky09345d12011-05-18 19:36:54 +00003090
Chris Lattner5f9e2722011-07-23 10:55:15 +00003091 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCallf85e1932011-06-15 23:02:42 +00003092 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
3093 }
3094
Roman Divacky09345d12011-05-18 19:36:54 +00003095 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3096 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003097 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divacky09345d12011-05-18 19:36:54 +00003098
3099 // 0-15 are the 16 integer registers.
Chris Lattner8b418682012-02-07 00:39:47 +00003100 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divacky09345d12011-05-18 19:36:54 +00003101 return false;
3102 }
John McCall49e34be2011-08-30 01:42:09 +00003103
3104 unsigned getSizeOfUnwindException() const {
3105 if (getABIInfo().isEABI()) return 88;
3106 return TargetCodeGenInfo::getSizeOfUnwindException();
3107 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003108};
3109
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003110}
3111
Chris Lattneree5dcd02010-07-29 02:31:05 +00003112void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Manman Renb3fa55f2012-10-30 23:21:41 +00003113 // To correctly handle Homogeneous Aggregate, we need to keep track of the
Manman Ren710c5172012-10-31 19:02:26 +00003114 // VFP registers allocated so far.
Manman Renb3fa55f2012-10-30 23:21:41 +00003115 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
3116 // VFP registers of the appropriate type unallocated then the argument is
3117 // allocated to the lowest-numbered sequence of such registers.
3118 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
3119 // unallocated are marked as unavailable.
3120 unsigned AllocatedVFP = 0;
Manman Ren710c5172012-10-31 19:02:26 +00003121 int VFPRegs[16] = { 0 };
Chris Lattnera3c109b2010-07-29 02:16:43 +00003122 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003123 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Manman Renb3fa55f2012-10-30 23:21:41 +00003124 it != ie; ++it) {
3125 unsigned PreAllocation = AllocatedVFP;
3126 bool IsHA = false;
3127 // 6.1.2.3 There is one VFP co-processor register class using registers
3128 // s0-s15 (d0-d7) for passing arguments.
3129 const unsigned NumVFPs = 16;
Manman Ren710c5172012-10-31 19:02:26 +00003130 it->info = classifyArgumentType(it->type, VFPRegs, AllocatedVFP, IsHA);
Manman Renb3fa55f2012-10-30 23:21:41 +00003131 // If we do not have enough VFP registers for the HA, any VFP registers
3132 // that are unallocated are marked as unavailable. To achieve this, we add
3133 // padding of (NumVFPs - PreAllocation) floats.
3134 if (IsHA && AllocatedVFP > NumVFPs && PreAllocation < NumVFPs) {
3135 llvm::Type *PaddingTy = llvm::ArrayType::get(
3136 llvm::Type::getFloatTy(getVMContext()), NumVFPs - PreAllocation);
3137 it->info = ABIArgInfo::getExpandWithPadding(false, PaddingTy);
3138 }
3139 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003140
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003141 // Always honor user-specified calling convention.
3142 if (FI.getCallingConvention() != llvm::CallingConv::C)
3143 return;
3144
John McCallbd7370a2013-02-28 19:01:20 +00003145 llvm::CallingConv::ID cc = getRuntimeCC();
3146 if (cc != llvm::CallingConv::C)
3147 FI.setEffectiveCallingConvention(cc);
3148}
Rafael Espindola25117ab2010-06-16 16:13:39 +00003149
John McCallbd7370a2013-02-28 19:01:20 +00003150/// Return the default calling convention that LLVM will use.
3151llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
3152 // The default calling convention that LLVM will infer.
John McCall64aa4b32013-04-16 22:48:15 +00003153 if (getTarget().getTriple().getEnvironmentName()=="gnueabihf")
John McCallbd7370a2013-02-28 19:01:20 +00003154 return llvm::CallingConv::ARM_AAPCS_VFP;
3155 else if (isEABI())
3156 return llvm::CallingConv::ARM_AAPCS;
3157 else
3158 return llvm::CallingConv::ARM_APCS;
3159}
3160
3161/// Return the calling convention that our ABI would like us to use
3162/// as the C calling convention.
3163llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003164 switch (getABIKind()) {
John McCallbd7370a2013-02-28 19:01:20 +00003165 case APCS: return llvm::CallingConv::ARM_APCS;
3166 case AAPCS: return llvm::CallingConv::ARM_AAPCS;
3167 case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003168 }
John McCallbd7370a2013-02-28 19:01:20 +00003169 llvm_unreachable("bad ABI kind");
3170}
3171
3172void ARMABIInfo::setRuntimeCC() {
3173 assert(getRuntimeCC() == llvm::CallingConv::C);
3174
3175 // Don't muddy up the IR with a ton of explicit annotations if
3176 // they'd just match what LLVM will infer from the triple.
3177 llvm::CallingConv::ID abiCC = getABIDefaultCC();
3178 if (abiCC != getLLVMDefaultCC())
3179 RuntimeCC = abiCC;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003180}
3181
Bob Wilson194f06a2011-08-03 05:58:22 +00003182/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
3183/// aggregate. If HAMembers is non-null, the number of base elements
3184/// contained in the type is returned through it; this is used for the
3185/// recursive calls that check aggregate component types.
3186static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
3187 ASTContext &Context,
3188 uint64_t *HAMembers = 0) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003189 uint64_t Members = 0;
Bob Wilson194f06a2011-08-03 05:58:22 +00003190 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
3191 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
3192 return false;
3193 Members *= AT->getSize().getZExtValue();
3194 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
3195 const RecordDecl *RD = RT->getDecl();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003196 if (RD->hasFlexibleArrayMember())
Bob Wilson194f06a2011-08-03 05:58:22 +00003197 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003198
Bob Wilson194f06a2011-08-03 05:58:22 +00003199 Members = 0;
3200 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3201 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00003202 const FieldDecl *FD = *i;
Bob Wilson194f06a2011-08-03 05:58:22 +00003203 uint64_t FldMembers;
3204 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
3205 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003206
3207 Members = (RD->isUnion() ?
3208 std::max(Members, FldMembers) : Members + FldMembers);
Bob Wilson194f06a2011-08-03 05:58:22 +00003209 }
3210 } else {
3211 Members = 1;
3212 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
3213 Members = 2;
3214 Ty = CT->getElementType();
3215 }
3216
3217 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
3218 // double, or 64-bit or 128-bit vectors.
3219 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3220 if (BT->getKind() != BuiltinType::Float &&
Tim Northoveradfa45f2012-07-20 22:29:29 +00003221 BT->getKind() != BuiltinType::Double &&
3222 BT->getKind() != BuiltinType::LongDouble)
Bob Wilson194f06a2011-08-03 05:58:22 +00003223 return false;
3224 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
3225 unsigned VecSize = Context.getTypeSize(VT);
3226 if (VecSize != 64 && VecSize != 128)
3227 return false;
3228 } else {
3229 return false;
3230 }
3231
3232 // The base type must be the same for all members. Vector types of the
3233 // same total size are treated as being equivalent here.
3234 const Type *TyPtr = Ty.getTypePtr();
3235 if (!Base)
3236 Base = TyPtr;
3237 if (Base != TyPtr &&
3238 (!Base->isVectorType() || !TyPtr->isVectorType() ||
3239 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
3240 return false;
3241 }
3242
3243 // Homogeneous Aggregates can have at most 4 members of the base type.
3244 if (HAMembers)
3245 *HAMembers = Members;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003246
3247 return (Members > 0 && Members <= 4);
Bob Wilson194f06a2011-08-03 05:58:22 +00003248}
3249
Manman Ren710c5172012-10-31 19:02:26 +00003250/// markAllocatedVFPs - update VFPRegs according to the alignment and
3251/// number of VFP registers (unit is S register) requested.
3252static void markAllocatedVFPs(int *VFPRegs, unsigned &AllocatedVFP,
3253 unsigned Alignment,
3254 unsigned NumRequired) {
3255 // Early Exit.
3256 if (AllocatedVFP >= 16)
3257 return;
3258 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
3259 // VFP registers of the appropriate type unallocated then the argument is
3260 // allocated to the lowest-numbered sequence of such registers.
3261 for (unsigned I = 0; I < 16; I += Alignment) {
3262 bool FoundSlot = true;
3263 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3264 if (J >= 16 || VFPRegs[J]) {
3265 FoundSlot = false;
3266 break;
3267 }
3268 if (FoundSlot) {
3269 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3270 VFPRegs[J] = 1;
3271 AllocatedVFP += NumRequired;
3272 return;
3273 }
3274 }
3275 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
3276 // unallocated are marked as unavailable.
3277 for (unsigned I = 0; I < 16; I++)
3278 VFPRegs[I] = 1;
3279 AllocatedVFP = 17; // We do not have enough VFP registers.
3280}
3281
3282ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, int *VFPRegs,
3283 unsigned &AllocatedVFP,
Manman Renb3fa55f2012-10-30 23:21:41 +00003284 bool &IsHA) const {
3285 // We update number of allocated VFPs according to
3286 // 6.1.2.1 The following argument types are VFP CPRCs:
3287 // A single-precision floating-point type (including promoted
3288 // half-precision types); A double-precision floating-point type;
3289 // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
3290 // with a Base Type of a single- or double-precision floating-point type,
3291 // 64-bit containerized vectors or 128-bit containerized vectors with one
3292 // to four Elements.
3293
Manman Ren97f81572012-10-16 19:18:39 +00003294 // Handle illegal vector types here.
3295 if (isIllegalVectorType(Ty)) {
3296 uint64_t Size = getContext().getTypeSize(Ty);
3297 if (Size <= 32) {
3298 llvm::Type *ResType =
3299 llvm::Type::getInt32Ty(getVMContext());
3300 return ABIArgInfo::getDirect(ResType);
3301 }
3302 if (Size == 64) {
3303 llvm::Type *ResType = llvm::VectorType::get(
3304 llvm::Type::getInt32Ty(getVMContext()), 2);
Manman Ren710c5172012-10-31 19:02:26 +00003305 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
Manman Ren97f81572012-10-16 19:18:39 +00003306 return ABIArgInfo::getDirect(ResType);
3307 }
3308 if (Size == 128) {
3309 llvm::Type *ResType = llvm::VectorType::get(
3310 llvm::Type::getInt32Ty(getVMContext()), 4);
Manman Ren710c5172012-10-31 19:02:26 +00003311 markAllocatedVFPs(VFPRegs, AllocatedVFP, 4, 4);
Manman Ren97f81572012-10-16 19:18:39 +00003312 return ABIArgInfo::getDirect(ResType);
3313 }
3314 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3315 }
Manman Ren710c5172012-10-31 19:02:26 +00003316 // Update VFPRegs for legal vector types.
Manman Renb3fa55f2012-10-30 23:21:41 +00003317 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3318 uint64_t Size = getContext().getTypeSize(VT);
3319 // Size of a legal vector should be power of 2 and above 64.
Manman Ren710c5172012-10-31 19:02:26 +00003320 markAllocatedVFPs(VFPRegs, AllocatedVFP, Size >= 128 ? 4 : 2, Size / 32);
Manman Renb3fa55f2012-10-30 23:21:41 +00003321 }
Manman Ren710c5172012-10-31 19:02:26 +00003322 // Update VFPRegs for floating point types.
Manman Renb3fa55f2012-10-30 23:21:41 +00003323 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3324 if (BT->getKind() == BuiltinType::Half ||
3325 BT->getKind() == BuiltinType::Float)
Manman Ren710c5172012-10-31 19:02:26 +00003326 markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, 1);
Manman Renb3fa55f2012-10-30 23:21:41 +00003327 if (BT->getKind() == BuiltinType::Double ||
Manman Ren710c5172012-10-31 19:02:26 +00003328 BT->getKind() == BuiltinType::LongDouble)
3329 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
Manman Renb3fa55f2012-10-30 23:21:41 +00003330 }
Manman Ren97f81572012-10-16 19:18:39 +00003331
John McCalld608cdb2010-08-22 10:59:02 +00003332 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003333 // Treat an enum type as its underlying type.
3334 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3335 Ty = EnumTy->getDecl()->getIntegerType();
3336
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003337 return (Ty->isPromotableIntegerType() ?
3338 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003339 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003340
Tim Northoverf5c3a252013-06-21 22:49:34 +00003341 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
3342 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
3343
Daniel Dunbar42025572009-09-14 21:54:03 +00003344 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003345 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00003346 return ABIArgInfo::getIgnore();
3347
Bob Wilson194f06a2011-08-03 05:58:22 +00003348 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
Manman Renb3fa55f2012-10-30 23:21:41 +00003349 // Homogeneous Aggregates need to be expanded when we can fit the aggregate
3350 // into VFP registers.
Bob Wilson194f06a2011-08-03 05:58:22 +00003351 const Type *Base = 0;
Manman Renb3fa55f2012-10-30 23:21:41 +00003352 uint64_t Members = 0;
3353 if (isHomogeneousAggregate(Ty, Base, getContext(), &Members)) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003354 assert(Base && "Base class should be set for homogeneous aggregate");
Manman Renb3fa55f2012-10-30 23:21:41 +00003355 // Base can be a floating-point or a vector.
3356 if (Base->isVectorType()) {
3357 // ElementSize is in number of floats.
3358 unsigned ElementSize = getContext().getTypeSize(Base) == 64 ? 2 : 4;
Manman Rencb489dd2012-11-06 19:05:29 +00003359 markAllocatedVFPs(VFPRegs, AllocatedVFP, ElementSize,
3360 Members * ElementSize);
Manman Renb3fa55f2012-10-30 23:21:41 +00003361 } else if (Base->isSpecificBuiltinType(BuiltinType::Float))
Manman Ren710c5172012-10-31 19:02:26 +00003362 markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, Members);
Manman Renb3fa55f2012-10-30 23:21:41 +00003363 else {
3364 assert(Base->isSpecificBuiltinType(BuiltinType::Double) ||
3365 Base->isSpecificBuiltinType(BuiltinType::LongDouble));
Manman Ren710c5172012-10-31 19:02:26 +00003366 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, Members * 2);
Manman Renb3fa55f2012-10-30 23:21:41 +00003367 }
3368 IsHA = true;
Bob Wilson194f06a2011-08-03 05:58:22 +00003369 return ABIArgInfo::getExpand();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003370 }
Bob Wilson194f06a2011-08-03 05:58:22 +00003371 }
3372
Manman Ren634b3d22012-08-13 21:23:55 +00003373 // Support byval for ARM.
Manman Rencb489dd2012-11-06 19:05:29 +00003374 // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
3375 // most 8-byte. We realign the indirect argument if type alignment is bigger
3376 // than ABI alignment.
Manman Renfd1ba912012-11-05 22:42:46 +00003377 uint64_t ABIAlign = 4;
3378 uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8;
3379 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3380 getABIKind() == ARMABIInfo::AAPCS)
3381 ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
Manman Ren885ad692012-11-06 04:58:01 +00003382 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
3383 return ABIArgInfo::getIndirect(0, /*ByVal=*/true,
Manman Rencb489dd2012-11-06 19:05:29 +00003384 /*Realign=*/TyAlign > ABIAlign);
Eli Friedman79f30982012-08-09 00:31:40 +00003385 }
3386
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00003387 // Otherwise, pass by coercing to a structure of the appropriate size.
Chris Lattner2acc6e32011-07-18 04:24:23 +00003388 llvm::Type* ElemTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003389 unsigned SizeRegs;
Eli Friedman79f30982012-08-09 00:31:40 +00003390 // FIXME: Try to match the types of the arguments more accurately where
3391 // we can.
3392 if (getContext().getTypeAlign(Ty) <= 32) {
Bob Wilson53fc1a62011-08-01 23:39:04 +00003393 ElemTy = llvm::Type::getInt32Ty(getVMContext());
3394 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Manman Ren78eb76e2012-06-25 22:04:00 +00003395 } else {
Manman Ren78eb76e2012-06-25 22:04:00 +00003396 ElemTy = llvm::Type::getInt64Ty(getVMContext());
3397 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastings67d097e2011-04-27 17:24:02 +00003398 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003399
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003400 llvm::Type *STy =
Chris Lattner7650d952011-06-18 22:49:11 +00003401 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003402 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003403}
3404
Chris Lattnera3c109b2010-07-29 02:16:43 +00003405static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00003406 llvm::LLVMContext &VMContext) {
3407 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
3408 // is called integer-like if its size is less than or equal to one word, and
3409 // the offset of each of its addressable sub-fields is zero.
3410
3411 uint64_t Size = Context.getTypeSize(Ty);
3412
3413 // Check that the type fits in a word.
3414 if (Size > 32)
3415 return false;
3416
3417 // FIXME: Handle vector types!
3418 if (Ty->isVectorType())
3419 return false;
3420
Daniel Dunbarb0d58192009-09-14 02:20:34 +00003421 // Float types are never treated as "integer like".
3422 if (Ty->isRealFloatingType())
3423 return false;
3424
Daniel Dunbar98303b92009-09-13 08:03:58 +00003425 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00003426 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00003427 return true;
3428
Daniel Dunbar45815812010-02-01 23:31:26 +00003429 // Small complex integer types are "integer like".
3430 if (const ComplexType *CT = Ty->getAs<ComplexType>())
3431 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003432
3433 // Single element and zero sized arrays should be allowed, by the definition
3434 // above, but they are not.
3435
3436 // Otherwise, it must be a record type.
3437 const RecordType *RT = Ty->getAs<RecordType>();
3438 if (!RT) return false;
3439
3440 // Ignore records with flexible arrays.
3441 const RecordDecl *RD = RT->getDecl();
3442 if (RD->hasFlexibleArrayMember())
3443 return false;
3444
3445 // Check that all sub-fields are at offset 0, and are themselves "integer
3446 // like".
3447 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3448
3449 bool HadField = false;
3450 unsigned idx = 0;
3451 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3452 i != e; ++i, ++idx) {
David Blaikie581deb32012-06-06 20:45:41 +00003453 const FieldDecl *FD = *i;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003454
Daniel Dunbar679855a2010-01-29 03:22:29 +00003455 // Bit-fields are not addressable, we only need to verify they are "integer
3456 // like". We still have to disallow a subsequent non-bitfield, for example:
3457 // struct { int : 0; int x }
3458 // is non-integer like according to gcc.
3459 if (FD->isBitField()) {
3460 if (!RD->isUnion())
3461 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003462
Daniel Dunbar679855a2010-01-29 03:22:29 +00003463 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3464 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003465
Daniel Dunbar679855a2010-01-29 03:22:29 +00003466 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003467 }
3468
Daniel Dunbar679855a2010-01-29 03:22:29 +00003469 // Check if this field is at offset 0.
3470 if (Layout.getFieldOffset(idx) != 0)
3471 return false;
3472
Daniel Dunbar98303b92009-09-13 08:03:58 +00003473 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3474 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003475
Daniel Dunbar679855a2010-01-29 03:22:29 +00003476 // Only allow at most one field in a structure. This doesn't match the
3477 // wording above, but follows gcc in situations with a field following an
3478 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00003479 if (!RD->isUnion()) {
3480 if (HadField)
3481 return false;
3482
3483 HadField = true;
3484 }
3485 }
3486
3487 return true;
3488}
3489
Chris Lattnera3c109b2010-07-29 02:16:43 +00003490ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003491 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003492 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00003493
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00003494 // Large vector types should be returned via memory.
3495 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
3496 return ABIArgInfo::getIndirect(0);
3497
John McCalld608cdb2010-08-22 10:59:02 +00003498 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003499 // Treat an enum type as its underlying type.
3500 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3501 RetTy = EnumTy->getDecl()->getIntegerType();
3502
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003503 return (RetTy->isPromotableIntegerType() ?
3504 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003505 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003506
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003507 // Structures with either a non-trivial destructor or a non-trivial
3508 // copy constructor are always indirect.
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00003509 if (isRecordReturnIndirect(RetTy, CGT))
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003510 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3511
Daniel Dunbar98303b92009-09-13 08:03:58 +00003512 // Are we following APCS?
3513 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00003514 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00003515 return ABIArgInfo::getIgnore();
3516
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003517 // Complex types are all returned as packed integers.
3518 //
3519 // FIXME: Consider using 2 x vector types if the back end handles them
3520 // correctly.
3521 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00003522 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00003523 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003524
Daniel Dunbar98303b92009-09-13 08:03:58 +00003525 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003526 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003527 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003528 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003529 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003530 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003531 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003532 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3533 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003534 }
3535
3536 // Otherwise return in memory.
3537 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003538 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003539
3540 // Otherwise this is an AAPCS variant.
3541
Chris Lattnera3c109b2010-07-29 02:16:43 +00003542 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00003543 return ABIArgInfo::getIgnore();
3544
Bob Wilson3b694fa2011-11-02 04:51:36 +00003545 // Check for homogeneous aggregates with AAPCS-VFP.
3546 if (getABIKind() == AAPCS_VFP) {
3547 const Type *Base = 0;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003548 if (isHomogeneousAggregate(RetTy, Base, getContext())) {
3549 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson3b694fa2011-11-02 04:51:36 +00003550 // Homogeneous Aggregates are returned directly.
3551 return ABIArgInfo::getDirect();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003552 }
Bob Wilson3b694fa2011-11-02 04:51:36 +00003553 }
3554
Daniel Dunbar98303b92009-09-13 08:03:58 +00003555 // Aggregates <= 4 bytes are returned in r0; other aggregates
3556 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003557 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00003558 if (Size <= 32) {
3559 // Return in the smallest viable integer type.
3560 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003561 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003562 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003563 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3564 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003565 }
3566
Daniel Dunbar98303b92009-09-13 08:03:58 +00003567 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003568}
3569
Manman Ren97f81572012-10-16 19:18:39 +00003570/// isIllegalVector - check whether Ty is an illegal vector type.
3571bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
3572 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3573 // Check whether VT is legal.
3574 unsigned NumElements = VT->getNumElements();
3575 uint64_t Size = getContext().getTypeSize(VT);
3576 // NumElements should be power of 2.
3577 if ((NumElements & (NumElements - 1)) != 0)
3578 return true;
3579 // Size should be greater than 32 bits.
3580 return Size <= 32;
3581 }
3582 return false;
3583}
3584
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003585llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00003586 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003587 llvm::Type *BP = CGF.Int8PtrTy;
3588 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003589
3590 CGBuilderTy &Builder = CGF.Builder;
Chris Lattner8b418682012-02-07 00:39:47 +00003591 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003592 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Manman Rend105e732012-10-16 19:01:37 +00003593
Tim Northover373ac0a2013-06-21 23:05:33 +00003594 if (isEmptyRecord(getContext(), Ty, true)) {
3595 // These are ignored for parameter passing purposes.
3596 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3597 return Builder.CreateBitCast(Addr, PTy);
3598 }
3599
Manman Rend105e732012-10-16 19:01:37 +00003600 uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8;
Rafael Espindolae164c182011-08-02 22:33:37 +00003601 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
Manman Ren97f81572012-10-16 19:18:39 +00003602 bool IsIndirect = false;
Manman Rend105e732012-10-16 19:01:37 +00003603
3604 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
3605 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
Manman Ren93371022012-10-16 19:51:48 +00003606 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3607 getABIKind() == ARMABIInfo::AAPCS)
3608 TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
3609 else
3610 TyAlign = 4;
Manman Ren97f81572012-10-16 19:18:39 +00003611 // Use indirect if size of the illegal vector is bigger than 16 bytes.
3612 if (isIllegalVectorType(Ty) && Size > 16) {
3613 IsIndirect = true;
3614 Size = 4;
3615 TyAlign = 4;
3616 }
Manman Rend105e732012-10-16 19:01:37 +00003617
3618 // Handle address alignment for ABI alignment > 4 bytes.
Rafael Espindolae164c182011-08-02 22:33:37 +00003619 if (TyAlign > 4) {
3620 assert((TyAlign & (TyAlign - 1)) == 0 &&
3621 "Alignment is not power of 2!");
3622 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3623 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
3624 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
Manman Rend105e732012-10-16 19:01:37 +00003625 Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align");
Rafael Espindolae164c182011-08-02 22:33:37 +00003626 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003627
3628 uint64_t Offset =
Manman Rend105e732012-10-16 19:01:37 +00003629 llvm::RoundUpToAlignment(Size, 4);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003630 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00003631 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003632 "ap.next");
3633 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3634
Manman Ren97f81572012-10-16 19:18:39 +00003635 if (IsIndirect)
3636 Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP));
Manman Ren93371022012-10-16 19:51:48 +00003637 else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) {
Manman Rend105e732012-10-16 19:01:37 +00003638 // We can't directly cast ap.cur to pointer to a vector type, since ap.cur
3639 // may not be correctly aligned for the vector type. We create an aligned
3640 // temporary space and copy the content over from ap.cur to the temporary
3641 // space. This is necessary if the natural alignment of the type is greater
3642 // than the ABI alignment.
3643 llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
3644 CharUnits CharSize = getContext().getTypeSizeInChars(Ty);
3645 llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty),
3646 "var.align");
3647 llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
3648 llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy);
3649 Builder.CreateMemCpy(Dst, Src,
3650 llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()),
3651 TyAlign, false);
3652 Addr = AlignedTemp; //The content is in aligned location.
3653 }
3654 llvm::Type *PTy =
3655 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3656 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3657
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003658 return AddrTyped;
3659}
3660
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00003661namespace {
3662
Derek Schuff263366f2012-10-16 22:30:41 +00003663class NaClARMABIInfo : public ABIInfo {
3664 public:
3665 NaClARMABIInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3666 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, Kind) {}
3667 virtual void computeInfo(CGFunctionInfo &FI) const;
3668 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3669 CodeGenFunction &CGF) const;
3670 private:
3671 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
3672 ARMABIInfo NInfo; // Used for everything else.
3673};
3674
3675class NaClARMTargetCodeGenInfo : public TargetCodeGenInfo {
3676 public:
3677 NaClARMTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3678 : TargetCodeGenInfo(new NaClARMABIInfo(CGT, Kind)) {}
3679};
3680
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00003681}
3682
Derek Schuff263366f2012-10-16 22:30:41 +00003683void NaClARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
3684 if (FI.getASTCallingConvention() == CC_PnaclCall)
3685 PInfo.computeInfo(FI);
3686 else
3687 static_cast<const ABIInfo&>(NInfo).computeInfo(FI);
3688}
3689
3690llvm::Value *NaClARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3691 CodeGenFunction &CGF) const {
3692 // Always use the native convention; calling pnacl-style varargs functions
3693 // is unsupported.
3694 return static_cast<const ABIInfo&>(NInfo).EmitVAArg(VAListAddr, Ty, CGF);
3695}
3696
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003697//===----------------------------------------------------------------------===//
Tim Northoverc264e162013-01-31 12:13:10 +00003698// AArch64 ABI Implementation
3699//===----------------------------------------------------------------------===//
3700
3701namespace {
3702
3703class AArch64ABIInfo : public ABIInfo {
3704public:
3705 AArch64ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3706
3707private:
3708 // The AArch64 PCS is explicit about return types and argument types being
3709 // handled identically, so we don't need to draw a distinction between
3710 // Argument and Return classification.
3711 ABIArgInfo classifyGenericType(QualType Ty, int &FreeIntRegs,
3712 int &FreeVFPRegs) const;
3713
3714 ABIArgInfo tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded, bool IsInt,
3715 llvm::Type *DirectTy = 0) const;
3716
3717 virtual void computeInfo(CGFunctionInfo &FI) const;
3718
3719 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3720 CodeGenFunction &CGF) const;
3721};
3722
3723class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
3724public:
3725 AArch64TargetCodeGenInfo(CodeGenTypes &CGT)
3726 :TargetCodeGenInfo(new AArch64ABIInfo(CGT)) {}
3727
3728 const AArch64ABIInfo &getABIInfo() const {
3729 return static_cast<const AArch64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
3730 }
3731
3732 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3733 return 31;
3734 }
3735
3736 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3737 llvm::Value *Address) const {
3738 // 0-31 are x0-x30 and sp: 8 bytes each
3739 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
3740 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 31);
3741
3742 // 64-95 are v0-v31: 16 bytes each
3743 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
3744 AssignToArrayRange(CGF.Builder, Address, Sixteen8, 64, 95);
3745
3746 return false;
3747 }
3748
3749};
3750
3751}
3752
3753void AArch64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
3754 int FreeIntRegs = 8, FreeVFPRegs = 8;
3755
3756 FI.getReturnInfo() = classifyGenericType(FI.getReturnType(),
3757 FreeIntRegs, FreeVFPRegs);
3758
3759 FreeIntRegs = FreeVFPRegs = 8;
3760 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3761 it != ie; ++it) {
3762 it->info = classifyGenericType(it->type, FreeIntRegs, FreeVFPRegs);
3763
3764 }
3765}
3766
3767ABIArgInfo
3768AArch64ABIInfo::tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded,
3769 bool IsInt, llvm::Type *DirectTy) const {
3770 if (FreeRegs >= RegsNeeded) {
3771 FreeRegs -= RegsNeeded;
3772 return ABIArgInfo::getDirect(DirectTy);
3773 }
3774
3775 llvm::Type *Padding = 0;
3776
3777 // We need padding so that later arguments don't get filled in anyway. That
3778 // wouldn't happen if only ByVal arguments followed in the same category, but
3779 // a large structure will simply seem to be a pointer as far as LLVM is
3780 // concerned.
3781 if (FreeRegs > 0) {
3782 if (IsInt)
3783 Padding = llvm::Type::getInt64Ty(getVMContext());
3784 else
3785 Padding = llvm::Type::getFloatTy(getVMContext());
3786
3787 // Either [N x i64] or [N x float].
3788 Padding = llvm::ArrayType::get(Padding, FreeRegs);
3789 FreeRegs = 0;
3790 }
3791
3792 return ABIArgInfo::getIndirect(getContext().getTypeAlign(Ty) / 8,
3793 /*IsByVal=*/ true, /*Realign=*/ false,
3794 Padding);
3795}
3796
3797
3798ABIArgInfo AArch64ABIInfo::classifyGenericType(QualType Ty,
3799 int &FreeIntRegs,
3800 int &FreeVFPRegs) const {
3801 // Can only occurs for return, but harmless otherwise.
3802 if (Ty->isVoidType())
3803 return ABIArgInfo::getIgnore();
3804
3805 // Large vector types should be returned via memory. There's no such concept
3806 // in the ABI, but they'd be over 16 bytes anyway so no matter how they're
3807 // classified they'd go into memory (see B.3).
3808 if (Ty->isVectorType() && getContext().getTypeSize(Ty) > 128) {
3809 if (FreeIntRegs > 0)
3810 --FreeIntRegs;
3811 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3812 }
3813
3814 // All non-aggregate LLVM types have a concrete ABI representation so they can
3815 // be passed directly. After this block we're guaranteed to be in a
3816 // complicated case.
3817 if (!isAggregateTypeForABI(Ty)) {
3818 // Treat an enum type as its underlying type.
3819 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3820 Ty = EnumTy->getDecl()->getIntegerType();
3821
3822 if (Ty->isFloatingType() || Ty->isVectorType())
3823 return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ false);
3824
3825 assert(getContext().getTypeSize(Ty) <= 128 &&
3826 "unexpectedly large scalar type");
3827
3828 int RegsNeeded = getContext().getTypeSize(Ty) > 64 ? 2 : 1;
3829
3830 // If the type may need padding registers to ensure "alignment", we must be
3831 // careful when this is accounted for. Increasing the effective size covers
3832 // all cases.
3833 if (getContext().getTypeAlign(Ty) == 128)
3834 RegsNeeded += FreeIntRegs % 2 != 0;
3835
3836 return tryUseRegs(Ty, FreeIntRegs, RegsNeeded, /*IsInt=*/ true);
3837 }
3838
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00003839 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) {
3840 if (FreeIntRegs > 0 && RAA == CGCXXABI::RAA_Indirect)
Tim Northoverc264e162013-01-31 12:13:10 +00003841 --FreeIntRegs;
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00003842 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Tim Northoverc264e162013-01-31 12:13:10 +00003843 }
3844
3845 if (isEmptyRecord(getContext(), Ty, true)) {
3846 if (!getContext().getLangOpts().CPlusPlus) {
3847 // Empty structs outside C++ mode are a GNU extension, so no ABI can
3848 // possibly tell us what to do. It turns out (I believe) that GCC ignores
3849 // the object for parameter-passsing purposes.
3850 return ABIArgInfo::getIgnore();
3851 }
3852
3853 // The combination of C++98 9p5 (sizeof(struct) != 0) and the pseudocode
3854 // description of va_arg in the PCS require that an empty struct does
3855 // actually occupy space for parameter-passing. I'm hoping for a
3856 // clarification giving an explicit paragraph to point to in future.
3857 return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ true,
3858 llvm::Type::getInt8Ty(getVMContext()));
3859 }
3860
3861 // Homogeneous vector aggregates get passed in registers or on the stack.
3862 const Type *Base = 0;
3863 uint64_t NumMembers = 0;
3864 if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)) {
3865 assert(Base && "Base class should be set for homogeneous aggregate");
3866 // Homogeneous aggregates are passed and returned directly.
3867 return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ NumMembers,
3868 /*IsInt=*/ false);
3869 }
3870
3871 uint64_t Size = getContext().getTypeSize(Ty);
3872 if (Size <= 128) {
3873 // Small structs can use the same direct type whether they're in registers
3874 // or on the stack.
3875 llvm::Type *BaseTy;
3876 unsigned NumBases;
3877 int SizeInRegs = (Size + 63) / 64;
3878
3879 if (getContext().getTypeAlign(Ty) == 128) {
3880 BaseTy = llvm::Type::getIntNTy(getVMContext(), 128);
3881 NumBases = 1;
3882
3883 // If the type may need padding registers to ensure "alignment", we must
3884 // be careful when this is accounted for. Increasing the effective size
3885 // covers all cases.
3886 SizeInRegs += FreeIntRegs % 2 != 0;
3887 } else {
3888 BaseTy = llvm::Type::getInt64Ty(getVMContext());
3889 NumBases = SizeInRegs;
3890 }
3891 llvm::Type *DirectTy = llvm::ArrayType::get(BaseTy, NumBases);
3892
3893 return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ SizeInRegs,
3894 /*IsInt=*/ true, DirectTy);
3895 }
3896
3897 // If the aggregate is > 16 bytes, it's passed and returned indirectly. In
3898 // LLVM terms the return uses an "sret" pointer, but that's handled elsewhere.
3899 --FreeIntRegs;
3900 return ABIArgInfo::getIndirect(0, /* byVal = */ false);
3901}
3902
3903llvm::Value *AArch64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3904 CodeGenFunction &CGF) const {
3905 // The AArch64 va_list type and handling is specified in the Procedure Call
3906 // Standard, section B.4:
3907 //
3908 // struct {
3909 // void *__stack;
3910 // void *__gr_top;
3911 // void *__vr_top;
3912 // int __gr_offs;
3913 // int __vr_offs;
3914 // };
3915
3916 assert(!CGF.CGM.getDataLayout().isBigEndian()
3917 && "va_arg not implemented for big-endian AArch64");
3918
3919 int FreeIntRegs = 8, FreeVFPRegs = 8;
3920 Ty = CGF.getContext().getCanonicalType(Ty);
3921 ABIArgInfo AI = classifyGenericType(Ty, FreeIntRegs, FreeVFPRegs);
3922
3923 llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
3924 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
3925 llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
3926 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
3927
3928 llvm::Value *reg_offs_p = 0, *reg_offs = 0;
3929 int reg_top_index;
3930 int RegSize;
3931 if (FreeIntRegs < 8) {
3932 assert(FreeVFPRegs == 8 && "Arguments never split between int & VFP regs");
3933 // 3 is the field number of __gr_offs
3934 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p");
3935 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
3936 reg_top_index = 1; // field number for __gr_top
3937 RegSize = 8 * (8 - FreeIntRegs);
3938 } else {
3939 assert(FreeVFPRegs < 8 && "Argument must go in VFP or int regs");
3940 // 4 is the field number of __vr_offs.
3941 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p");
3942 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
3943 reg_top_index = 2; // field number for __vr_top
3944 RegSize = 16 * (8 - FreeVFPRegs);
3945 }
3946
3947 //=======================================
3948 // Find out where argument was passed
3949 //=======================================
3950
3951 // If reg_offs >= 0 we're already using the stack for this type of
3952 // argument. We don't want to keep updating reg_offs (in case it overflows,
3953 // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
3954 // whatever they get).
3955 llvm::Value *UsingStack = 0;
3956 UsingStack = CGF.Builder.CreateICmpSGE(reg_offs,
3957 llvm::ConstantInt::get(CGF.Int32Ty, 0));
3958
3959 CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
3960
3961 // Otherwise, at least some kind of argument could go in these registers, the
3962 // quesiton is whether this particular type is too big.
3963 CGF.EmitBlock(MaybeRegBlock);
3964
3965 // Integer arguments may need to correct register alignment (for example a
3966 // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
3967 // align __gr_offs to calculate the potential address.
3968 if (FreeIntRegs < 8 && AI.isDirect() && getContext().getTypeAlign(Ty) > 64) {
3969 int Align = getContext().getTypeAlign(Ty) / 8;
3970
3971 reg_offs = CGF.Builder.CreateAdd(reg_offs,
3972 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
3973 "align_regoffs");
3974 reg_offs = CGF.Builder.CreateAnd(reg_offs,
3975 llvm::ConstantInt::get(CGF.Int32Ty, -Align),
3976 "aligned_regoffs");
3977 }
3978
3979 // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
3980 llvm::Value *NewOffset = 0;
3981 NewOffset = CGF.Builder.CreateAdd(reg_offs,
3982 llvm::ConstantInt::get(CGF.Int32Ty, RegSize),
3983 "new_reg_offs");
3984 CGF.Builder.CreateStore(NewOffset, reg_offs_p);
3985
3986 // Now we're in a position to decide whether this argument really was in
3987 // registers or not.
3988 llvm::Value *InRegs = 0;
3989 InRegs = CGF.Builder.CreateICmpSLE(NewOffset,
3990 llvm::ConstantInt::get(CGF.Int32Ty, 0),
3991 "inreg");
3992
3993 CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
3994
3995 //=======================================
3996 // Argument was in registers
3997 //=======================================
3998
3999 // Now we emit the code for if the argument was originally passed in
4000 // registers. First start the appropriate block:
4001 CGF.EmitBlock(InRegBlock);
4002
4003 llvm::Value *reg_top_p = 0, *reg_top = 0;
4004 reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p");
4005 reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
4006 llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs);
4007 llvm::Value *RegAddr = 0;
4008 llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
4009
4010 if (!AI.isDirect()) {
4011 // If it's been passed indirectly (actually a struct), whatever we find from
4012 // stored registers or on the stack will actually be a struct **.
4013 MemTy = llvm::PointerType::getUnqual(MemTy);
4014 }
4015
4016 const Type *Base = 0;
4017 uint64_t NumMembers;
4018 if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)
4019 && NumMembers > 1) {
4020 // Homogeneous aggregates passed in registers will have their elements split
4021 // and stored 16-bytes apart regardless of size (they're notionally in qN,
4022 // qN+1, ...). We reload and store into a temporary local variable
4023 // contiguously.
4024 assert(AI.isDirect() && "Homogeneous aggregates should be passed directly");
4025 llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
4026 llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
4027 llvm::Value *Tmp = CGF.CreateTempAlloca(HFATy);
4028
4029 for (unsigned i = 0; i < NumMembers; ++i) {
4030 llvm::Value *BaseOffset = llvm::ConstantInt::get(CGF.Int32Ty, 16 * i);
4031 llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset);
4032 LoadAddr = CGF.Builder.CreateBitCast(LoadAddr,
4033 llvm::PointerType::getUnqual(BaseTy));
4034 llvm::Value *StoreAddr = CGF.Builder.CreateStructGEP(Tmp, i);
4035
4036 llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
4037 CGF.Builder.CreateStore(Elem, StoreAddr);
4038 }
4039
4040 RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy);
4041 } else {
4042 // Otherwise the object is contiguous in memory
4043 RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy);
4044 }
4045
4046 CGF.EmitBranch(ContBlock);
4047
4048 //=======================================
4049 // Argument was on the stack
4050 //=======================================
4051 CGF.EmitBlock(OnStackBlock);
4052
4053 llvm::Value *stack_p = 0, *OnStackAddr = 0;
4054 stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p");
4055 OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack");
4056
4057 // Again, stack arguments may need realigmnent. In this case both integer and
4058 // floating-point ones might be affected.
4059 if (AI.isDirect() && getContext().getTypeAlign(Ty) > 64) {
4060 int Align = getContext().getTypeAlign(Ty) / 8;
4061
4062 OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty);
4063
4064 OnStackAddr = CGF.Builder.CreateAdd(OnStackAddr,
4065 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
4066 "align_stack");
4067 OnStackAddr = CGF.Builder.CreateAnd(OnStackAddr,
4068 llvm::ConstantInt::get(CGF.Int64Ty, -Align),
4069 "align_stack");
4070
4071 OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy);
4072 }
4073
4074 uint64_t StackSize;
4075 if (AI.isDirect())
4076 StackSize = getContext().getTypeSize(Ty) / 8;
4077 else
4078 StackSize = 8;
4079
4080 // All stack slots are 8 bytes
4081 StackSize = llvm::RoundUpToAlignment(StackSize, 8);
4082
4083 llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize);
4084 llvm::Value *NewStack = CGF.Builder.CreateGEP(OnStackAddr, StackSizeC,
4085 "new_stack");
4086
4087 // Write the new value of __stack for the next call to va_arg
4088 CGF.Builder.CreateStore(NewStack, stack_p);
4089
4090 OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy);
4091
4092 CGF.EmitBranch(ContBlock);
4093
4094 //=======================================
4095 // Tidy up
4096 //=======================================
4097 CGF.EmitBlock(ContBlock);
4098
4099 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr");
4100 ResAddr->addIncoming(RegAddr, InRegBlock);
4101 ResAddr->addIncoming(OnStackAddr, OnStackBlock);
4102
4103 if (AI.isDirect())
4104 return ResAddr;
4105
4106 return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr");
4107}
4108
4109//===----------------------------------------------------------------------===//
Justin Holewinski2c585b92012-05-24 17:43:12 +00004110// NVPTX ABI Implementation
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004111//===----------------------------------------------------------------------===//
4112
4113namespace {
4114
Justin Holewinski2c585b92012-05-24 17:43:12 +00004115class NVPTXABIInfo : public ABIInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004116public:
Justin Holewinskidca8f332013-03-30 14:38:24 +00004117 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004118
4119 ABIArgInfo classifyReturnType(QualType RetTy) const;
4120 ABIArgInfo classifyArgumentType(QualType Ty) const;
4121
4122 virtual void computeInfo(CGFunctionInfo &FI) const;
4123 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4124 CodeGenFunction &CFG) const;
4125};
4126
Justin Holewinski2c585b92012-05-24 17:43:12 +00004127class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004128public:
Justin Holewinski2c585b92012-05-24 17:43:12 +00004129 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
4130 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
Justin Holewinski818eafb2011-10-05 17:58:44 +00004131
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004132 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4133 CodeGen::CodeGenModule &M) const;
Justin Holewinskidca8f332013-03-30 14:38:24 +00004134private:
4135 static void addKernelMetadata(llvm::Function *F);
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004136};
4137
Justin Holewinski2c585b92012-05-24 17:43:12 +00004138ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004139 if (RetTy->isVoidType())
4140 return ABIArgInfo::getIgnore();
4141 if (isAggregateTypeForABI(RetTy))
4142 return ABIArgInfo::getIndirect(0);
4143 return ABIArgInfo::getDirect();
4144}
4145
Justin Holewinski2c585b92012-05-24 17:43:12 +00004146ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004147 if (isAggregateTypeForABI(Ty))
4148 return ABIArgInfo::getIndirect(0);
4149
4150 return ABIArgInfo::getDirect();
4151}
4152
Justin Holewinski2c585b92012-05-24 17:43:12 +00004153void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004154 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4155 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4156 it != ie; ++it)
4157 it->info = classifyArgumentType(it->type);
4158
4159 // Always honor user-specified calling convention.
4160 if (FI.getCallingConvention() != llvm::CallingConv::C)
4161 return;
4162
John McCallbd7370a2013-02-28 19:01:20 +00004163 FI.setEffectiveCallingConvention(getRuntimeCC());
4164}
4165
Justin Holewinski2c585b92012-05-24 17:43:12 +00004166llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4167 CodeGenFunction &CFG) const {
4168 llvm_unreachable("NVPTX does not support varargs");
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004169}
4170
Justin Holewinski2c585b92012-05-24 17:43:12 +00004171void NVPTXTargetCodeGenInfo::
4172SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4173 CodeGen::CodeGenModule &M) const{
Justin Holewinski818eafb2011-10-05 17:58:44 +00004174 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4175 if (!FD) return;
4176
4177 llvm::Function *F = cast<llvm::Function>(GV);
4178
4179 // Perform special handling in OpenCL mode
David Blaikie4e4d0842012-03-11 07:00:24 +00004180 if (M.getLangOpts().OpenCL) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00004181 // Use OpenCL function attributes to check for kernel functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00004182 // By default, all functions are device functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00004183 if (FD->hasAttr<OpenCLKernelAttr>()) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00004184 // OpenCL __kernel functions get kernel metadata
4185 addKernelMetadata(F);
Justin Holewinski818eafb2011-10-05 17:58:44 +00004186 // And kernel functions are not subject to inlining
Bill Wendling72390b32012-12-20 19:27:06 +00004187 F->addFnAttr(llvm::Attribute::NoInline);
Justin Holewinski818eafb2011-10-05 17:58:44 +00004188 }
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004189 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00004190
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004191 // Perform special handling in CUDA mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00004192 if (M.getLangOpts().CUDA) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00004193 // CUDA __global__ functions get a kernel metadata entry. Since
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004194 // __global__ functions cannot be called from the device, we do not
4195 // need to set the noinline attribute.
4196 if (FD->getAttr<CUDAGlobalAttr>())
Justin Holewinskidca8f332013-03-30 14:38:24 +00004197 addKernelMetadata(F);
Justin Holewinski818eafb2011-10-05 17:58:44 +00004198 }
4199}
4200
Justin Holewinskidca8f332013-03-30 14:38:24 +00004201void NVPTXTargetCodeGenInfo::addKernelMetadata(llvm::Function *F) {
4202 llvm::Module *M = F->getParent();
4203 llvm::LLVMContext &Ctx = M->getContext();
4204
4205 // Get "nvvm.annotations" metadata node
4206 llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
4207
4208 // Create !{<func-ref>, metadata !"kernel", i32 1} node
4209 llvm::SmallVector<llvm::Value *, 3> MDVals;
4210 MDVals.push_back(F);
4211 MDVals.push_back(llvm::MDString::get(Ctx, "kernel"));
4212 MDVals.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), 1));
4213
4214 // Append metadata to nvvm.annotations
4215 MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
4216}
4217
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004218}
4219
4220//===----------------------------------------------------------------------===//
Ulrich Weigandb8409212013-05-06 16:26:41 +00004221// SystemZ ABI Implementation
4222//===----------------------------------------------------------------------===//
4223
4224namespace {
4225
4226class SystemZABIInfo : public ABIInfo {
4227public:
4228 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
4229
4230 bool isPromotableIntegerType(QualType Ty) const;
4231 bool isCompoundType(QualType Ty) const;
4232 bool isFPArgumentType(QualType Ty) const;
4233
4234 ABIArgInfo classifyReturnType(QualType RetTy) const;
4235 ABIArgInfo classifyArgumentType(QualType ArgTy) const;
4236
4237 virtual void computeInfo(CGFunctionInfo &FI) const {
4238 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4239 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4240 it != ie; ++it)
4241 it->info = classifyArgumentType(it->type);
4242 }
4243
4244 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4245 CodeGenFunction &CGF) const;
4246};
4247
4248class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
4249public:
4250 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
4251 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
4252};
4253
4254}
4255
4256bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
4257 // Treat an enum type as its underlying type.
4258 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4259 Ty = EnumTy->getDecl()->getIntegerType();
4260
4261 // Promotable integer types are required to be promoted by the ABI.
4262 if (Ty->isPromotableIntegerType())
4263 return true;
4264
4265 // 32-bit values must also be promoted.
4266 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4267 switch (BT->getKind()) {
4268 case BuiltinType::Int:
4269 case BuiltinType::UInt:
4270 return true;
4271 default:
4272 return false;
4273 }
4274 return false;
4275}
4276
4277bool SystemZABIInfo::isCompoundType(QualType Ty) const {
4278 return Ty->isAnyComplexType() || isAggregateTypeForABI(Ty);
4279}
4280
4281bool SystemZABIInfo::isFPArgumentType(QualType Ty) const {
4282 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4283 switch (BT->getKind()) {
4284 case BuiltinType::Float:
4285 case BuiltinType::Double:
4286 return true;
4287 default:
4288 return false;
4289 }
4290
4291 if (const RecordType *RT = Ty->getAsStructureType()) {
4292 const RecordDecl *RD = RT->getDecl();
4293 bool Found = false;
4294
4295 // If this is a C++ record, check the bases first.
4296 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
4297 for (CXXRecordDecl::base_class_const_iterator I = CXXRD->bases_begin(),
4298 E = CXXRD->bases_end(); I != E; ++I) {
4299 QualType Base = I->getType();
4300
4301 // Empty bases don't affect things either way.
4302 if (isEmptyRecord(getContext(), Base, true))
4303 continue;
4304
4305 if (Found)
4306 return false;
4307 Found = isFPArgumentType(Base);
4308 if (!Found)
4309 return false;
4310 }
4311
4312 // Check the fields.
4313 for (RecordDecl::field_iterator I = RD->field_begin(),
4314 E = RD->field_end(); I != E; ++I) {
4315 const FieldDecl *FD = *I;
4316
4317 // Empty bitfields don't affect things either way.
4318 // Unlike isSingleElementStruct(), empty structure and array fields
4319 // do count. So do anonymous bitfields that aren't zero-sized.
4320 if (FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
4321 return true;
4322
4323 // Unlike isSingleElementStruct(), arrays do not count.
4324 // Nested isFPArgumentType structures still do though.
4325 if (Found)
4326 return false;
4327 Found = isFPArgumentType(FD->getType());
4328 if (!Found)
4329 return false;
4330 }
4331
4332 // Unlike isSingleElementStruct(), trailing padding is allowed.
4333 // An 8-byte aligned struct s { float f; } is passed as a double.
4334 return Found;
4335 }
4336
4337 return false;
4338}
4339
4340llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4341 CodeGenFunction &CGF) const {
4342 // Assume that va_list type is correct; should be pointer to LLVM type:
4343 // struct {
4344 // i64 __gpr;
4345 // i64 __fpr;
4346 // i8 *__overflow_arg_area;
4347 // i8 *__reg_save_area;
4348 // };
4349
4350 // Every argument occupies 8 bytes and is passed by preference in either
4351 // GPRs or FPRs.
4352 Ty = CGF.getContext().getCanonicalType(Ty);
4353 ABIArgInfo AI = classifyArgumentType(Ty);
4354 bool InFPRs = isFPArgumentType(Ty);
4355
4356 llvm::Type *APTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
4357 bool IsIndirect = AI.isIndirect();
4358 unsigned UnpaddedBitSize;
4359 if (IsIndirect) {
4360 APTy = llvm::PointerType::getUnqual(APTy);
4361 UnpaddedBitSize = 64;
4362 } else
4363 UnpaddedBitSize = getContext().getTypeSize(Ty);
4364 unsigned PaddedBitSize = 64;
4365 assert((UnpaddedBitSize <= PaddedBitSize) && "Invalid argument size.");
4366
4367 unsigned PaddedSize = PaddedBitSize / 8;
4368 unsigned Padding = (PaddedBitSize - UnpaddedBitSize) / 8;
4369
4370 unsigned MaxRegs, RegCountField, RegSaveIndex, RegPadding;
4371 if (InFPRs) {
4372 MaxRegs = 4; // Maximum of 4 FPR arguments
4373 RegCountField = 1; // __fpr
4374 RegSaveIndex = 16; // save offset for f0
4375 RegPadding = 0; // floats are passed in the high bits of an FPR
4376 } else {
4377 MaxRegs = 5; // Maximum of 5 GPR arguments
4378 RegCountField = 0; // __gpr
4379 RegSaveIndex = 2; // save offset for r2
4380 RegPadding = Padding; // values are passed in the low bits of a GPR
4381 }
4382
4383 llvm::Value *RegCountPtr =
4384 CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr");
4385 llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count");
4386 llvm::Type *IndexTy = RegCount->getType();
4387 llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs);
4388 llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV,
4389 "fits_in_regs");
4390
4391 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
4392 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
4393 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
4394 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
4395
4396 // Emit code to load the value if it was passed in registers.
4397 CGF.EmitBlock(InRegBlock);
4398
4399 // Work out the address of an argument register.
4400 llvm::Value *PaddedSizeV = llvm::ConstantInt::get(IndexTy, PaddedSize);
4401 llvm::Value *ScaledRegCount =
4402 CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count");
4403 llvm::Value *RegBase =
4404 llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize + RegPadding);
4405 llvm::Value *RegOffset =
4406 CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset");
4407 llvm::Value *RegSaveAreaPtr =
4408 CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr");
4409 llvm::Value *RegSaveArea =
4410 CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area");
4411 llvm::Value *RawRegAddr =
4412 CGF.Builder.CreateGEP(RegSaveArea, RegOffset, "raw_reg_addr");
4413 llvm::Value *RegAddr =
4414 CGF.Builder.CreateBitCast(RawRegAddr, APTy, "reg_addr");
4415
4416 // Update the register count
4417 llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1);
4418 llvm::Value *NewRegCount =
4419 CGF.Builder.CreateAdd(RegCount, One, "reg_count");
4420 CGF.Builder.CreateStore(NewRegCount, RegCountPtr);
4421 CGF.EmitBranch(ContBlock);
4422
4423 // Emit code to load the value if it was passed in memory.
4424 CGF.EmitBlock(InMemBlock);
4425
4426 // Work out the address of a stack argument.
4427 llvm::Value *OverflowArgAreaPtr =
4428 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr");
4429 llvm::Value *OverflowArgArea =
4430 CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area");
4431 llvm::Value *PaddingV = llvm::ConstantInt::get(IndexTy, Padding);
4432 llvm::Value *RawMemAddr =
4433 CGF.Builder.CreateGEP(OverflowArgArea, PaddingV, "raw_mem_addr");
4434 llvm::Value *MemAddr =
4435 CGF.Builder.CreateBitCast(RawMemAddr, APTy, "mem_addr");
4436
4437 // Update overflow_arg_area_ptr pointer
4438 llvm::Value *NewOverflowArgArea =
4439 CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area");
4440 CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
4441 CGF.EmitBranch(ContBlock);
4442
4443 // Return the appropriate result.
4444 CGF.EmitBlock(ContBlock);
4445 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(APTy, 2, "va_arg.addr");
4446 ResAddr->addIncoming(RegAddr, InRegBlock);
4447 ResAddr->addIncoming(MemAddr, InMemBlock);
4448
4449 if (IsIndirect)
4450 return CGF.Builder.CreateLoad(ResAddr, "indirect_arg");
4451
4452 return ResAddr;
4453}
4454
John McCallb8b52972013-06-18 02:46:29 +00004455bool X86_32TargetCodeGenInfo::isStructReturnInRegABI(
4456 const llvm::Triple &Triple, const CodeGenOptions &Opts) {
4457 assert(Triple.getArch() == llvm::Triple::x86);
4458
4459 switch (Opts.getStructReturnConvention()) {
4460 case CodeGenOptions::SRCK_Default:
4461 break;
4462 case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return
4463 return false;
4464 case CodeGenOptions::SRCK_InRegs: // -freg-struct-return
4465 return true;
4466 }
4467
4468 if (Triple.isOSDarwin())
4469 return true;
4470
4471 switch (Triple.getOS()) {
4472 case llvm::Triple::Cygwin:
4473 case llvm::Triple::MinGW32:
4474 case llvm::Triple::AuroraUX:
4475 case llvm::Triple::DragonFly:
4476 case llvm::Triple::FreeBSD:
4477 case llvm::Triple::OpenBSD:
4478 case llvm::Triple::Bitrig:
4479 case llvm::Triple::Win32:
4480 return true;
4481 default:
4482 return false;
4483 }
4484}
Ulrich Weigandb8409212013-05-06 16:26:41 +00004485
4486ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
4487 if (RetTy->isVoidType())
4488 return ABIArgInfo::getIgnore();
4489 if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64)
4490 return ABIArgInfo::getIndirect(0);
4491 return (isPromotableIntegerType(RetTy) ?
4492 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4493}
4494
4495ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
4496 // Handle the generic C++ ABI.
4497 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
4498 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
4499
4500 // Integers and enums are extended to full register width.
4501 if (isPromotableIntegerType(Ty))
4502 return ABIArgInfo::getExtend();
4503
4504 // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly.
4505 uint64_t Size = getContext().getTypeSize(Ty);
4506 if (Size != 8 && Size != 16 && Size != 32 && Size != 64)
4507 return ABIArgInfo::getIndirect(0);
4508
4509 // Handle small structures.
4510 if (const RecordType *RT = Ty->getAs<RecordType>()) {
4511 // Structures with flexible arrays have variable length, so really
4512 // fail the size test above.
4513 const RecordDecl *RD = RT->getDecl();
4514 if (RD->hasFlexibleArrayMember())
4515 return ABIArgInfo::getIndirect(0);
4516
4517 // The structure is passed as an unextended integer, a float, or a double.
4518 llvm::Type *PassTy;
4519 if (isFPArgumentType(Ty)) {
4520 assert(Size == 32 || Size == 64);
4521 if (Size == 32)
4522 PassTy = llvm::Type::getFloatTy(getVMContext());
4523 else
4524 PassTy = llvm::Type::getDoubleTy(getVMContext());
4525 } else
4526 PassTy = llvm::IntegerType::get(getVMContext(), Size);
4527 return ABIArgInfo::getDirect(PassTy);
4528 }
4529
4530 // Non-structure compounds are passed indirectly.
4531 if (isCompoundType(Ty))
4532 return ABIArgInfo::getIndirect(0);
4533
4534 return ABIArgInfo::getDirect(0);
4535}
4536
4537//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004538// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004539//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004540
4541namespace {
4542
4543class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
4544public:
Chris Lattnerea044322010-07-29 02:01:43 +00004545 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
4546 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004547 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4548 CodeGen::CodeGenModule &M) const;
4549};
4550
4551}
4552
4553void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4554 llvm::GlobalValue *GV,
4555 CodeGen::CodeGenModule &M) const {
4556 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4557 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
4558 // Handle 'interrupt' attribute:
4559 llvm::Function *F = cast<llvm::Function>(GV);
4560
4561 // Step 1: Set ISR calling convention.
4562 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
4563
4564 // Step 2: Add attributes goodness.
Bill Wendling72390b32012-12-20 19:27:06 +00004565 F->addFnAttr(llvm::Attribute::NoInline);
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004566
4567 // Step 3: Emit ISR vector alias.
Anton Korobeynikovf419a852012-11-26 18:59:10 +00004568 unsigned Num = attr->getNumber() / 2;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004569 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Anton Korobeynikovf419a852012-11-26 18:59:10 +00004570 "__isr_" + Twine(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004571 GV, &M.getModule());
4572 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004573 }
4574}
4575
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004576//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00004577// MIPS ABI Implementation. This works for both little-endian and
4578// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004579//===----------------------------------------------------------------------===//
4580
John McCallaeeb7012010-05-27 06:19:26 +00004581namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00004582class MipsABIInfo : public ABIInfo {
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004583 bool IsO32;
Akira Hatanakac359f202012-07-03 19:24:06 +00004584 unsigned MinABIStackAlignInBytes, StackAlignInBytes;
4585 void CoerceToIntArgs(uint64_t TySize,
Craig Topper6b9240e2013-07-05 19:34:19 +00004586 SmallVectorImpl<llvm::Type *> &ArgList) const;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004587 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004588 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004589 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004590public:
Akira Hatanakab551dd32011-11-03 00:05:50 +00004591 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
Akira Hatanakac359f202012-07-03 19:24:06 +00004592 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
4593 StackAlignInBytes(IsO32 ? 8 : 16) {}
Akira Hatanaka619e8872011-06-02 00:09:17 +00004594
4595 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004596 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004597 virtual void computeInfo(CGFunctionInfo &FI) const;
4598 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4599 CodeGenFunction &CGF) const;
4600};
4601
John McCallaeeb7012010-05-27 06:19:26 +00004602class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanakae624fa02011-09-20 18:23:28 +00004603 unsigned SizeOfUnwindException;
John McCallaeeb7012010-05-27 06:19:26 +00004604public:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004605 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
4606 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
4607 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCallaeeb7012010-05-27 06:19:26 +00004608
4609 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
4610 return 29;
4611 }
4612
Reed Kotler7dfd1822013-01-16 17:10:28 +00004613 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4614 CodeGen::CodeGenModule &CGM) const {
Reed Kotlerad4b8b42013-03-13 20:40:30 +00004615 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4616 if (!FD) return;
Rafael Espindolad8e6d6d2013-03-19 14:32:23 +00004617 llvm::Function *Fn = cast<llvm::Function>(GV);
Reed Kotlerad4b8b42013-03-13 20:40:30 +00004618 if (FD->hasAttr<Mips16Attr>()) {
4619 Fn->addFnAttr("mips16");
4620 }
4621 else if (FD->hasAttr<NoMips16Attr>()) {
4622 Fn->addFnAttr("nomips16");
4623 }
Reed Kotler7dfd1822013-01-16 17:10:28 +00004624 }
Reed Kotlerad4b8b42013-03-13 20:40:30 +00004625
John McCallaeeb7012010-05-27 06:19:26 +00004626 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00004627 llvm::Value *Address) const;
John McCall49e34be2011-08-30 01:42:09 +00004628
4629 unsigned getSizeOfUnwindException() const {
Akira Hatanakae624fa02011-09-20 18:23:28 +00004630 return SizeOfUnwindException;
John McCall49e34be2011-08-30 01:42:09 +00004631 }
John McCallaeeb7012010-05-27 06:19:26 +00004632};
4633}
4634
Akira Hatanakac359f202012-07-03 19:24:06 +00004635void MipsABIInfo::CoerceToIntArgs(uint64_t TySize,
Craig Topper6b9240e2013-07-05 19:34:19 +00004636 SmallVectorImpl<llvm::Type *> &ArgList) const {
Akira Hatanakac359f202012-07-03 19:24:06 +00004637 llvm::IntegerType *IntTy =
4638 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004639
4640 // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
4641 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
4642 ArgList.push_back(IntTy);
4643
4644 // If necessary, add one more integer type to ArgList.
4645 unsigned R = TySize % (MinABIStackAlignInBytes * 8);
4646
4647 if (R)
4648 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004649}
4650
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004651// In N32/64, an aligned double precision floating point field is passed in
4652// a register.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004653llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
Akira Hatanakac359f202012-07-03 19:24:06 +00004654 SmallVector<llvm::Type*, 8> ArgList, IntArgList;
4655
4656 if (IsO32) {
4657 CoerceToIntArgs(TySize, ArgList);
4658 return llvm::StructType::get(getVMContext(), ArgList);
4659 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004660
Akira Hatanaka2afd23d2012-01-12 00:52:17 +00004661 if (Ty->isComplexType())
4662 return CGT.ConvertType(Ty);
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00004663
Akira Hatanakaa34e9212012-02-09 19:54:16 +00004664 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004665
Akira Hatanakac359f202012-07-03 19:24:06 +00004666 // Unions/vectors are passed in integer registers.
4667 if (!RT || !RT->isStructureOrClassType()) {
4668 CoerceToIntArgs(TySize, ArgList);
4669 return llvm::StructType::get(getVMContext(), ArgList);
4670 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004671
4672 const RecordDecl *RD = RT->getDecl();
4673 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004674 assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004675
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004676 uint64_t LastOffset = 0;
4677 unsigned idx = 0;
4678 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
4679
Akira Hatanakaa34e9212012-02-09 19:54:16 +00004680 // Iterate over fields in the struct/class and check if there are any aligned
4681 // double fields.
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004682 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
4683 i != e; ++i, ++idx) {
David Blaikie262bc182012-04-30 02:36:29 +00004684 const QualType Ty = i->getType();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004685 const BuiltinType *BT = Ty->getAs<BuiltinType>();
4686
4687 if (!BT || BT->getKind() != BuiltinType::Double)
4688 continue;
4689
4690 uint64_t Offset = Layout.getFieldOffset(idx);
4691 if (Offset % 64) // Ignore doubles that are not aligned.
4692 continue;
4693
4694 // Add ((Offset - LastOffset) / 64) args of type i64.
4695 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
4696 ArgList.push_back(I64);
4697
4698 // Add double type.
4699 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
4700 LastOffset = Offset + 64;
4701 }
4702
Akira Hatanakac359f202012-07-03 19:24:06 +00004703 CoerceToIntArgs(TySize - LastOffset, IntArgList);
4704 ArgList.append(IntArgList.begin(), IntArgList.end());
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004705
4706 return llvm::StructType::get(getVMContext(), ArgList);
4707}
4708
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004709llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004710 assert((Offset % MinABIStackAlignInBytes) == 0);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004711
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004712 if ((Align - 1) & Offset)
4713 return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
4714
4715 return 0;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004716}
Akira Hatanaka9659d592012-01-10 22:44:52 +00004717
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004718ABIArgInfo
4719MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004720 uint64_t OrigOffset = Offset;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004721 uint64_t TySize = getContext().getTypeSize(Ty);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004722 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004723
Akira Hatanakac359f202012-07-03 19:24:06 +00004724 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
4725 (uint64_t)StackAlignInBytes);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004726 Offset = llvm::RoundUpToAlignment(Offset, Align);
4727 Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004728
Akira Hatanakac359f202012-07-03 19:24:06 +00004729 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
Akira Hatanaka619e8872011-06-02 00:09:17 +00004730 // Ignore empty aggregates.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004731 if (TySize == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00004732 return ABIArgInfo::getIgnore();
4733
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00004734 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004735 Offset = OrigOffset + MinABIStackAlignInBytes;
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00004736 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004737 }
Akira Hatanaka511949b2011-08-01 18:09:58 +00004738
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004739 // If we have reached here, aggregates are passed directly by coercing to
4740 // another structure type. Padding is inserted if the offset of the
4741 // aggregate is unaligned.
4742 return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
4743 getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00004744 }
4745
4746 // Treat an enum type as its underlying type.
4747 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4748 Ty = EnumTy->getDecl()->getIntegerType();
4749
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004750 if (Ty->isPromotableIntegerType())
4751 return ABIArgInfo::getExtend();
4752
Akira Hatanaka4055cfc2013-01-24 21:47:33 +00004753 return ABIArgInfo::getDirect(0, 0,
4754 IsO32 ? 0 : getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00004755}
4756
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004757llvm::Type*
4758MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakada54ff32012-02-09 18:49:26 +00004759 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakac359f202012-07-03 19:24:06 +00004760 SmallVector<llvm::Type*, 8> RTList;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004761
Akira Hatanakada54ff32012-02-09 18:49:26 +00004762 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004763 const RecordDecl *RD = RT->getDecl();
Akira Hatanakada54ff32012-02-09 18:49:26 +00004764 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
4765 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004766
Akira Hatanakada54ff32012-02-09 18:49:26 +00004767 // N32/64 returns struct/classes in floating point registers if the
4768 // following conditions are met:
4769 // 1. The size of the struct/class is no larger than 128-bit.
4770 // 2. The struct/class has one or two fields all of which are floating
4771 // point types.
4772 // 3. The offset of the first field is zero (this follows what gcc does).
4773 //
4774 // Any other composite results are returned in integer registers.
4775 //
4776 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
4777 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
4778 for (; b != e; ++b) {
David Blaikie262bc182012-04-30 02:36:29 +00004779 const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004780
Akira Hatanakada54ff32012-02-09 18:49:26 +00004781 if (!BT || !BT->isFloatingPoint())
4782 break;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004783
David Blaikie262bc182012-04-30 02:36:29 +00004784 RTList.push_back(CGT.ConvertType(b->getType()));
Akira Hatanakada54ff32012-02-09 18:49:26 +00004785 }
4786
4787 if (b == e)
4788 return llvm::StructType::get(getVMContext(), RTList,
4789 RD->hasAttr<PackedAttr>());
4790
4791 RTList.clear();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004792 }
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004793 }
4794
Akira Hatanakac359f202012-07-03 19:24:06 +00004795 CoerceToIntArgs(Size, RTList);
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004796 return llvm::StructType::get(getVMContext(), RTList);
4797}
4798
Akira Hatanaka619e8872011-06-02 00:09:17 +00004799ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanakaa8536c02012-01-23 23:18:57 +00004800 uint64_t Size = getContext().getTypeSize(RetTy);
4801
4802 if (RetTy->isVoidType() || Size == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00004803 return ABIArgInfo::getIgnore();
4804
Akira Hatanaka8aeb1472012-05-11 21:01:17 +00004805 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00004806 if (isRecordReturnIndirect(RetTy, CGT))
4807 return ABIArgInfo::getIndirect(0);
4808
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004809 if (Size <= 128) {
4810 if (RetTy->isAnyComplexType())
4811 return ABIArgInfo::getDirect();
4812
Akira Hatanakac359f202012-07-03 19:24:06 +00004813 // O32 returns integer vectors in registers.
4814 if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())
4815 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
4816
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00004817 if (!IsO32)
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004818 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
4819 }
Akira Hatanaka619e8872011-06-02 00:09:17 +00004820
4821 return ABIArgInfo::getIndirect(0);
4822 }
4823
4824 // Treat an enum type as its underlying type.
4825 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4826 RetTy = EnumTy->getDecl()->getIntegerType();
4827
4828 return (RetTy->isPromotableIntegerType() ?
4829 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4830}
4831
4832void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanakacc662542012-01-12 01:10:09 +00004833 ABIArgInfo &RetInfo = FI.getReturnInfo();
4834 RetInfo = classifyReturnType(FI.getReturnType());
4835
4836 // Check if a pointer to an aggregate is passed as a hidden argument.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004837 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
Akira Hatanakacc662542012-01-12 01:10:09 +00004838
Akira Hatanaka619e8872011-06-02 00:09:17 +00004839 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4840 it != ie; ++it)
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004841 it->info = classifyArgumentType(it->type, Offset);
Akira Hatanaka619e8872011-06-02 00:09:17 +00004842}
4843
4844llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4845 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00004846 llvm::Type *BP = CGF.Int8PtrTy;
4847 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004848
4849 CGBuilderTy &Builder = CGF.Builder;
4850 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
4851 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004852 int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004853 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
4854 llvm::Value *AddrTyped;
John McCall64aa4b32013-04-16 22:48:15 +00004855 unsigned PtrWidth = getTarget().getPointerWidth(0);
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004856 llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004857
4858 if (TypeAlign > MinABIStackAlignInBytes) {
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004859 llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
4860 llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
4861 llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
4862 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004863 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
4864 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
4865 }
4866 else
4867 AddrTyped = Builder.CreateBitCast(Addr, PTy);
4868
4869 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004870 TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004871 uint64_t Offset =
4872 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
4873 llvm::Value *NextAddr =
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004874 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004875 "ap.next");
4876 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
4877
4878 return AddrTyped;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004879}
4880
John McCallaeeb7012010-05-27 06:19:26 +00004881bool
4882MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4883 llvm::Value *Address) const {
4884 // This information comes from gcc's implementation, which seems to
4885 // as canonical as it gets.
4886
John McCallaeeb7012010-05-27 06:19:26 +00004887 // Everything on MIPS is 4 bytes. Double-precision FP registers
4888 // are aliased to pairs of single-precision FP registers.
Chris Lattner8b418682012-02-07 00:39:47 +00004889 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCallaeeb7012010-05-27 06:19:26 +00004890
4891 // 0-31 are the general purpose registers, $0 - $31.
4892 // 32-63 are the floating-point registers, $f0 - $f31.
4893 // 64 and 65 are the multiply/divide registers, $hi and $lo.
4894 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattner8b418682012-02-07 00:39:47 +00004895 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCallaeeb7012010-05-27 06:19:26 +00004896
4897 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
4898 // They are one bit wide and ignored here.
4899
4900 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
4901 // (coprocessor 1 is the FP unit)
4902 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
4903 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
4904 // 176-181 are the DSP accumulator registers.
Chris Lattner8b418682012-02-07 00:39:47 +00004905 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCallaeeb7012010-05-27 06:19:26 +00004906 return false;
4907}
4908
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004909//===----------------------------------------------------------------------===//
4910// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
4911// Currently subclassed only to implement custom OpenCL C function attribute
4912// handling.
4913//===----------------------------------------------------------------------===//
4914
4915namespace {
4916
4917class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
4918public:
4919 TCETargetCodeGenInfo(CodeGenTypes &CGT)
4920 : DefaultTargetCodeGenInfo(CGT) {}
4921
4922 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4923 CodeGen::CodeGenModule &M) const;
4924};
4925
4926void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4927 llvm::GlobalValue *GV,
4928 CodeGen::CodeGenModule &M) const {
4929 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4930 if (!FD) return;
4931
4932 llvm::Function *F = cast<llvm::Function>(GV);
4933
David Blaikie4e4d0842012-03-11 07:00:24 +00004934 if (M.getLangOpts().OpenCL) {
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004935 if (FD->hasAttr<OpenCLKernelAttr>()) {
4936 // OpenCL C Kernel functions are not subject to inlining
Bill Wendling72390b32012-12-20 19:27:06 +00004937 F->addFnAttr(llvm::Attribute::NoInline);
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004938
4939 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
4940
4941 // Convert the reqd_work_group_size() attributes to metadata.
4942 llvm::LLVMContext &Context = F->getContext();
4943 llvm::NamedMDNode *OpenCLMetadata =
4944 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
4945
4946 SmallVector<llvm::Value*, 5> Operands;
4947 Operands.push_back(F);
4948
Chris Lattner8b418682012-02-07 00:39:47 +00004949 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4950 llvm::APInt(32,
4951 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
4952 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4953 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004954 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
Chris Lattner8b418682012-02-07 00:39:47 +00004955 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4956 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004957 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
4958
4959 // Add a boolean constant operand for "required" (true) or "hint" (false)
4960 // for implementing the work_group_size_hint attr later. Currently
4961 // always true as the hint is not yet implemented.
Chris Lattner8b418682012-02-07 00:39:47 +00004962 Operands.push_back(llvm::ConstantInt::getTrue(Context));
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004963 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
4964 }
4965 }
4966 }
4967}
4968
4969}
John McCallaeeb7012010-05-27 06:19:26 +00004970
Tony Linthicum96319392011-12-12 21:14:55 +00004971//===----------------------------------------------------------------------===//
4972// Hexagon ABI Implementation
4973//===----------------------------------------------------------------------===//
4974
4975namespace {
4976
4977class HexagonABIInfo : public ABIInfo {
4978
4979
4980public:
4981 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
4982
4983private:
4984
4985 ABIArgInfo classifyReturnType(QualType RetTy) const;
4986 ABIArgInfo classifyArgumentType(QualType RetTy) const;
4987
4988 virtual void computeInfo(CGFunctionInfo &FI) const;
4989
4990 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4991 CodeGenFunction &CGF) const;
4992};
4993
4994class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
4995public:
4996 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
4997 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
4998
4999 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
5000 return 29;
5001 }
5002};
5003
5004}
5005
5006void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
5007 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
5008 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
5009 it != ie; ++it)
5010 it->info = classifyArgumentType(it->type);
5011}
5012
5013ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
5014 if (!isAggregateTypeForABI(Ty)) {
5015 // Treat an enum type as its underlying type.
5016 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5017 Ty = EnumTy->getDecl()->getIntegerType();
5018
5019 return (Ty->isPromotableIntegerType() ?
5020 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
5021 }
5022
5023 // Ignore empty records.
5024 if (isEmptyRecord(getContext(), Ty, true))
5025 return ABIArgInfo::getIgnore();
5026
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00005027 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
5028 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Tony Linthicum96319392011-12-12 21:14:55 +00005029
5030 uint64_t Size = getContext().getTypeSize(Ty);
5031 if (Size > 64)
5032 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
5033 // Pass in the smallest viable integer type.
5034 else if (Size > 32)
5035 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
5036 else if (Size > 16)
5037 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
5038 else if (Size > 8)
5039 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5040 else
5041 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
5042}
5043
5044ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
5045 if (RetTy->isVoidType())
5046 return ABIArgInfo::getIgnore();
5047
5048 // Large vector types should be returned via memory.
5049 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
5050 return ABIArgInfo::getIndirect(0);
5051
5052 if (!isAggregateTypeForABI(RetTy)) {
5053 // Treat an enum type as its underlying type.
5054 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5055 RetTy = EnumTy->getDecl()->getIntegerType();
5056
5057 return (RetTy->isPromotableIntegerType() ?
5058 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
5059 }
5060
5061 // Structures with either a non-trivial destructor or a non-trivial
5062 // copy constructor are always indirect.
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00005063 if (isRecordReturnIndirect(RetTy, CGT))
Tony Linthicum96319392011-12-12 21:14:55 +00005064 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
5065
5066 if (isEmptyRecord(getContext(), RetTy, true))
5067 return ABIArgInfo::getIgnore();
5068
5069 // Aggregates <= 8 bytes are returned in r0; other aggregates
5070 // are returned indirectly.
5071 uint64_t Size = getContext().getTypeSize(RetTy);
5072 if (Size <= 64) {
5073 // Return in the smallest viable integer type.
5074 if (Size <= 8)
5075 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
5076 if (Size <= 16)
5077 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5078 if (Size <= 32)
5079 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
5080 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
5081 }
5082
5083 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
5084}
5085
5086llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner8b418682012-02-07 00:39:47 +00005087 CodeGenFunction &CGF) const {
Tony Linthicum96319392011-12-12 21:14:55 +00005088 // FIXME: Need to handle alignment
Chris Lattner8b418682012-02-07 00:39:47 +00005089 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Tony Linthicum96319392011-12-12 21:14:55 +00005090
5091 CGBuilderTy &Builder = CGF.Builder;
5092 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
5093 "ap");
5094 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
5095 llvm::Type *PTy =
5096 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
5097 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
5098
5099 uint64_t Offset =
5100 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
5101 llvm::Value *NextAddr =
5102 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
5103 "ap.next");
5104 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
5105
5106 return AddrTyped;
5107}
5108
5109
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00005110//===----------------------------------------------------------------------===//
5111// SPARC v9 ABI Implementation.
5112// Based on the SPARC Compliance Definition version 2.4.1.
5113//
5114// Function arguments a mapped to a nominal "parameter array" and promoted to
5115// registers depending on their type. Each argument occupies 8 or 16 bytes in
5116// the array, structs larger than 16 bytes are passed indirectly.
5117//
5118// One case requires special care:
5119//
5120// struct mixed {
5121// int i;
5122// float f;
5123// };
5124//
5125// When a struct mixed is passed by value, it only occupies 8 bytes in the
5126// parameter array, but the int is passed in an integer register, and the float
5127// is passed in a floating point register. This is represented as two arguments
5128// with the LLVM IR inreg attribute:
5129//
5130// declare void f(i32 inreg %i, float inreg %f)
5131//
5132// The code generator will only allocate 4 bytes from the parameter array for
5133// the inreg arguments. All other arguments are allocated a multiple of 8
5134// bytes.
5135//
5136namespace {
5137class SparcV9ABIInfo : public ABIInfo {
5138public:
5139 SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
5140
5141private:
5142 ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const;
5143 virtual void computeInfo(CGFunctionInfo &FI) const;
5144 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5145 CodeGenFunction &CGF) const;
Jakob Stoklund Olesenfc782fb2013-05-28 04:57:37 +00005146
5147 // Coercion type builder for structs passed in registers. The coercion type
5148 // serves two purposes:
5149 //
5150 // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned'
5151 // in registers.
5152 // 2. Expose aligned floating point elements as first-level elements, so the
5153 // code generator knows to pass them in floating point registers.
5154 //
5155 // We also compute the InReg flag which indicates that the struct contains
5156 // aligned 32-bit floats.
5157 //
5158 struct CoerceBuilder {
5159 llvm::LLVMContext &Context;
5160 const llvm::DataLayout &DL;
5161 SmallVector<llvm::Type*, 8> Elems;
5162 uint64_t Size;
5163 bool InReg;
5164
5165 CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl)
5166 : Context(c), DL(dl), Size(0), InReg(false) {}
5167
5168 // Pad Elems with integers until Size is ToSize.
5169 void pad(uint64_t ToSize) {
5170 assert(ToSize >= Size && "Cannot remove elements");
5171 if (ToSize == Size)
5172 return;
5173
5174 // Finish the current 64-bit word.
5175 uint64_t Aligned = llvm::RoundUpToAlignment(Size, 64);
5176 if (Aligned > Size && Aligned <= ToSize) {
5177 Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size));
5178 Size = Aligned;
5179 }
5180
5181 // Add whole 64-bit words.
5182 while (Size + 64 <= ToSize) {
5183 Elems.push_back(llvm::Type::getInt64Ty(Context));
5184 Size += 64;
5185 }
5186
5187 // Final in-word padding.
5188 if (Size < ToSize) {
5189 Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size));
5190 Size = ToSize;
5191 }
5192 }
5193
5194 // Add a floating point element at Offset.
5195 void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) {
5196 // Unaligned floats are treated as integers.
5197 if (Offset % Bits)
5198 return;
5199 // The InReg flag is only required if there are any floats < 64 bits.
5200 if (Bits < 64)
5201 InReg = true;
5202 pad(Offset);
5203 Elems.push_back(Ty);
5204 Size = Offset + Bits;
5205 }
5206
5207 // Add a struct type to the coercion type, starting at Offset (in bits).
5208 void addStruct(uint64_t Offset, llvm::StructType *StrTy) {
5209 const llvm::StructLayout *Layout = DL.getStructLayout(StrTy);
5210 for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) {
5211 llvm::Type *ElemTy = StrTy->getElementType(i);
5212 uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i);
5213 switch (ElemTy->getTypeID()) {
5214 case llvm::Type::StructTyID:
5215 addStruct(ElemOffset, cast<llvm::StructType>(ElemTy));
5216 break;
5217 case llvm::Type::FloatTyID:
5218 addFloat(ElemOffset, ElemTy, 32);
5219 break;
5220 case llvm::Type::DoubleTyID:
5221 addFloat(ElemOffset, ElemTy, 64);
5222 break;
5223 case llvm::Type::FP128TyID:
5224 addFloat(ElemOffset, ElemTy, 128);
5225 break;
5226 case llvm::Type::PointerTyID:
5227 if (ElemOffset % 64 == 0) {
5228 pad(ElemOffset);
5229 Elems.push_back(ElemTy);
5230 Size += 64;
5231 }
5232 break;
5233 default:
5234 break;
5235 }
5236 }
5237 }
5238
5239 // Check if Ty is a usable substitute for the coercion type.
5240 bool isUsableType(llvm::StructType *Ty) const {
5241 if (Ty->getNumElements() != Elems.size())
5242 return false;
5243 for (unsigned i = 0, e = Elems.size(); i != e; ++i)
5244 if (Elems[i] != Ty->getElementType(i))
5245 return false;
5246 return true;
5247 }
5248
5249 // Get the coercion type as a literal struct type.
5250 llvm::Type *getType() const {
5251 if (Elems.size() == 1)
5252 return Elems.front();
5253 else
5254 return llvm::StructType::get(Context, Elems);
5255 }
5256 };
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00005257};
5258} // end anonymous namespace
5259
5260ABIArgInfo
5261SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const {
5262 if (Ty->isVoidType())
5263 return ABIArgInfo::getIgnore();
5264
5265 uint64_t Size = getContext().getTypeSize(Ty);
5266
5267 // Anything too big to fit in registers is passed with an explicit indirect
5268 // pointer / sret pointer.
5269 if (Size > SizeLimit)
5270 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
5271
5272 // Treat an enum type as its underlying type.
5273 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5274 Ty = EnumTy->getDecl()->getIntegerType();
5275
5276 // Integer types smaller than a register are extended.
5277 if (Size < 64 && Ty->isIntegerType())
5278 return ABIArgInfo::getExtend();
5279
5280 // Other non-aggregates go in registers.
5281 if (!isAggregateTypeForABI(Ty))
5282 return ABIArgInfo::getDirect();
5283
5284 // This is a small aggregate type that should be passed in registers.
Jakob Stoklund Olesenfc782fb2013-05-28 04:57:37 +00005285 // Build a coercion type from the LLVM struct type.
5286 llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty));
5287 if (!StrTy)
5288 return ABIArgInfo::getDirect();
5289
5290 CoerceBuilder CB(getVMContext(), getDataLayout());
5291 CB.addStruct(0, StrTy);
5292 CB.pad(llvm::RoundUpToAlignment(CB.DL.getTypeSizeInBits(StrTy), 64));
5293
5294 // Try to use the original type for coercion.
5295 llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();
5296
5297 if (CB.InReg)
5298 return ABIArgInfo::getDirectInReg(CoerceTy);
5299 else
5300 return ABIArgInfo::getDirect(CoerceTy);
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00005301}
5302
5303llvm::Value *SparcV9ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5304 CodeGenFunction &CGF) const {
Jakob Stoklund Olesena4b56d32013-06-05 03:00:18 +00005305 ABIArgInfo AI = classifyType(Ty, 16 * 8);
5306 llvm::Type *ArgTy = CGT.ConvertType(Ty);
5307 if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
5308 AI.setCoerceToType(ArgTy);
5309
5310 llvm::Type *BPP = CGF.Int8PtrPtrTy;
5311 CGBuilderTy &Builder = CGF.Builder;
5312 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
5313 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
5314 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
5315 llvm::Value *ArgAddr;
5316 unsigned Stride;
5317
5318 switch (AI.getKind()) {
5319 case ABIArgInfo::Expand:
5320 llvm_unreachable("Unsupported ABI kind for va_arg");
5321
5322 case ABIArgInfo::Extend:
5323 Stride = 8;
5324 ArgAddr = Builder
5325 .CreateConstGEP1_32(Addr, 8 - getDataLayout().getTypeAllocSize(ArgTy),
5326 "extend");
5327 break;
5328
5329 case ABIArgInfo::Direct:
5330 Stride = getDataLayout().getTypeAllocSize(AI.getCoerceToType());
5331 ArgAddr = Addr;
5332 break;
5333
5334 case ABIArgInfo::Indirect:
5335 Stride = 8;
5336 ArgAddr = Builder.CreateBitCast(Addr,
5337 llvm::PointerType::getUnqual(ArgPtrTy),
5338 "indirect");
5339 ArgAddr = Builder.CreateLoad(ArgAddr, "indirect.arg");
5340 break;
5341
5342 case ABIArgInfo::Ignore:
5343 return llvm::UndefValue::get(ArgPtrTy);
5344 }
5345
5346 // Update VAList.
5347 Addr = Builder.CreateConstGEP1_32(Addr, Stride, "ap.next");
5348 Builder.CreateStore(Addr, VAListAddrAsBPP);
5349
5350 return Builder.CreatePointerCast(ArgAddr, ArgPtrTy, "arg.addr");
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00005351}
5352
5353void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const {
5354 FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8);
5355 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
5356 it != ie; ++it)
5357 it->info = classifyType(it->type, 16 * 8);
5358}
5359
5360namespace {
5361class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo {
5362public:
5363 SparcV9TargetCodeGenInfo(CodeGenTypes &CGT)
5364 : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {}
5365};
5366} // end anonymous namespace
5367
5368
Chris Lattnerea044322010-07-29 02:01:43 +00005369const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005370 if (TheTargetCodeGenInfo)
5371 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005372
John McCall64aa4b32013-04-16 22:48:15 +00005373 const llvm::Triple &Triple = getTarget().getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00005374 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00005375 default:
Chris Lattnerea044322010-07-29 02:01:43 +00005376 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00005377
Derek Schuff9ed63f82012-09-06 17:37:28 +00005378 case llvm::Triple::le32:
5379 return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
John McCallaeeb7012010-05-27 06:19:26 +00005380 case llvm::Triple::mips:
5381 case llvm::Triple::mipsel:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00005382 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCallaeeb7012010-05-27 06:19:26 +00005383
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00005384 case llvm::Triple::mips64:
5385 case llvm::Triple::mips64el:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00005386 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00005387
Tim Northoverc264e162013-01-31 12:13:10 +00005388 case llvm::Triple::aarch64:
5389 return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types));
5390
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00005391 case llvm::Triple::arm:
5392 case llvm::Triple::thumb:
Sandeep Patel34c1af82011-04-05 00:23:47 +00005393 {
5394 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
John McCall64aa4b32013-04-16 22:48:15 +00005395 if (strcmp(getTarget().getABI(), "apcs-gnu") == 0)
Sandeep Patel34c1af82011-04-05 00:23:47 +00005396 Kind = ARMABIInfo::APCS;
David Tweedb16abb12012-10-25 13:33:01 +00005397 else if (CodeGenOpts.FloatABI == "hard" ||
John McCall64aa4b32013-04-16 22:48:15 +00005398 (CodeGenOpts.FloatABI != "soft" &&
5399 Triple.getEnvironment() == llvm::Triple::GNUEABIHF))
Sandeep Patel34c1af82011-04-05 00:23:47 +00005400 Kind = ARMABIInfo::AAPCS_VFP;
5401
Derek Schuff263366f2012-10-16 22:30:41 +00005402 switch (Triple.getOS()) {
Eli Bendersky441d9f72012-12-04 18:38:10 +00005403 case llvm::Triple::NaCl:
Derek Schuff263366f2012-10-16 22:30:41 +00005404 return *(TheTargetCodeGenInfo =
5405 new NaClARMTargetCodeGenInfo(Types, Kind));
5406 default:
5407 return *(TheTargetCodeGenInfo =
5408 new ARMTargetCodeGenInfo(Types, Kind));
5409 }
Sandeep Patel34c1af82011-04-05 00:23:47 +00005410 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00005411
John McCallec853ba2010-03-11 00:10:12 +00005412 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00005413 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
Roman Divacky0fbc4b92012-05-09 18:22:46 +00005414 case llvm::Triple::ppc64:
Bill Schmidt2fc107f2012-10-03 19:18:57 +00005415 if (Triple.isOSBinFormatELF())
5416 return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types));
5417 else
5418 return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
Bill Schmidtea7fb0c2013-07-26 01:36:11 +00005419 case llvm::Triple::ppc64le:
5420 assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!");
5421 return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00005422
Peter Collingbourneedb66f32012-05-20 23:28:41 +00005423 case llvm::Triple::nvptx:
5424 case llvm::Triple::nvptx64:
Justin Holewinski2c585b92012-05-24 17:43:12 +00005425 return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types));
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005426
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005427 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00005428 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00005429
Ulrich Weigandb8409212013-05-06 16:26:41 +00005430 case llvm::Triple::systemz:
5431 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
5432
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00005433 case llvm::Triple::tce:
5434 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
5435
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00005436 case llvm::Triple::x86: {
John McCallb8b52972013-06-18 02:46:29 +00005437 bool IsDarwinVectorABI = Triple.isOSDarwin();
5438 bool IsSmallStructInRegABI =
5439 X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
5440 bool IsWin32FloatStructABI = (Triple.getOS() == llvm::Triple::Win32);
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00005441
John McCallb8b52972013-06-18 02:46:29 +00005442 if (Triple.getOS() == llvm::Triple::Win32) {
Eli Friedman55fc7e22012-01-25 22:46:34 +00005443 return *(TheTargetCodeGenInfo =
Reid Kleckner3190ca92013-05-08 13:44:39 +00005444 new WinX86_32TargetCodeGenInfo(Types,
John McCallb8b52972013-06-18 02:46:29 +00005445 IsDarwinVectorABI, IsSmallStructInRegABI,
5446 IsWin32FloatStructABI,
Reid Kleckner3190ca92013-05-08 13:44:39 +00005447 CodeGenOpts.NumRegisterParameters));
John McCallb8b52972013-06-18 02:46:29 +00005448 } else {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005449 return *(TheTargetCodeGenInfo =
John McCallb8b52972013-06-18 02:46:29 +00005450 new X86_32TargetCodeGenInfo(Types,
5451 IsDarwinVectorABI, IsSmallStructInRegABI,
5452 IsWin32FloatStructABI,
Rafael Espindolab48280b2012-07-31 02:44:24 +00005453 CodeGenOpts.NumRegisterParameters));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005454 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00005455 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005456
Eli Friedmanee1ad992011-12-02 00:11:43 +00005457 case llvm::Triple::x86_64: {
John McCall64aa4b32013-04-16 22:48:15 +00005458 bool HasAVX = strcmp(getTarget().getABI(), "avx") == 0;
Eli Friedmanee1ad992011-12-02 00:11:43 +00005459
Chris Lattnerf13721d2010-08-31 16:44:54 +00005460 switch (Triple.getOS()) {
5461 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00005462 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00005463 case llvm::Triple::Cygwin:
5464 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
Eli Bendersky441d9f72012-12-04 18:38:10 +00005465 case llvm::Triple::NaCl:
John McCall64aa4b32013-04-16 22:48:15 +00005466 return *(TheTargetCodeGenInfo = new NaClX86_64TargetCodeGenInfo(Types,
5467 HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00005468 default:
Eli Friedmanee1ad992011-12-02 00:11:43 +00005469 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
5470 HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00005471 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00005472 }
Tony Linthicum96319392011-12-12 21:14:55 +00005473 case llvm::Triple::hexagon:
5474 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00005475 case llvm::Triple::sparcv9:
5476 return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types));
Eli Friedmanee1ad992011-12-02 00:11:43 +00005477 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005478}