blob: 6881ea7fec066e16066584895a47eeb865fd3582 [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
578 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
579 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000580
581 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
582 // Darwin uses different dwarf register numbers for EH.
John McCall64aa4b32013-04-16 22:48:15 +0000583 if (CGM.getTarget().getTriple().isOSDarwin()) return 5;
John McCall6374c332010-03-06 00:35:14 +0000584 return 4;
585 }
586
587 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
588 llvm::Value *Address) const;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000589
Jay Foadef6de3d2011-07-11 09:56:20 +0000590 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000591 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000592 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000593 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
594 }
595
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000596};
597
598}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000599
600/// shouldReturnTypeInRegister - Determine if the given type should be
601/// passed in a register (for the Darwin ABI).
602bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000603 ASTContext &Context,
604 unsigned callingConvention) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000605 uint64_t Size = Context.getTypeSize(Ty);
606
607 // Type must be register sized.
608 if (!isRegisterSize(Size))
609 return false;
610
611 if (Ty->isVectorType()) {
612 // 64- and 128- bit vectors inside structures are not returned in
613 // registers.
614 if (Size == 64 || Size == 128)
615 return false;
616
617 return true;
618 }
619
Daniel Dunbar77115232010-05-15 00:00:30 +0000620 // If this is a builtin, pointer, enum, complex type, member pointer, or
621 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000622 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000623 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000624 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000625 return true;
626
627 // Arrays are treated like records.
628 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000629 return shouldReturnTypeInRegister(AT->getElementType(), Context,
630 callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000631
632 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000633 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000634 if (!RT) return false;
635
Anders Carlssona8874232010-01-27 03:25:19 +0000636 // FIXME: Traverse bases here too.
637
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000638 // For thiscall conventions, structures will never be returned in
639 // a register. This is for compatibility with the MSVC ABI
640 if (callingConvention == llvm::CallingConv::X86_ThisCall &&
641 RT->isStructureType()) {
642 return false;
643 }
644
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000645 // Structure types are passed in register if all fields would be
646 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000647 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
648 e = RT->getDecl()->field_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000649 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000650
651 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000652 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000653 continue;
654
655 // Check fields recursively.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000656 if (!shouldReturnTypeInRegister(FD->getType(), Context,
657 callingConvention))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000658 return false;
659 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000660 return true;
661}
662
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000663ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
664 unsigned callingConvention) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000665 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000666 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000667
Chris Lattnera3c109b2010-07-29 02:16:43 +0000668 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000669 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000670 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000671 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000672
673 // 128-bit vectors are a special case; they are returned in
674 // registers and we need to make sure to pick a type the LLVM
675 // backend will like.
676 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +0000677 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000678 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000679
680 // Always return in register if it fits in a general purpose
681 // register, or if it is 64 bits and has a single element.
682 if ((Size == 8 || Size == 16 || Size == 32) ||
683 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +0000684 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000685 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000686
687 return ABIArgInfo::getIndirect(0);
688 }
689
690 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000691 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000692
John McCalld608cdb2010-08-22 10:59:02 +0000693 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000694 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000695 if (isRecordReturnIndirect(RT, CGT))
Anders Carlsson40092972009-10-20 22:07:59 +0000696 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000697
Anders Carlsson40092972009-10-20 22:07:59 +0000698 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000699 if (RT->getDecl()->hasFlexibleArrayMember())
700 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000701 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000702
David Chisnall1e4249c2009-08-17 23:08:21 +0000703 // If specified, structs and unions are always indirect.
704 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000705 return ABIArgInfo::getIndirect(0);
706
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000707 // Small structures which are register sized are generally returned
708 // in a register.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000709 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(),
710 callingConvention)) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000711 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000712
713 // As a special-case, if the struct is a "single-element" struct, and
714 // the field is of type "float" or "double", return it in a
Eli Friedman55fc7e22012-01-25 22:46:34 +0000715 // floating-point register. (MSVC does not apply this special case.)
716 // We apply a similar transformation for pointer types to improve the
717 // quality of the generated IR.
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000718 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000719 if ((!IsWin32StructABI && SeltTy->isRealFloatingType())
Eli Friedman55fc7e22012-01-25 22:46:34 +0000720 || SeltTy->hasPointerRepresentation())
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000721 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
722
723 // FIXME: We should be able to narrow this integer in cases with dead
724 // padding.
Chris Lattner800588f2010-07-29 06:26:06 +0000725 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000726 }
727
728 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000729 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000730
Chris Lattnera3c109b2010-07-29 02:16:43 +0000731 // Treat an enum type as its underlying type.
732 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
733 RetTy = EnumTy->getDecl()->getIntegerType();
734
735 return (RetTy->isPromotableIntegerType() ?
736 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000737}
738
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000739static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
740 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
741}
742
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000743static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
744 const RecordType *RT = Ty->getAs<RecordType>();
745 if (!RT)
746 return 0;
747 const RecordDecl *RD = RT->getDecl();
748
749 // If this is a C++ record, check the bases first.
750 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
751 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
752 e = CXXRD->bases_end(); i != e; ++i)
753 if (!isRecordWithSSEVectorType(Context, i->getType()))
754 return false;
755
756 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
757 i != e; ++i) {
758 QualType FT = i->getType();
759
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000760 if (isSSEVectorType(Context, FT))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000761 return true;
762
763 if (isRecordWithSSEVectorType(Context, FT))
764 return true;
765 }
766
767 return false;
768}
769
Daniel Dunbare59d8582010-09-16 20:42:06 +0000770unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
771 unsigned Align) const {
772 // Otherwise, if the alignment is less than or equal to the minimum ABI
773 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000774 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +0000775 return 0; // Use default alignment.
776
777 // On non-Darwin, the stack type alignment is always 4.
778 if (!IsDarwinVectorABI) {
779 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000780 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +0000781 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000782
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000783 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000784 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
785 isRecordWithSSEVectorType(getContext(), Ty)))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000786 return 16;
787
788 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000789}
790
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000791ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
792 unsigned &FreeRegs) const {
793 if (!ByVal) {
794 if (FreeRegs) {
795 --FreeRegs; // Non byval indirects just use one pointer.
796 return ABIArgInfo::getIndirectInReg(0, false);
797 }
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000798 return ABIArgInfo::getIndirect(0, false);
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000799 }
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000800
Daniel Dunbare59d8582010-09-16 20:42:06 +0000801 // Compute the byval alignment.
802 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
803 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
804 if (StackAlign == 0)
Chris Lattnerde92d732011-05-22 23:35:00 +0000805 return ABIArgInfo::getIndirect(4);
Daniel Dunbare59d8582010-09-16 20:42:06 +0000806
807 // If the stack alignment is less than the type alignment, realign the
808 // argument.
809 if (StackAlign < TypeAlign)
810 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
811 /*Realign=*/true);
812
813 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000814}
815
Rafael Espindolab48280b2012-07-31 02:44:24 +0000816X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
817 const Type *T = isSingleElementStruct(Ty, getContext());
818 if (!T)
819 T = Ty.getTypePtr();
820
821 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
822 BuiltinType::Kind K = BT->getKind();
823 if (K == BuiltinType::Float || K == BuiltinType::Double)
824 return Float;
825 }
826 return Integer;
827}
828
Rafael Espindolab6932692012-10-24 01:58:58 +0000829bool X86_32ABIInfo::shouldUseInReg(QualType Ty, unsigned &FreeRegs,
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000830 bool IsFastCall, bool &NeedsPadding) const {
831 NeedsPadding = false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000832 Class C = classify(Ty);
833 if (C == Float)
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000834 return false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000835
Rafael Espindolab6932692012-10-24 01:58:58 +0000836 unsigned Size = getContext().getTypeSize(Ty);
837 unsigned SizeInRegs = (Size + 31) / 32;
Rafael Espindola5f14fcb2012-10-23 02:04:01 +0000838
839 if (SizeInRegs == 0)
840 return false;
841
Rafael Espindolab48280b2012-07-31 02:44:24 +0000842 if (SizeInRegs > FreeRegs) {
843 FreeRegs = 0;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000844 return false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000845 }
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000846
Rafael Espindolab48280b2012-07-31 02:44:24 +0000847 FreeRegs -= SizeInRegs;
Rafael Espindolab6932692012-10-24 01:58:58 +0000848
849 if (IsFastCall) {
850 if (Size > 32)
851 return false;
852
853 if (Ty->isIntegralOrEnumerationType())
854 return true;
855
856 if (Ty->isPointerType())
857 return true;
858
859 if (Ty->isReferenceType())
860 return true;
861
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000862 if (FreeRegs)
863 NeedsPadding = true;
864
Rafael Espindolab6932692012-10-24 01:58:58 +0000865 return false;
866 }
867
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000868 return true;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000869}
870
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000871ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Rafael Espindolab6932692012-10-24 01:58:58 +0000872 unsigned &FreeRegs,
873 bool IsFastCall) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000874 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000875 if (isAggregateTypeForABI(Ty)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000876 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000877 if (IsWin32StructABI)
878 return getIndirectResult(Ty, true, FreeRegs);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000879
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000880 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, CGT))
881 return getIndirectResult(Ty, RAA == CGCXXABI::RAA_DirectInMemory, FreeRegs);
882
883 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000884 if (RT->getDecl()->hasFlexibleArrayMember())
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000885 return getIndirectResult(Ty, true, FreeRegs);
Anders Carlssona8874232010-01-27 03:25:19 +0000886 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000887
Eli Friedman5a4d3522011-11-18 00:28:11 +0000888 // Ignore empty structs/unions.
Eli Friedman5a1ac892011-11-18 04:01:36 +0000889 if (isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000890 return ABIArgInfo::getIgnore();
891
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000892 llvm::LLVMContext &LLVMContext = getVMContext();
893 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
894 bool NeedsPadding;
895 if (shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding)) {
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000896 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000897 SmallVector<llvm::Type*, 3> Elements;
898 for (unsigned I = 0; I < SizeInRegs; ++I)
899 Elements.push_back(Int32);
900 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
901 return ABIArgInfo::getDirectInReg(Result);
902 }
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000903 llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : 0;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000904
Daniel Dunbar53012f42009-11-09 01:33:53 +0000905 // Expand small (<= 128-bit) record types when we know that the stack layout
906 // of those arguments will match the struct. This is important because the
907 // LLVM backend isn't smart enough to remove byval, which inhibits many
908 // optimizations.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000909 if (getContext().getTypeSize(Ty) <= 4*32 &&
910 canExpandIndirectArgument(Ty, getContext()))
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000911 return ABIArgInfo::getExpandWithPadding(IsFastCall, PaddingType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000912
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000913 return getIndirectResult(Ty, true, FreeRegs);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000914 }
915
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000916 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +0000917 // On Darwin, some vectors are passed in memory, we handle this by passing
918 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000919 if (IsDarwinVectorABI) {
920 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000921 if ((Size == 8 || Size == 16 || Size == 32) ||
922 (Size == 64 && VT->getNumElements() == 1))
923 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
924 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000925 }
Bill Wendlingbb465d72010-10-18 03:41:31 +0000926
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000927 if (IsX86_MMXType(CGT.ConvertType(Ty)))
928 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64));
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000929
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000930 return ABIArgInfo::getDirect();
931 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000932
933
Chris Lattnera3c109b2010-07-29 02:16:43 +0000934 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
935 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000936
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000937 bool NeedsPadding;
938 bool InReg = shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding);
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000939
940 if (Ty->isPromotableIntegerType()) {
941 if (InReg)
942 return ABIArgInfo::getExtendInReg();
943 return ABIArgInfo::getExtend();
944 }
945 if (InReg)
946 return ABIArgInfo::getDirectInReg();
947 return ABIArgInfo::getDirect();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000948}
949
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000950void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
951 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
952 FI.getCallingConvention());
Rafael Espindolab48280b2012-07-31 02:44:24 +0000953
Rafael Espindolab6932692012-10-24 01:58:58 +0000954 unsigned CC = FI.getCallingConvention();
955 bool IsFastCall = CC == llvm::CallingConv::X86_FastCall;
956 unsigned FreeRegs;
957 if (IsFastCall)
958 FreeRegs = 2;
959 else if (FI.getHasRegParm())
960 FreeRegs = FI.getRegParm();
961 else
962 FreeRegs = DefaultNumRegisterParameters;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000963
964 // If the return value is indirect, then the hidden argument is consuming one
965 // integer register.
966 if (FI.getReturnInfo().isIndirect() && FreeRegs) {
967 --FreeRegs;
968 ABIArgInfo &Old = FI.getReturnInfo();
969 Old = ABIArgInfo::getIndirectInReg(Old.getIndirectAlign(),
970 Old.getIndirectByVal(),
971 Old.getIndirectRealign());
972 }
973
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000974 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
975 it != ie; ++it)
Rafael Espindolab6932692012-10-24 01:58:58 +0000976 it->info = classifyArgumentType(it->type, FreeRegs, IsFastCall);
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000977}
978
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000979llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
980 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +0000981 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000982
983 CGBuilderTy &Builder = CGF.Builder;
984 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
985 "ap");
986 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Eli Friedman7b1fb812011-11-18 02:12:09 +0000987
988 // Compute if the address needs to be aligned
989 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
990 Align = getTypeStackAlignInBytes(Ty, Align);
991 Align = std::max(Align, 4U);
992 if (Align > 4) {
993 // addr = (addr + align - 1) & -align;
994 llvm::Value *Offset =
995 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
996 Addr = CGF.Builder.CreateGEP(Addr, Offset);
997 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
998 CGF.Int32Ty);
999 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
1000 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1001 Addr->getType(),
1002 "ap.cur.aligned");
1003 }
1004
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001005 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00001006 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001007 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1008
1009 uint64_t Offset =
Eli Friedman7b1fb812011-11-18 02:12:09 +00001010 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001011 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00001012 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001013 "ap.next");
1014 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1015
1016 return AddrTyped;
1017}
1018
Charles Davis74f72932010-02-13 15:54:06 +00001019void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
1020 llvm::GlobalValue *GV,
1021 CodeGen::CodeGenModule &CGM) const {
1022 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1023 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
1024 // Get the LLVM function.
1025 llvm::Function *Fn = cast<llvm::Function>(GV);
1026
1027 // Now add the 'alignstack' attribute with a value of 16.
Bill Wendling0d583392012-10-15 20:36:26 +00001028 llvm::AttrBuilder B;
Bill Wendlinge91e9ec2012-10-14 03:28:14 +00001029 B.addStackAlignmentAttr(16);
Bill Wendling909b6de2013-01-23 00:21:06 +00001030 Fn->addAttributes(llvm::AttributeSet::FunctionIndex,
1031 llvm::AttributeSet::get(CGM.getLLVMContext(),
1032 llvm::AttributeSet::FunctionIndex,
1033 B));
Charles Davis74f72932010-02-13 15:54:06 +00001034 }
1035 }
1036}
1037
John McCall6374c332010-03-06 00:35:14 +00001038bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
1039 CodeGen::CodeGenFunction &CGF,
1040 llvm::Value *Address) const {
1041 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCall6374c332010-03-06 00:35:14 +00001042
Chris Lattner8b418682012-02-07 00:39:47 +00001043 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001044
John McCall6374c332010-03-06 00:35:14 +00001045 // 0-7 are the eight integer registers; the order is different
1046 // on Darwin (for EH), but the range is the same.
1047 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +00001048 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +00001049
John McCall64aa4b32013-04-16 22:48:15 +00001050 if (CGF.CGM.getTarget().getTriple().isOSDarwin()) {
John McCall6374c332010-03-06 00:35:14 +00001051 // 12-16 are st(0..4). Not sure why we stop at 4.
1052 // These have size 16, which is sizeof(long double) on
1053 // platforms with 8-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001054 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCallaeeb7012010-05-27 06:19:26 +00001055 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001056
John McCall6374c332010-03-06 00:35:14 +00001057 } else {
1058 // 9 is %eflags, which doesn't get a size on Darwin for some
1059 // reason.
1060 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
1061
1062 // 11-16 are st(0..5). Not sure why we stop at 5.
1063 // These have size 12, which is sizeof(long double) on
1064 // platforms with 4-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001065 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCallaeeb7012010-05-27 06:19:26 +00001066 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1067 }
John McCall6374c332010-03-06 00:35:14 +00001068
1069 return false;
1070}
1071
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001072//===----------------------------------------------------------------------===//
1073// X86-64 ABI Implementation
1074//===----------------------------------------------------------------------===//
1075
1076
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001077namespace {
1078/// X86_64ABIInfo - The X86_64 ABI information.
1079class X86_64ABIInfo : public ABIInfo {
1080 enum Class {
1081 Integer = 0,
1082 SSE,
1083 SSEUp,
1084 X87,
1085 X87Up,
1086 ComplexX87,
1087 NoClass,
1088 Memory
1089 };
1090
1091 /// merge - Implement the X86_64 ABI merging algorithm.
1092 ///
1093 /// Merge an accumulating classification \arg Accum with a field
1094 /// classification \arg Field.
1095 ///
1096 /// \param Accum - The accumulating classification. This should
1097 /// always be either NoClass or the result of a previous merge
1098 /// call. In addition, this should never be Memory (the caller
1099 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001100 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001101
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001102 /// postMerge - Implement the X86_64 ABI post merging algorithm.
1103 ///
1104 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
1105 /// final MEMORY or SSE classes when necessary.
1106 ///
1107 /// \param AggregateSize - The size of the current aggregate in
1108 /// the classification process.
1109 ///
1110 /// \param Lo - The classification for the parts of the type
1111 /// residing in the low word of the containing object.
1112 ///
1113 /// \param Hi - The classification for the parts of the type
1114 /// residing in the higher words of the containing object.
1115 ///
1116 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
1117
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001118 /// classify - Determine the x86_64 register classes in which the
1119 /// given type T should be passed.
1120 ///
1121 /// \param Lo - The classification for the parts of the type
1122 /// residing in the low word of the containing object.
1123 ///
1124 /// \param Hi - The classification for the parts of the type
1125 /// residing in the high word of the containing object.
1126 ///
1127 /// \param OffsetBase - The bit offset of this type in the
1128 /// containing object. Some parameters are classified different
1129 /// depending on whether they straddle an eightbyte boundary.
1130 ///
1131 /// If a word is unused its result will be NoClass; if a type should
1132 /// be passed in Memory then at least the classification of \arg Lo
1133 /// will be Memory.
1134 ///
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001135 /// The \arg Lo class will be NoClass iff the argument is ignored.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001136 ///
1137 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
1138 /// also be ComplexX87.
Chris Lattner9c254f02010-06-29 06:01:59 +00001139 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001140
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001141 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001142 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
1143 unsigned IROffset, QualType SourceTy,
1144 unsigned SourceOffset) const;
1145 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
1146 unsigned IROffset, QualType SourceTy,
1147 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001148
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001149 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001150 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +00001151 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001152
1153 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001154 /// such that the argument will be passed in memory.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001155 ///
1156 /// \param freeIntRegs - The number of free integer registers remaining
1157 /// available.
1158 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001159
Chris Lattnera3c109b2010-07-29 02:16:43 +00001160 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001161
Bill Wendlingbb465d72010-10-18 03:41:31 +00001162 ABIArgInfo classifyArgumentType(QualType Ty,
Daniel Dunbaredfac032012-03-10 01:03:58 +00001163 unsigned freeIntRegs,
Bill Wendlingbb465d72010-10-18 03:41:31 +00001164 unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +00001165 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001166
Eli Friedmanee1ad992011-12-02 00:11:43 +00001167 bool IsIllegalVectorType(QualType Ty) const;
1168
John McCall67a57732011-04-21 01:20:55 +00001169 /// The 0.98 ABI revision clarified a lot of ambiguities,
1170 /// unfortunately in ways that were not always consistent with
1171 /// certain previous compilers. In particular, platforms which
1172 /// required strict binary compatibility with older versions of GCC
1173 /// may need to exempt themselves.
1174 bool honorsRevision0_98() const {
John McCall64aa4b32013-04-16 22:48:15 +00001175 return !getTarget().getTriple().isOSDarwin();
John McCall67a57732011-04-21 01:20:55 +00001176 }
1177
Eli Friedmanee1ad992011-12-02 00:11:43 +00001178 bool HasAVX;
Derek Schuffbabaf312012-10-11 15:52:22 +00001179 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
1180 // 64-bit hardware.
1181 bool Has64BitPointers;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001182
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001183public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001184 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
Derek Schuffbabaf312012-10-11 15:52:22 +00001185 ABIInfo(CGT), HasAVX(hasavx),
Derek Schuff90da80c2012-10-11 18:21:13 +00001186 Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001187 }
Chris Lattner9c254f02010-06-29 06:01:59 +00001188
John McCallde5d3c72012-02-17 03:33:10 +00001189 bool isPassedUsingAVXType(QualType type) const {
1190 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00001191 // The freeIntRegs argument doesn't matter here.
1192 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE);
John McCallde5d3c72012-02-17 03:33:10 +00001193 if (info.isDirect()) {
1194 llvm::Type *ty = info.getCoerceToType();
1195 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
1196 return (vectorTy->getBitWidth() > 128);
1197 }
1198 return false;
1199 }
1200
Chris Lattneree5dcd02010-07-29 02:31:05 +00001201 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001202
1203 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1204 CodeGenFunction &CGF) const;
1205};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001206
Chris Lattnerf13721d2010-08-31 16:44:54 +00001207/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001208class WinX86_64ABIInfo : public ABIInfo {
1209
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00001210 ABIArgInfo classify(QualType Ty, bool IsReturnType) const;
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001211
Chris Lattnerf13721d2010-08-31 16:44:54 +00001212public:
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001213 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
1214
1215 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattnerf13721d2010-08-31 16:44:54 +00001216
1217 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1218 CodeGenFunction &CGF) const;
1219};
1220
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001221class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1222public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001223 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
Derek Schuffbabaf312012-10-11 15:52:22 +00001224 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
John McCall6374c332010-03-06 00:35:14 +00001225
John McCallde5d3c72012-02-17 03:33:10 +00001226 const X86_64ABIInfo &getABIInfo() const {
1227 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
1228 }
1229
John McCall6374c332010-03-06 00:35:14 +00001230 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1231 return 7;
1232 }
1233
1234 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1235 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001236 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001237
John McCallaeeb7012010-05-27 06:19:26 +00001238 // 0-15 are the 16 integer registers.
1239 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001240 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +00001241 return false;
1242 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001243
Jay Foadef6de3d2011-07-11 09:56:20 +00001244 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001245 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +00001246 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001247 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1248 }
1249
John McCallde5d3c72012-02-17 03:33:10 +00001250 bool isNoProtoCallVariadic(const CallArgList &args,
1251 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +00001252 // The default CC on x86-64 sets %al to the number of SSA
1253 // registers used, and GCC sets this when calling an unprototyped
Eli Friedman3ed79032011-12-01 04:53:19 +00001254 // function, so we override the default behavior. However, don't do
Eli Friedman68805fe2011-12-06 03:08:26 +00001255 // that when AVX types are involved: the ABI explicitly states it is
1256 // undefined, and it doesn't work in practice because of how the ABI
1257 // defines varargs anyway.
John McCallde5d3c72012-02-17 03:33:10 +00001258 if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) {
Eli Friedman3ed79032011-12-01 04:53:19 +00001259 bool HasAVXType = false;
John McCallde5d3c72012-02-17 03:33:10 +00001260 for (CallArgList::const_iterator
1261 it = args.begin(), ie = args.end(); it != ie; ++it) {
1262 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1263 HasAVXType = true;
1264 break;
Eli Friedman3ed79032011-12-01 04:53:19 +00001265 }
1266 }
John McCallde5d3c72012-02-17 03:33:10 +00001267
Eli Friedman3ed79032011-12-01 04:53:19 +00001268 if (!HasAVXType)
1269 return true;
1270 }
John McCall01f151e2011-09-21 08:08:30 +00001271
John McCallde5d3c72012-02-17 03:33:10 +00001272 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
John McCall01f151e2011-09-21 08:08:30 +00001273 }
1274
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001275};
1276
Aaron Ballman89735b92013-05-24 15:06:56 +00001277static std::string qualifyWindowsLibrary(llvm::StringRef Lib) {
1278 // If the argument does not end in .lib, automatically add the suffix. This
1279 // matches the behavior of MSVC.
1280 std::string ArgStr = Lib;
1281 if (Lib.size() <= 4 ||
1282 Lib.substr(Lib.size() - 4).compare_lower(".lib") != 0) {
1283 ArgStr += ".lib";
1284 }
1285 return ArgStr;
1286}
1287
Reid Kleckner3190ca92013-05-08 13:44:39 +00001288class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo {
1289public:
1290 WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned RegParms)
1291 : X86_32TargetCodeGenInfo(CGT, false, true, true, RegParms) {}
1292
1293 void getDependentLibraryOption(llvm::StringRef Lib,
1294 llvm::SmallString<24> &Opt) const {
1295 Opt = "/DEFAULTLIB:";
Aaron Ballman89735b92013-05-24 15:06:56 +00001296 Opt += qualifyWindowsLibrary(Lib);
Reid Kleckner3190ca92013-05-08 13:44:39 +00001297 }
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00001298
1299 void getDetectMismatchOption(llvm::StringRef Name,
1300 llvm::StringRef Value,
1301 llvm::SmallString<32> &Opt) const {
1302 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
1303 }
Reid Kleckner3190ca92013-05-08 13:44:39 +00001304};
1305
Chris Lattnerf13721d2010-08-31 16:44:54 +00001306class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1307public:
1308 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1309 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1310
1311 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1312 return 7;
1313 }
1314
1315 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1316 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001317 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001318
Chris Lattnerf13721d2010-08-31 16:44:54 +00001319 // 0-15 are the 16 integer registers.
1320 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001321 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattnerf13721d2010-08-31 16:44:54 +00001322 return false;
1323 }
Reid Kleckner3190ca92013-05-08 13:44:39 +00001324
1325 void getDependentLibraryOption(llvm::StringRef Lib,
1326 llvm::SmallString<24> &Opt) const {
1327 Opt = "/DEFAULTLIB:";
Aaron Ballman89735b92013-05-24 15:06:56 +00001328 Opt += qualifyWindowsLibrary(Lib);
Reid Kleckner3190ca92013-05-08 13:44:39 +00001329 }
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00001330
1331 void getDetectMismatchOption(llvm::StringRef Name,
1332 llvm::StringRef Value,
1333 llvm::SmallString<32> &Opt) const {
1334 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
1335 }
Chris Lattnerf13721d2010-08-31 16:44:54 +00001336};
1337
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001338}
1339
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001340void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1341 Class &Hi) const {
1342 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1343 //
1344 // (a) If one of the classes is Memory, the whole argument is passed in
1345 // memory.
1346 //
1347 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1348 // memory.
1349 //
1350 // (c) If the size of the aggregate exceeds two eightbytes and the first
1351 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1352 // argument is passed in memory. NOTE: This is necessary to keep the
1353 // ABI working for processors that don't support the __m256 type.
1354 //
1355 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1356 //
1357 // Some of these are enforced by the merging logic. Others can arise
1358 // only with unions; for example:
1359 // union { _Complex double; unsigned; }
1360 //
1361 // Note that clauses (b) and (c) were added in 0.98.
1362 //
1363 if (Hi == Memory)
1364 Lo = Memory;
1365 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1366 Lo = Memory;
1367 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1368 Lo = Memory;
1369 if (Hi == SSEUp && Lo != SSE)
1370 Hi = SSE;
1371}
1372
Chris Lattner1090a9b2010-06-28 21:43:59 +00001373X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001374 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1375 // classified recursively so that always two fields are
1376 // considered. The resulting class is calculated according to
1377 // the classes of the fields in the eightbyte:
1378 //
1379 // (a) If both classes are equal, this is the resulting class.
1380 //
1381 // (b) If one of the classes is NO_CLASS, the resulting class is
1382 // the other class.
1383 //
1384 // (c) If one of the classes is MEMORY, the result is the MEMORY
1385 // class.
1386 //
1387 // (d) If one of the classes is INTEGER, the result is the
1388 // INTEGER.
1389 //
1390 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1391 // MEMORY is used as class.
1392 //
1393 // (f) Otherwise class SSE is used.
1394
1395 // Accum should never be memory (we should have returned) or
1396 // ComplexX87 (because this cannot be passed in a structure).
1397 assert((Accum != Memory && Accum != ComplexX87) &&
1398 "Invalid accumulated classification during merge.");
1399 if (Accum == Field || Field == NoClass)
1400 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001401 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001402 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001403 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001404 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001405 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001406 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001407 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1408 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001409 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001410 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001411}
1412
Chris Lattnerbcaedae2010-06-30 19:14:05 +00001413void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001414 Class &Lo, Class &Hi) const {
1415 // FIXME: This code can be simplified by introducing a simple value class for
1416 // Class pairs with appropriate constructor methods for the various
1417 // situations.
1418
1419 // FIXME: Some of the split computations are wrong; unaligned vectors
1420 // shouldn't be passed in registers for example, so there is no chance they
1421 // can straddle an eightbyte. Verify & simplify.
1422
1423 Lo = Hi = NoClass;
1424
1425 Class &Current = OffsetBase < 64 ? Lo : Hi;
1426 Current = Memory;
1427
John McCall183700f2009-09-21 23:43:11 +00001428 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001429 BuiltinType::Kind k = BT->getKind();
1430
1431 if (k == BuiltinType::Void) {
1432 Current = NoClass;
1433 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1434 Lo = Integer;
1435 Hi = Integer;
1436 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1437 Current = Integer;
Derek Schuff7da46f92012-10-11 16:55:58 +00001438 } else if ((k == BuiltinType::Float || k == BuiltinType::Double) ||
1439 (k == BuiltinType::LongDouble &&
John McCall64aa4b32013-04-16 22:48:15 +00001440 getTarget().getTriple().getOS() == llvm::Triple::NaCl)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001441 Current = SSE;
1442 } else if (k == BuiltinType::LongDouble) {
1443 Lo = X87;
1444 Hi = X87Up;
1445 }
1446 // FIXME: _Decimal32 and _Decimal64 are SSE.
1447 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001448 return;
1449 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001450
Chris Lattner1090a9b2010-06-28 21:43:59 +00001451 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001452 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +00001453 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001454 return;
1455 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001456
Chris Lattner1090a9b2010-06-28 21:43:59 +00001457 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001458 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001459 return;
1460 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001461
Chris Lattner1090a9b2010-06-28 21:43:59 +00001462 if (Ty->isMemberPointerType()) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001463 if (Ty->isMemberFunctionPointerType() && Has64BitPointers)
Daniel Dunbar67d438d2010-05-15 00:00:37 +00001464 Lo = Hi = Integer;
1465 else
1466 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 (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001471 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001472 if (Size == 32) {
1473 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1474 // float> as integer.
1475 Current = Integer;
1476
1477 // If this type crosses an eightbyte boundary, it should be
1478 // split.
1479 uint64_t EB_Real = (OffsetBase) / 64;
1480 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1481 if (EB_Real != EB_Imag)
1482 Hi = Lo;
1483 } else if (Size == 64) {
1484 // gcc passes <1 x double> in memory. :(
1485 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1486 return;
1487
1488 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001489 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001490 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1491 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1492 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001493 Current = Integer;
1494 else
1495 Current = SSE;
1496
1497 // If this type crosses an eightbyte boundary, it should be
1498 // split.
1499 if (OffsetBase && OffsetBase != 64)
1500 Hi = Lo;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001501 } else if (Size == 128 || (HasAVX && Size == 256)) {
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001502 // Arguments of 256-bits are split into four eightbyte chunks. The
1503 // least significant one belongs to class SSE and all the others to class
1504 // SSEUP. The original Lo and Hi design considers that types can't be
1505 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1506 // This design isn't correct for 256-bits, but since there're no cases
1507 // where the upper parts would need to be inspected, avoid adding
1508 // complexity and just consider Hi to match the 64-256 part.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001509 Lo = SSE;
1510 Hi = SSEUp;
1511 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001512 return;
1513 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001514
Chris Lattner1090a9b2010-06-28 21:43:59 +00001515 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001516 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001517
Chris Lattnerea044322010-07-29 02:01:43 +00001518 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001519 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001520 if (Size <= 64)
1521 Current = Integer;
1522 else if (Size <= 128)
1523 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001524 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001525 Current = SSE;
Derek Schuff7da46f92012-10-11 16:55:58 +00001526 else if (ET == getContext().DoubleTy ||
1527 (ET == getContext().LongDoubleTy &&
John McCall64aa4b32013-04-16 22:48:15 +00001528 getTarget().getTriple().getOS() == llvm::Triple::NaCl))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001529 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001530 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001531 Current = ComplexX87;
1532
1533 // If this complex type crosses an eightbyte boundary then it
1534 // should be split.
1535 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001536 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001537 if (Hi == NoClass && EB_Real != EB_Imag)
1538 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001539
Chris Lattner1090a9b2010-06-28 21:43:59 +00001540 return;
1541 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001542
Chris Lattnerea044322010-07-29 02:01:43 +00001543 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001544 // Arrays are treated like structures.
1545
Chris Lattnerea044322010-07-29 02:01:43 +00001546 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001547
1548 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001549 // than four eightbytes, ..., it has class MEMORY.
1550 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001551 return;
1552
1553 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1554 // fields, it has class MEMORY.
1555 //
1556 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001557 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001558 return;
1559
1560 // Otherwise implement simplified merge. We could be smarter about
1561 // this, but it isn't worth it and would be harder to verify.
1562 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001563 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001564 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes089d8922011-07-12 01:27:38 +00001565
1566 // The only case a 256-bit wide vector could be used is when the array
1567 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1568 // to work for sizes wider than 128, early check and fallback to memory.
1569 if (Size > 128 && EltSize != 256)
1570 return;
1571
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001572 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1573 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001574 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001575 Lo = merge(Lo, FieldLo);
1576 Hi = merge(Hi, FieldHi);
1577 if (Lo == Memory || Hi == Memory)
1578 break;
1579 }
1580
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001581 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001582 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001583 return;
1584 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001585
Chris Lattner1090a9b2010-06-28 21:43:59 +00001586 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001587 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001588
1589 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001590 // than four eightbytes, ..., it has class MEMORY.
1591 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001592 return;
1593
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001594 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1595 // copy constructor or a non-trivial destructor, it is passed by invisible
1596 // reference.
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00001597 if (getRecordArgABI(RT, CGT))
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001598 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001599
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001600 const RecordDecl *RD = RT->getDecl();
1601
1602 // Assume variable sized types are passed in memory.
1603 if (RD->hasFlexibleArrayMember())
1604 return;
1605
Chris Lattnerea044322010-07-29 02:01:43 +00001606 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001607
1608 // Reset Lo class, this will be recomputed.
1609 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001610
1611 // If this is a C++ record, classify the bases first.
1612 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1613 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1614 e = CXXRD->bases_end(); i != e; ++i) {
1615 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1616 "Unexpected base class!");
1617 const CXXRecordDecl *Base =
1618 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1619
1620 // Classify this field.
1621 //
1622 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1623 // single eightbyte, each is classified separately. Each eightbyte gets
1624 // initialized to class NO_CLASS.
1625 Class FieldLo, FieldHi;
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001626 uint64_t Offset =
1627 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
Chris Lattner9c254f02010-06-29 06:01:59 +00001628 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001629 Lo = merge(Lo, FieldLo);
1630 Hi = merge(Hi, FieldHi);
1631 if (Lo == Memory || Hi == Memory)
1632 break;
1633 }
1634 }
1635
1636 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001637 unsigned idx = 0;
Bruno Cardoso Lopes548e4782011-07-12 22:30:58 +00001638 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001639 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001640 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1641 bool BitField = i->isBitField();
1642
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001643 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1644 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001645 //
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001646 // The only case a 256-bit wide vector could be used is when the struct
1647 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1648 // to work for sizes wider than 128, early check and fallback to memory.
1649 //
1650 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1651 Lo = Memory;
1652 return;
1653 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001654 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001655 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001656 Lo = Memory;
1657 return;
1658 }
1659
1660 // Classify this field.
1661 //
1662 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1663 // exceeds a single eightbyte, each is classified
1664 // separately. Each eightbyte gets initialized to class
1665 // NO_CLASS.
1666 Class FieldLo, FieldHi;
1667
1668 // Bit-fields require special handling, they do not force the
1669 // structure to be passed in memory even if unaligned, and
1670 // therefore they can straddle an eightbyte.
1671 if (BitField) {
1672 // Ignore padding bit-fields.
1673 if (i->isUnnamedBitfield())
1674 continue;
1675
1676 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001677 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001678
1679 uint64_t EB_Lo = Offset / 64;
1680 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1681 FieldLo = FieldHi = NoClass;
1682 if (EB_Lo) {
1683 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1684 FieldLo = NoClass;
1685 FieldHi = Integer;
1686 } else {
1687 FieldLo = Integer;
1688 FieldHi = EB_Hi ? Integer : NoClass;
1689 }
1690 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001691 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001692 Lo = merge(Lo, FieldLo);
1693 Hi = merge(Hi, FieldHi);
1694 if (Lo == Memory || Hi == Memory)
1695 break;
1696 }
1697
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001698 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001699 }
1700}
1701
Chris Lattner9c254f02010-06-29 06:01:59 +00001702ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001703 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1704 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001705 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001706 // Treat an enum type as its underlying type.
1707 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1708 Ty = EnumTy->getDecl()->getIntegerType();
1709
1710 return (Ty->isPromotableIntegerType() ?
1711 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1712 }
1713
1714 return ABIArgInfo::getIndirect(0);
1715}
1716
Eli Friedmanee1ad992011-12-02 00:11:43 +00001717bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1718 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1719 uint64_t Size = getContext().getTypeSize(VecTy);
1720 unsigned LargestVector = HasAVX ? 256 : 128;
1721 if (Size <= 64 || Size > LargestVector)
1722 return true;
1723 }
1724
1725 return false;
1726}
1727
Daniel Dunbaredfac032012-03-10 01:03:58 +00001728ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1729 unsigned freeIntRegs) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001730 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1731 // place naturally.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001732 //
1733 // This assumption is optimistic, as there could be free registers available
1734 // when we need to pass this argument in memory, and LLVM could try to pass
1735 // the argument in the free register. This does not seem to happen currently,
1736 // but this code would be much safer if we could mark the argument with
1737 // 'onstack'. See PR12193.
Eli Friedmanee1ad992011-12-02 00:11:43 +00001738 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001739 // Treat an enum type as its underlying type.
1740 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1741 Ty = EnumTy->getDecl()->getIntegerType();
1742
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001743 return (Ty->isPromotableIntegerType() ?
1744 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001745 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001746
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00001747 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
1748 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001749
Chris Lattner855d2272011-05-22 23:21:23 +00001750 // Compute the byval alignment. We specify the alignment of the byval in all
1751 // cases so that the mid-level optimizer knows the alignment of the byval.
1752 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
Daniel Dunbaredfac032012-03-10 01:03:58 +00001753
1754 // Attempt to avoid passing indirect results using byval when possible. This
1755 // is important for good codegen.
1756 //
1757 // We do this by coercing the value into a scalar type which the backend can
1758 // handle naturally (i.e., without using byval).
1759 //
1760 // For simplicity, we currently only do this when we have exhausted all of the
1761 // free integer registers. Doing this when there are free integer registers
1762 // would require more care, as we would have to ensure that the coerced value
1763 // did not claim the unused register. That would require either reording the
1764 // arguments to the function (so that any subsequent inreg values came first),
1765 // or only doing this optimization when there were no following arguments that
1766 // might be inreg.
1767 //
1768 // We currently expect it to be rare (particularly in well written code) for
1769 // arguments to be passed on the stack when there are still free integer
1770 // registers available (this would typically imply large structs being passed
1771 // by value), so this seems like a fair tradeoff for now.
1772 //
1773 // We can revisit this if the backend grows support for 'onstack' parameter
1774 // attributes. See PR12193.
1775 if (freeIntRegs == 0) {
1776 uint64_t Size = getContext().getTypeSize(Ty);
1777
1778 // If this type fits in an eightbyte, coerce it into the matching integral
1779 // type, which will end up on the stack (with alignment 8).
1780 if (Align == 8 && Size <= 64)
1781 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1782 Size));
1783 }
1784
Chris Lattner855d2272011-05-22 23:21:23 +00001785 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001786}
1787
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001788/// GetByteVectorType - The ABI specifies that a value should be passed in an
1789/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner0f408f52010-07-29 04:56:46 +00001790/// vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001791llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001792 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001793
Chris Lattner15842bd2010-07-29 05:02:29 +00001794 // Wrapper structs that just contain vectors are passed just like vectors,
1795 // strip them off if present.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001796 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner15842bd2010-07-29 05:02:29 +00001797 while (STy && STy->getNumElements() == 1) {
1798 IRType = STy->getElementType(0);
1799 STy = dyn_cast<llvm::StructType>(IRType);
1800 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001801
Bruno Cardoso Lopes528a8c72011-07-08 22:57:35 +00001802 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001803 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1804 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001805 unsigned BitWidth = VT->getBitWidth();
Tanya Lattnerce275672011-11-28 23:18:11 +00001806 if ((BitWidth >= 128 && BitWidth <= 256) &&
Chris Lattner0f408f52010-07-29 04:56:46 +00001807 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1808 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1809 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1810 EltTy->isIntegerTy(128)))
1811 return VT;
1812 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001813
Chris Lattner0f408f52010-07-29 04:56:46 +00001814 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1815}
1816
Chris Lattnere2962be2010-07-29 07:30:00 +00001817/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1818/// is known to either be off the end of the specified type or being in
1819/// alignment padding. The user type specified is known to be at most 128 bits
1820/// in size, and have passed through X86_64ABIInfo::classify with a successful
1821/// classification that put one of the two halves in the INTEGER class.
1822///
1823/// It is conservatively correct to return false.
1824static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1825 unsigned EndBit, ASTContext &Context) {
1826 // If the bytes being queried are off the end of the type, there is no user
1827 // data hiding here. This handles analysis of builtins, vectors and other
1828 // types that don't contain interesting padding.
1829 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1830 if (TySize <= StartBit)
1831 return true;
1832
Chris Lattner021c3a32010-07-29 07:43:55 +00001833 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1834 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1835 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1836
1837 // Check each element to see if the element overlaps with the queried range.
1838 for (unsigned i = 0; i != NumElts; ++i) {
1839 // If the element is after the span we care about, then we're done..
1840 unsigned EltOffset = i*EltSize;
1841 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001842
Chris Lattner021c3a32010-07-29 07:43:55 +00001843 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1844 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1845 EndBit-EltOffset, Context))
1846 return false;
1847 }
1848 // If it overlaps no elements, then it is safe to process as padding.
1849 return true;
1850 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001851
Chris Lattnere2962be2010-07-29 07:30:00 +00001852 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1853 const RecordDecl *RD = RT->getDecl();
1854 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001855
Chris Lattnere2962be2010-07-29 07:30:00 +00001856 // If this is a C++ record, check the bases first.
1857 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1858 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1859 e = CXXRD->bases_end(); i != e; ++i) {
1860 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1861 "Unexpected base class!");
1862 const CXXRecordDecl *Base =
1863 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001864
Chris Lattnere2962be2010-07-29 07:30:00 +00001865 // If the base is after the span we care about, ignore it.
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001866 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
Chris Lattnere2962be2010-07-29 07:30:00 +00001867 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001868
Chris Lattnere2962be2010-07-29 07:30:00 +00001869 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1870 if (!BitsContainNoUserData(i->getType(), BaseStart,
1871 EndBit-BaseOffset, Context))
1872 return false;
1873 }
1874 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001875
Chris Lattnere2962be2010-07-29 07:30:00 +00001876 // Verify that no field has data that overlaps the region of interest. Yes
1877 // this could be sped up a lot by being smarter about queried fields,
1878 // however we're only looking at structs up to 16 bytes, so we don't care
1879 // much.
1880 unsigned idx = 0;
1881 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1882 i != e; ++i, ++idx) {
1883 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001884
Chris Lattnere2962be2010-07-29 07:30:00 +00001885 // If we found a field after the region we care about, then we're done.
1886 if (FieldOffset >= EndBit) break;
1887
1888 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1889 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1890 Context))
1891 return false;
1892 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001893
Chris Lattnere2962be2010-07-29 07:30:00 +00001894 // If nothing in this record overlapped the area of interest, then we're
1895 // clean.
1896 return true;
1897 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001898
Chris Lattnere2962be2010-07-29 07:30:00 +00001899 return false;
1900}
1901
Chris Lattner0b362002010-07-29 18:39:32 +00001902/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1903/// float member at the specified offset. For example, {int,{float}} has a
1904/// float at offset 4. It is conservatively correct for this routine to return
1905/// false.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001906static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Micah Villmow25a6a842012-10-08 16:25:52 +00001907 const llvm::DataLayout &TD) {
Chris Lattner0b362002010-07-29 18:39:32 +00001908 // Base case if we find a float.
1909 if (IROffset == 0 && IRType->isFloatTy())
1910 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001911
Chris Lattner0b362002010-07-29 18:39:32 +00001912 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001913 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner0b362002010-07-29 18:39:32 +00001914 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1915 unsigned Elt = SL->getElementContainingOffset(IROffset);
1916 IROffset -= SL->getElementOffset(Elt);
1917 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1918 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001919
Chris Lattner0b362002010-07-29 18:39:32 +00001920 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001921 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1922 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner0b362002010-07-29 18:39:32 +00001923 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1924 IROffset -= IROffset/EltSize*EltSize;
1925 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1926 }
1927
1928 return false;
1929}
1930
Chris Lattnerf47c9442010-07-29 18:13:09 +00001931
1932/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1933/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001934llvm::Type *X86_64ABIInfo::
1935GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00001936 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001937 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001938 // pass as float if the last 4 bytes is just padding. This happens for
1939 // structs that contain 3 floats.
1940 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1941 SourceOffset*8+64, getContext()))
1942 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001943
Chris Lattner0b362002010-07-29 18:39:32 +00001944 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1945 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1946 // case.
Micah Villmow25a6a842012-10-08 16:25:52 +00001947 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
1948 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001949 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001950
Chris Lattnerf47c9442010-07-29 18:13:09 +00001951 return llvm::Type::getDoubleTy(getVMContext());
1952}
1953
1954
Chris Lattner0d2656d2010-07-29 17:40:35 +00001955/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1956/// an 8-byte GPR. This means that we either have a scalar or we are talking
1957/// about the high or low part of an up-to-16-byte struct. This routine picks
1958/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001959/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1960/// etc).
1961///
1962/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1963/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1964/// the 8-byte value references. PrefType may be null.
1965///
1966/// SourceTy is the source level type for the entire argument. SourceOffset is
1967/// an offset into this that we're processing (which is always either 0 or 8).
1968///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001969llvm::Type *X86_64ABIInfo::
1970GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00001971 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001972 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1973 // returning an 8-byte unit starting with it. See if we can safely use it.
1974 if (IROffset == 0) {
1975 // Pointers and int64's always fill the 8-byte unit.
Derek Schuffbabaf312012-10-11 15:52:22 +00001976 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
1977 IRType->isIntegerTy(64))
Chris Lattnere2962be2010-07-29 07:30:00 +00001978 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001979
Chris Lattnere2962be2010-07-29 07:30:00 +00001980 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1981 // goodness in the source type is just tail padding. This is allowed to
1982 // kick in for struct {double,int} on the int, but not on
1983 // struct{double,int,int} because we wouldn't return the second int. We
1984 // have to do this analysis on the source type because we can't depend on
1985 // unions being lowered a specific way etc.
1986 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
Derek Schuffbabaf312012-10-11 15:52:22 +00001987 IRType->isIntegerTy(32) ||
1988 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
1989 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
1990 cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001991
Chris Lattnere2962be2010-07-29 07:30:00 +00001992 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1993 SourceOffset*8+64, getContext()))
1994 return IRType;
1995 }
1996 }
Chris Lattner49382de2010-07-28 22:44:07 +00001997
Chris Lattner2acc6e32011-07-18 04:24:23 +00001998 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001999 // If this is a struct, recurse into the field at the specified offset.
Micah Villmow25a6a842012-10-08 16:25:52 +00002000 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00002001 if (IROffset < SL->getSizeInBytes()) {
2002 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
2003 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002004
Chris Lattner0d2656d2010-07-29 17:40:35 +00002005 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
2006 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002007 }
Chris Lattner49382de2010-07-28 22:44:07 +00002008 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002009
Chris Lattner2acc6e32011-07-18 04:24:23 +00002010 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002011 llvm::Type *EltTy = ATy->getElementType();
Micah Villmow25a6a842012-10-08 16:25:52 +00002012 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
Chris Lattner021c3a32010-07-29 07:43:55 +00002013 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00002014 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
2015 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00002016 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002017
Chris Lattner49382de2010-07-28 22:44:07 +00002018 // Okay, we don't have any better idea of what to pass, so we pass this in an
2019 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00002020 unsigned TySizeInBytes =
2021 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00002022
Chris Lattner9e45a3d2010-07-29 17:34:39 +00002023 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002024
Chris Lattner49382de2010-07-28 22:44:07 +00002025 // It is always safe to classify this as an integer type up to i64 that
2026 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00002027 return llvm::IntegerType::get(getVMContext(),
2028 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00002029}
2030
Chris Lattner66e7b682010-09-01 00:50:20 +00002031
2032/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
2033/// be used as elements of a two register pair to pass or return, return a
2034/// first class aggregate to represent them. For example, if the low part of
2035/// a by-value argument should be passed as i32* and the high part as float,
2036/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002037static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00002038GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Micah Villmow25a6a842012-10-08 16:25:52 +00002039 const llvm::DataLayout &TD) {
Chris Lattner66e7b682010-09-01 00:50:20 +00002040 // In order to correctly satisfy the ABI, we need to the high part to start
2041 // at offset 8. If the high and low parts we inferred are both 4-byte types
2042 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
2043 // the second element at offset 8. Check for this:
2044 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
2045 unsigned HiAlign = TD.getABITypeAlignment(Hi);
Micah Villmow25a6a842012-10-08 16:25:52 +00002046 unsigned HiStart = llvm::DataLayout::RoundUpAlignment(LoSize, HiAlign);
Chris Lattner66e7b682010-09-01 00:50:20 +00002047 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002048
Chris Lattner66e7b682010-09-01 00:50:20 +00002049 // To handle this, we have to increase the size of the low part so that the
2050 // second element will start at an 8 byte offset. We can't increase the size
2051 // of the second element because it might make us access off the end of the
2052 // struct.
2053 if (HiStart != 8) {
2054 // There are only two sorts of types the ABI generation code can produce for
2055 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
2056 // Promote these to a larger type.
2057 if (Lo->isFloatTy())
2058 Lo = llvm::Type::getDoubleTy(Lo->getContext());
2059 else {
2060 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
2061 Lo = llvm::Type::getInt64Ty(Lo->getContext());
2062 }
2063 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002064
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002065 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002066
2067
Chris Lattner66e7b682010-09-01 00:50:20 +00002068 // Verify that the second element is at an 8-byte offset.
2069 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
2070 "Invalid x86-64 argument pair!");
2071 return Result;
2072}
2073
Chris Lattner519f68c2010-07-28 23:06:14 +00002074ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00002075classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00002076 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
2077 // classification algorithm.
2078 X86_64ABIInfo::Class Lo, Hi;
2079 classify(RetTy, 0, Lo, Hi);
2080
2081 // Check some invariants.
2082 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002083 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2084
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002085 llvm::Type *ResType = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002086 switch (Lo) {
2087 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002088 if (Hi == NoClass)
2089 return ABIArgInfo::getIgnore();
2090 // If the low part is just padding, it takes no register, leave ResType
2091 // null.
2092 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2093 "Unknown missing lo part");
2094 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002095
2096 case SSEUp:
2097 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002098 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002099
2100 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
2101 // hidden argument.
2102 case Memory:
2103 return getIndirectReturnResult(RetTy);
2104
2105 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
2106 // available register of the sequence %rax, %rdx is used.
2107 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002108 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002109
Chris Lattnereb518b42010-07-29 21:42:50 +00002110 // If we have a sign or zero extended integer, make sure to return Extend
2111 // so that the parameter gets the right LLVM IR attributes.
2112 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2113 // Treat an enum type as its underlying type.
2114 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2115 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002116
Chris Lattnereb518b42010-07-29 21:42:50 +00002117 if (RetTy->isIntegralOrEnumerationType() &&
2118 RetTy->isPromotableIntegerType())
2119 return ABIArgInfo::getExtend();
2120 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002121 break;
2122
2123 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
2124 // available SSE register of the sequence %xmm0, %xmm1 is used.
2125 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002126 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00002127 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002128
2129 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
2130 // returned on the X87 stack in %st0 as 80-bit x87 number.
2131 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00002132 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00002133 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002134
2135 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
2136 // part of the value is returned in %st0 and the imaginary part in
2137 // %st1.
2138 case ComplexX87:
2139 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00002140 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00002141 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00002142 NULL);
2143 break;
2144 }
2145
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002146 llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002147 switch (Hi) {
2148 // Memory was handled previously and X87 should
2149 // never occur as a hi class.
2150 case Memory:
2151 case X87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002152 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002153
2154 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00002155 case NoClass:
2156 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002157
Chris Lattner3db4dde2010-09-01 00:20:33 +00002158 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002159 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002160 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2161 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002162 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00002163 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002164 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002165 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2166 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002167 break;
2168
2169 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002170 // is passed in the next available eightbyte chunk if the last used
2171 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00002172 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002173 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00002174 case SSEUp:
2175 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002176 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00002177 break;
2178
2179 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
2180 // returned together with the previous X87 value in %st0.
2181 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002182 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00002183 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002184 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00002185 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00002186 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002187 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002188 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2189 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00002190 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002191 break;
2192 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002193
Chris Lattner3db4dde2010-09-01 00:20:33 +00002194 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00002195 // known to pass in the high eightbyte of the result. We do this by forming a
2196 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00002197 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002198 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Chris Lattner519f68c2010-07-28 23:06:14 +00002199
Chris Lattnereb518b42010-07-29 21:42:50 +00002200 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00002201}
2202
Daniel Dunbaredfac032012-03-10 01:03:58 +00002203ABIArgInfo X86_64ABIInfo::classifyArgumentType(
2204 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE)
2205 const
2206{
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002207 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00002208 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002209
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002210 // Check some invariants.
2211 // FIXME: Enforce these by construction.
2212 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002213 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2214
2215 neededInt = 0;
2216 neededSSE = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002217 llvm::Type *ResType = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002218 switch (Lo) {
2219 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002220 if (Hi == NoClass)
2221 return ABIArgInfo::getIgnore();
2222 // If the low part is just padding, it takes no register, leave ResType
2223 // null.
2224 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2225 "Unknown missing lo part");
2226 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002227
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002228 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
2229 // on the stack.
2230 case Memory:
2231
2232 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
2233 // COMPLEX_X87, it is passed in memory.
2234 case X87:
2235 case ComplexX87:
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002236 if (getRecordArgABI(Ty, CGT) == CGCXXABI::RAA_Indirect)
Eli Friedmanded137f2011-06-29 07:04:55 +00002237 ++neededInt;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002238 return getIndirectResult(Ty, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002239
2240 case SSEUp:
2241 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002242 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002243
2244 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
2245 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
2246 // and %r9 is used.
2247 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00002248 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002249
Chris Lattner49382de2010-07-28 22:44:07 +00002250 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002251 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00002252
2253 // If we have a sign or zero extended integer, make sure to return Extend
2254 // so that the parameter gets the right LLVM IR attributes.
2255 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2256 // Treat an enum type as its underlying type.
2257 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2258 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002259
Chris Lattnereb518b42010-07-29 21:42:50 +00002260 if (Ty->isIntegralOrEnumerationType() &&
2261 Ty->isPromotableIntegerType())
2262 return ABIArgInfo::getExtend();
2263 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002264
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002265 break;
2266
2267 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
2268 // available SSE register is used, the registers are taken in the
2269 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00002270 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002271 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00002272 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00002273 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002274 break;
2275 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00002276 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002277
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002278 llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002279 switch (Hi) {
2280 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002281 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002282 // which is passed in memory.
2283 case Memory:
2284 case X87:
2285 case ComplexX87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002286 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002287
2288 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002289
Chris Lattner645406a2010-09-01 00:24:35 +00002290 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002291 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00002292 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002293 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002294
Chris Lattner645406a2010-09-01 00:24:35 +00002295 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2296 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002297 break;
2298
2299 // X87Up generally doesn't occur here (long double is passed in
2300 // memory), except in situations involving unions.
2301 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00002302 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002303 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002304
Chris Lattner645406a2010-09-01 00:24:35 +00002305 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2306 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00002307
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002308 ++neededSSE;
2309 break;
2310
2311 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
2312 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002313 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002314 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00002315 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002316 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002317 break;
2318 }
2319
Chris Lattner645406a2010-09-01 00:24:35 +00002320 // If a high part was specified, merge it together with the low part. It is
2321 // known to pass in the high eightbyte of the result. We do this by forming a
2322 // first class struct aggregate with the high and low part: {low, high}
2323 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002324 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002325
Chris Lattnereb518b42010-07-29 21:42:50 +00002326 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002327}
2328
Chris Lattneree5dcd02010-07-29 02:31:05 +00002329void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002330
Chris Lattnera3c109b2010-07-29 02:16:43 +00002331 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002332
2333 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00002334 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002335
2336 // If the return value is indirect, then the hidden argument is consuming one
2337 // integer register.
2338 if (FI.getReturnInfo().isIndirect())
2339 --freeIntRegs;
2340
2341 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2342 // get assigned (in left-to-right order) for passing as follows...
2343 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2344 it != ie; ++it) {
Bill Wendling99aaae82010-10-18 23:51:38 +00002345 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002346 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
2347 neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002348
2349 // AMD64-ABI 3.2.3p3: If there are no registers available for any
2350 // eightbyte of an argument, the whole argument is passed on the
2351 // stack. If registers have already been assigned for some
2352 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00002353 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002354 freeIntRegs -= neededInt;
2355 freeSSERegs -= neededSSE;
2356 } else {
Daniel Dunbaredfac032012-03-10 01:03:58 +00002357 it->info = getIndirectResult(it->type, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002358 }
2359 }
2360}
2361
2362static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2363 QualType Ty,
2364 CodeGenFunction &CGF) {
2365 llvm::Value *overflow_arg_area_p =
2366 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2367 llvm::Value *overflow_arg_area =
2368 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2369
2370 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2371 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedman8d2fe422011-11-18 02:44:19 +00002372 // It isn't stated explicitly in the standard, but in practice we use
2373 // alignment greater than 16 where necessary.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002374 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2375 if (Align > 8) {
Eli Friedman8d2fe422011-11-18 02:44:19 +00002376 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
Owen Anderson0032b272009-08-13 21:57:51 +00002377 llvm::Value *Offset =
Eli Friedman8d2fe422011-11-18 02:44:19 +00002378 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002379 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2380 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00002381 CGF.Int64Ty);
Eli Friedman8d2fe422011-11-18 02:44:19 +00002382 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002383 overflow_arg_area =
2384 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2385 overflow_arg_area->getType(),
2386 "overflow_arg_area.align");
2387 }
2388
2389 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002390 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002391 llvm::Value *Res =
2392 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002393 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002394
2395 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2396 // l->overflow_arg_area + sizeof(type).
2397 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2398 // an 8 byte boundary.
2399
2400 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00002401 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00002402 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002403 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2404 "overflow_arg_area.next");
2405 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2406
2407 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2408 return Res;
2409}
2410
2411llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2412 CodeGenFunction &CGF) const {
2413 // Assume that va_list type is correct; should be pointer to LLVM type:
2414 // struct {
2415 // i32 gp_offset;
2416 // i32 fp_offset;
2417 // i8* overflow_arg_area;
2418 // i8* reg_save_area;
2419 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00002420 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002421
Chris Lattnera14db752010-03-11 18:19:55 +00002422 Ty = CGF.getContext().getCanonicalType(Ty);
Daniel Dunbaredfac032012-03-10 01:03:58 +00002423 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002424
2425 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2426 // in the registers. If not go to step 7.
2427 if (!neededInt && !neededSSE)
2428 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2429
2430 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2431 // general purpose registers needed to pass type and num_fp to hold
2432 // the number of floating point registers needed.
2433
2434 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2435 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2436 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2437 //
2438 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2439 // register save space).
2440
2441 llvm::Value *InRegs = 0;
2442 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2443 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2444 if (neededInt) {
2445 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2446 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002447 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2448 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002449 }
2450
2451 if (neededSSE) {
2452 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2453 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2454 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00002455 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2456 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002457 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2458 }
2459
2460 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2461 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2462 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2463 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2464
2465 // Emit code to load the value if it was passed in registers.
2466
2467 CGF.EmitBlock(InRegBlock);
2468
2469 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2470 // an offset of l->gp_offset and/or l->fp_offset. This may require
2471 // copying to a temporary location in case the parameter is passed
2472 // in different register classes or requires an alignment greater
2473 // than 8 for general purpose registers and 16 for XMM registers.
2474 //
2475 // FIXME: This really results in shameful code when we end up needing to
2476 // collect arguments from different places; often what should result in a
2477 // simple assembling of a structure from scattered addresses has many more
2478 // loads than necessary. Can we clean this up?
Chris Lattner2acc6e32011-07-18 04:24:23 +00002479 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002480 llvm::Value *RegAddr =
2481 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2482 "reg_save_area");
2483 if (neededInt && neededSSE) {
2484 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002485 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002486 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002487 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2488 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002489 llvm::Type *TyLo = ST->getElementType(0);
2490 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002491 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002492 "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002493 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2494 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002495 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2496 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002497 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2498 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002499 llvm::Value *V =
2500 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2501 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2502 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2503 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2504
Owen Andersona1cf15f2009-07-14 23:10:40 +00002505 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002506 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002507 } else if (neededInt) {
2508 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2509 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002510 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002511 } else if (neededSSE == 1) {
2512 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2513 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2514 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002515 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002516 assert(neededSSE == 2 && "Invalid number of needed registers!");
2517 // SSE registers are spaced 16 bytes apart in the register save
2518 // area, we need to collect the two eightbytes together.
2519 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002520 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner8b418682012-02-07 00:39:47 +00002521 llvm::Type *DoubleTy = CGF.DoubleTy;
Chris Lattner2acc6e32011-07-18 04:24:23 +00002522 llvm::Type *DblPtrTy =
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002523 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002524 llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002525 DoubleTy, NULL);
2526 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2527 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2528 DblPtrTy));
2529 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2530 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2531 DblPtrTy));
2532 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2533 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2534 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002535 }
2536
2537 // AMD64-ABI 3.5.7p5: Step 5. Set:
2538 // l->gp_offset = l->gp_offset + num_gp * 8
2539 // l->fp_offset = l->fp_offset + num_fp * 16.
2540 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002541 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002542 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2543 gp_offset_p);
2544 }
2545 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002546 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002547 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2548 fp_offset_p);
2549 }
2550 CGF.EmitBranch(ContBlock);
2551
2552 // Emit code to load the value if it was passed in memory.
2553
2554 CGF.EmitBlock(InMemBlock);
2555 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2556
2557 // Return the appropriate result.
2558
2559 CGF.EmitBlock(ContBlock);
Jay Foadbbf3bac2011-03-30 11:28:58 +00002560 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002561 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002562 ResAddr->addIncoming(RegAddr, InRegBlock);
2563 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002564 return ResAddr;
2565}
2566
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002567ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, bool IsReturnType) const {
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002568
2569 if (Ty->isVoidType())
2570 return ABIArgInfo::getIgnore();
2571
2572 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2573 Ty = EnumTy->getDecl()->getIntegerType();
2574
2575 uint64_t Size = getContext().getTypeSize(Ty);
2576
2577 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002578 if (IsReturnType) {
2579 if (isRecordReturnIndirect(RT, CGT))
2580 return ABIArgInfo::getIndirect(0, false);
2581 } else {
2582 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, CGT))
2583 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
2584 }
2585
2586 if (RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002587 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2588
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002589 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
John McCall64aa4b32013-04-16 22:48:15 +00002590 if (Size == 128 && getTarget().getTriple().getOS() == llvm::Triple::MinGW32)
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002591 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2592 Size));
2593
2594 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2595 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2596 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002597 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002598 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2599 Size));
2600
2601 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2602 }
2603
2604 if (Ty->isPromotableIntegerType())
2605 return ABIArgInfo::getExtend();
2606
2607 return ABIArgInfo::getDirect();
2608}
2609
2610void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2611
2612 QualType RetTy = FI.getReturnType();
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002613 FI.getReturnInfo() = classify(RetTy, true);
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002614
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002615 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2616 it != ie; ++it)
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002617 it->info = classify(it->type, false);
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002618}
2619
Chris Lattnerf13721d2010-08-31 16:44:54 +00002620llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2621 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002622 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002623
Chris Lattnerf13721d2010-08-31 16:44:54 +00002624 CGBuilderTy &Builder = CGF.Builder;
2625 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2626 "ap");
2627 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2628 llvm::Type *PTy =
2629 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2630 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2631
2632 uint64_t Offset =
2633 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2634 llvm::Value *NextAddr =
2635 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2636 "ap.next");
2637 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2638
2639 return AddrTyped;
2640}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002641
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00002642namespace {
2643
Derek Schuff263366f2012-10-16 22:30:41 +00002644class NaClX86_64ABIInfo : public ABIInfo {
2645 public:
2646 NaClX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2647 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, HasAVX) {}
2648 virtual void computeInfo(CGFunctionInfo &FI) const;
2649 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2650 CodeGenFunction &CGF) const;
2651 private:
2652 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
2653 X86_64ABIInfo NInfo; // Used for everything else.
2654};
2655
2656class NaClX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2657 public:
2658 NaClX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2659 : TargetCodeGenInfo(new NaClX86_64ABIInfo(CGT, HasAVX)) {}
2660};
2661
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00002662}
2663
Derek Schuff263366f2012-10-16 22:30:41 +00002664void NaClX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2665 if (FI.getASTCallingConvention() == CC_PnaclCall)
2666 PInfo.computeInfo(FI);
2667 else
2668 NInfo.computeInfo(FI);
2669}
2670
2671llvm::Value *NaClX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2672 CodeGenFunction &CGF) const {
2673 // Always use the native convention; calling pnacl-style varargs functions
2674 // is unuspported.
2675 return NInfo.EmitVAArg(VAListAddr, Ty, CGF);
2676}
2677
2678
John McCallec853ba2010-03-11 00:10:12 +00002679// PowerPC-32
2680
2681namespace {
2682class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2683public:
Chris Lattnerea044322010-07-29 02:01:43 +00002684 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002685
John McCallec853ba2010-03-11 00:10:12 +00002686 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2687 // This is recovered from gcc output.
2688 return 1; // r1 is the dedicated stack pointer
2689 }
2690
2691 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002692 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002693};
2694
2695}
2696
2697bool
2698PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2699 llvm::Value *Address) const {
2700 // This is calculated from the LLVM and GCC tables and verified
2701 // against gcc output. AFAIK all ABIs use the same encoding.
2702
2703 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallec853ba2010-03-11 00:10:12 +00002704
Chris Lattner8b418682012-02-07 00:39:47 +00002705 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallec853ba2010-03-11 00:10:12 +00002706 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2707 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2708 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2709
2710 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002711 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002712
2713 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002714 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002715
2716 // 64-76 are various 4-byte special-purpose registers:
2717 // 64: mq
2718 // 65: lr
2719 // 66: ctr
2720 // 67: ap
2721 // 68-75 cr0-7
2722 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002723 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002724
2725 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002726 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002727
2728 // 109: vrsave
2729 // 110: vscr
2730 // 111: spe_acc
2731 // 112: spefscr
2732 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002733 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002734
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002735 return false;
John McCallec853ba2010-03-11 00:10:12 +00002736}
2737
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002738// PowerPC-64
2739
2740namespace {
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002741/// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
2742class PPC64_SVR4_ABIInfo : public DefaultABIInfo {
2743
2744public:
2745 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
2746
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002747 bool isPromotableTypeForABI(QualType Ty) const;
2748
2749 ABIArgInfo classifyReturnType(QualType RetTy) const;
2750 ABIArgInfo classifyArgumentType(QualType Ty) const;
2751
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00002752 // TODO: We can add more logic to computeInfo to improve performance.
2753 // Example: For aggregate arguments that fit in a register, we could
2754 // use getDirectInReg (as is done below for structs containing a single
2755 // floating-point value) to avoid pushing them to memory on function
2756 // entry. This would require changing the logic in PPCISelLowering
2757 // when lowering the parameters in the caller and args in the callee.
2758 virtual void computeInfo(CGFunctionInfo &FI) const {
2759 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2760 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2761 it != ie; ++it) {
2762 // We rely on the default argument classification for the most part.
2763 // One exception: An aggregate containing a single floating-point
2764 // item must be passed in a register if one is available.
2765 const Type *T = isSingleElementStruct(it->type, getContext());
2766 if (T) {
2767 const BuiltinType *BT = T->getAs<BuiltinType>();
2768 if (BT && BT->isFloatingPoint()) {
2769 QualType QT(T, 0);
2770 it->info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
2771 continue;
2772 }
2773 }
2774 it->info = classifyArgumentType(it->type);
2775 }
2776 }
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002777
2778 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr,
2779 QualType Ty,
2780 CodeGenFunction &CGF) const;
2781};
2782
2783class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
2784public:
2785 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT)
2786 : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT)) {}
2787
2788 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2789 // This is recovered from gcc output.
2790 return 1; // r1 is the dedicated stack pointer
2791 }
2792
2793 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2794 llvm::Value *Address) const;
2795};
2796
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002797class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2798public:
2799 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
2800
2801 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2802 // This is recovered from gcc output.
2803 return 1; // r1 is the dedicated stack pointer
2804 }
2805
2806 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2807 llvm::Value *Address) const;
2808};
2809
2810}
2811
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002812// Return true if the ABI requires Ty to be passed sign- or zero-
2813// extended to 64 bits.
2814bool
2815PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
2816 // Treat an enum type as its underlying type.
2817 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2818 Ty = EnumTy->getDecl()->getIntegerType();
2819
2820 // Promotable integer types are required to be promoted by the ABI.
2821 if (Ty->isPromotableIntegerType())
2822 return true;
2823
2824 // In addition to the usual promotable integer types, we also need to
2825 // extend all 32-bit types, since the ABI requires promotion to 64 bits.
2826 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2827 switch (BT->getKind()) {
2828 case BuiltinType::Int:
2829 case BuiltinType::UInt:
2830 return true;
2831 default:
2832 break;
2833 }
2834
2835 return false;
2836}
2837
2838ABIArgInfo
2839PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
Bill Schmidtc9715fc2012-11-27 02:46:43 +00002840 if (Ty->isAnyComplexType())
2841 return ABIArgInfo::getDirect();
2842
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002843 if (isAggregateTypeForABI(Ty)) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002844 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
2845 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002846
2847 return ABIArgInfo::getIndirect(0);
2848 }
2849
2850 return (isPromotableTypeForABI(Ty) ?
2851 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2852}
2853
2854ABIArgInfo
2855PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
2856 if (RetTy->isVoidType())
2857 return ABIArgInfo::getIgnore();
2858
Bill Schmidt9e6111a2012-12-17 04:20:17 +00002859 if (RetTy->isAnyComplexType())
2860 return ABIArgInfo::getDirect();
2861
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002862 if (isAggregateTypeForABI(RetTy))
2863 return ABIArgInfo::getIndirect(0);
2864
2865 return (isPromotableTypeForABI(RetTy) ?
2866 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2867}
2868
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002869// Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
2870llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr,
2871 QualType Ty,
2872 CodeGenFunction &CGF) const {
2873 llvm::Type *BP = CGF.Int8PtrTy;
2874 llvm::Type *BPP = CGF.Int8PtrPtrTy;
2875
2876 CGBuilderTy &Builder = CGF.Builder;
2877 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
2878 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2879
Bill Schmidt19f8e852013-01-14 17:45:36 +00002880 // Update the va_list pointer. The pointer should be bumped by the
2881 // size of the object. We can trust getTypeSize() except for a complex
2882 // type whose base type is smaller than a doubleword. For these, the
2883 // size of the object is 16 bytes; see below for further explanation.
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002884 unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8;
Bill Schmidt19f8e852013-01-14 17:45:36 +00002885 QualType BaseTy;
2886 unsigned CplxBaseSize = 0;
2887
2888 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
2889 BaseTy = CTy->getElementType();
2890 CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8;
2891 if (CplxBaseSize < 8)
2892 SizeInBytes = 16;
2893 }
2894
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002895 unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8);
2896 llvm::Value *NextAddr =
2897 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset),
2898 "ap.next");
2899 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2900
Bill Schmidt19f8e852013-01-14 17:45:36 +00002901 // If we have a complex type and the base type is smaller than 8 bytes,
2902 // the ABI calls for the real and imaginary parts to be right-adjusted
2903 // in separate doublewords. However, Clang expects us to produce a
2904 // pointer to a structure with the two parts packed tightly. So generate
2905 // loads of the real and imaginary parts relative to the va_list pointer,
2906 // and store them to a temporary structure.
2907 if (CplxBaseSize && CplxBaseSize < 8) {
2908 llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2909 llvm::Value *ImagAddr = RealAddr;
2910 RealAddr = Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize));
2911 ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize));
2912 llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy));
2913 RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy);
2914 ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy);
2915 llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal");
2916 llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag");
2917 llvm::Value *Ptr = CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty),
2918 "vacplx");
2919 llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, ".real");
2920 llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, ".imag");
2921 Builder.CreateStore(Real, RealPtr, false);
2922 Builder.CreateStore(Imag, ImagPtr, false);
2923 return Ptr;
2924 }
2925
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002926 // If the argument is smaller than 8 bytes, it is right-adjusted in
2927 // its doubleword slot. Adjust the pointer to pick it up from the
2928 // correct offset.
2929 if (SizeInBytes < 8) {
2930 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2931 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes));
2932 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2933 }
2934
2935 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2936 return Builder.CreateBitCast(Addr, PTy);
2937}
2938
2939static bool
2940PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2941 llvm::Value *Address) {
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002942 // This is calculated from the LLVM and GCC tables and verified
2943 // against gcc output. AFAIK all ABIs use the same encoding.
2944
2945 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2946
2947 llvm::IntegerType *i8 = CGF.Int8Ty;
2948 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2949 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2950 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2951
2952 // 0-31: r0-31, the 8-byte general-purpose registers
2953 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
2954
2955 // 32-63: fp0-31, the 8-byte floating-point registers
2956 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
2957
2958 // 64-76 are various 4-byte special-purpose registers:
2959 // 64: mq
2960 // 65: lr
2961 // 66: ctr
2962 // 67: ap
2963 // 68-75 cr0-7
2964 // 76: xer
2965 AssignToArrayRange(Builder, Address, Four8, 64, 76);
2966
2967 // 77-108: v0-31, the 16-byte vector registers
2968 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
2969
2970 // 109: vrsave
2971 // 110: vscr
2972 // 111: spe_acc
2973 // 112: spefscr
2974 // 113: sfp
2975 AssignToArrayRange(Builder, Address, Four8, 109, 113);
2976
2977 return false;
2978}
John McCallec853ba2010-03-11 00:10:12 +00002979
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002980bool
2981PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
2982 CodeGen::CodeGenFunction &CGF,
2983 llvm::Value *Address) const {
2984
2985 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
2986}
2987
2988bool
2989PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2990 llvm::Value *Address) const {
2991
2992 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
2993}
2994
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002995//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002996// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002997//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002998
2999namespace {
3000
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003001class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003002public:
3003 enum ABIKind {
3004 APCS = 0,
3005 AAPCS = 1,
3006 AAPCS_VFP
3007 };
3008
3009private:
3010 ABIKind Kind;
3011
3012public:
John McCallbd7370a2013-02-28 19:01:20 +00003013 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {
3014 setRuntimeCC();
3015 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003016
John McCall49e34be2011-08-30 01:42:09 +00003017 bool isEABI() const {
John McCall64aa4b32013-04-16 22:48:15 +00003018 StringRef Env = getTarget().getTriple().getEnvironmentName();
Logan Chien94a71422012-09-02 09:30:11 +00003019 return (Env == "gnueabi" || Env == "eabi" ||
3020 Env == "android" || Env == "androideabi");
John McCall49e34be2011-08-30 01:42:09 +00003021 }
3022
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003023private:
3024 ABIKind getABIKind() const { return Kind; }
3025
Chris Lattnera3c109b2010-07-29 02:16:43 +00003026 ABIArgInfo classifyReturnType(QualType RetTy) const;
Manman Ren710c5172012-10-31 19:02:26 +00003027 ABIArgInfo classifyArgumentType(QualType RetTy, int *VFPRegs,
3028 unsigned &AllocatedVFP,
Manman Renb3fa55f2012-10-30 23:21:41 +00003029 bool &IsHA) const;
Manman Ren97f81572012-10-16 19:18:39 +00003030 bool isIllegalVectorType(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003031
Chris Lattneree5dcd02010-07-29 02:31:05 +00003032 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003033
3034 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3035 CodeGenFunction &CGF) const;
John McCallbd7370a2013-02-28 19:01:20 +00003036
3037 llvm::CallingConv::ID getLLVMDefaultCC() const;
3038 llvm::CallingConv::ID getABIDefaultCC() const;
3039 void setRuntimeCC();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003040};
3041
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003042class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
3043public:
Chris Lattnerea044322010-07-29 02:01:43 +00003044 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
3045 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00003046
John McCall49e34be2011-08-30 01:42:09 +00003047 const ARMABIInfo &getABIInfo() const {
3048 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
3049 }
3050
John McCall6374c332010-03-06 00:35:14 +00003051 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3052 return 13;
3053 }
Roman Divacky09345d12011-05-18 19:36:54 +00003054
Chris Lattner5f9e2722011-07-23 10:55:15 +00003055 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCallf85e1932011-06-15 23:02:42 +00003056 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
3057 }
3058
Roman Divacky09345d12011-05-18 19:36:54 +00003059 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3060 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003061 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divacky09345d12011-05-18 19:36:54 +00003062
3063 // 0-15 are the 16 integer registers.
Chris Lattner8b418682012-02-07 00:39:47 +00003064 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divacky09345d12011-05-18 19:36:54 +00003065 return false;
3066 }
John McCall49e34be2011-08-30 01:42:09 +00003067
3068 unsigned getSizeOfUnwindException() const {
3069 if (getABIInfo().isEABI()) return 88;
3070 return TargetCodeGenInfo::getSizeOfUnwindException();
3071 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003072};
3073
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003074}
3075
Chris Lattneree5dcd02010-07-29 02:31:05 +00003076void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Manman Renb3fa55f2012-10-30 23:21:41 +00003077 // To correctly handle Homogeneous Aggregate, we need to keep track of the
Manman Ren710c5172012-10-31 19:02:26 +00003078 // VFP registers allocated so far.
Manman Renb3fa55f2012-10-30 23:21:41 +00003079 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
3080 // VFP registers of the appropriate type unallocated then the argument is
3081 // allocated to the lowest-numbered sequence of such registers.
3082 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
3083 // unallocated are marked as unavailable.
3084 unsigned AllocatedVFP = 0;
Manman Ren710c5172012-10-31 19:02:26 +00003085 int VFPRegs[16] = { 0 };
Chris Lattnera3c109b2010-07-29 02:16:43 +00003086 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003087 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Manman Renb3fa55f2012-10-30 23:21:41 +00003088 it != ie; ++it) {
3089 unsigned PreAllocation = AllocatedVFP;
3090 bool IsHA = false;
3091 // 6.1.2.3 There is one VFP co-processor register class using registers
3092 // s0-s15 (d0-d7) for passing arguments.
3093 const unsigned NumVFPs = 16;
Manman Ren710c5172012-10-31 19:02:26 +00003094 it->info = classifyArgumentType(it->type, VFPRegs, AllocatedVFP, IsHA);
Manman Renb3fa55f2012-10-30 23:21:41 +00003095 // If we do not have enough VFP registers for the HA, any VFP registers
3096 // that are unallocated are marked as unavailable. To achieve this, we add
3097 // padding of (NumVFPs - PreAllocation) floats.
3098 if (IsHA && AllocatedVFP > NumVFPs && PreAllocation < NumVFPs) {
3099 llvm::Type *PaddingTy = llvm::ArrayType::get(
3100 llvm::Type::getFloatTy(getVMContext()), NumVFPs - PreAllocation);
3101 it->info = ABIArgInfo::getExpandWithPadding(false, PaddingTy);
3102 }
3103 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003104
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003105 // Always honor user-specified calling convention.
3106 if (FI.getCallingConvention() != llvm::CallingConv::C)
3107 return;
3108
John McCallbd7370a2013-02-28 19:01:20 +00003109 llvm::CallingConv::ID cc = getRuntimeCC();
3110 if (cc != llvm::CallingConv::C)
3111 FI.setEffectiveCallingConvention(cc);
3112}
Rafael Espindola25117ab2010-06-16 16:13:39 +00003113
John McCallbd7370a2013-02-28 19:01:20 +00003114/// Return the default calling convention that LLVM will use.
3115llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
3116 // The default calling convention that LLVM will infer.
John McCall64aa4b32013-04-16 22:48:15 +00003117 if (getTarget().getTriple().getEnvironmentName()=="gnueabihf")
John McCallbd7370a2013-02-28 19:01:20 +00003118 return llvm::CallingConv::ARM_AAPCS_VFP;
3119 else if (isEABI())
3120 return llvm::CallingConv::ARM_AAPCS;
3121 else
3122 return llvm::CallingConv::ARM_APCS;
3123}
3124
3125/// Return the calling convention that our ABI would like us to use
3126/// as the C calling convention.
3127llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003128 switch (getABIKind()) {
John McCallbd7370a2013-02-28 19:01:20 +00003129 case APCS: return llvm::CallingConv::ARM_APCS;
3130 case AAPCS: return llvm::CallingConv::ARM_AAPCS;
3131 case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003132 }
John McCallbd7370a2013-02-28 19:01:20 +00003133 llvm_unreachable("bad ABI kind");
3134}
3135
3136void ARMABIInfo::setRuntimeCC() {
3137 assert(getRuntimeCC() == llvm::CallingConv::C);
3138
3139 // Don't muddy up the IR with a ton of explicit annotations if
3140 // they'd just match what LLVM will infer from the triple.
3141 llvm::CallingConv::ID abiCC = getABIDefaultCC();
3142 if (abiCC != getLLVMDefaultCC())
3143 RuntimeCC = abiCC;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003144}
3145
Bob Wilson194f06a2011-08-03 05:58:22 +00003146/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
3147/// aggregate. If HAMembers is non-null, the number of base elements
3148/// contained in the type is returned through it; this is used for the
3149/// recursive calls that check aggregate component types.
3150static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
3151 ASTContext &Context,
3152 uint64_t *HAMembers = 0) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003153 uint64_t Members = 0;
Bob Wilson194f06a2011-08-03 05:58:22 +00003154 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
3155 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
3156 return false;
3157 Members *= AT->getSize().getZExtValue();
3158 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
3159 const RecordDecl *RD = RT->getDecl();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003160 if (RD->hasFlexibleArrayMember())
Bob Wilson194f06a2011-08-03 05:58:22 +00003161 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003162
Bob Wilson194f06a2011-08-03 05:58:22 +00003163 Members = 0;
3164 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3165 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00003166 const FieldDecl *FD = *i;
Bob Wilson194f06a2011-08-03 05:58:22 +00003167 uint64_t FldMembers;
3168 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
3169 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003170
3171 Members = (RD->isUnion() ?
3172 std::max(Members, FldMembers) : Members + FldMembers);
Bob Wilson194f06a2011-08-03 05:58:22 +00003173 }
3174 } else {
3175 Members = 1;
3176 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
3177 Members = 2;
3178 Ty = CT->getElementType();
3179 }
3180
3181 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
3182 // double, or 64-bit or 128-bit vectors.
3183 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3184 if (BT->getKind() != BuiltinType::Float &&
Tim Northoveradfa45f2012-07-20 22:29:29 +00003185 BT->getKind() != BuiltinType::Double &&
3186 BT->getKind() != BuiltinType::LongDouble)
Bob Wilson194f06a2011-08-03 05:58:22 +00003187 return false;
3188 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
3189 unsigned VecSize = Context.getTypeSize(VT);
3190 if (VecSize != 64 && VecSize != 128)
3191 return false;
3192 } else {
3193 return false;
3194 }
3195
3196 // The base type must be the same for all members. Vector types of the
3197 // same total size are treated as being equivalent here.
3198 const Type *TyPtr = Ty.getTypePtr();
3199 if (!Base)
3200 Base = TyPtr;
3201 if (Base != TyPtr &&
3202 (!Base->isVectorType() || !TyPtr->isVectorType() ||
3203 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
3204 return false;
3205 }
3206
3207 // Homogeneous Aggregates can have at most 4 members of the base type.
3208 if (HAMembers)
3209 *HAMembers = Members;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003210
3211 return (Members > 0 && Members <= 4);
Bob Wilson194f06a2011-08-03 05:58:22 +00003212}
3213
Manman Ren710c5172012-10-31 19:02:26 +00003214/// markAllocatedVFPs - update VFPRegs according to the alignment and
3215/// number of VFP registers (unit is S register) requested.
3216static void markAllocatedVFPs(int *VFPRegs, unsigned &AllocatedVFP,
3217 unsigned Alignment,
3218 unsigned NumRequired) {
3219 // Early Exit.
3220 if (AllocatedVFP >= 16)
3221 return;
3222 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
3223 // VFP registers of the appropriate type unallocated then the argument is
3224 // allocated to the lowest-numbered sequence of such registers.
3225 for (unsigned I = 0; I < 16; I += Alignment) {
3226 bool FoundSlot = true;
3227 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3228 if (J >= 16 || VFPRegs[J]) {
3229 FoundSlot = false;
3230 break;
3231 }
3232 if (FoundSlot) {
3233 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3234 VFPRegs[J] = 1;
3235 AllocatedVFP += NumRequired;
3236 return;
3237 }
3238 }
3239 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
3240 // unallocated are marked as unavailable.
3241 for (unsigned I = 0; I < 16; I++)
3242 VFPRegs[I] = 1;
3243 AllocatedVFP = 17; // We do not have enough VFP registers.
3244}
3245
3246ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, int *VFPRegs,
3247 unsigned &AllocatedVFP,
Manman Renb3fa55f2012-10-30 23:21:41 +00003248 bool &IsHA) const {
3249 // We update number of allocated VFPs according to
3250 // 6.1.2.1 The following argument types are VFP CPRCs:
3251 // A single-precision floating-point type (including promoted
3252 // half-precision types); A double-precision floating-point type;
3253 // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
3254 // with a Base Type of a single- or double-precision floating-point type,
3255 // 64-bit containerized vectors or 128-bit containerized vectors with one
3256 // to four Elements.
3257
Manman Ren97f81572012-10-16 19:18:39 +00003258 // Handle illegal vector types here.
3259 if (isIllegalVectorType(Ty)) {
3260 uint64_t Size = getContext().getTypeSize(Ty);
3261 if (Size <= 32) {
3262 llvm::Type *ResType =
3263 llvm::Type::getInt32Ty(getVMContext());
3264 return ABIArgInfo::getDirect(ResType);
3265 }
3266 if (Size == 64) {
3267 llvm::Type *ResType = llvm::VectorType::get(
3268 llvm::Type::getInt32Ty(getVMContext()), 2);
Manman Ren710c5172012-10-31 19:02:26 +00003269 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
Manman Ren97f81572012-10-16 19:18:39 +00003270 return ABIArgInfo::getDirect(ResType);
3271 }
3272 if (Size == 128) {
3273 llvm::Type *ResType = llvm::VectorType::get(
3274 llvm::Type::getInt32Ty(getVMContext()), 4);
Manman Ren710c5172012-10-31 19:02:26 +00003275 markAllocatedVFPs(VFPRegs, AllocatedVFP, 4, 4);
Manman Ren97f81572012-10-16 19:18:39 +00003276 return ABIArgInfo::getDirect(ResType);
3277 }
3278 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3279 }
Manman Ren710c5172012-10-31 19:02:26 +00003280 // Update VFPRegs for legal vector types.
Manman Renb3fa55f2012-10-30 23:21:41 +00003281 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3282 uint64_t Size = getContext().getTypeSize(VT);
3283 // Size of a legal vector should be power of 2 and above 64.
Manman Ren710c5172012-10-31 19:02:26 +00003284 markAllocatedVFPs(VFPRegs, AllocatedVFP, Size >= 128 ? 4 : 2, Size / 32);
Manman Renb3fa55f2012-10-30 23:21:41 +00003285 }
Manman Ren710c5172012-10-31 19:02:26 +00003286 // Update VFPRegs for floating point types.
Manman Renb3fa55f2012-10-30 23:21:41 +00003287 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3288 if (BT->getKind() == BuiltinType::Half ||
3289 BT->getKind() == BuiltinType::Float)
Manman Ren710c5172012-10-31 19:02:26 +00003290 markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, 1);
Manman Renb3fa55f2012-10-30 23:21:41 +00003291 if (BT->getKind() == BuiltinType::Double ||
Manman Ren710c5172012-10-31 19:02:26 +00003292 BT->getKind() == BuiltinType::LongDouble)
3293 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
Manman Renb3fa55f2012-10-30 23:21:41 +00003294 }
Manman Ren97f81572012-10-16 19:18:39 +00003295
John McCalld608cdb2010-08-22 10:59:02 +00003296 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003297 // Treat an enum type as its underlying type.
3298 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3299 Ty = EnumTy->getDecl()->getIntegerType();
3300
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003301 return (Ty->isPromotableIntegerType() ?
3302 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003303 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003304
Daniel Dunbar42025572009-09-14 21:54:03 +00003305 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003306 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00003307 return ABIArgInfo::getIgnore();
3308
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00003309 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
3310 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003311
Bob Wilson194f06a2011-08-03 05:58:22 +00003312 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
Manman Renb3fa55f2012-10-30 23:21:41 +00003313 // Homogeneous Aggregates need to be expanded when we can fit the aggregate
3314 // into VFP registers.
Bob Wilson194f06a2011-08-03 05:58:22 +00003315 const Type *Base = 0;
Manman Renb3fa55f2012-10-30 23:21:41 +00003316 uint64_t Members = 0;
3317 if (isHomogeneousAggregate(Ty, Base, getContext(), &Members)) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003318 assert(Base && "Base class should be set for homogeneous aggregate");
Manman Renb3fa55f2012-10-30 23:21:41 +00003319 // Base can be a floating-point or a vector.
3320 if (Base->isVectorType()) {
3321 // ElementSize is in number of floats.
3322 unsigned ElementSize = getContext().getTypeSize(Base) == 64 ? 2 : 4;
Manman Rencb489dd2012-11-06 19:05:29 +00003323 markAllocatedVFPs(VFPRegs, AllocatedVFP, ElementSize,
3324 Members * ElementSize);
Manman Renb3fa55f2012-10-30 23:21:41 +00003325 } else if (Base->isSpecificBuiltinType(BuiltinType::Float))
Manman Ren710c5172012-10-31 19:02:26 +00003326 markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, Members);
Manman Renb3fa55f2012-10-30 23:21:41 +00003327 else {
3328 assert(Base->isSpecificBuiltinType(BuiltinType::Double) ||
3329 Base->isSpecificBuiltinType(BuiltinType::LongDouble));
Manman Ren710c5172012-10-31 19:02:26 +00003330 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, Members * 2);
Manman Renb3fa55f2012-10-30 23:21:41 +00003331 }
3332 IsHA = true;
Bob Wilson194f06a2011-08-03 05:58:22 +00003333 return ABIArgInfo::getExpand();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003334 }
Bob Wilson194f06a2011-08-03 05:58:22 +00003335 }
3336
Manman Ren634b3d22012-08-13 21:23:55 +00003337 // Support byval for ARM.
Manman Rencb489dd2012-11-06 19:05:29 +00003338 // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
3339 // most 8-byte. We realign the indirect argument if type alignment is bigger
3340 // than ABI alignment.
Manman Renfd1ba912012-11-05 22:42:46 +00003341 uint64_t ABIAlign = 4;
3342 uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8;
3343 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3344 getABIKind() == ARMABIInfo::AAPCS)
3345 ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
Manman Ren885ad692012-11-06 04:58:01 +00003346 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
3347 return ABIArgInfo::getIndirect(0, /*ByVal=*/true,
Manman Rencb489dd2012-11-06 19:05:29 +00003348 /*Realign=*/TyAlign > ABIAlign);
Eli Friedman79f30982012-08-09 00:31:40 +00003349 }
3350
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00003351 // Otherwise, pass by coercing to a structure of the appropriate size.
Chris Lattner2acc6e32011-07-18 04:24:23 +00003352 llvm::Type* ElemTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003353 unsigned SizeRegs;
Eli Friedman79f30982012-08-09 00:31:40 +00003354 // FIXME: Try to match the types of the arguments more accurately where
3355 // we can.
3356 if (getContext().getTypeAlign(Ty) <= 32) {
Bob Wilson53fc1a62011-08-01 23:39:04 +00003357 ElemTy = llvm::Type::getInt32Ty(getVMContext());
3358 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Manman Ren78eb76e2012-06-25 22:04:00 +00003359 } else {
Manman Ren78eb76e2012-06-25 22:04:00 +00003360 ElemTy = llvm::Type::getInt64Ty(getVMContext());
3361 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastings67d097e2011-04-27 17:24:02 +00003362 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003363
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003364 llvm::Type *STy =
Chris Lattner7650d952011-06-18 22:49:11 +00003365 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003366 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003367}
3368
Chris Lattnera3c109b2010-07-29 02:16:43 +00003369static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00003370 llvm::LLVMContext &VMContext) {
3371 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
3372 // is called integer-like if its size is less than or equal to one word, and
3373 // the offset of each of its addressable sub-fields is zero.
3374
3375 uint64_t Size = Context.getTypeSize(Ty);
3376
3377 // Check that the type fits in a word.
3378 if (Size > 32)
3379 return false;
3380
3381 // FIXME: Handle vector types!
3382 if (Ty->isVectorType())
3383 return false;
3384
Daniel Dunbarb0d58192009-09-14 02:20:34 +00003385 // Float types are never treated as "integer like".
3386 if (Ty->isRealFloatingType())
3387 return false;
3388
Daniel Dunbar98303b92009-09-13 08:03:58 +00003389 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00003390 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00003391 return true;
3392
Daniel Dunbar45815812010-02-01 23:31:26 +00003393 // Small complex integer types are "integer like".
3394 if (const ComplexType *CT = Ty->getAs<ComplexType>())
3395 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003396
3397 // Single element and zero sized arrays should be allowed, by the definition
3398 // above, but they are not.
3399
3400 // Otherwise, it must be a record type.
3401 const RecordType *RT = Ty->getAs<RecordType>();
3402 if (!RT) return false;
3403
3404 // Ignore records with flexible arrays.
3405 const RecordDecl *RD = RT->getDecl();
3406 if (RD->hasFlexibleArrayMember())
3407 return false;
3408
3409 // Check that all sub-fields are at offset 0, and are themselves "integer
3410 // like".
3411 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3412
3413 bool HadField = false;
3414 unsigned idx = 0;
3415 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3416 i != e; ++i, ++idx) {
David Blaikie581deb32012-06-06 20:45:41 +00003417 const FieldDecl *FD = *i;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003418
Daniel Dunbar679855a2010-01-29 03:22:29 +00003419 // Bit-fields are not addressable, we only need to verify they are "integer
3420 // like". We still have to disallow a subsequent non-bitfield, for example:
3421 // struct { int : 0; int x }
3422 // is non-integer like according to gcc.
3423 if (FD->isBitField()) {
3424 if (!RD->isUnion())
3425 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003426
Daniel Dunbar679855a2010-01-29 03:22:29 +00003427 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3428 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003429
Daniel Dunbar679855a2010-01-29 03:22:29 +00003430 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003431 }
3432
Daniel Dunbar679855a2010-01-29 03:22:29 +00003433 // Check if this field is at offset 0.
3434 if (Layout.getFieldOffset(idx) != 0)
3435 return false;
3436
Daniel Dunbar98303b92009-09-13 08:03:58 +00003437 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3438 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003439
Daniel Dunbar679855a2010-01-29 03:22:29 +00003440 // Only allow at most one field in a structure. This doesn't match the
3441 // wording above, but follows gcc in situations with a field following an
3442 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00003443 if (!RD->isUnion()) {
3444 if (HadField)
3445 return false;
3446
3447 HadField = true;
3448 }
3449 }
3450
3451 return true;
3452}
3453
Chris Lattnera3c109b2010-07-29 02:16:43 +00003454ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003455 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003456 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00003457
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00003458 // Large vector types should be returned via memory.
3459 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
3460 return ABIArgInfo::getIndirect(0);
3461
John McCalld608cdb2010-08-22 10:59:02 +00003462 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003463 // Treat an enum type as its underlying type.
3464 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3465 RetTy = EnumTy->getDecl()->getIntegerType();
3466
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003467 return (RetTy->isPromotableIntegerType() ?
3468 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003469 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003470
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003471 // Structures with either a non-trivial destructor or a non-trivial
3472 // copy constructor are always indirect.
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00003473 if (isRecordReturnIndirect(RetTy, CGT))
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003474 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3475
Daniel Dunbar98303b92009-09-13 08:03:58 +00003476 // Are we following APCS?
3477 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00003478 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00003479 return ABIArgInfo::getIgnore();
3480
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003481 // Complex types are all returned as packed integers.
3482 //
3483 // FIXME: Consider using 2 x vector types if the back end handles them
3484 // correctly.
3485 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00003486 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00003487 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003488
Daniel Dunbar98303b92009-09-13 08:03:58 +00003489 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003490 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003491 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003492 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003493 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003494 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003495 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003496 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3497 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003498 }
3499
3500 // Otherwise return in memory.
3501 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003502 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003503
3504 // Otherwise this is an AAPCS variant.
3505
Chris Lattnera3c109b2010-07-29 02:16:43 +00003506 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00003507 return ABIArgInfo::getIgnore();
3508
Bob Wilson3b694fa2011-11-02 04:51:36 +00003509 // Check for homogeneous aggregates with AAPCS-VFP.
3510 if (getABIKind() == AAPCS_VFP) {
3511 const Type *Base = 0;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003512 if (isHomogeneousAggregate(RetTy, Base, getContext())) {
3513 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson3b694fa2011-11-02 04:51:36 +00003514 // Homogeneous Aggregates are returned directly.
3515 return ABIArgInfo::getDirect();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003516 }
Bob Wilson3b694fa2011-11-02 04:51:36 +00003517 }
3518
Daniel Dunbar98303b92009-09-13 08:03:58 +00003519 // Aggregates <= 4 bytes are returned in r0; other aggregates
3520 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003521 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00003522 if (Size <= 32) {
3523 // Return in the smallest viable integer type.
3524 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003525 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003526 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003527 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3528 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003529 }
3530
Daniel Dunbar98303b92009-09-13 08:03:58 +00003531 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003532}
3533
Manman Ren97f81572012-10-16 19:18:39 +00003534/// isIllegalVector - check whether Ty is an illegal vector type.
3535bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
3536 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3537 // Check whether VT is legal.
3538 unsigned NumElements = VT->getNumElements();
3539 uint64_t Size = getContext().getTypeSize(VT);
3540 // NumElements should be power of 2.
3541 if ((NumElements & (NumElements - 1)) != 0)
3542 return true;
3543 // Size should be greater than 32 bits.
3544 return Size <= 32;
3545 }
3546 return false;
3547}
3548
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003549llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00003550 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003551 llvm::Type *BP = CGF.Int8PtrTy;
3552 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003553
3554 CGBuilderTy &Builder = CGF.Builder;
Chris Lattner8b418682012-02-07 00:39:47 +00003555 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003556 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Manman Rend105e732012-10-16 19:01:37 +00003557
3558 uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8;
Rafael Espindolae164c182011-08-02 22:33:37 +00003559 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
Manman Ren97f81572012-10-16 19:18:39 +00003560 bool IsIndirect = false;
Manman Rend105e732012-10-16 19:01:37 +00003561
3562 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
3563 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
Manman Ren93371022012-10-16 19:51:48 +00003564 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3565 getABIKind() == ARMABIInfo::AAPCS)
3566 TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
3567 else
3568 TyAlign = 4;
Manman Ren97f81572012-10-16 19:18:39 +00003569 // Use indirect if size of the illegal vector is bigger than 16 bytes.
3570 if (isIllegalVectorType(Ty) && Size > 16) {
3571 IsIndirect = true;
3572 Size = 4;
3573 TyAlign = 4;
3574 }
Manman Rend105e732012-10-16 19:01:37 +00003575
3576 // Handle address alignment for ABI alignment > 4 bytes.
Rafael Espindolae164c182011-08-02 22:33:37 +00003577 if (TyAlign > 4) {
3578 assert((TyAlign & (TyAlign - 1)) == 0 &&
3579 "Alignment is not power of 2!");
3580 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3581 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
3582 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
Manman Rend105e732012-10-16 19:01:37 +00003583 Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align");
Rafael Espindolae164c182011-08-02 22:33:37 +00003584 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003585
3586 uint64_t Offset =
Manman Rend105e732012-10-16 19:01:37 +00003587 llvm::RoundUpToAlignment(Size, 4);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003588 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00003589 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003590 "ap.next");
3591 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3592
Manman Ren97f81572012-10-16 19:18:39 +00003593 if (IsIndirect)
3594 Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP));
Manman Ren93371022012-10-16 19:51:48 +00003595 else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) {
Manman Rend105e732012-10-16 19:01:37 +00003596 // We can't directly cast ap.cur to pointer to a vector type, since ap.cur
3597 // may not be correctly aligned for the vector type. We create an aligned
3598 // temporary space and copy the content over from ap.cur to the temporary
3599 // space. This is necessary if the natural alignment of the type is greater
3600 // than the ABI alignment.
3601 llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
3602 CharUnits CharSize = getContext().getTypeSizeInChars(Ty);
3603 llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty),
3604 "var.align");
3605 llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
3606 llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy);
3607 Builder.CreateMemCpy(Dst, Src,
3608 llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()),
3609 TyAlign, false);
3610 Addr = AlignedTemp; //The content is in aligned location.
3611 }
3612 llvm::Type *PTy =
3613 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3614 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3615
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003616 return AddrTyped;
3617}
3618
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00003619namespace {
3620
Derek Schuff263366f2012-10-16 22:30:41 +00003621class NaClARMABIInfo : public ABIInfo {
3622 public:
3623 NaClARMABIInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3624 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, Kind) {}
3625 virtual void computeInfo(CGFunctionInfo &FI) const;
3626 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3627 CodeGenFunction &CGF) const;
3628 private:
3629 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
3630 ARMABIInfo NInfo; // Used for everything else.
3631};
3632
3633class NaClARMTargetCodeGenInfo : public TargetCodeGenInfo {
3634 public:
3635 NaClARMTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3636 : TargetCodeGenInfo(new NaClARMABIInfo(CGT, Kind)) {}
3637};
3638
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00003639}
3640
Derek Schuff263366f2012-10-16 22:30:41 +00003641void NaClARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
3642 if (FI.getASTCallingConvention() == CC_PnaclCall)
3643 PInfo.computeInfo(FI);
3644 else
3645 static_cast<const ABIInfo&>(NInfo).computeInfo(FI);
3646}
3647
3648llvm::Value *NaClARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3649 CodeGenFunction &CGF) const {
3650 // Always use the native convention; calling pnacl-style varargs functions
3651 // is unsupported.
3652 return static_cast<const ABIInfo&>(NInfo).EmitVAArg(VAListAddr, Ty, CGF);
3653}
3654
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003655//===----------------------------------------------------------------------===//
Tim Northoverc264e162013-01-31 12:13:10 +00003656// AArch64 ABI Implementation
3657//===----------------------------------------------------------------------===//
3658
3659namespace {
3660
3661class AArch64ABIInfo : public ABIInfo {
3662public:
3663 AArch64ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3664
3665private:
3666 // The AArch64 PCS is explicit about return types and argument types being
3667 // handled identically, so we don't need to draw a distinction between
3668 // Argument and Return classification.
3669 ABIArgInfo classifyGenericType(QualType Ty, int &FreeIntRegs,
3670 int &FreeVFPRegs) const;
3671
3672 ABIArgInfo tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded, bool IsInt,
3673 llvm::Type *DirectTy = 0) const;
3674
3675 virtual void computeInfo(CGFunctionInfo &FI) const;
3676
3677 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3678 CodeGenFunction &CGF) const;
3679};
3680
3681class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
3682public:
3683 AArch64TargetCodeGenInfo(CodeGenTypes &CGT)
3684 :TargetCodeGenInfo(new AArch64ABIInfo(CGT)) {}
3685
3686 const AArch64ABIInfo &getABIInfo() const {
3687 return static_cast<const AArch64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
3688 }
3689
3690 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3691 return 31;
3692 }
3693
3694 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3695 llvm::Value *Address) const {
3696 // 0-31 are x0-x30 and sp: 8 bytes each
3697 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
3698 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 31);
3699
3700 // 64-95 are v0-v31: 16 bytes each
3701 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
3702 AssignToArrayRange(CGF.Builder, Address, Sixteen8, 64, 95);
3703
3704 return false;
3705 }
3706
3707};
3708
3709}
3710
3711void AArch64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
3712 int FreeIntRegs = 8, FreeVFPRegs = 8;
3713
3714 FI.getReturnInfo() = classifyGenericType(FI.getReturnType(),
3715 FreeIntRegs, FreeVFPRegs);
3716
3717 FreeIntRegs = FreeVFPRegs = 8;
3718 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3719 it != ie; ++it) {
3720 it->info = classifyGenericType(it->type, FreeIntRegs, FreeVFPRegs);
3721
3722 }
3723}
3724
3725ABIArgInfo
3726AArch64ABIInfo::tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded,
3727 bool IsInt, llvm::Type *DirectTy) const {
3728 if (FreeRegs >= RegsNeeded) {
3729 FreeRegs -= RegsNeeded;
3730 return ABIArgInfo::getDirect(DirectTy);
3731 }
3732
3733 llvm::Type *Padding = 0;
3734
3735 // We need padding so that later arguments don't get filled in anyway. That
3736 // wouldn't happen if only ByVal arguments followed in the same category, but
3737 // a large structure will simply seem to be a pointer as far as LLVM is
3738 // concerned.
3739 if (FreeRegs > 0) {
3740 if (IsInt)
3741 Padding = llvm::Type::getInt64Ty(getVMContext());
3742 else
3743 Padding = llvm::Type::getFloatTy(getVMContext());
3744
3745 // Either [N x i64] or [N x float].
3746 Padding = llvm::ArrayType::get(Padding, FreeRegs);
3747 FreeRegs = 0;
3748 }
3749
3750 return ABIArgInfo::getIndirect(getContext().getTypeAlign(Ty) / 8,
3751 /*IsByVal=*/ true, /*Realign=*/ false,
3752 Padding);
3753}
3754
3755
3756ABIArgInfo AArch64ABIInfo::classifyGenericType(QualType Ty,
3757 int &FreeIntRegs,
3758 int &FreeVFPRegs) const {
3759 // Can only occurs for return, but harmless otherwise.
3760 if (Ty->isVoidType())
3761 return ABIArgInfo::getIgnore();
3762
3763 // Large vector types should be returned via memory. There's no such concept
3764 // in the ABI, but they'd be over 16 bytes anyway so no matter how they're
3765 // classified they'd go into memory (see B.3).
3766 if (Ty->isVectorType() && getContext().getTypeSize(Ty) > 128) {
3767 if (FreeIntRegs > 0)
3768 --FreeIntRegs;
3769 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3770 }
3771
3772 // All non-aggregate LLVM types have a concrete ABI representation so they can
3773 // be passed directly. After this block we're guaranteed to be in a
3774 // complicated case.
3775 if (!isAggregateTypeForABI(Ty)) {
3776 // Treat an enum type as its underlying type.
3777 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3778 Ty = EnumTy->getDecl()->getIntegerType();
3779
3780 if (Ty->isFloatingType() || Ty->isVectorType())
3781 return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ false);
3782
3783 assert(getContext().getTypeSize(Ty) <= 128 &&
3784 "unexpectedly large scalar type");
3785
3786 int RegsNeeded = getContext().getTypeSize(Ty) > 64 ? 2 : 1;
3787
3788 // If the type may need padding registers to ensure "alignment", we must be
3789 // careful when this is accounted for. Increasing the effective size covers
3790 // all cases.
3791 if (getContext().getTypeAlign(Ty) == 128)
3792 RegsNeeded += FreeIntRegs % 2 != 0;
3793
3794 return tryUseRegs(Ty, FreeIntRegs, RegsNeeded, /*IsInt=*/ true);
3795 }
3796
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00003797 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) {
3798 if (FreeIntRegs > 0 && RAA == CGCXXABI::RAA_Indirect)
Tim Northoverc264e162013-01-31 12:13:10 +00003799 --FreeIntRegs;
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00003800 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Tim Northoverc264e162013-01-31 12:13:10 +00003801 }
3802
3803 if (isEmptyRecord(getContext(), Ty, true)) {
3804 if (!getContext().getLangOpts().CPlusPlus) {
3805 // Empty structs outside C++ mode are a GNU extension, so no ABI can
3806 // possibly tell us what to do. It turns out (I believe) that GCC ignores
3807 // the object for parameter-passsing purposes.
3808 return ABIArgInfo::getIgnore();
3809 }
3810
3811 // The combination of C++98 9p5 (sizeof(struct) != 0) and the pseudocode
3812 // description of va_arg in the PCS require that an empty struct does
3813 // actually occupy space for parameter-passing. I'm hoping for a
3814 // clarification giving an explicit paragraph to point to in future.
3815 return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ true,
3816 llvm::Type::getInt8Ty(getVMContext()));
3817 }
3818
3819 // Homogeneous vector aggregates get passed in registers or on the stack.
3820 const Type *Base = 0;
3821 uint64_t NumMembers = 0;
3822 if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)) {
3823 assert(Base && "Base class should be set for homogeneous aggregate");
3824 // Homogeneous aggregates are passed and returned directly.
3825 return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ NumMembers,
3826 /*IsInt=*/ false);
3827 }
3828
3829 uint64_t Size = getContext().getTypeSize(Ty);
3830 if (Size <= 128) {
3831 // Small structs can use the same direct type whether they're in registers
3832 // or on the stack.
3833 llvm::Type *BaseTy;
3834 unsigned NumBases;
3835 int SizeInRegs = (Size + 63) / 64;
3836
3837 if (getContext().getTypeAlign(Ty) == 128) {
3838 BaseTy = llvm::Type::getIntNTy(getVMContext(), 128);
3839 NumBases = 1;
3840
3841 // If the type may need padding registers to ensure "alignment", we must
3842 // be careful when this is accounted for. Increasing the effective size
3843 // covers all cases.
3844 SizeInRegs += FreeIntRegs % 2 != 0;
3845 } else {
3846 BaseTy = llvm::Type::getInt64Ty(getVMContext());
3847 NumBases = SizeInRegs;
3848 }
3849 llvm::Type *DirectTy = llvm::ArrayType::get(BaseTy, NumBases);
3850
3851 return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ SizeInRegs,
3852 /*IsInt=*/ true, DirectTy);
3853 }
3854
3855 // If the aggregate is > 16 bytes, it's passed and returned indirectly. In
3856 // LLVM terms the return uses an "sret" pointer, but that's handled elsewhere.
3857 --FreeIntRegs;
3858 return ABIArgInfo::getIndirect(0, /* byVal = */ false);
3859}
3860
3861llvm::Value *AArch64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3862 CodeGenFunction &CGF) const {
3863 // The AArch64 va_list type and handling is specified in the Procedure Call
3864 // Standard, section B.4:
3865 //
3866 // struct {
3867 // void *__stack;
3868 // void *__gr_top;
3869 // void *__vr_top;
3870 // int __gr_offs;
3871 // int __vr_offs;
3872 // };
3873
3874 assert(!CGF.CGM.getDataLayout().isBigEndian()
3875 && "va_arg not implemented for big-endian AArch64");
3876
3877 int FreeIntRegs = 8, FreeVFPRegs = 8;
3878 Ty = CGF.getContext().getCanonicalType(Ty);
3879 ABIArgInfo AI = classifyGenericType(Ty, FreeIntRegs, FreeVFPRegs);
3880
3881 llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
3882 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
3883 llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
3884 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
3885
3886 llvm::Value *reg_offs_p = 0, *reg_offs = 0;
3887 int reg_top_index;
3888 int RegSize;
3889 if (FreeIntRegs < 8) {
3890 assert(FreeVFPRegs == 8 && "Arguments never split between int & VFP regs");
3891 // 3 is the field number of __gr_offs
3892 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p");
3893 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
3894 reg_top_index = 1; // field number for __gr_top
3895 RegSize = 8 * (8 - FreeIntRegs);
3896 } else {
3897 assert(FreeVFPRegs < 8 && "Argument must go in VFP or int regs");
3898 // 4 is the field number of __vr_offs.
3899 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p");
3900 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
3901 reg_top_index = 2; // field number for __vr_top
3902 RegSize = 16 * (8 - FreeVFPRegs);
3903 }
3904
3905 //=======================================
3906 // Find out where argument was passed
3907 //=======================================
3908
3909 // If reg_offs >= 0 we're already using the stack for this type of
3910 // argument. We don't want to keep updating reg_offs (in case it overflows,
3911 // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
3912 // whatever they get).
3913 llvm::Value *UsingStack = 0;
3914 UsingStack = CGF.Builder.CreateICmpSGE(reg_offs,
3915 llvm::ConstantInt::get(CGF.Int32Ty, 0));
3916
3917 CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
3918
3919 // Otherwise, at least some kind of argument could go in these registers, the
3920 // quesiton is whether this particular type is too big.
3921 CGF.EmitBlock(MaybeRegBlock);
3922
3923 // Integer arguments may need to correct register alignment (for example a
3924 // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
3925 // align __gr_offs to calculate the potential address.
3926 if (FreeIntRegs < 8 && AI.isDirect() && getContext().getTypeAlign(Ty) > 64) {
3927 int Align = getContext().getTypeAlign(Ty) / 8;
3928
3929 reg_offs = CGF.Builder.CreateAdd(reg_offs,
3930 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
3931 "align_regoffs");
3932 reg_offs = CGF.Builder.CreateAnd(reg_offs,
3933 llvm::ConstantInt::get(CGF.Int32Ty, -Align),
3934 "aligned_regoffs");
3935 }
3936
3937 // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
3938 llvm::Value *NewOffset = 0;
3939 NewOffset = CGF.Builder.CreateAdd(reg_offs,
3940 llvm::ConstantInt::get(CGF.Int32Ty, RegSize),
3941 "new_reg_offs");
3942 CGF.Builder.CreateStore(NewOffset, reg_offs_p);
3943
3944 // Now we're in a position to decide whether this argument really was in
3945 // registers or not.
3946 llvm::Value *InRegs = 0;
3947 InRegs = CGF.Builder.CreateICmpSLE(NewOffset,
3948 llvm::ConstantInt::get(CGF.Int32Ty, 0),
3949 "inreg");
3950
3951 CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
3952
3953 //=======================================
3954 // Argument was in registers
3955 //=======================================
3956
3957 // Now we emit the code for if the argument was originally passed in
3958 // registers. First start the appropriate block:
3959 CGF.EmitBlock(InRegBlock);
3960
3961 llvm::Value *reg_top_p = 0, *reg_top = 0;
3962 reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p");
3963 reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
3964 llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs);
3965 llvm::Value *RegAddr = 0;
3966 llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
3967
3968 if (!AI.isDirect()) {
3969 // If it's been passed indirectly (actually a struct), whatever we find from
3970 // stored registers or on the stack will actually be a struct **.
3971 MemTy = llvm::PointerType::getUnqual(MemTy);
3972 }
3973
3974 const Type *Base = 0;
3975 uint64_t NumMembers;
3976 if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)
3977 && NumMembers > 1) {
3978 // Homogeneous aggregates passed in registers will have their elements split
3979 // and stored 16-bytes apart regardless of size (they're notionally in qN,
3980 // qN+1, ...). We reload and store into a temporary local variable
3981 // contiguously.
3982 assert(AI.isDirect() && "Homogeneous aggregates should be passed directly");
3983 llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
3984 llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
3985 llvm::Value *Tmp = CGF.CreateTempAlloca(HFATy);
3986
3987 for (unsigned i = 0; i < NumMembers; ++i) {
3988 llvm::Value *BaseOffset = llvm::ConstantInt::get(CGF.Int32Ty, 16 * i);
3989 llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset);
3990 LoadAddr = CGF.Builder.CreateBitCast(LoadAddr,
3991 llvm::PointerType::getUnqual(BaseTy));
3992 llvm::Value *StoreAddr = CGF.Builder.CreateStructGEP(Tmp, i);
3993
3994 llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
3995 CGF.Builder.CreateStore(Elem, StoreAddr);
3996 }
3997
3998 RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy);
3999 } else {
4000 // Otherwise the object is contiguous in memory
4001 RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy);
4002 }
4003
4004 CGF.EmitBranch(ContBlock);
4005
4006 //=======================================
4007 // Argument was on the stack
4008 //=======================================
4009 CGF.EmitBlock(OnStackBlock);
4010
4011 llvm::Value *stack_p = 0, *OnStackAddr = 0;
4012 stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p");
4013 OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack");
4014
4015 // Again, stack arguments may need realigmnent. In this case both integer and
4016 // floating-point ones might be affected.
4017 if (AI.isDirect() && getContext().getTypeAlign(Ty) > 64) {
4018 int Align = getContext().getTypeAlign(Ty) / 8;
4019
4020 OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty);
4021
4022 OnStackAddr = CGF.Builder.CreateAdd(OnStackAddr,
4023 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
4024 "align_stack");
4025 OnStackAddr = CGF.Builder.CreateAnd(OnStackAddr,
4026 llvm::ConstantInt::get(CGF.Int64Ty, -Align),
4027 "align_stack");
4028
4029 OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy);
4030 }
4031
4032 uint64_t StackSize;
4033 if (AI.isDirect())
4034 StackSize = getContext().getTypeSize(Ty) / 8;
4035 else
4036 StackSize = 8;
4037
4038 // All stack slots are 8 bytes
4039 StackSize = llvm::RoundUpToAlignment(StackSize, 8);
4040
4041 llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize);
4042 llvm::Value *NewStack = CGF.Builder.CreateGEP(OnStackAddr, StackSizeC,
4043 "new_stack");
4044
4045 // Write the new value of __stack for the next call to va_arg
4046 CGF.Builder.CreateStore(NewStack, stack_p);
4047
4048 OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy);
4049
4050 CGF.EmitBranch(ContBlock);
4051
4052 //=======================================
4053 // Tidy up
4054 //=======================================
4055 CGF.EmitBlock(ContBlock);
4056
4057 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr");
4058 ResAddr->addIncoming(RegAddr, InRegBlock);
4059 ResAddr->addIncoming(OnStackAddr, OnStackBlock);
4060
4061 if (AI.isDirect())
4062 return ResAddr;
4063
4064 return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr");
4065}
4066
4067//===----------------------------------------------------------------------===//
Justin Holewinski2c585b92012-05-24 17:43:12 +00004068// NVPTX ABI Implementation
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004069//===----------------------------------------------------------------------===//
4070
4071namespace {
4072
Justin Holewinski2c585b92012-05-24 17:43:12 +00004073class NVPTXABIInfo : public ABIInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004074public:
Justin Holewinskidca8f332013-03-30 14:38:24 +00004075 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004076
4077 ABIArgInfo classifyReturnType(QualType RetTy) const;
4078 ABIArgInfo classifyArgumentType(QualType Ty) const;
4079
4080 virtual void computeInfo(CGFunctionInfo &FI) const;
4081 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4082 CodeGenFunction &CFG) const;
4083};
4084
Justin Holewinski2c585b92012-05-24 17:43:12 +00004085class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004086public:
Justin Holewinski2c585b92012-05-24 17:43:12 +00004087 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
4088 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
Justin Holewinski818eafb2011-10-05 17:58:44 +00004089
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004090 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4091 CodeGen::CodeGenModule &M) const;
Justin Holewinskidca8f332013-03-30 14:38:24 +00004092private:
4093 static void addKernelMetadata(llvm::Function *F);
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004094};
4095
Justin Holewinski2c585b92012-05-24 17:43:12 +00004096ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004097 if (RetTy->isVoidType())
4098 return ABIArgInfo::getIgnore();
4099 if (isAggregateTypeForABI(RetTy))
4100 return ABIArgInfo::getIndirect(0);
4101 return ABIArgInfo::getDirect();
4102}
4103
Justin Holewinski2c585b92012-05-24 17:43:12 +00004104ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004105 if (isAggregateTypeForABI(Ty))
4106 return ABIArgInfo::getIndirect(0);
4107
4108 return ABIArgInfo::getDirect();
4109}
4110
Justin Holewinski2c585b92012-05-24 17:43:12 +00004111void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004112 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4113 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4114 it != ie; ++it)
4115 it->info = classifyArgumentType(it->type);
4116
4117 // Always honor user-specified calling convention.
4118 if (FI.getCallingConvention() != llvm::CallingConv::C)
4119 return;
4120
John McCallbd7370a2013-02-28 19:01:20 +00004121 FI.setEffectiveCallingConvention(getRuntimeCC());
4122}
4123
Justin Holewinski2c585b92012-05-24 17:43:12 +00004124llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4125 CodeGenFunction &CFG) const {
4126 llvm_unreachable("NVPTX does not support varargs");
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004127}
4128
Justin Holewinski2c585b92012-05-24 17:43:12 +00004129void NVPTXTargetCodeGenInfo::
4130SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4131 CodeGen::CodeGenModule &M) const{
Justin Holewinski818eafb2011-10-05 17:58:44 +00004132 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4133 if (!FD) return;
4134
4135 llvm::Function *F = cast<llvm::Function>(GV);
4136
4137 // Perform special handling in OpenCL mode
David Blaikie4e4d0842012-03-11 07:00:24 +00004138 if (M.getLangOpts().OpenCL) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00004139 // Use OpenCL function attributes to check for kernel functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00004140 // By default, all functions are device functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00004141 if (FD->hasAttr<OpenCLKernelAttr>()) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00004142 // OpenCL __kernel functions get kernel metadata
4143 addKernelMetadata(F);
Justin Holewinski818eafb2011-10-05 17:58:44 +00004144 // And kernel functions are not subject to inlining
Bill Wendling72390b32012-12-20 19:27:06 +00004145 F->addFnAttr(llvm::Attribute::NoInline);
Justin Holewinski818eafb2011-10-05 17:58:44 +00004146 }
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004147 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00004148
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004149 // Perform special handling in CUDA mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00004150 if (M.getLangOpts().CUDA) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00004151 // CUDA __global__ functions get a kernel metadata entry. Since
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004152 // __global__ functions cannot be called from the device, we do not
4153 // need to set the noinline attribute.
4154 if (FD->getAttr<CUDAGlobalAttr>())
Justin Holewinskidca8f332013-03-30 14:38:24 +00004155 addKernelMetadata(F);
Justin Holewinski818eafb2011-10-05 17:58:44 +00004156 }
4157}
4158
Justin Holewinskidca8f332013-03-30 14:38:24 +00004159void NVPTXTargetCodeGenInfo::addKernelMetadata(llvm::Function *F) {
4160 llvm::Module *M = F->getParent();
4161 llvm::LLVMContext &Ctx = M->getContext();
4162
4163 // Get "nvvm.annotations" metadata node
4164 llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
4165
4166 // Create !{<func-ref>, metadata !"kernel", i32 1} node
4167 llvm::SmallVector<llvm::Value *, 3> MDVals;
4168 MDVals.push_back(F);
4169 MDVals.push_back(llvm::MDString::get(Ctx, "kernel"));
4170 MDVals.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), 1));
4171
4172 // Append metadata to nvvm.annotations
4173 MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
4174}
4175
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004176}
4177
4178//===----------------------------------------------------------------------===//
Ulrich Weigandb8409212013-05-06 16:26:41 +00004179// SystemZ ABI Implementation
4180//===----------------------------------------------------------------------===//
4181
4182namespace {
4183
4184class SystemZABIInfo : public ABIInfo {
4185public:
4186 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
4187
4188 bool isPromotableIntegerType(QualType Ty) const;
4189 bool isCompoundType(QualType Ty) const;
4190 bool isFPArgumentType(QualType Ty) const;
4191
4192 ABIArgInfo classifyReturnType(QualType RetTy) const;
4193 ABIArgInfo classifyArgumentType(QualType ArgTy) const;
4194
4195 virtual void computeInfo(CGFunctionInfo &FI) const {
4196 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4197 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4198 it != ie; ++it)
4199 it->info = classifyArgumentType(it->type);
4200 }
4201
4202 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4203 CodeGenFunction &CGF) const;
4204};
4205
4206class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
4207public:
4208 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
4209 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
4210};
4211
4212}
4213
4214bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
4215 // Treat an enum type as its underlying type.
4216 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4217 Ty = EnumTy->getDecl()->getIntegerType();
4218
4219 // Promotable integer types are required to be promoted by the ABI.
4220 if (Ty->isPromotableIntegerType())
4221 return true;
4222
4223 // 32-bit values must also be promoted.
4224 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4225 switch (BT->getKind()) {
4226 case BuiltinType::Int:
4227 case BuiltinType::UInt:
4228 return true;
4229 default:
4230 return false;
4231 }
4232 return false;
4233}
4234
4235bool SystemZABIInfo::isCompoundType(QualType Ty) const {
4236 return Ty->isAnyComplexType() || isAggregateTypeForABI(Ty);
4237}
4238
4239bool SystemZABIInfo::isFPArgumentType(QualType Ty) const {
4240 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4241 switch (BT->getKind()) {
4242 case BuiltinType::Float:
4243 case BuiltinType::Double:
4244 return true;
4245 default:
4246 return false;
4247 }
4248
4249 if (const RecordType *RT = Ty->getAsStructureType()) {
4250 const RecordDecl *RD = RT->getDecl();
4251 bool Found = false;
4252
4253 // If this is a C++ record, check the bases first.
4254 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
4255 for (CXXRecordDecl::base_class_const_iterator I = CXXRD->bases_begin(),
4256 E = CXXRD->bases_end(); I != E; ++I) {
4257 QualType Base = I->getType();
4258
4259 // Empty bases don't affect things either way.
4260 if (isEmptyRecord(getContext(), Base, true))
4261 continue;
4262
4263 if (Found)
4264 return false;
4265 Found = isFPArgumentType(Base);
4266 if (!Found)
4267 return false;
4268 }
4269
4270 // Check the fields.
4271 for (RecordDecl::field_iterator I = RD->field_begin(),
4272 E = RD->field_end(); I != E; ++I) {
4273 const FieldDecl *FD = *I;
4274
4275 // Empty bitfields don't affect things either way.
4276 // Unlike isSingleElementStruct(), empty structure and array fields
4277 // do count. So do anonymous bitfields that aren't zero-sized.
4278 if (FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
4279 return true;
4280
4281 // Unlike isSingleElementStruct(), arrays do not count.
4282 // Nested isFPArgumentType structures still do though.
4283 if (Found)
4284 return false;
4285 Found = isFPArgumentType(FD->getType());
4286 if (!Found)
4287 return false;
4288 }
4289
4290 // Unlike isSingleElementStruct(), trailing padding is allowed.
4291 // An 8-byte aligned struct s { float f; } is passed as a double.
4292 return Found;
4293 }
4294
4295 return false;
4296}
4297
4298llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4299 CodeGenFunction &CGF) const {
4300 // Assume that va_list type is correct; should be pointer to LLVM type:
4301 // struct {
4302 // i64 __gpr;
4303 // i64 __fpr;
4304 // i8 *__overflow_arg_area;
4305 // i8 *__reg_save_area;
4306 // };
4307
4308 // Every argument occupies 8 bytes and is passed by preference in either
4309 // GPRs or FPRs.
4310 Ty = CGF.getContext().getCanonicalType(Ty);
4311 ABIArgInfo AI = classifyArgumentType(Ty);
4312 bool InFPRs = isFPArgumentType(Ty);
4313
4314 llvm::Type *APTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
4315 bool IsIndirect = AI.isIndirect();
4316 unsigned UnpaddedBitSize;
4317 if (IsIndirect) {
4318 APTy = llvm::PointerType::getUnqual(APTy);
4319 UnpaddedBitSize = 64;
4320 } else
4321 UnpaddedBitSize = getContext().getTypeSize(Ty);
4322 unsigned PaddedBitSize = 64;
4323 assert((UnpaddedBitSize <= PaddedBitSize) && "Invalid argument size.");
4324
4325 unsigned PaddedSize = PaddedBitSize / 8;
4326 unsigned Padding = (PaddedBitSize - UnpaddedBitSize) / 8;
4327
4328 unsigned MaxRegs, RegCountField, RegSaveIndex, RegPadding;
4329 if (InFPRs) {
4330 MaxRegs = 4; // Maximum of 4 FPR arguments
4331 RegCountField = 1; // __fpr
4332 RegSaveIndex = 16; // save offset for f0
4333 RegPadding = 0; // floats are passed in the high bits of an FPR
4334 } else {
4335 MaxRegs = 5; // Maximum of 5 GPR arguments
4336 RegCountField = 0; // __gpr
4337 RegSaveIndex = 2; // save offset for r2
4338 RegPadding = Padding; // values are passed in the low bits of a GPR
4339 }
4340
4341 llvm::Value *RegCountPtr =
4342 CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr");
4343 llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count");
4344 llvm::Type *IndexTy = RegCount->getType();
4345 llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs);
4346 llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV,
4347 "fits_in_regs");
4348
4349 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
4350 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
4351 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
4352 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
4353
4354 // Emit code to load the value if it was passed in registers.
4355 CGF.EmitBlock(InRegBlock);
4356
4357 // Work out the address of an argument register.
4358 llvm::Value *PaddedSizeV = llvm::ConstantInt::get(IndexTy, PaddedSize);
4359 llvm::Value *ScaledRegCount =
4360 CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count");
4361 llvm::Value *RegBase =
4362 llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize + RegPadding);
4363 llvm::Value *RegOffset =
4364 CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset");
4365 llvm::Value *RegSaveAreaPtr =
4366 CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr");
4367 llvm::Value *RegSaveArea =
4368 CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area");
4369 llvm::Value *RawRegAddr =
4370 CGF.Builder.CreateGEP(RegSaveArea, RegOffset, "raw_reg_addr");
4371 llvm::Value *RegAddr =
4372 CGF.Builder.CreateBitCast(RawRegAddr, APTy, "reg_addr");
4373
4374 // Update the register count
4375 llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1);
4376 llvm::Value *NewRegCount =
4377 CGF.Builder.CreateAdd(RegCount, One, "reg_count");
4378 CGF.Builder.CreateStore(NewRegCount, RegCountPtr);
4379 CGF.EmitBranch(ContBlock);
4380
4381 // Emit code to load the value if it was passed in memory.
4382 CGF.EmitBlock(InMemBlock);
4383
4384 // Work out the address of a stack argument.
4385 llvm::Value *OverflowArgAreaPtr =
4386 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr");
4387 llvm::Value *OverflowArgArea =
4388 CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area");
4389 llvm::Value *PaddingV = llvm::ConstantInt::get(IndexTy, Padding);
4390 llvm::Value *RawMemAddr =
4391 CGF.Builder.CreateGEP(OverflowArgArea, PaddingV, "raw_mem_addr");
4392 llvm::Value *MemAddr =
4393 CGF.Builder.CreateBitCast(RawMemAddr, APTy, "mem_addr");
4394
4395 // Update overflow_arg_area_ptr pointer
4396 llvm::Value *NewOverflowArgArea =
4397 CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area");
4398 CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
4399 CGF.EmitBranch(ContBlock);
4400
4401 // Return the appropriate result.
4402 CGF.EmitBlock(ContBlock);
4403 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(APTy, 2, "va_arg.addr");
4404 ResAddr->addIncoming(RegAddr, InRegBlock);
4405 ResAddr->addIncoming(MemAddr, InMemBlock);
4406
4407 if (IsIndirect)
4408 return CGF.Builder.CreateLoad(ResAddr, "indirect_arg");
4409
4410 return ResAddr;
4411}
4412
4413
4414ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
4415 if (RetTy->isVoidType())
4416 return ABIArgInfo::getIgnore();
4417 if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64)
4418 return ABIArgInfo::getIndirect(0);
4419 return (isPromotableIntegerType(RetTy) ?
4420 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4421}
4422
4423ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
4424 // Handle the generic C++ ABI.
4425 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
4426 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
4427
4428 // Integers and enums are extended to full register width.
4429 if (isPromotableIntegerType(Ty))
4430 return ABIArgInfo::getExtend();
4431
4432 // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly.
4433 uint64_t Size = getContext().getTypeSize(Ty);
4434 if (Size != 8 && Size != 16 && Size != 32 && Size != 64)
4435 return ABIArgInfo::getIndirect(0);
4436
4437 // Handle small structures.
4438 if (const RecordType *RT = Ty->getAs<RecordType>()) {
4439 // Structures with flexible arrays have variable length, so really
4440 // fail the size test above.
4441 const RecordDecl *RD = RT->getDecl();
4442 if (RD->hasFlexibleArrayMember())
4443 return ABIArgInfo::getIndirect(0);
4444
4445 // The structure is passed as an unextended integer, a float, or a double.
4446 llvm::Type *PassTy;
4447 if (isFPArgumentType(Ty)) {
4448 assert(Size == 32 || Size == 64);
4449 if (Size == 32)
4450 PassTy = llvm::Type::getFloatTy(getVMContext());
4451 else
4452 PassTy = llvm::Type::getDoubleTy(getVMContext());
4453 } else
4454 PassTy = llvm::IntegerType::get(getVMContext(), Size);
4455 return ABIArgInfo::getDirect(PassTy);
4456 }
4457
4458 // Non-structure compounds are passed indirectly.
4459 if (isCompoundType(Ty))
4460 return ABIArgInfo::getIndirect(0);
4461
4462 return ABIArgInfo::getDirect(0);
4463}
4464
4465//===----------------------------------------------------------------------===//
Wesley Peck276fdf42010-12-19 19:57:51 +00004466// MBlaze ABI Implementation
4467//===----------------------------------------------------------------------===//
4468
4469namespace {
4470
4471class MBlazeABIInfo : public ABIInfo {
4472public:
4473 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
4474
4475 bool isPromotableIntegerType(QualType Ty) const;
4476
4477 ABIArgInfo classifyReturnType(QualType RetTy) const;
4478 ABIArgInfo classifyArgumentType(QualType RetTy) const;
4479
4480 virtual void computeInfo(CGFunctionInfo &FI) const {
4481 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4482 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4483 it != ie; ++it)
4484 it->info = classifyArgumentType(it->type);
4485 }
4486
4487 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4488 CodeGenFunction &CGF) const;
4489};
4490
4491class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
4492public:
4493 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
4494 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
4495 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4496 CodeGen::CodeGenModule &M) const;
4497};
4498
4499}
4500
4501bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
4502 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
4503 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4504 switch (BT->getKind()) {
4505 case BuiltinType::Bool:
4506 case BuiltinType::Char_S:
4507 case BuiltinType::Char_U:
4508 case BuiltinType::SChar:
4509 case BuiltinType::UChar:
4510 case BuiltinType::Short:
4511 case BuiltinType::UShort:
4512 return true;
4513 default:
4514 return false;
4515 }
4516 return false;
4517}
4518
4519llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4520 CodeGenFunction &CGF) const {
4521 // FIXME: Implement
4522 return 0;
4523}
4524
4525
4526ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
4527 if (RetTy->isVoidType())
4528 return ABIArgInfo::getIgnore();
4529 if (isAggregateTypeForABI(RetTy))
4530 return ABIArgInfo::getIndirect(0);
4531
4532 return (isPromotableIntegerType(RetTy) ?
4533 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4534}
4535
4536ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
4537 if (isAggregateTypeForABI(Ty))
4538 return ABIArgInfo::getIndirect(0);
4539
4540 return (isPromotableIntegerType(Ty) ?
4541 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4542}
4543
4544void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4545 llvm::GlobalValue *GV,
4546 CodeGen::CodeGenModule &M)
4547 const {
4548 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4549 if (!FD) return;
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00004550
Wesley Peck276fdf42010-12-19 19:57:51 +00004551 llvm::CallingConv::ID CC = llvm::CallingConv::C;
4552 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
4553 CC = llvm::CallingConv::MBLAZE_INTR;
4554 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
4555 CC = llvm::CallingConv::MBLAZE_SVOL;
4556
4557 if (CC != llvm::CallingConv::C) {
4558 // Handle 'interrupt_handler' attribute:
4559 llvm::Function *F = cast<llvm::Function>(GV);
4560
4561 // Step 1: Set ISR calling convention.
4562 F->setCallingConv(CC);
4563
4564 // Step 2: Add attributes goodness.
Bill Wendling72390b32012-12-20 19:27:06 +00004565 F->addFnAttr(llvm::Attribute::NoInline);
Wesley Peck276fdf42010-12-19 19:57:51 +00004566 }
4567
4568 // Step 3: Emit _interrupt_handler alias.
4569 if (CC == llvm::CallingConv::MBLAZE_INTR)
4570 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
4571 "_interrupt_handler", GV, &M.getModule());
4572}
4573
4574
4575//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004576// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004577//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004578
4579namespace {
4580
4581class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
4582public:
Chris Lattnerea044322010-07-29 02:01:43 +00004583 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
4584 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004585 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4586 CodeGen::CodeGenModule &M) const;
4587};
4588
4589}
4590
4591void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4592 llvm::GlobalValue *GV,
4593 CodeGen::CodeGenModule &M) const {
4594 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4595 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
4596 // Handle 'interrupt' attribute:
4597 llvm::Function *F = cast<llvm::Function>(GV);
4598
4599 // Step 1: Set ISR calling convention.
4600 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
4601
4602 // Step 2: Add attributes goodness.
Bill Wendling72390b32012-12-20 19:27:06 +00004603 F->addFnAttr(llvm::Attribute::NoInline);
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004604
4605 // Step 3: Emit ISR vector alias.
Anton Korobeynikovf419a852012-11-26 18:59:10 +00004606 unsigned Num = attr->getNumber() / 2;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004607 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Anton Korobeynikovf419a852012-11-26 18:59:10 +00004608 "__isr_" + Twine(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004609 GV, &M.getModule());
4610 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004611 }
4612}
4613
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004614//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00004615// MIPS ABI Implementation. This works for both little-endian and
4616// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004617//===----------------------------------------------------------------------===//
4618
John McCallaeeb7012010-05-27 06:19:26 +00004619namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00004620class MipsABIInfo : public ABIInfo {
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004621 bool IsO32;
Akira Hatanakac359f202012-07-03 19:24:06 +00004622 unsigned MinABIStackAlignInBytes, StackAlignInBytes;
4623 void CoerceToIntArgs(uint64_t TySize,
4624 SmallVector<llvm::Type*, 8> &ArgList) const;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004625 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004626 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004627 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004628public:
Akira Hatanakab551dd32011-11-03 00:05:50 +00004629 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
Akira Hatanakac359f202012-07-03 19:24:06 +00004630 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
4631 StackAlignInBytes(IsO32 ? 8 : 16) {}
Akira Hatanaka619e8872011-06-02 00:09:17 +00004632
4633 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004634 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004635 virtual void computeInfo(CGFunctionInfo &FI) const;
4636 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4637 CodeGenFunction &CGF) const;
4638};
4639
John McCallaeeb7012010-05-27 06:19:26 +00004640class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanakae624fa02011-09-20 18:23:28 +00004641 unsigned SizeOfUnwindException;
John McCallaeeb7012010-05-27 06:19:26 +00004642public:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004643 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
4644 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
4645 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCallaeeb7012010-05-27 06:19:26 +00004646
4647 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
4648 return 29;
4649 }
4650
Reed Kotler7dfd1822013-01-16 17:10:28 +00004651 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4652 CodeGen::CodeGenModule &CGM) const {
Reed Kotlerad4b8b42013-03-13 20:40:30 +00004653 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4654 if (!FD) return;
Rafael Espindolad8e6d6d2013-03-19 14:32:23 +00004655 llvm::Function *Fn = cast<llvm::Function>(GV);
Reed Kotlerad4b8b42013-03-13 20:40:30 +00004656 if (FD->hasAttr<Mips16Attr>()) {
4657 Fn->addFnAttr("mips16");
4658 }
4659 else if (FD->hasAttr<NoMips16Attr>()) {
4660 Fn->addFnAttr("nomips16");
4661 }
Reed Kotler7dfd1822013-01-16 17:10:28 +00004662 }
Reed Kotlerad4b8b42013-03-13 20:40:30 +00004663
John McCallaeeb7012010-05-27 06:19:26 +00004664 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00004665 llvm::Value *Address) const;
John McCall49e34be2011-08-30 01:42:09 +00004666
4667 unsigned getSizeOfUnwindException() const {
Akira Hatanakae624fa02011-09-20 18:23:28 +00004668 return SizeOfUnwindException;
John McCall49e34be2011-08-30 01:42:09 +00004669 }
John McCallaeeb7012010-05-27 06:19:26 +00004670};
4671}
4672
Akira Hatanakac359f202012-07-03 19:24:06 +00004673void MipsABIInfo::CoerceToIntArgs(uint64_t TySize,
4674 SmallVector<llvm::Type*, 8> &ArgList) const {
4675 llvm::IntegerType *IntTy =
4676 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004677
4678 // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
4679 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
4680 ArgList.push_back(IntTy);
4681
4682 // If necessary, add one more integer type to ArgList.
4683 unsigned R = TySize % (MinABIStackAlignInBytes * 8);
4684
4685 if (R)
4686 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004687}
4688
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004689// In N32/64, an aligned double precision floating point field is passed in
4690// a register.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004691llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
Akira Hatanakac359f202012-07-03 19:24:06 +00004692 SmallVector<llvm::Type*, 8> ArgList, IntArgList;
4693
4694 if (IsO32) {
4695 CoerceToIntArgs(TySize, ArgList);
4696 return llvm::StructType::get(getVMContext(), ArgList);
4697 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004698
Akira Hatanaka2afd23d2012-01-12 00:52:17 +00004699 if (Ty->isComplexType())
4700 return CGT.ConvertType(Ty);
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00004701
Akira Hatanakaa34e9212012-02-09 19:54:16 +00004702 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004703
Akira Hatanakac359f202012-07-03 19:24:06 +00004704 // Unions/vectors are passed in integer registers.
4705 if (!RT || !RT->isStructureOrClassType()) {
4706 CoerceToIntArgs(TySize, ArgList);
4707 return llvm::StructType::get(getVMContext(), ArgList);
4708 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004709
4710 const RecordDecl *RD = RT->getDecl();
4711 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004712 assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004713
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004714 uint64_t LastOffset = 0;
4715 unsigned idx = 0;
4716 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
4717
Akira Hatanakaa34e9212012-02-09 19:54:16 +00004718 // Iterate over fields in the struct/class and check if there are any aligned
4719 // double fields.
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004720 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
4721 i != e; ++i, ++idx) {
David Blaikie262bc182012-04-30 02:36:29 +00004722 const QualType Ty = i->getType();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004723 const BuiltinType *BT = Ty->getAs<BuiltinType>();
4724
4725 if (!BT || BT->getKind() != BuiltinType::Double)
4726 continue;
4727
4728 uint64_t Offset = Layout.getFieldOffset(idx);
4729 if (Offset % 64) // Ignore doubles that are not aligned.
4730 continue;
4731
4732 // Add ((Offset - LastOffset) / 64) args of type i64.
4733 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
4734 ArgList.push_back(I64);
4735
4736 // Add double type.
4737 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
4738 LastOffset = Offset + 64;
4739 }
4740
Akira Hatanakac359f202012-07-03 19:24:06 +00004741 CoerceToIntArgs(TySize - LastOffset, IntArgList);
4742 ArgList.append(IntArgList.begin(), IntArgList.end());
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004743
4744 return llvm::StructType::get(getVMContext(), ArgList);
4745}
4746
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004747llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004748 assert((Offset % MinABIStackAlignInBytes) == 0);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004749
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004750 if ((Align - 1) & Offset)
4751 return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
4752
4753 return 0;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004754}
Akira Hatanaka9659d592012-01-10 22:44:52 +00004755
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004756ABIArgInfo
4757MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004758 uint64_t OrigOffset = Offset;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004759 uint64_t TySize = getContext().getTypeSize(Ty);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004760 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004761
Akira Hatanakac359f202012-07-03 19:24:06 +00004762 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
4763 (uint64_t)StackAlignInBytes);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004764 Offset = llvm::RoundUpToAlignment(Offset, Align);
4765 Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004766
Akira Hatanakac359f202012-07-03 19:24:06 +00004767 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
Akira Hatanaka619e8872011-06-02 00:09:17 +00004768 // Ignore empty aggregates.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004769 if (TySize == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00004770 return ABIArgInfo::getIgnore();
4771
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00004772 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004773 Offset = OrigOffset + MinABIStackAlignInBytes;
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00004774 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004775 }
Akira Hatanaka511949b2011-08-01 18:09:58 +00004776
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004777 // If we have reached here, aggregates are passed directly by coercing to
4778 // another structure type. Padding is inserted if the offset of the
4779 // aggregate is unaligned.
4780 return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
4781 getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00004782 }
4783
4784 // Treat an enum type as its underlying type.
4785 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4786 Ty = EnumTy->getDecl()->getIntegerType();
4787
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004788 if (Ty->isPromotableIntegerType())
4789 return ABIArgInfo::getExtend();
4790
Akira Hatanaka4055cfc2013-01-24 21:47:33 +00004791 return ABIArgInfo::getDirect(0, 0,
4792 IsO32 ? 0 : getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00004793}
4794
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004795llvm::Type*
4796MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakada54ff32012-02-09 18:49:26 +00004797 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakac359f202012-07-03 19:24:06 +00004798 SmallVector<llvm::Type*, 8> RTList;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004799
Akira Hatanakada54ff32012-02-09 18:49:26 +00004800 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004801 const RecordDecl *RD = RT->getDecl();
Akira Hatanakada54ff32012-02-09 18:49:26 +00004802 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
4803 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004804
Akira Hatanakada54ff32012-02-09 18:49:26 +00004805 // N32/64 returns struct/classes in floating point registers if the
4806 // following conditions are met:
4807 // 1. The size of the struct/class is no larger than 128-bit.
4808 // 2. The struct/class has one or two fields all of which are floating
4809 // point types.
4810 // 3. The offset of the first field is zero (this follows what gcc does).
4811 //
4812 // Any other composite results are returned in integer registers.
4813 //
4814 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
4815 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
4816 for (; b != e; ++b) {
David Blaikie262bc182012-04-30 02:36:29 +00004817 const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004818
Akira Hatanakada54ff32012-02-09 18:49:26 +00004819 if (!BT || !BT->isFloatingPoint())
4820 break;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004821
David Blaikie262bc182012-04-30 02:36:29 +00004822 RTList.push_back(CGT.ConvertType(b->getType()));
Akira Hatanakada54ff32012-02-09 18:49:26 +00004823 }
4824
4825 if (b == e)
4826 return llvm::StructType::get(getVMContext(), RTList,
4827 RD->hasAttr<PackedAttr>());
4828
4829 RTList.clear();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004830 }
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004831 }
4832
Akira Hatanakac359f202012-07-03 19:24:06 +00004833 CoerceToIntArgs(Size, RTList);
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004834 return llvm::StructType::get(getVMContext(), RTList);
4835}
4836
Akira Hatanaka619e8872011-06-02 00:09:17 +00004837ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanakaa8536c02012-01-23 23:18:57 +00004838 uint64_t Size = getContext().getTypeSize(RetTy);
4839
4840 if (RetTy->isVoidType() || Size == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00004841 return ABIArgInfo::getIgnore();
4842
Akira Hatanaka8aeb1472012-05-11 21:01:17 +00004843 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00004844 if (isRecordReturnIndirect(RetTy, CGT))
4845 return ABIArgInfo::getIndirect(0);
4846
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004847 if (Size <= 128) {
4848 if (RetTy->isAnyComplexType())
4849 return ABIArgInfo::getDirect();
4850
Akira Hatanakac359f202012-07-03 19:24:06 +00004851 // O32 returns integer vectors in registers.
4852 if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())
4853 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
4854
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00004855 if (!IsO32)
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004856 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
4857 }
Akira Hatanaka619e8872011-06-02 00:09:17 +00004858
4859 return ABIArgInfo::getIndirect(0);
4860 }
4861
4862 // Treat an enum type as its underlying type.
4863 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4864 RetTy = EnumTy->getDecl()->getIntegerType();
4865
4866 return (RetTy->isPromotableIntegerType() ?
4867 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4868}
4869
4870void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanakacc662542012-01-12 01:10:09 +00004871 ABIArgInfo &RetInfo = FI.getReturnInfo();
4872 RetInfo = classifyReturnType(FI.getReturnType());
4873
4874 // Check if a pointer to an aggregate is passed as a hidden argument.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004875 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
Akira Hatanakacc662542012-01-12 01:10:09 +00004876
Akira Hatanaka619e8872011-06-02 00:09:17 +00004877 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4878 it != ie; ++it)
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004879 it->info = classifyArgumentType(it->type, Offset);
Akira Hatanaka619e8872011-06-02 00:09:17 +00004880}
4881
4882llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4883 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00004884 llvm::Type *BP = CGF.Int8PtrTy;
4885 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004886
4887 CGBuilderTy &Builder = CGF.Builder;
4888 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
4889 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004890 int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004891 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
4892 llvm::Value *AddrTyped;
John McCall64aa4b32013-04-16 22:48:15 +00004893 unsigned PtrWidth = getTarget().getPointerWidth(0);
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004894 llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004895
4896 if (TypeAlign > MinABIStackAlignInBytes) {
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004897 llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
4898 llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
4899 llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
4900 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004901 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
4902 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
4903 }
4904 else
4905 AddrTyped = Builder.CreateBitCast(Addr, PTy);
4906
4907 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004908 TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004909 uint64_t Offset =
4910 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
4911 llvm::Value *NextAddr =
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004912 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004913 "ap.next");
4914 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
4915
4916 return AddrTyped;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004917}
4918
John McCallaeeb7012010-05-27 06:19:26 +00004919bool
4920MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4921 llvm::Value *Address) const {
4922 // This information comes from gcc's implementation, which seems to
4923 // as canonical as it gets.
4924
John McCallaeeb7012010-05-27 06:19:26 +00004925 // Everything on MIPS is 4 bytes. Double-precision FP registers
4926 // are aliased to pairs of single-precision FP registers.
Chris Lattner8b418682012-02-07 00:39:47 +00004927 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCallaeeb7012010-05-27 06:19:26 +00004928
4929 // 0-31 are the general purpose registers, $0 - $31.
4930 // 32-63 are the floating-point registers, $f0 - $f31.
4931 // 64 and 65 are the multiply/divide registers, $hi and $lo.
4932 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattner8b418682012-02-07 00:39:47 +00004933 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCallaeeb7012010-05-27 06:19:26 +00004934
4935 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
4936 // They are one bit wide and ignored here.
4937
4938 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
4939 // (coprocessor 1 is the FP unit)
4940 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
4941 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
4942 // 176-181 are the DSP accumulator registers.
Chris Lattner8b418682012-02-07 00:39:47 +00004943 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCallaeeb7012010-05-27 06:19:26 +00004944 return false;
4945}
4946
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004947//===----------------------------------------------------------------------===//
4948// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
4949// Currently subclassed only to implement custom OpenCL C function attribute
4950// handling.
4951//===----------------------------------------------------------------------===//
4952
4953namespace {
4954
4955class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
4956public:
4957 TCETargetCodeGenInfo(CodeGenTypes &CGT)
4958 : DefaultTargetCodeGenInfo(CGT) {}
4959
4960 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4961 CodeGen::CodeGenModule &M) const;
4962};
4963
4964void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4965 llvm::GlobalValue *GV,
4966 CodeGen::CodeGenModule &M) const {
4967 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4968 if (!FD) return;
4969
4970 llvm::Function *F = cast<llvm::Function>(GV);
4971
David Blaikie4e4d0842012-03-11 07:00:24 +00004972 if (M.getLangOpts().OpenCL) {
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004973 if (FD->hasAttr<OpenCLKernelAttr>()) {
4974 // OpenCL C Kernel functions are not subject to inlining
Bill Wendling72390b32012-12-20 19:27:06 +00004975 F->addFnAttr(llvm::Attribute::NoInline);
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004976
4977 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
4978
4979 // Convert the reqd_work_group_size() attributes to metadata.
4980 llvm::LLVMContext &Context = F->getContext();
4981 llvm::NamedMDNode *OpenCLMetadata =
4982 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
4983
4984 SmallVector<llvm::Value*, 5> Operands;
4985 Operands.push_back(F);
4986
Chris Lattner8b418682012-02-07 00:39:47 +00004987 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4988 llvm::APInt(32,
4989 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
4990 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4991 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004992 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
Chris Lattner8b418682012-02-07 00:39:47 +00004993 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4994 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004995 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
4996
4997 // Add a boolean constant operand for "required" (true) or "hint" (false)
4998 // for implementing the work_group_size_hint attr later. Currently
4999 // always true as the hint is not yet implemented.
Chris Lattner8b418682012-02-07 00:39:47 +00005000 Operands.push_back(llvm::ConstantInt::getTrue(Context));
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00005001 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
5002 }
5003 }
5004 }
5005}
5006
5007}
John McCallaeeb7012010-05-27 06:19:26 +00005008
Tony Linthicum96319392011-12-12 21:14:55 +00005009//===----------------------------------------------------------------------===//
5010// Hexagon ABI Implementation
5011//===----------------------------------------------------------------------===//
5012
5013namespace {
5014
5015class HexagonABIInfo : public ABIInfo {
5016
5017
5018public:
5019 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
5020
5021private:
5022
5023 ABIArgInfo classifyReturnType(QualType RetTy) const;
5024 ABIArgInfo classifyArgumentType(QualType RetTy) const;
5025
5026 virtual void computeInfo(CGFunctionInfo &FI) const;
5027
5028 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5029 CodeGenFunction &CGF) const;
5030};
5031
5032class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
5033public:
5034 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
5035 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
5036
5037 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
5038 return 29;
5039 }
5040};
5041
5042}
5043
5044void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
5045 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
5046 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
5047 it != ie; ++it)
5048 it->info = classifyArgumentType(it->type);
5049}
5050
5051ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
5052 if (!isAggregateTypeForABI(Ty)) {
5053 // Treat an enum type as its underlying type.
5054 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5055 Ty = EnumTy->getDecl()->getIntegerType();
5056
5057 return (Ty->isPromotableIntegerType() ?
5058 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
5059 }
5060
5061 // Ignore empty records.
5062 if (isEmptyRecord(getContext(), Ty, true))
5063 return ABIArgInfo::getIgnore();
5064
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00005065 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
5066 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Tony Linthicum96319392011-12-12 21:14:55 +00005067
5068 uint64_t Size = getContext().getTypeSize(Ty);
5069 if (Size > 64)
5070 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
5071 // Pass in the smallest viable integer type.
5072 else if (Size > 32)
5073 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
5074 else if (Size > 16)
5075 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
5076 else if (Size > 8)
5077 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5078 else
5079 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
5080}
5081
5082ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
5083 if (RetTy->isVoidType())
5084 return ABIArgInfo::getIgnore();
5085
5086 // Large vector types should be returned via memory.
5087 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
5088 return ABIArgInfo::getIndirect(0);
5089
5090 if (!isAggregateTypeForABI(RetTy)) {
5091 // Treat an enum type as its underlying type.
5092 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5093 RetTy = EnumTy->getDecl()->getIntegerType();
5094
5095 return (RetTy->isPromotableIntegerType() ?
5096 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
5097 }
5098
5099 // Structures with either a non-trivial destructor or a non-trivial
5100 // copy constructor are always indirect.
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00005101 if (isRecordReturnIndirect(RetTy, CGT))
Tony Linthicum96319392011-12-12 21:14:55 +00005102 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
5103
5104 if (isEmptyRecord(getContext(), RetTy, true))
5105 return ABIArgInfo::getIgnore();
5106
5107 // Aggregates <= 8 bytes are returned in r0; other aggregates
5108 // are returned indirectly.
5109 uint64_t Size = getContext().getTypeSize(RetTy);
5110 if (Size <= 64) {
5111 // Return in the smallest viable integer type.
5112 if (Size <= 8)
5113 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
5114 if (Size <= 16)
5115 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5116 if (Size <= 32)
5117 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
5118 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
5119 }
5120
5121 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
5122}
5123
5124llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner8b418682012-02-07 00:39:47 +00005125 CodeGenFunction &CGF) const {
Tony Linthicum96319392011-12-12 21:14:55 +00005126 // FIXME: Need to handle alignment
Chris Lattner8b418682012-02-07 00:39:47 +00005127 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Tony Linthicum96319392011-12-12 21:14:55 +00005128
5129 CGBuilderTy &Builder = CGF.Builder;
5130 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
5131 "ap");
5132 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
5133 llvm::Type *PTy =
5134 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
5135 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
5136
5137 uint64_t Offset =
5138 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
5139 llvm::Value *NextAddr =
5140 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
5141 "ap.next");
5142 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
5143
5144 return AddrTyped;
5145}
5146
5147
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00005148//===----------------------------------------------------------------------===//
5149// SPARC v9 ABI Implementation.
5150// Based on the SPARC Compliance Definition version 2.4.1.
5151//
5152// Function arguments a mapped to a nominal "parameter array" and promoted to
5153// registers depending on their type. Each argument occupies 8 or 16 bytes in
5154// the array, structs larger than 16 bytes are passed indirectly.
5155//
5156// One case requires special care:
5157//
5158// struct mixed {
5159// int i;
5160// float f;
5161// };
5162//
5163// When a struct mixed is passed by value, it only occupies 8 bytes in the
5164// parameter array, but the int is passed in an integer register, and the float
5165// is passed in a floating point register. This is represented as two arguments
5166// with the LLVM IR inreg attribute:
5167//
5168// declare void f(i32 inreg %i, float inreg %f)
5169//
5170// The code generator will only allocate 4 bytes from the parameter array for
5171// the inreg arguments. All other arguments are allocated a multiple of 8
5172// bytes.
5173//
5174namespace {
5175class SparcV9ABIInfo : public ABIInfo {
5176public:
5177 SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
5178
5179private:
5180 ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const;
5181 virtual void computeInfo(CGFunctionInfo &FI) const;
5182 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5183 CodeGenFunction &CGF) const;
Jakob Stoklund Olesenfc782fb2013-05-28 04:57:37 +00005184
5185 // Coercion type builder for structs passed in registers. The coercion type
5186 // serves two purposes:
5187 //
5188 // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned'
5189 // in registers.
5190 // 2. Expose aligned floating point elements as first-level elements, so the
5191 // code generator knows to pass them in floating point registers.
5192 //
5193 // We also compute the InReg flag which indicates that the struct contains
5194 // aligned 32-bit floats.
5195 //
5196 struct CoerceBuilder {
5197 llvm::LLVMContext &Context;
5198 const llvm::DataLayout &DL;
5199 SmallVector<llvm::Type*, 8> Elems;
5200 uint64_t Size;
5201 bool InReg;
5202
5203 CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl)
5204 : Context(c), DL(dl), Size(0), InReg(false) {}
5205
5206 // Pad Elems with integers until Size is ToSize.
5207 void pad(uint64_t ToSize) {
5208 assert(ToSize >= Size && "Cannot remove elements");
5209 if (ToSize == Size)
5210 return;
5211
5212 // Finish the current 64-bit word.
5213 uint64_t Aligned = llvm::RoundUpToAlignment(Size, 64);
5214 if (Aligned > Size && Aligned <= ToSize) {
5215 Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size));
5216 Size = Aligned;
5217 }
5218
5219 // Add whole 64-bit words.
5220 while (Size + 64 <= ToSize) {
5221 Elems.push_back(llvm::Type::getInt64Ty(Context));
5222 Size += 64;
5223 }
5224
5225 // Final in-word padding.
5226 if (Size < ToSize) {
5227 Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size));
5228 Size = ToSize;
5229 }
5230 }
5231
5232 // Add a floating point element at Offset.
5233 void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) {
5234 // Unaligned floats are treated as integers.
5235 if (Offset % Bits)
5236 return;
5237 // The InReg flag is only required if there are any floats < 64 bits.
5238 if (Bits < 64)
5239 InReg = true;
5240 pad(Offset);
5241 Elems.push_back(Ty);
5242 Size = Offset + Bits;
5243 }
5244
5245 // Add a struct type to the coercion type, starting at Offset (in bits).
5246 void addStruct(uint64_t Offset, llvm::StructType *StrTy) {
5247 const llvm::StructLayout *Layout = DL.getStructLayout(StrTy);
5248 for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) {
5249 llvm::Type *ElemTy = StrTy->getElementType(i);
5250 uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i);
5251 switch (ElemTy->getTypeID()) {
5252 case llvm::Type::StructTyID:
5253 addStruct(ElemOffset, cast<llvm::StructType>(ElemTy));
5254 break;
5255 case llvm::Type::FloatTyID:
5256 addFloat(ElemOffset, ElemTy, 32);
5257 break;
5258 case llvm::Type::DoubleTyID:
5259 addFloat(ElemOffset, ElemTy, 64);
5260 break;
5261 case llvm::Type::FP128TyID:
5262 addFloat(ElemOffset, ElemTy, 128);
5263 break;
5264 case llvm::Type::PointerTyID:
5265 if (ElemOffset % 64 == 0) {
5266 pad(ElemOffset);
5267 Elems.push_back(ElemTy);
5268 Size += 64;
5269 }
5270 break;
5271 default:
5272 break;
5273 }
5274 }
5275 }
5276
5277 // Check if Ty is a usable substitute for the coercion type.
5278 bool isUsableType(llvm::StructType *Ty) const {
5279 if (Ty->getNumElements() != Elems.size())
5280 return false;
5281 for (unsigned i = 0, e = Elems.size(); i != e; ++i)
5282 if (Elems[i] != Ty->getElementType(i))
5283 return false;
5284 return true;
5285 }
5286
5287 // Get the coercion type as a literal struct type.
5288 llvm::Type *getType() const {
5289 if (Elems.size() == 1)
5290 return Elems.front();
5291 else
5292 return llvm::StructType::get(Context, Elems);
5293 }
5294 };
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00005295};
5296} // end anonymous namespace
5297
5298ABIArgInfo
5299SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const {
5300 if (Ty->isVoidType())
5301 return ABIArgInfo::getIgnore();
5302
5303 uint64_t Size = getContext().getTypeSize(Ty);
5304
5305 // Anything too big to fit in registers is passed with an explicit indirect
5306 // pointer / sret pointer.
5307 if (Size > SizeLimit)
5308 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
5309
5310 // Treat an enum type as its underlying type.
5311 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5312 Ty = EnumTy->getDecl()->getIntegerType();
5313
5314 // Integer types smaller than a register are extended.
5315 if (Size < 64 && Ty->isIntegerType())
5316 return ABIArgInfo::getExtend();
5317
5318 // Other non-aggregates go in registers.
5319 if (!isAggregateTypeForABI(Ty))
5320 return ABIArgInfo::getDirect();
5321
5322 // This is a small aggregate type that should be passed in registers.
Jakob Stoklund Olesenfc782fb2013-05-28 04:57:37 +00005323 // Build a coercion type from the LLVM struct type.
5324 llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty));
5325 if (!StrTy)
5326 return ABIArgInfo::getDirect();
5327
5328 CoerceBuilder CB(getVMContext(), getDataLayout());
5329 CB.addStruct(0, StrTy);
5330 CB.pad(llvm::RoundUpToAlignment(CB.DL.getTypeSizeInBits(StrTy), 64));
5331
5332 // Try to use the original type for coercion.
5333 llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();
5334
5335 if (CB.InReg)
5336 return ABIArgInfo::getDirectInReg(CoerceTy);
5337 else
5338 return ABIArgInfo::getDirect(CoerceTy);
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00005339}
5340
5341llvm::Value *SparcV9ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5342 CodeGenFunction &CGF) const {
Jakob Stoklund Olesena4b56d32013-06-05 03:00:18 +00005343 ABIArgInfo AI = classifyType(Ty, 16 * 8);
5344 llvm::Type *ArgTy = CGT.ConvertType(Ty);
5345 if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
5346 AI.setCoerceToType(ArgTy);
5347
5348 llvm::Type *BPP = CGF.Int8PtrPtrTy;
5349 CGBuilderTy &Builder = CGF.Builder;
5350 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
5351 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
5352 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
5353 llvm::Value *ArgAddr;
5354 unsigned Stride;
5355
5356 switch (AI.getKind()) {
5357 case ABIArgInfo::Expand:
5358 llvm_unreachable("Unsupported ABI kind for va_arg");
5359
5360 case ABIArgInfo::Extend:
5361 Stride = 8;
5362 ArgAddr = Builder
5363 .CreateConstGEP1_32(Addr, 8 - getDataLayout().getTypeAllocSize(ArgTy),
5364 "extend");
5365 break;
5366
5367 case ABIArgInfo::Direct:
5368 Stride = getDataLayout().getTypeAllocSize(AI.getCoerceToType());
5369 ArgAddr = Addr;
5370 break;
5371
5372 case ABIArgInfo::Indirect:
5373 Stride = 8;
5374 ArgAddr = Builder.CreateBitCast(Addr,
5375 llvm::PointerType::getUnqual(ArgPtrTy),
5376 "indirect");
5377 ArgAddr = Builder.CreateLoad(ArgAddr, "indirect.arg");
5378 break;
5379
5380 case ABIArgInfo::Ignore:
5381 return llvm::UndefValue::get(ArgPtrTy);
5382 }
5383
5384 // Update VAList.
5385 Addr = Builder.CreateConstGEP1_32(Addr, Stride, "ap.next");
5386 Builder.CreateStore(Addr, VAListAddrAsBPP);
5387
5388 return Builder.CreatePointerCast(ArgAddr, ArgPtrTy, "arg.addr");
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00005389}
5390
5391void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const {
5392 FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8);
5393 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
5394 it != ie; ++it)
5395 it->info = classifyType(it->type, 16 * 8);
5396}
5397
5398namespace {
5399class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo {
5400public:
5401 SparcV9TargetCodeGenInfo(CodeGenTypes &CGT)
5402 : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {}
5403};
5404} // end anonymous namespace
5405
5406
Chris Lattnerea044322010-07-29 02:01:43 +00005407const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005408 if (TheTargetCodeGenInfo)
5409 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005410
John McCall64aa4b32013-04-16 22:48:15 +00005411 const llvm::Triple &Triple = getTarget().getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00005412 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00005413 default:
Chris Lattnerea044322010-07-29 02:01:43 +00005414 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00005415
Derek Schuff9ed63f82012-09-06 17:37:28 +00005416 case llvm::Triple::le32:
5417 return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
John McCallaeeb7012010-05-27 06:19:26 +00005418 case llvm::Triple::mips:
5419 case llvm::Triple::mipsel:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00005420 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCallaeeb7012010-05-27 06:19:26 +00005421
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00005422 case llvm::Triple::mips64:
5423 case llvm::Triple::mips64el:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00005424 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00005425
Tim Northoverc264e162013-01-31 12:13:10 +00005426 case llvm::Triple::aarch64:
5427 return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types));
5428
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00005429 case llvm::Triple::arm:
5430 case llvm::Triple::thumb:
Sandeep Patel34c1af82011-04-05 00:23:47 +00005431 {
5432 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
John McCall64aa4b32013-04-16 22:48:15 +00005433 if (strcmp(getTarget().getABI(), "apcs-gnu") == 0)
Sandeep Patel34c1af82011-04-05 00:23:47 +00005434 Kind = ARMABIInfo::APCS;
David Tweedb16abb12012-10-25 13:33:01 +00005435 else if (CodeGenOpts.FloatABI == "hard" ||
John McCall64aa4b32013-04-16 22:48:15 +00005436 (CodeGenOpts.FloatABI != "soft" &&
5437 Triple.getEnvironment() == llvm::Triple::GNUEABIHF))
Sandeep Patel34c1af82011-04-05 00:23:47 +00005438 Kind = ARMABIInfo::AAPCS_VFP;
5439
Derek Schuff263366f2012-10-16 22:30:41 +00005440 switch (Triple.getOS()) {
Eli Bendersky441d9f72012-12-04 18:38:10 +00005441 case llvm::Triple::NaCl:
Derek Schuff263366f2012-10-16 22:30:41 +00005442 return *(TheTargetCodeGenInfo =
5443 new NaClARMTargetCodeGenInfo(Types, Kind));
5444 default:
5445 return *(TheTargetCodeGenInfo =
5446 new ARMTargetCodeGenInfo(Types, Kind));
5447 }
Sandeep Patel34c1af82011-04-05 00:23:47 +00005448 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00005449
John McCallec853ba2010-03-11 00:10:12 +00005450 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00005451 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
Roman Divacky0fbc4b92012-05-09 18:22:46 +00005452 case llvm::Triple::ppc64:
Bill Schmidt2fc107f2012-10-03 19:18:57 +00005453 if (Triple.isOSBinFormatELF())
5454 return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types));
5455 else
5456 return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00005457
Peter Collingbourneedb66f32012-05-20 23:28:41 +00005458 case llvm::Triple::nvptx:
5459 case llvm::Triple::nvptx64:
Justin Holewinski2c585b92012-05-24 17:43:12 +00005460 return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types));
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005461
Wesley Peck276fdf42010-12-19 19:57:51 +00005462 case llvm::Triple::mblaze:
5463 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
5464
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005465 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00005466 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00005467
Ulrich Weigandb8409212013-05-06 16:26:41 +00005468 case llvm::Triple::systemz:
5469 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
5470
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00005471 case llvm::Triple::tce:
5472 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
5473
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00005474 case llvm::Triple::x86: {
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00005475 if (Triple.isOSDarwin())
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005476 return *(TheTargetCodeGenInfo =
Chad Rosier1f1df1f2013-03-25 21:00:27 +00005477 new X86_32TargetCodeGenInfo(Types, true, true, false,
Rafael Espindolab48280b2012-07-31 02:44:24 +00005478 CodeGenOpts.NumRegisterParameters));
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00005479
5480 switch (Triple.getOS()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00005481 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00005482 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00005483 case llvm::Triple::AuroraUX:
5484 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00005485 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00005486 case llvm::Triple::OpenBSD:
Eli Friedman42f74f22012-08-08 23:57:20 +00005487 case llvm::Triple::Bitrig:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005488 return *(TheTargetCodeGenInfo =
Chad Rosier1f1df1f2013-03-25 21:00:27 +00005489 new X86_32TargetCodeGenInfo(Types, false, true, false,
Rafael Espindolab48280b2012-07-31 02:44:24 +00005490 CodeGenOpts.NumRegisterParameters));
Eli Friedman55fc7e22012-01-25 22:46:34 +00005491
5492 case llvm::Triple::Win32:
5493 return *(TheTargetCodeGenInfo =
Reid Kleckner3190ca92013-05-08 13:44:39 +00005494 new WinX86_32TargetCodeGenInfo(Types,
5495 CodeGenOpts.NumRegisterParameters));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00005496
5497 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005498 return *(TheTargetCodeGenInfo =
Chad Rosier1f1df1f2013-03-25 21:00:27 +00005499 new X86_32TargetCodeGenInfo(Types, false, false, false,
Rafael Espindolab48280b2012-07-31 02:44:24 +00005500 CodeGenOpts.NumRegisterParameters));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005501 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00005502 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005503
Eli Friedmanee1ad992011-12-02 00:11:43 +00005504 case llvm::Triple::x86_64: {
John McCall64aa4b32013-04-16 22:48:15 +00005505 bool HasAVX = strcmp(getTarget().getABI(), "avx") == 0;
Eli Friedmanee1ad992011-12-02 00:11:43 +00005506
Chris Lattnerf13721d2010-08-31 16:44:54 +00005507 switch (Triple.getOS()) {
5508 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00005509 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00005510 case llvm::Triple::Cygwin:
5511 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
Eli Bendersky441d9f72012-12-04 18:38:10 +00005512 case llvm::Triple::NaCl:
John McCall64aa4b32013-04-16 22:48:15 +00005513 return *(TheTargetCodeGenInfo = new NaClX86_64TargetCodeGenInfo(Types,
5514 HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00005515 default:
Eli Friedmanee1ad992011-12-02 00:11:43 +00005516 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
5517 HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00005518 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00005519 }
Tony Linthicum96319392011-12-12 21:14:55 +00005520 case llvm::Triple::hexagon:
5521 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00005522 case llvm::Triple::sparcv9:
5523 return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types));
Eli Friedmanee1ad992011-12-02 00:11:43 +00005524 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005525}