blob: bef0d058d6f3ff02fd45e5bc779ef3f285d32a72 [file] [log] [blame]
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000016#include "ABIInfo.h"
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +000017#include "CGCXXABI.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000018#include "CodeGenFunction.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000019#include "clang/AST/RecordLayout.h"
Sandeep Patel34c1af82011-04-05 00:23:47 +000020#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbar2c0843f2009-08-24 08:52:16 +000021#include "llvm/ADT/Triple.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000022#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/Type.h"
Daniel Dunbar28df7a52009-12-03 09:13:49 +000024#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000025using namespace clang;
26using namespace CodeGen;
27
John McCallaeeb7012010-05-27 06:19:26 +000028static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
29 llvm::Value *Array,
30 llvm::Value *Value,
31 unsigned FirstIndex,
32 unsigned LastIndex) {
33 // Alternatively, we could emit this as a loop in the source.
34 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
35 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
36 Builder.CreateStore(Value, Cell);
37 }
38}
39
John McCalld608cdb2010-08-22 10:59:02 +000040static bool isAggregateTypeForABI(QualType T) {
John McCall9d232c82013-03-07 21:37:08 +000041 return !CodeGenFunction::hasScalarEvaluationKind(T) ||
John McCalld608cdb2010-08-22 10:59:02 +000042 T->isMemberFunctionPointerType();
43}
44
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000045ABIInfo::~ABIInfo() {}
46
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +000047static bool isRecordReturnIndirect(const RecordType *RT, CodeGen::CodeGenTypes &CGT) {
48 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
49 if (!RD)
50 return false;
51 return CGT.CGM.getCXXABI().isReturnTypeIndirect(RD);
52}
53
54
55static bool isRecordReturnIndirect(QualType T, CodeGen::CodeGenTypes &CGT) {
56 const RecordType *RT = T->getAs<RecordType>();
57 if (!RT)
58 return false;
59 return isRecordReturnIndirect(RT, CGT);
60}
61
62static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT,
63 CodeGen::CodeGenTypes &CGT) {
64 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
65 if (!RD)
66 return CGCXXABI::RAA_Default;
67 return CGT.CGM.getCXXABI().getRecordArgABI(RD);
68}
69
70static CGCXXABI::RecordArgABI getRecordArgABI(QualType T,
71 CodeGen::CodeGenTypes &CGT) {
72 const RecordType *RT = T->getAs<RecordType>();
73 if (!RT)
74 return CGCXXABI::RAA_Default;
75 return getRecordArgABI(RT, CGT);
76}
77
Chris Lattnerea044322010-07-29 02:01:43 +000078ASTContext &ABIInfo::getContext() const {
79 return CGT.getContext();
80}
81
82llvm::LLVMContext &ABIInfo::getVMContext() const {
83 return CGT.getLLVMContext();
84}
85
Micah Villmow25a6a842012-10-08 16:25:52 +000086const llvm::DataLayout &ABIInfo::getDataLayout() const {
87 return CGT.getDataLayout();
Chris Lattnerea044322010-07-29 02:01:43 +000088}
89
John McCall64aa4b32013-04-16 22:48:15 +000090const TargetInfo &ABIInfo::getTarget() const {
91 return CGT.getTarget();
92}
Chris Lattnerea044322010-07-29 02:01:43 +000093
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000094void ABIArgInfo::dump() const {
Chris Lattner5f9e2722011-07-23 10:55:15 +000095 raw_ostream &OS = llvm::errs();
Daniel Dunbar28df7a52009-12-03 09:13:49 +000096 OS << "(ABIArgInfo Kind=";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000097 switch (TheKind) {
98 case Direct:
Chris Lattner800588f2010-07-29 06:26:06 +000099 OS << "Direct Type=";
Chris Lattner2acc6e32011-07-18 04:24:23 +0000100 if (llvm::Type *Ty = getCoerceToType())
Chris Lattner800588f2010-07-29 06:26:06 +0000101 Ty->print(OS);
102 else
103 OS << "null";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000104 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000105 case Extend:
Daniel Dunbar28df7a52009-12-03 09:13:49 +0000106 OS << "Extend";
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000107 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000108 case Ignore:
Daniel Dunbar28df7a52009-12-03 09:13:49 +0000109 OS << "Ignore";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000110 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000111 case Indirect:
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000112 OS << "Indirect Align=" << getIndirectAlign()
Joerg Sonnenbergere9b5d772011-07-15 18:23:44 +0000113 << " ByVal=" << getIndirectByVal()
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000114 << " Realign=" << getIndirectRealign();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000115 break;
116 case Expand:
Daniel Dunbar28df7a52009-12-03 09:13:49 +0000117 OS << "Expand";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000118 break;
119 }
Daniel Dunbar28df7a52009-12-03 09:13:49 +0000120 OS << ")\n";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000121}
122
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000123TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
124
John McCall49e34be2011-08-30 01:42:09 +0000125// If someone can figure out a general rule for this, that would be great.
126// It's probably just doomed to be platform-dependent, though.
127unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
128 // Verified for:
129 // x86-64 FreeBSD, Linux, Darwin
130 // x86-32 FreeBSD, Linux, Darwin
131 // PowerPC Linux, Darwin
132 // ARM Darwin (*not* EABI)
Tim Northoverc264e162013-01-31 12:13:10 +0000133 // AArch64 Linux
John McCall49e34be2011-08-30 01:42:09 +0000134 return 32;
135}
136
John McCallde5d3c72012-02-17 03:33:10 +0000137bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
138 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +0000139 // The following conventions are known to require this to be false:
140 // x86_stdcall
141 // MIPS
142 // For everything else, we just prefer false unless we opt out.
143 return false;
144}
145
Reid Kleckner3190ca92013-05-08 13:44:39 +0000146void
147TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib,
148 llvm::SmallString<24> &Opt) const {
149 // This assumes the user is passing a library name like "rt" instead of a
150 // filename like "librt.a/so", and that they don't care whether it's static or
151 // dynamic.
152 Opt = "-l";
153 Opt += Lib;
154}
155
Daniel Dunbar98303b92009-09-13 08:03:58 +0000156static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000157
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000158/// isEmptyField - Return true iff a the field is "empty", that is it
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000159/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +0000160static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
161 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000162 if (FD->isUnnamedBitfield())
163 return true;
164
165 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000166
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000167 // Constant arrays of empty records count as empty, strip them off.
168 // Constant arrays of zero length always count as empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000169 if (AllowArrays)
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000170 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
171 if (AT->getSize() == 0)
172 return true;
Daniel Dunbar98303b92009-09-13 08:03:58 +0000173 FT = AT->getElementType();
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000174 }
Daniel Dunbar98303b92009-09-13 08:03:58 +0000175
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000176 const RecordType *RT = FT->getAs<RecordType>();
177 if (!RT)
178 return false;
179
180 // C++ record fields are never empty, at least in the Itanium ABI.
181 //
182 // FIXME: We should use a predicate for whether this behavior is true in the
183 // current ABI.
184 if (isa<CXXRecordDecl>(RT->getDecl()))
185 return false;
186
Daniel Dunbar98303b92009-09-13 08:03:58 +0000187 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000188}
189
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000190/// isEmptyRecord - Return true iff a structure contains only empty
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000191/// fields. Note that a structure with a flexible array member is not
192/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000193static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000194 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000195 if (!RT)
196 return 0;
197 const RecordDecl *RD = RT->getDecl();
198 if (RD->hasFlexibleArrayMember())
199 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000200
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000201 // If this is a C++ record, check the bases first.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000202 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000203 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
204 e = CXXRD->bases_end(); i != e; ++i)
205 if (!isEmptyRecord(Context, i->getType(), true))
206 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000207
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000208 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
209 i != e; ++i)
David Blaikie581deb32012-06-06 20:45:41 +0000210 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000211 return false;
212 return true;
213}
214
215/// isSingleElementStruct - Determine if a structure is a "single
216/// element struct", i.e. it has exactly one non-empty field or
217/// exactly one field which is itself a single element
218/// struct. Structures with flexible array members are never
219/// considered single element structs.
220///
221/// \return The field declaration for the single non-empty field, if
222/// it exists.
223static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
224 const RecordType *RT = T->getAsStructureType();
225 if (!RT)
226 return 0;
227
228 const RecordDecl *RD = RT->getDecl();
229 if (RD->hasFlexibleArrayMember())
230 return 0;
231
232 const Type *Found = 0;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000233
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000234 // If this is a C++ record, check the bases first.
235 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
236 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
237 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000238 // Ignore empty records.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000239 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000240 continue;
241
242 // If we already found an element then this isn't a single-element struct.
243 if (Found)
244 return 0;
245
246 // If this is non-empty and not a single element struct, the composite
247 // cannot be a single element struct.
248 Found = isSingleElementStruct(i->getType(), Context);
249 if (!Found)
250 return 0;
251 }
252 }
253
254 // Check for single element.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000255 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
256 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000257 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000258 QualType FT = FD->getType();
259
260 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000261 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000262 continue;
263
264 // If we already found an element then this isn't a single-element
265 // struct.
266 if (Found)
267 return 0;
268
269 // Treat single element arrays as the element.
270 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
271 if (AT->getSize().getZExtValue() != 1)
272 break;
273 FT = AT->getElementType();
274 }
275
John McCalld608cdb2010-08-22 10:59:02 +0000276 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000277 Found = FT.getTypePtr();
278 } else {
279 Found = isSingleElementStruct(FT, Context);
280 if (!Found)
281 return 0;
282 }
283 }
284
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000285 // We don't consider a struct a single-element struct if it has
286 // padding beyond the element type.
287 if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
288 return 0;
289
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000290 return Found;
291}
292
293static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Eli Friedmandb748a32012-11-29 23:21:04 +0000294 // Treat complex types as the element type.
295 if (const ComplexType *CTy = Ty->getAs<ComplexType>())
296 Ty = CTy->getElementType();
297
298 // Check for a type which we know has a simple scalar argument-passing
299 // convention without any padding. (We're specifically looking for 32
300 // and 64-bit integer and integer-equivalents, float, and double.)
Daniel Dunbara1842d32010-05-14 03:40:53 +0000301 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Eli Friedmandb748a32012-11-29 23:21:04 +0000302 !Ty->isEnumeralType() && !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000303 return false;
304
305 uint64_t Size = Context.getTypeSize(Ty);
306 return Size == 32 || Size == 64;
307}
308
Daniel Dunbar53012f42009-11-09 01:33:53 +0000309/// canExpandIndirectArgument - Test whether an argument type which is to be
310/// passed indirectly (on the stack) would have the equivalent layout if it was
311/// expanded into separate arguments. If so, we prefer to do the latter to avoid
312/// inhibiting optimizations.
313///
314// FIXME: This predicate is missing many cases, currently it just follows
315// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
316// should probably make this smarter, or better yet make the LLVM backend
317// capable of handling it.
318static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
319 // We can only expand structure types.
320 const RecordType *RT = Ty->getAs<RecordType>();
321 if (!RT)
322 return false;
323
324 // We can only expand (C) structures.
325 //
326 // FIXME: This needs to be generalized to handle classes as well.
327 const RecordDecl *RD = RT->getDecl();
328 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
329 return false;
330
Eli Friedman506d4e32011-11-18 01:32:26 +0000331 uint64_t Size = 0;
332
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000333 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
334 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000335 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000336
337 if (!is32Or64BitBasicType(FD->getType(), Context))
338 return false;
339
340 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
341 // how to expand them yet, and the predicate for telling if a bitfield still
342 // counts as "basic" is more complicated than what we were doing previously.
343 if (FD->isBitField())
344 return false;
Eli Friedman506d4e32011-11-18 01:32:26 +0000345
346 Size += Context.getTypeSize(FD->getType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000347 }
348
Eli Friedman506d4e32011-11-18 01:32:26 +0000349 // Make sure there are not any holes in the struct.
350 if (Size != Context.getTypeSize(Ty))
351 return false;
352
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000353 return true;
354}
355
356namespace {
357/// DefaultABIInfo - The default implementation for ABI specific
358/// details. This implementation provides information which results in
359/// self-consistent and sensible LLVM IR generation, but does not
360/// conform to any particular ABI.
361class DefaultABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +0000362public:
363 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000364
Chris Lattnera3c109b2010-07-29 02:16:43 +0000365 ABIArgInfo classifyReturnType(QualType RetTy) const;
366 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000367
Chris Lattneree5dcd02010-07-29 02:31:05 +0000368 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000369 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000370 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
371 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000372 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000373 }
374
375 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
376 CodeGenFunction &CGF) const;
377};
378
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000379class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
380public:
Chris Lattnerea044322010-07-29 02:01:43 +0000381 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
382 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000383};
384
385llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
386 CodeGenFunction &CGF) const {
387 return 0;
388}
389
Chris Lattnera3c109b2010-07-29 02:16:43 +0000390ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
Jan Wen Voung90306932011-11-03 00:59:44 +0000391 if (isAggregateTypeForABI(Ty)) {
392 // Records with non trivial destructors/constructors should not be passed
393 // by value.
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000394 if (isRecordReturnIndirect(Ty, CGT))
Jan Wen Voung90306932011-11-03 00:59:44 +0000395 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
396
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000397 return ABIArgInfo::getIndirect(0);
Jan Wen Voung90306932011-11-03 00:59:44 +0000398 }
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000399
Chris Lattnera14db752010-03-11 18:19:55 +0000400 // Treat an enum type as its underlying type.
401 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
402 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000403
Chris Lattnera14db752010-03-11 18:19:55 +0000404 return (Ty->isPromotableIntegerType() ?
405 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000406}
407
Bob Wilson0024f942011-01-10 23:54:17 +0000408ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
409 if (RetTy->isVoidType())
410 return ABIArgInfo::getIgnore();
411
412 if (isAggregateTypeForABI(RetTy))
413 return ABIArgInfo::getIndirect(0);
414
415 // Treat an enum type as its underlying type.
416 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
417 RetTy = EnumTy->getDecl()->getIntegerType();
418
419 return (RetTy->isPromotableIntegerType() ?
420 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
421}
422
Derek Schuff9ed63f82012-09-06 17:37:28 +0000423//===----------------------------------------------------------------------===//
424// le32/PNaCl bitcode ABI Implementation
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000425//
426// This is a simplified version of the x86_32 ABI. Arguments and return values
427// are always passed on the stack.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000428//===----------------------------------------------------------------------===//
429
430class PNaClABIInfo : public ABIInfo {
431 public:
432 PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
433
434 ABIArgInfo classifyReturnType(QualType RetTy) const;
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000435 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Derek Schuff9ed63f82012-09-06 17:37:28 +0000436
437 virtual void computeInfo(CGFunctionInfo &FI) const;
438 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
439 CodeGenFunction &CGF) const;
440};
441
442class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
443 public:
444 PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
445 : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {}
446};
447
448void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
449 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
450
Derek Schuff9ed63f82012-09-06 17:37:28 +0000451 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
452 it != ie; ++it)
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000453 it->info = classifyArgumentType(it->type);
Derek Schuff9ed63f82012-09-06 17:37:28 +0000454 }
455
456llvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
457 CodeGenFunction &CGF) const {
458 return 0;
459}
460
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000461/// \brief Classify argument of given type \p Ty.
462ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const {
Derek Schuff9ed63f82012-09-06 17:37:28 +0000463 if (isAggregateTypeForABI(Ty)) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000464 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
465 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Derek Schuff9ed63f82012-09-06 17:37:28 +0000466 return ABIArgInfo::getIndirect(0);
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000467 } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
468 // Treat an enum type as its underlying type.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000469 Ty = EnumTy->getDecl()->getIntegerType();
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000470 } else if (Ty->isFloatingType()) {
471 // Floating-point types don't go inreg.
472 return ABIArgInfo::getDirect();
Derek Schuff9ed63f82012-09-06 17:37:28 +0000473 }
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000474
475 return (Ty->isPromotableIntegerType() ?
476 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Derek Schuff9ed63f82012-09-06 17:37:28 +0000477}
478
479ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
480 if (RetTy->isVoidType())
481 return ABIArgInfo::getIgnore();
482
Eli Benderskye45dfd12013-04-04 22:49:35 +0000483 // In the PNaCl ABI we always return records/structures on the stack.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000484 if (isAggregateTypeForABI(RetTy))
485 return ABIArgInfo::getIndirect(0);
486
487 // Treat an enum type as its underlying type.
488 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
489 RetTy = EnumTy->getDecl()->getIntegerType();
490
491 return (RetTy->isPromotableIntegerType() ?
492 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
493}
494
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000495/// IsX86_MMXType - Return true if this is an MMX type.
496bool IsX86_MMXType(llvm::Type *IRType) {
497 // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>.
Bill Wendlingbb465d72010-10-18 03:41:31 +0000498 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
499 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
500 IRType->getScalarSizeInBits() != 64;
501}
502
Jay Foadef6de3d2011-07-11 09:56:20 +0000503static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000504 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000505 llvm::Type* Ty) {
Tim Northover1bea6532013-06-07 00:04:50 +0000506 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) {
507 if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) {
508 // Invalid MMX constraint
509 return 0;
510 }
511
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000512 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
Tim Northover1bea6532013-06-07 00:04:50 +0000513 }
514
515 // No operation needed
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000516 return Ty;
517}
518
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000519//===----------------------------------------------------------------------===//
520// X86-32 ABI Implementation
521//===----------------------------------------------------------------------===//
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000522
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000523/// X86_32ABIInfo - The X86-32 ABI information.
524class X86_32ABIInfo : public ABIInfo {
Rafael Espindolab48280b2012-07-31 02:44:24 +0000525 enum Class {
526 Integer,
527 Float
528 };
529
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000530 static const unsigned MinABIStackAlignInBytes = 4;
531
David Chisnall1e4249c2009-08-17 23:08:21 +0000532 bool IsDarwinVectorABI;
533 bool IsSmallStructInRegABI;
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000534 bool IsWin32StructABI;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000535 unsigned DefaultNumRegisterParameters;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000536
537 static bool isRegisterSize(unsigned Size) {
538 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
539 }
540
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000541 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context,
542 unsigned callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000543
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000544 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
545 /// such that the argument will be passed in memory.
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000546 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal,
547 unsigned &FreeRegs) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000548
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000549 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbare59d8582010-09-16 20:42:06 +0000550 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000551
Rafael Espindolab48280b2012-07-31 02:44:24 +0000552 Class classify(QualType Ty) const;
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000553 ABIArgInfo classifyReturnType(QualType RetTy,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000554 unsigned callingConvention) const;
Rafael Espindolab6932692012-10-24 01:58:58 +0000555 ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &FreeRegs,
556 bool IsFastCall) const;
557 bool shouldUseInReg(QualType Ty, unsigned &FreeRegs,
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000558 bool IsFastCall, bool &NeedsPadding) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000559
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000560public:
561
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000562 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000563 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
564 CodeGenFunction &CGF) const;
565
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000566 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool w,
Rafael Espindolab48280b2012-07-31 02:44:24 +0000567 unsigned r)
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000568 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000569 IsWin32StructABI(w), DefaultNumRegisterParameters(r) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000570};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000571
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000572class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
573public:
Eli Friedman55fc7e22012-01-25 22:46:34 +0000574 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000575 bool d, bool p, bool w, unsigned r)
576 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, w, r)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000577
John McCallb8b52972013-06-18 02:46:29 +0000578 static bool isStructReturnInRegABI(
579 const llvm::Triple &Triple, const CodeGenOptions &Opts);
580
Charles Davis74f72932010-02-13 15:54:06 +0000581 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
582 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000583
584 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
585 // Darwin uses different dwarf register numbers for EH.
John McCall64aa4b32013-04-16 22:48:15 +0000586 if (CGM.getTarget().getTriple().isOSDarwin()) return 5;
John McCall6374c332010-03-06 00:35:14 +0000587 return 4;
588 }
589
590 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
591 llvm::Value *Address) const;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000592
Jay Foadef6de3d2011-07-11 09:56:20 +0000593 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000594 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000595 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000596 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
597 }
598
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000599};
600
601}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000602
603/// shouldReturnTypeInRegister - Determine if the given type should be
604/// passed in a register (for the Darwin ABI).
605bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000606 ASTContext &Context,
607 unsigned callingConvention) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000608 uint64_t Size = Context.getTypeSize(Ty);
609
610 // Type must be register sized.
611 if (!isRegisterSize(Size))
612 return false;
613
614 if (Ty->isVectorType()) {
615 // 64- and 128- bit vectors inside structures are not returned in
616 // registers.
617 if (Size == 64 || Size == 128)
618 return false;
619
620 return true;
621 }
622
Daniel Dunbar77115232010-05-15 00:00:30 +0000623 // If this is a builtin, pointer, enum, complex type, member pointer, or
624 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000625 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000626 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000627 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000628 return true;
629
630 // Arrays are treated like records.
631 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000632 return shouldReturnTypeInRegister(AT->getElementType(), Context,
633 callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000634
635 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000636 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000637 if (!RT) return false;
638
Anders Carlssona8874232010-01-27 03:25:19 +0000639 // FIXME: Traverse bases here too.
640
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000641 // For thiscall conventions, structures will never be returned in
642 // a register. This is for compatibility with the MSVC ABI
643 if (callingConvention == llvm::CallingConv::X86_ThisCall &&
644 RT->isStructureType()) {
645 return false;
646 }
647
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000648 // Structure types are passed in register if all fields would be
649 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000650 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
651 e = RT->getDecl()->field_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000652 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000653
654 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000655 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000656 continue;
657
658 // Check fields recursively.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000659 if (!shouldReturnTypeInRegister(FD->getType(), Context,
660 callingConvention))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000661 return false;
662 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000663 return true;
664}
665
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000666ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
667 unsigned callingConvention) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000668 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000669 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000670
Chris Lattnera3c109b2010-07-29 02:16:43 +0000671 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000672 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000673 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000674 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000675
676 // 128-bit vectors are a special case; they are returned in
677 // registers and we need to make sure to pick a type the LLVM
678 // backend will like.
679 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +0000680 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000681 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000682
683 // Always return in register if it fits in a general purpose
684 // register, or if it is 64 bits and has a single element.
685 if ((Size == 8 || Size == 16 || Size == 32) ||
686 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +0000687 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000688 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000689
690 return ABIArgInfo::getIndirect(0);
691 }
692
693 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000694 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000695
John McCalld608cdb2010-08-22 10:59:02 +0000696 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000697 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000698 if (isRecordReturnIndirect(RT, CGT))
Anders Carlsson40092972009-10-20 22:07:59 +0000699 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000700
Anders Carlsson40092972009-10-20 22:07:59 +0000701 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000702 if (RT->getDecl()->hasFlexibleArrayMember())
703 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000704 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000705
David Chisnall1e4249c2009-08-17 23:08:21 +0000706 // If specified, structs and unions are always indirect.
707 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000708 return ABIArgInfo::getIndirect(0);
709
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000710 // Small structures which are register sized are generally returned
711 // in a register.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000712 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(),
713 callingConvention)) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000714 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000715
716 // As a special-case, if the struct is a "single-element" struct, and
717 // the field is of type "float" or "double", return it in a
Eli Friedman55fc7e22012-01-25 22:46:34 +0000718 // floating-point register. (MSVC does not apply this special case.)
719 // We apply a similar transformation for pointer types to improve the
720 // quality of the generated IR.
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000721 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000722 if ((!IsWin32StructABI && SeltTy->isRealFloatingType())
Eli Friedman55fc7e22012-01-25 22:46:34 +0000723 || SeltTy->hasPointerRepresentation())
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000724 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
725
726 // FIXME: We should be able to narrow this integer in cases with dead
727 // padding.
Chris Lattner800588f2010-07-29 06:26:06 +0000728 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000729 }
730
731 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000732 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000733
Chris Lattnera3c109b2010-07-29 02:16:43 +0000734 // Treat an enum type as its underlying type.
735 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
736 RetTy = EnumTy->getDecl()->getIntegerType();
737
738 return (RetTy->isPromotableIntegerType() ?
739 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000740}
741
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000742static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
743 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
744}
745
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000746static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
747 const RecordType *RT = Ty->getAs<RecordType>();
748 if (!RT)
749 return 0;
750 const RecordDecl *RD = RT->getDecl();
751
752 // If this is a C++ record, check the bases first.
753 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
754 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
755 e = CXXRD->bases_end(); i != e; ++i)
756 if (!isRecordWithSSEVectorType(Context, i->getType()))
757 return false;
758
759 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
760 i != e; ++i) {
761 QualType FT = i->getType();
762
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000763 if (isSSEVectorType(Context, FT))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000764 return true;
765
766 if (isRecordWithSSEVectorType(Context, FT))
767 return true;
768 }
769
770 return false;
771}
772
Daniel Dunbare59d8582010-09-16 20:42:06 +0000773unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
774 unsigned Align) const {
775 // Otherwise, if the alignment is less than or equal to the minimum ABI
776 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000777 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +0000778 return 0; // Use default alignment.
779
780 // On non-Darwin, the stack type alignment is always 4.
781 if (!IsDarwinVectorABI) {
782 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000783 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +0000784 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000785
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000786 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000787 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
788 isRecordWithSSEVectorType(getContext(), Ty)))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000789 return 16;
790
791 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000792}
793
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000794ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
795 unsigned &FreeRegs) const {
796 if (!ByVal) {
797 if (FreeRegs) {
798 --FreeRegs; // Non byval indirects just use one pointer.
799 return ABIArgInfo::getIndirectInReg(0, false);
800 }
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000801 return ABIArgInfo::getIndirect(0, false);
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000802 }
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000803
Daniel Dunbare59d8582010-09-16 20:42:06 +0000804 // Compute the byval alignment.
805 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
806 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
807 if (StackAlign == 0)
Chris Lattnerde92d732011-05-22 23:35:00 +0000808 return ABIArgInfo::getIndirect(4);
Daniel Dunbare59d8582010-09-16 20:42:06 +0000809
810 // If the stack alignment is less than the type alignment, realign the
811 // argument.
812 if (StackAlign < TypeAlign)
813 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
814 /*Realign=*/true);
815
816 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000817}
818
Rafael Espindolab48280b2012-07-31 02:44:24 +0000819X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
820 const Type *T = isSingleElementStruct(Ty, getContext());
821 if (!T)
822 T = Ty.getTypePtr();
823
824 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
825 BuiltinType::Kind K = BT->getKind();
826 if (K == BuiltinType::Float || K == BuiltinType::Double)
827 return Float;
828 }
829 return Integer;
830}
831
Rafael Espindolab6932692012-10-24 01:58:58 +0000832bool X86_32ABIInfo::shouldUseInReg(QualType Ty, unsigned &FreeRegs,
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000833 bool IsFastCall, bool &NeedsPadding) const {
834 NeedsPadding = false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000835 Class C = classify(Ty);
836 if (C == Float)
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000837 return false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000838
Rafael Espindolab6932692012-10-24 01:58:58 +0000839 unsigned Size = getContext().getTypeSize(Ty);
840 unsigned SizeInRegs = (Size + 31) / 32;
Rafael Espindola5f14fcb2012-10-23 02:04:01 +0000841
842 if (SizeInRegs == 0)
843 return false;
844
Rafael Espindolab48280b2012-07-31 02:44:24 +0000845 if (SizeInRegs > FreeRegs) {
846 FreeRegs = 0;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000847 return false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000848 }
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000849
Rafael Espindolab48280b2012-07-31 02:44:24 +0000850 FreeRegs -= SizeInRegs;
Rafael Espindolab6932692012-10-24 01:58:58 +0000851
852 if (IsFastCall) {
853 if (Size > 32)
854 return false;
855
856 if (Ty->isIntegralOrEnumerationType())
857 return true;
858
859 if (Ty->isPointerType())
860 return true;
861
862 if (Ty->isReferenceType())
863 return true;
864
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000865 if (FreeRegs)
866 NeedsPadding = true;
867
Rafael Espindolab6932692012-10-24 01:58:58 +0000868 return false;
869 }
870
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000871 return true;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000872}
873
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000874ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Rafael Espindolab6932692012-10-24 01:58:58 +0000875 unsigned &FreeRegs,
876 bool IsFastCall) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000877 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000878 if (isAggregateTypeForABI(Ty)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000879 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000880 if (IsWin32StructABI)
881 return getIndirectResult(Ty, true, FreeRegs);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000882
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000883 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, CGT))
884 return getIndirectResult(Ty, RAA == CGCXXABI::RAA_DirectInMemory, FreeRegs);
885
886 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000887 if (RT->getDecl()->hasFlexibleArrayMember())
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000888 return getIndirectResult(Ty, true, FreeRegs);
Anders Carlssona8874232010-01-27 03:25:19 +0000889 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000890
Eli Friedman5a4d3522011-11-18 00:28:11 +0000891 // Ignore empty structs/unions.
Eli Friedman5a1ac892011-11-18 04:01:36 +0000892 if (isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000893 return ABIArgInfo::getIgnore();
894
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000895 llvm::LLVMContext &LLVMContext = getVMContext();
896 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
897 bool NeedsPadding;
898 if (shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding)) {
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000899 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000900 SmallVector<llvm::Type*, 3> Elements;
901 for (unsigned I = 0; I < SizeInRegs; ++I)
902 Elements.push_back(Int32);
903 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
904 return ABIArgInfo::getDirectInReg(Result);
905 }
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000906 llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : 0;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000907
Daniel Dunbar53012f42009-11-09 01:33:53 +0000908 // Expand small (<= 128-bit) record types when we know that the stack layout
909 // of those arguments will match the struct. This is important because the
910 // LLVM backend isn't smart enough to remove byval, which inhibits many
911 // optimizations.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000912 if (getContext().getTypeSize(Ty) <= 4*32 &&
913 canExpandIndirectArgument(Ty, getContext()))
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000914 return ABIArgInfo::getExpandWithPadding(IsFastCall, PaddingType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000915
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000916 return getIndirectResult(Ty, true, FreeRegs);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000917 }
918
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000919 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +0000920 // On Darwin, some vectors are passed in memory, we handle this by passing
921 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000922 if (IsDarwinVectorABI) {
923 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000924 if ((Size == 8 || Size == 16 || Size == 32) ||
925 (Size == 64 && VT->getNumElements() == 1))
926 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
927 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000928 }
Bill Wendlingbb465d72010-10-18 03:41:31 +0000929
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000930 if (IsX86_MMXType(CGT.ConvertType(Ty)))
931 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64));
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000932
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000933 return ABIArgInfo::getDirect();
934 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000935
936
Chris Lattnera3c109b2010-07-29 02:16:43 +0000937 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
938 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000939
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000940 bool NeedsPadding;
941 bool InReg = shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding);
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000942
943 if (Ty->isPromotableIntegerType()) {
944 if (InReg)
945 return ABIArgInfo::getExtendInReg();
946 return ABIArgInfo::getExtend();
947 }
948 if (InReg)
949 return ABIArgInfo::getDirectInReg();
950 return ABIArgInfo::getDirect();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000951}
952
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000953void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
954 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
955 FI.getCallingConvention());
Rafael Espindolab48280b2012-07-31 02:44:24 +0000956
Rafael Espindolab6932692012-10-24 01:58:58 +0000957 unsigned CC = FI.getCallingConvention();
958 bool IsFastCall = CC == llvm::CallingConv::X86_FastCall;
959 unsigned FreeRegs;
960 if (IsFastCall)
961 FreeRegs = 2;
962 else if (FI.getHasRegParm())
963 FreeRegs = FI.getRegParm();
964 else
965 FreeRegs = DefaultNumRegisterParameters;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000966
967 // If the return value is indirect, then the hidden argument is consuming one
968 // integer register.
969 if (FI.getReturnInfo().isIndirect() && FreeRegs) {
970 --FreeRegs;
971 ABIArgInfo &Old = FI.getReturnInfo();
972 Old = ABIArgInfo::getIndirectInReg(Old.getIndirectAlign(),
973 Old.getIndirectByVal(),
974 Old.getIndirectRealign());
975 }
976
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000977 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
978 it != ie; ++it)
Rafael Espindolab6932692012-10-24 01:58:58 +0000979 it->info = classifyArgumentType(it->type, FreeRegs, IsFastCall);
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000980}
981
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000982llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
983 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +0000984 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000985
986 CGBuilderTy &Builder = CGF.Builder;
987 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
988 "ap");
989 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Eli Friedman7b1fb812011-11-18 02:12:09 +0000990
991 // Compute if the address needs to be aligned
992 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
993 Align = getTypeStackAlignInBytes(Ty, Align);
994 Align = std::max(Align, 4U);
995 if (Align > 4) {
996 // addr = (addr + align - 1) & -align;
997 llvm::Value *Offset =
998 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
999 Addr = CGF.Builder.CreateGEP(Addr, Offset);
1000 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
1001 CGF.Int32Ty);
1002 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
1003 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1004 Addr->getType(),
1005 "ap.cur.aligned");
1006 }
1007
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001008 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00001009 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001010 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1011
1012 uint64_t Offset =
Eli Friedman7b1fb812011-11-18 02:12:09 +00001013 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001014 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00001015 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001016 "ap.next");
1017 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1018
1019 return AddrTyped;
1020}
1021
Charles Davis74f72932010-02-13 15:54:06 +00001022void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
1023 llvm::GlobalValue *GV,
1024 CodeGen::CodeGenModule &CGM) const {
1025 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1026 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
1027 // Get the LLVM function.
1028 llvm::Function *Fn = cast<llvm::Function>(GV);
1029
1030 // Now add the 'alignstack' attribute with a value of 16.
Bill Wendling0d583392012-10-15 20:36:26 +00001031 llvm::AttrBuilder B;
Bill Wendlinge91e9ec2012-10-14 03:28:14 +00001032 B.addStackAlignmentAttr(16);
Bill Wendling909b6de2013-01-23 00:21:06 +00001033 Fn->addAttributes(llvm::AttributeSet::FunctionIndex,
1034 llvm::AttributeSet::get(CGM.getLLVMContext(),
1035 llvm::AttributeSet::FunctionIndex,
1036 B));
Charles Davis74f72932010-02-13 15:54:06 +00001037 }
1038 }
1039}
1040
John McCall6374c332010-03-06 00:35:14 +00001041bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
1042 CodeGen::CodeGenFunction &CGF,
1043 llvm::Value *Address) const {
1044 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCall6374c332010-03-06 00:35:14 +00001045
Chris Lattner8b418682012-02-07 00:39:47 +00001046 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001047
John McCall6374c332010-03-06 00:35:14 +00001048 // 0-7 are the eight integer registers; the order is different
1049 // on Darwin (for EH), but the range is the same.
1050 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +00001051 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +00001052
John McCall64aa4b32013-04-16 22:48:15 +00001053 if (CGF.CGM.getTarget().getTriple().isOSDarwin()) {
John McCall6374c332010-03-06 00:35:14 +00001054 // 12-16 are st(0..4). Not sure why we stop at 4.
1055 // These have size 16, which is sizeof(long double) on
1056 // platforms with 8-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001057 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCallaeeb7012010-05-27 06:19:26 +00001058 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001059
John McCall6374c332010-03-06 00:35:14 +00001060 } else {
1061 // 9 is %eflags, which doesn't get a size on Darwin for some
1062 // reason.
1063 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
1064
1065 // 11-16 are st(0..5). Not sure why we stop at 5.
1066 // These have size 12, which is sizeof(long double) on
1067 // platforms with 4-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001068 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCallaeeb7012010-05-27 06:19:26 +00001069 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1070 }
John McCall6374c332010-03-06 00:35:14 +00001071
1072 return false;
1073}
1074
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001075//===----------------------------------------------------------------------===//
1076// X86-64 ABI Implementation
1077//===----------------------------------------------------------------------===//
1078
1079
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001080namespace {
1081/// X86_64ABIInfo - The X86_64 ABI information.
1082class X86_64ABIInfo : public ABIInfo {
1083 enum Class {
1084 Integer = 0,
1085 SSE,
1086 SSEUp,
1087 X87,
1088 X87Up,
1089 ComplexX87,
1090 NoClass,
1091 Memory
1092 };
1093
1094 /// merge - Implement the X86_64 ABI merging algorithm.
1095 ///
1096 /// Merge an accumulating classification \arg Accum with a field
1097 /// classification \arg Field.
1098 ///
1099 /// \param Accum - The accumulating classification. This should
1100 /// always be either NoClass or the result of a previous merge
1101 /// call. In addition, this should never be Memory (the caller
1102 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001103 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001104
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001105 /// postMerge - Implement the X86_64 ABI post merging algorithm.
1106 ///
1107 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
1108 /// final MEMORY or SSE classes when necessary.
1109 ///
1110 /// \param AggregateSize - The size of the current aggregate in
1111 /// the classification process.
1112 ///
1113 /// \param Lo - The classification for the parts of the type
1114 /// residing in the low word of the containing object.
1115 ///
1116 /// \param Hi - The classification for the parts of the type
1117 /// residing in the higher words of the containing object.
1118 ///
1119 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
1120
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001121 /// classify - Determine the x86_64 register classes in which the
1122 /// given type T should be passed.
1123 ///
1124 /// \param Lo - The classification for the parts of the type
1125 /// residing in the low word of the containing object.
1126 ///
1127 /// \param Hi - The classification for the parts of the type
1128 /// residing in the high word of the containing object.
1129 ///
1130 /// \param OffsetBase - The bit offset of this type in the
1131 /// containing object. Some parameters are classified different
1132 /// depending on whether they straddle an eightbyte boundary.
1133 ///
Eli Friedman7a1b5862013-06-12 00:13:45 +00001134 /// \param isNamedArg - Whether the argument in question is a "named"
1135 /// argument, as used in AMD64-ABI 3.5.7.
1136 ///
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001137 /// If a word is unused its result will be NoClass; if a type should
1138 /// be passed in Memory then at least the classification of \arg Lo
1139 /// will be Memory.
1140 ///
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001141 /// The \arg Lo class will be NoClass iff the argument is ignored.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001142 ///
1143 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
1144 /// also be ComplexX87.
Eli Friedman7a1b5862013-06-12 00:13:45 +00001145 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi,
1146 bool isNamedArg) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001147
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001148 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001149 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
1150 unsigned IROffset, QualType SourceTy,
1151 unsigned SourceOffset) const;
1152 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
1153 unsigned IROffset, QualType SourceTy,
1154 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001155
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001156 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001157 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +00001158 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001159
1160 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001161 /// such that the argument will be passed in memory.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001162 ///
1163 /// \param freeIntRegs - The number of free integer registers remaining
1164 /// available.
1165 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001166
Chris Lattnera3c109b2010-07-29 02:16:43 +00001167 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001168
Bill Wendlingbb465d72010-10-18 03:41:31 +00001169 ABIArgInfo classifyArgumentType(QualType Ty,
Daniel Dunbaredfac032012-03-10 01:03:58 +00001170 unsigned freeIntRegs,
Bill Wendlingbb465d72010-10-18 03:41:31 +00001171 unsigned &neededInt,
Eli Friedman7a1b5862013-06-12 00:13:45 +00001172 unsigned &neededSSE,
1173 bool isNamedArg) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001174
Eli Friedmanee1ad992011-12-02 00:11:43 +00001175 bool IsIllegalVectorType(QualType Ty) const;
1176
John McCall67a57732011-04-21 01:20:55 +00001177 /// The 0.98 ABI revision clarified a lot of ambiguities,
1178 /// unfortunately in ways that were not always consistent with
1179 /// certain previous compilers. In particular, platforms which
1180 /// required strict binary compatibility with older versions of GCC
1181 /// may need to exempt themselves.
1182 bool honorsRevision0_98() const {
John McCall64aa4b32013-04-16 22:48:15 +00001183 return !getTarget().getTriple().isOSDarwin();
John McCall67a57732011-04-21 01:20:55 +00001184 }
1185
Eli Friedmanee1ad992011-12-02 00:11:43 +00001186 bool HasAVX;
Derek Schuffbabaf312012-10-11 15:52:22 +00001187 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
1188 // 64-bit hardware.
1189 bool Has64BitPointers;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001190
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001191public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001192 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
Derek Schuffbabaf312012-10-11 15:52:22 +00001193 ABIInfo(CGT), HasAVX(hasavx),
Derek Schuff90da80c2012-10-11 18:21:13 +00001194 Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001195 }
Chris Lattner9c254f02010-06-29 06:01:59 +00001196
John McCallde5d3c72012-02-17 03:33:10 +00001197 bool isPassedUsingAVXType(QualType type) const {
1198 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00001199 // The freeIntRegs argument doesn't matter here.
Eli Friedman7a1b5862013-06-12 00:13:45 +00001200 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE,
1201 /*isNamedArg*/true);
John McCallde5d3c72012-02-17 03:33:10 +00001202 if (info.isDirect()) {
1203 llvm::Type *ty = info.getCoerceToType();
1204 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
1205 return (vectorTy->getBitWidth() > 128);
1206 }
1207 return false;
1208 }
1209
Chris Lattneree5dcd02010-07-29 02:31:05 +00001210 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001211
1212 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1213 CodeGenFunction &CGF) const;
1214};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001215
Chris Lattnerf13721d2010-08-31 16:44:54 +00001216/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001217class WinX86_64ABIInfo : public ABIInfo {
1218
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00001219 ABIArgInfo classify(QualType Ty, bool IsReturnType) const;
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001220
Chris Lattnerf13721d2010-08-31 16:44:54 +00001221public:
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001222 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
1223
1224 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattnerf13721d2010-08-31 16:44:54 +00001225
1226 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1227 CodeGenFunction &CGF) const;
1228};
1229
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001230class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1231public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001232 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
Derek Schuffbabaf312012-10-11 15:52:22 +00001233 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
John McCall6374c332010-03-06 00:35:14 +00001234
John McCallde5d3c72012-02-17 03:33:10 +00001235 const X86_64ABIInfo &getABIInfo() const {
1236 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
1237 }
1238
John McCall6374c332010-03-06 00:35:14 +00001239 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1240 return 7;
1241 }
1242
1243 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1244 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001245 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001246
John McCallaeeb7012010-05-27 06:19:26 +00001247 // 0-15 are the 16 integer registers.
1248 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001249 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +00001250 return false;
1251 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001252
Jay Foadef6de3d2011-07-11 09:56:20 +00001253 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001254 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +00001255 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001256 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1257 }
1258
John McCallde5d3c72012-02-17 03:33:10 +00001259 bool isNoProtoCallVariadic(const CallArgList &args,
1260 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +00001261 // The default CC on x86-64 sets %al to the number of SSA
1262 // registers used, and GCC sets this when calling an unprototyped
Eli Friedman3ed79032011-12-01 04:53:19 +00001263 // function, so we override the default behavior. However, don't do
Eli Friedman68805fe2011-12-06 03:08:26 +00001264 // that when AVX types are involved: the ABI explicitly states it is
1265 // undefined, and it doesn't work in practice because of how the ABI
1266 // defines varargs anyway.
John McCallde5d3c72012-02-17 03:33:10 +00001267 if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) {
Eli Friedman3ed79032011-12-01 04:53:19 +00001268 bool HasAVXType = false;
John McCallde5d3c72012-02-17 03:33:10 +00001269 for (CallArgList::const_iterator
1270 it = args.begin(), ie = args.end(); it != ie; ++it) {
1271 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1272 HasAVXType = true;
1273 break;
Eli Friedman3ed79032011-12-01 04:53:19 +00001274 }
1275 }
John McCallde5d3c72012-02-17 03:33:10 +00001276
Eli Friedman3ed79032011-12-01 04:53:19 +00001277 if (!HasAVXType)
1278 return true;
1279 }
John McCall01f151e2011-09-21 08:08:30 +00001280
John McCallde5d3c72012-02-17 03:33:10 +00001281 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
John McCall01f151e2011-09-21 08:08:30 +00001282 }
1283
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001284};
1285
Aaron Ballman89735b92013-05-24 15:06:56 +00001286static std::string qualifyWindowsLibrary(llvm::StringRef Lib) {
1287 // If the argument does not end in .lib, automatically add the suffix. This
1288 // matches the behavior of MSVC.
1289 std::string ArgStr = Lib;
1290 if (Lib.size() <= 4 ||
1291 Lib.substr(Lib.size() - 4).compare_lower(".lib") != 0) {
1292 ArgStr += ".lib";
1293 }
1294 return ArgStr;
1295}
1296
Reid Kleckner3190ca92013-05-08 13:44:39 +00001297class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo {
1298public:
John McCallb8b52972013-06-18 02:46:29 +00001299 WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
1300 bool d, bool p, bool w, unsigned RegParms)
1301 : X86_32TargetCodeGenInfo(CGT, d, p, w, RegParms) {}
Reid Kleckner3190ca92013-05-08 13:44:39 +00001302
1303 void getDependentLibraryOption(llvm::StringRef Lib,
1304 llvm::SmallString<24> &Opt) const {
1305 Opt = "/DEFAULTLIB:";
Aaron Ballman89735b92013-05-24 15:06:56 +00001306 Opt += qualifyWindowsLibrary(Lib);
Reid Kleckner3190ca92013-05-08 13:44:39 +00001307 }
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00001308
1309 void getDetectMismatchOption(llvm::StringRef Name,
1310 llvm::StringRef Value,
1311 llvm::SmallString<32> &Opt) const {
Eli Friedman572ac322013-06-07 22:42:22 +00001312 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00001313 }
Reid Kleckner3190ca92013-05-08 13:44:39 +00001314};
1315
Chris Lattnerf13721d2010-08-31 16:44:54 +00001316class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1317public:
1318 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1319 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1320
1321 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1322 return 7;
1323 }
1324
1325 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1326 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001327 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001328
Chris Lattnerf13721d2010-08-31 16:44:54 +00001329 // 0-15 are the 16 integer registers.
1330 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001331 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattnerf13721d2010-08-31 16:44:54 +00001332 return false;
1333 }
Reid Kleckner3190ca92013-05-08 13:44:39 +00001334
1335 void getDependentLibraryOption(llvm::StringRef Lib,
1336 llvm::SmallString<24> &Opt) const {
1337 Opt = "/DEFAULTLIB:";
Aaron Ballman89735b92013-05-24 15:06:56 +00001338 Opt += qualifyWindowsLibrary(Lib);
Reid Kleckner3190ca92013-05-08 13:44:39 +00001339 }
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00001340
1341 void getDetectMismatchOption(llvm::StringRef Name,
1342 llvm::StringRef Value,
1343 llvm::SmallString<32> &Opt) const {
Eli Friedman572ac322013-06-07 22:42:22 +00001344 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00001345 }
Chris Lattnerf13721d2010-08-31 16:44:54 +00001346};
1347
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001348}
1349
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001350void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1351 Class &Hi) const {
1352 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1353 //
1354 // (a) If one of the classes is Memory, the whole argument is passed in
1355 // memory.
1356 //
1357 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1358 // memory.
1359 //
1360 // (c) If the size of the aggregate exceeds two eightbytes and the first
1361 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1362 // argument is passed in memory. NOTE: This is necessary to keep the
1363 // ABI working for processors that don't support the __m256 type.
1364 //
1365 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1366 //
1367 // Some of these are enforced by the merging logic. Others can arise
1368 // only with unions; for example:
1369 // union { _Complex double; unsigned; }
1370 //
1371 // Note that clauses (b) and (c) were added in 0.98.
1372 //
1373 if (Hi == Memory)
1374 Lo = Memory;
1375 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1376 Lo = Memory;
1377 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1378 Lo = Memory;
1379 if (Hi == SSEUp && Lo != SSE)
1380 Hi = SSE;
1381}
1382
Chris Lattner1090a9b2010-06-28 21:43:59 +00001383X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001384 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1385 // classified recursively so that always two fields are
1386 // considered. The resulting class is calculated according to
1387 // the classes of the fields in the eightbyte:
1388 //
1389 // (a) If both classes are equal, this is the resulting class.
1390 //
1391 // (b) If one of the classes is NO_CLASS, the resulting class is
1392 // the other class.
1393 //
1394 // (c) If one of the classes is MEMORY, the result is the MEMORY
1395 // class.
1396 //
1397 // (d) If one of the classes is INTEGER, the result is the
1398 // INTEGER.
1399 //
1400 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1401 // MEMORY is used as class.
1402 //
1403 // (f) Otherwise class SSE is used.
1404
1405 // Accum should never be memory (we should have returned) or
1406 // ComplexX87 (because this cannot be passed in a structure).
1407 assert((Accum != Memory && Accum != ComplexX87) &&
1408 "Invalid accumulated classification during merge.");
1409 if (Accum == Field || Field == NoClass)
1410 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001411 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001412 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001413 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001414 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001415 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001416 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001417 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1418 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001419 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001420 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001421}
1422
Chris Lattnerbcaedae2010-06-30 19:14:05 +00001423void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Eli Friedman7a1b5862013-06-12 00:13:45 +00001424 Class &Lo, Class &Hi, bool isNamedArg) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001425 // FIXME: This code can be simplified by introducing a simple value class for
1426 // Class pairs with appropriate constructor methods for the various
1427 // situations.
1428
1429 // FIXME: Some of the split computations are wrong; unaligned vectors
1430 // shouldn't be passed in registers for example, so there is no chance they
1431 // can straddle an eightbyte. Verify & simplify.
1432
1433 Lo = Hi = NoClass;
1434
1435 Class &Current = OffsetBase < 64 ? Lo : Hi;
1436 Current = Memory;
1437
John McCall183700f2009-09-21 23:43:11 +00001438 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001439 BuiltinType::Kind k = BT->getKind();
1440
1441 if (k == BuiltinType::Void) {
1442 Current = NoClass;
1443 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1444 Lo = Integer;
1445 Hi = Integer;
1446 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1447 Current = Integer;
Derek Schuff7da46f92012-10-11 16:55:58 +00001448 } else if ((k == BuiltinType::Float || k == BuiltinType::Double) ||
1449 (k == BuiltinType::LongDouble &&
John McCall64aa4b32013-04-16 22:48:15 +00001450 getTarget().getTriple().getOS() == llvm::Triple::NaCl)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001451 Current = SSE;
1452 } else if (k == BuiltinType::LongDouble) {
1453 Lo = X87;
1454 Hi = X87Up;
1455 }
1456 // FIXME: _Decimal32 and _Decimal64 are SSE.
1457 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001458 return;
1459 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001460
Chris Lattner1090a9b2010-06-28 21:43:59 +00001461 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001462 // Classify the underlying integer type.
Eli Friedman7a1b5862013-06-12 00:13:45 +00001463 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001464 return;
1465 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001466
Chris Lattner1090a9b2010-06-28 21:43:59 +00001467 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001468 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001469 return;
1470 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001471
Chris Lattner1090a9b2010-06-28 21:43:59 +00001472 if (Ty->isMemberPointerType()) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001473 if (Ty->isMemberFunctionPointerType() && Has64BitPointers)
Daniel Dunbar67d438d2010-05-15 00:00:37 +00001474 Lo = Hi = Integer;
1475 else
1476 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001477 return;
1478 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001479
Chris Lattner1090a9b2010-06-28 21:43:59 +00001480 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001481 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001482 if (Size == 32) {
1483 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1484 // float> as integer.
1485 Current = Integer;
1486
1487 // If this type crosses an eightbyte boundary, it should be
1488 // split.
1489 uint64_t EB_Real = (OffsetBase) / 64;
1490 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1491 if (EB_Real != EB_Imag)
1492 Hi = Lo;
1493 } else if (Size == 64) {
1494 // gcc passes <1 x double> in memory. :(
1495 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1496 return;
1497
1498 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001499 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001500 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1501 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1502 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001503 Current = Integer;
1504 else
1505 Current = SSE;
1506
1507 // If this type crosses an eightbyte boundary, it should be
1508 // split.
1509 if (OffsetBase && OffsetBase != 64)
1510 Hi = Lo;
Eli Friedman7a1b5862013-06-12 00:13:45 +00001511 } else if (Size == 128 || (HasAVX && isNamedArg && Size == 256)) {
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001512 // Arguments of 256-bits are split into four eightbyte chunks. The
1513 // least significant one belongs to class SSE and all the others to class
1514 // SSEUP. The original Lo and Hi design considers that types can't be
1515 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1516 // This design isn't correct for 256-bits, but since there're no cases
1517 // where the upper parts would need to be inspected, avoid adding
1518 // complexity and just consider Hi to match the 64-256 part.
Eli Friedman7a1b5862013-06-12 00:13:45 +00001519 //
1520 // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in
1521 // registers if they are "named", i.e. not part of the "..." of a
1522 // variadic function.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001523 Lo = SSE;
1524 Hi = SSEUp;
1525 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001526 return;
1527 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001528
Chris Lattner1090a9b2010-06-28 21:43:59 +00001529 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001530 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001531
Chris Lattnerea044322010-07-29 02:01:43 +00001532 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001533 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001534 if (Size <= 64)
1535 Current = Integer;
1536 else if (Size <= 128)
1537 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001538 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001539 Current = SSE;
Derek Schuff7da46f92012-10-11 16:55:58 +00001540 else if (ET == getContext().DoubleTy ||
1541 (ET == getContext().LongDoubleTy &&
John McCall64aa4b32013-04-16 22:48:15 +00001542 getTarget().getTriple().getOS() == llvm::Triple::NaCl))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001543 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001544 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001545 Current = ComplexX87;
1546
1547 // If this complex type crosses an eightbyte boundary then it
1548 // should be split.
1549 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001550 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001551 if (Hi == NoClass && EB_Real != EB_Imag)
1552 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001553
Chris Lattner1090a9b2010-06-28 21:43:59 +00001554 return;
1555 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001556
Chris Lattnerea044322010-07-29 02:01:43 +00001557 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001558 // Arrays are treated like structures.
1559
Chris Lattnerea044322010-07-29 02:01:43 +00001560 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001561
1562 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001563 // than four eightbytes, ..., it has class MEMORY.
1564 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001565 return;
1566
1567 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1568 // fields, it has class MEMORY.
1569 //
1570 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001571 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001572 return;
1573
1574 // Otherwise implement simplified merge. We could be smarter about
1575 // this, but it isn't worth it and would be harder to verify.
1576 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001577 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001578 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes089d8922011-07-12 01:27:38 +00001579
1580 // The only case a 256-bit wide vector could be used is when the array
1581 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1582 // to work for sizes wider than 128, early check and fallback to memory.
1583 if (Size > 128 && EltSize != 256)
1584 return;
1585
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001586 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1587 Class FieldLo, FieldHi;
Eli Friedman7a1b5862013-06-12 00:13:45 +00001588 classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001589 Lo = merge(Lo, FieldLo);
1590 Hi = merge(Hi, FieldHi);
1591 if (Lo == Memory || Hi == Memory)
1592 break;
1593 }
1594
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001595 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001596 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001597 return;
1598 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001599
Chris Lattner1090a9b2010-06-28 21:43:59 +00001600 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001601 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001602
1603 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001604 // than four eightbytes, ..., it has class MEMORY.
1605 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001606 return;
1607
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001608 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1609 // copy constructor or a non-trivial destructor, it is passed by invisible
1610 // reference.
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00001611 if (getRecordArgABI(RT, CGT))
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001612 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001613
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001614 const RecordDecl *RD = RT->getDecl();
1615
1616 // Assume variable sized types are passed in memory.
1617 if (RD->hasFlexibleArrayMember())
1618 return;
1619
Chris Lattnerea044322010-07-29 02:01:43 +00001620 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001621
1622 // Reset Lo class, this will be recomputed.
1623 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001624
1625 // If this is a C++ record, classify the bases first.
1626 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1627 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1628 e = CXXRD->bases_end(); i != e; ++i) {
1629 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1630 "Unexpected base class!");
1631 const CXXRecordDecl *Base =
1632 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1633
1634 // Classify this field.
1635 //
1636 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1637 // single eightbyte, each is classified separately. Each eightbyte gets
1638 // initialized to class NO_CLASS.
1639 Class FieldLo, FieldHi;
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001640 uint64_t Offset =
1641 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
Eli Friedman7a1b5862013-06-12 00:13:45 +00001642 classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001643 Lo = merge(Lo, FieldLo);
1644 Hi = merge(Hi, FieldHi);
1645 if (Lo == Memory || Hi == Memory)
1646 break;
1647 }
1648 }
1649
1650 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001651 unsigned idx = 0;
Bruno Cardoso Lopes548e4782011-07-12 22:30:58 +00001652 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001653 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001654 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1655 bool BitField = i->isBitField();
1656
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001657 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1658 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001659 //
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001660 // The only case a 256-bit wide vector could be used is when the struct
1661 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1662 // to work for sizes wider than 128, early check and fallback to memory.
1663 //
1664 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1665 Lo = Memory;
1666 return;
1667 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001668 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001669 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001670 Lo = Memory;
1671 return;
1672 }
1673
1674 // Classify this field.
1675 //
1676 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1677 // exceeds a single eightbyte, each is classified
1678 // separately. Each eightbyte gets initialized to class
1679 // NO_CLASS.
1680 Class FieldLo, FieldHi;
1681
1682 // Bit-fields require special handling, they do not force the
1683 // structure to be passed in memory even if unaligned, and
1684 // therefore they can straddle an eightbyte.
1685 if (BitField) {
1686 // Ignore padding bit-fields.
1687 if (i->isUnnamedBitfield())
1688 continue;
1689
1690 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001691 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001692
1693 uint64_t EB_Lo = Offset / 64;
1694 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1695 FieldLo = FieldHi = NoClass;
1696 if (EB_Lo) {
1697 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1698 FieldLo = NoClass;
1699 FieldHi = Integer;
1700 } else {
1701 FieldLo = Integer;
1702 FieldHi = EB_Hi ? Integer : NoClass;
1703 }
1704 } else
Eli Friedman7a1b5862013-06-12 00:13:45 +00001705 classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001706 Lo = merge(Lo, FieldLo);
1707 Hi = merge(Hi, FieldHi);
1708 if (Lo == Memory || Hi == Memory)
1709 break;
1710 }
1711
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001712 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001713 }
1714}
1715
Chris Lattner9c254f02010-06-29 06:01:59 +00001716ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001717 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1718 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001719 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001720 // Treat an enum type as its underlying type.
1721 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1722 Ty = EnumTy->getDecl()->getIntegerType();
1723
1724 return (Ty->isPromotableIntegerType() ?
1725 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1726 }
1727
1728 return ABIArgInfo::getIndirect(0);
1729}
1730
Eli Friedmanee1ad992011-12-02 00:11:43 +00001731bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1732 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1733 uint64_t Size = getContext().getTypeSize(VecTy);
1734 unsigned LargestVector = HasAVX ? 256 : 128;
1735 if (Size <= 64 || Size > LargestVector)
1736 return true;
1737 }
1738
1739 return false;
1740}
1741
Daniel Dunbaredfac032012-03-10 01:03:58 +00001742ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1743 unsigned freeIntRegs) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001744 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1745 // place naturally.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001746 //
1747 // This assumption is optimistic, as there could be free registers available
1748 // when we need to pass this argument in memory, and LLVM could try to pass
1749 // the argument in the free register. This does not seem to happen currently,
1750 // but this code would be much safer if we could mark the argument with
1751 // 'onstack'. See PR12193.
Eli Friedmanee1ad992011-12-02 00:11:43 +00001752 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001753 // Treat an enum type as its underlying type.
1754 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1755 Ty = EnumTy->getDecl()->getIntegerType();
1756
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001757 return (Ty->isPromotableIntegerType() ?
1758 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001759 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001760
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00001761 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
1762 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001763
Chris Lattner855d2272011-05-22 23:21:23 +00001764 // Compute the byval alignment. We specify the alignment of the byval in all
1765 // cases so that the mid-level optimizer knows the alignment of the byval.
1766 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
Daniel Dunbaredfac032012-03-10 01:03:58 +00001767
1768 // Attempt to avoid passing indirect results using byval when possible. This
1769 // is important for good codegen.
1770 //
1771 // We do this by coercing the value into a scalar type which the backend can
1772 // handle naturally (i.e., without using byval).
1773 //
1774 // For simplicity, we currently only do this when we have exhausted all of the
1775 // free integer registers. Doing this when there are free integer registers
1776 // would require more care, as we would have to ensure that the coerced value
1777 // did not claim the unused register. That would require either reording the
1778 // arguments to the function (so that any subsequent inreg values came first),
1779 // or only doing this optimization when there were no following arguments that
1780 // might be inreg.
1781 //
1782 // We currently expect it to be rare (particularly in well written code) for
1783 // arguments to be passed on the stack when there are still free integer
1784 // registers available (this would typically imply large structs being passed
1785 // by value), so this seems like a fair tradeoff for now.
1786 //
1787 // We can revisit this if the backend grows support for 'onstack' parameter
1788 // attributes. See PR12193.
1789 if (freeIntRegs == 0) {
1790 uint64_t Size = getContext().getTypeSize(Ty);
1791
1792 // If this type fits in an eightbyte, coerce it into the matching integral
1793 // type, which will end up on the stack (with alignment 8).
1794 if (Align == 8 && Size <= 64)
1795 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1796 Size));
1797 }
1798
Chris Lattner855d2272011-05-22 23:21:23 +00001799 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001800}
1801
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001802/// GetByteVectorType - The ABI specifies that a value should be passed in an
1803/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner0f408f52010-07-29 04:56:46 +00001804/// vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001805llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001806 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001807
Chris Lattner15842bd2010-07-29 05:02:29 +00001808 // Wrapper structs that just contain vectors are passed just like vectors,
1809 // strip them off if present.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001810 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner15842bd2010-07-29 05:02:29 +00001811 while (STy && STy->getNumElements() == 1) {
1812 IRType = STy->getElementType(0);
1813 STy = dyn_cast<llvm::StructType>(IRType);
1814 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001815
Bruno Cardoso Lopes528a8c72011-07-08 22:57:35 +00001816 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001817 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1818 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001819 unsigned BitWidth = VT->getBitWidth();
Tanya Lattnerce275672011-11-28 23:18:11 +00001820 if ((BitWidth >= 128 && BitWidth <= 256) &&
Chris Lattner0f408f52010-07-29 04:56:46 +00001821 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1822 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1823 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1824 EltTy->isIntegerTy(128)))
1825 return VT;
1826 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001827
Chris Lattner0f408f52010-07-29 04:56:46 +00001828 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1829}
1830
Chris Lattnere2962be2010-07-29 07:30:00 +00001831/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1832/// is known to either be off the end of the specified type or being in
1833/// alignment padding. The user type specified is known to be at most 128 bits
1834/// in size, and have passed through X86_64ABIInfo::classify with a successful
1835/// classification that put one of the two halves in the INTEGER class.
1836///
1837/// It is conservatively correct to return false.
1838static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1839 unsigned EndBit, ASTContext &Context) {
1840 // If the bytes being queried are off the end of the type, there is no user
1841 // data hiding here. This handles analysis of builtins, vectors and other
1842 // types that don't contain interesting padding.
1843 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1844 if (TySize <= StartBit)
1845 return true;
1846
Chris Lattner021c3a32010-07-29 07:43:55 +00001847 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1848 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1849 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1850
1851 // Check each element to see if the element overlaps with the queried range.
1852 for (unsigned i = 0; i != NumElts; ++i) {
1853 // If the element is after the span we care about, then we're done..
1854 unsigned EltOffset = i*EltSize;
1855 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001856
Chris Lattner021c3a32010-07-29 07:43:55 +00001857 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1858 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1859 EndBit-EltOffset, Context))
1860 return false;
1861 }
1862 // If it overlaps no elements, then it is safe to process as padding.
1863 return true;
1864 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001865
Chris Lattnere2962be2010-07-29 07:30:00 +00001866 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1867 const RecordDecl *RD = RT->getDecl();
1868 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001869
Chris Lattnere2962be2010-07-29 07:30:00 +00001870 // If this is a C++ record, check the bases first.
1871 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1872 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1873 e = CXXRD->bases_end(); i != e; ++i) {
1874 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1875 "Unexpected base class!");
1876 const CXXRecordDecl *Base =
1877 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001878
Chris Lattnere2962be2010-07-29 07:30:00 +00001879 // If the base is after the span we care about, ignore it.
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001880 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
Chris Lattnere2962be2010-07-29 07:30:00 +00001881 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001882
Chris Lattnere2962be2010-07-29 07:30:00 +00001883 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1884 if (!BitsContainNoUserData(i->getType(), BaseStart,
1885 EndBit-BaseOffset, Context))
1886 return false;
1887 }
1888 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001889
Chris Lattnere2962be2010-07-29 07:30:00 +00001890 // Verify that no field has data that overlaps the region of interest. Yes
1891 // this could be sped up a lot by being smarter about queried fields,
1892 // however we're only looking at structs up to 16 bytes, so we don't care
1893 // much.
1894 unsigned idx = 0;
1895 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1896 i != e; ++i, ++idx) {
1897 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001898
Chris Lattnere2962be2010-07-29 07:30:00 +00001899 // If we found a field after the region we care about, then we're done.
1900 if (FieldOffset >= EndBit) break;
1901
1902 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1903 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1904 Context))
1905 return false;
1906 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001907
Chris Lattnere2962be2010-07-29 07:30:00 +00001908 // If nothing in this record overlapped the area of interest, then we're
1909 // clean.
1910 return true;
1911 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001912
Chris Lattnere2962be2010-07-29 07:30:00 +00001913 return false;
1914}
1915
Chris Lattner0b362002010-07-29 18:39:32 +00001916/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1917/// float member at the specified offset. For example, {int,{float}} has a
1918/// float at offset 4. It is conservatively correct for this routine to return
1919/// false.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001920static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Micah Villmow25a6a842012-10-08 16:25:52 +00001921 const llvm::DataLayout &TD) {
Chris Lattner0b362002010-07-29 18:39:32 +00001922 // Base case if we find a float.
1923 if (IROffset == 0 && IRType->isFloatTy())
1924 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001925
Chris Lattner0b362002010-07-29 18:39:32 +00001926 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001927 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner0b362002010-07-29 18:39:32 +00001928 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1929 unsigned Elt = SL->getElementContainingOffset(IROffset);
1930 IROffset -= SL->getElementOffset(Elt);
1931 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1932 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001933
Chris Lattner0b362002010-07-29 18:39:32 +00001934 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001935 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1936 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner0b362002010-07-29 18:39:32 +00001937 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1938 IROffset -= IROffset/EltSize*EltSize;
1939 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1940 }
1941
1942 return false;
1943}
1944
Chris Lattnerf47c9442010-07-29 18:13:09 +00001945
1946/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1947/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001948llvm::Type *X86_64ABIInfo::
1949GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00001950 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001951 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001952 // pass as float if the last 4 bytes is just padding. This happens for
1953 // structs that contain 3 floats.
1954 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1955 SourceOffset*8+64, getContext()))
1956 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001957
Chris Lattner0b362002010-07-29 18:39:32 +00001958 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1959 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1960 // case.
Micah Villmow25a6a842012-10-08 16:25:52 +00001961 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
1962 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001963 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001964
Chris Lattnerf47c9442010-07-29 18:13:09 +00001965 return llvm::Type::getDoubleTy(getVMContext());
1966}
1967
1968
Chris Lattner0d2656d2010-07-29 17:40:35 +00001969/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1970/// an 8-byte GPR. This means that we either have a scalar or we are talking
1971/// about the high or low part of an up-to-16-byte struct. This routine picks
1972/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001973/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1974/// etc).
1975///
1976/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1977/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1978/// the 8-byte value references. PrefType may be null.
1979///
1980/// SourceTy is the source level type for the entire argument. SourceOffset is
1981/// an offset into this that we're processing (which is always either 0 or 8).
1982///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001983llvm::Type *X86_64ABIInfo::
1984GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00001985 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001986 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1987 // returning an 8-byte unit starting with it. See if we can safely use it.
1988 if (IROffset == 0) {
1989 // Pointers and int64's always fill the 8-byte unit.
Derek Schuffbabaf312012-10-11 15:52:22 +00001990 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
1991 IRType->isIntegerTy(64))
Chris Lattnere2962be2010-07-29 07:30:00 +00001992 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001993
Chris Lattnere2962be2010-07-29 07:30:00 +00001994 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1995 // goodness in the source type is just tail padding. This is allowed to
1996 // kick in for struct {double,int} on the int, but not on
1997 // struct{double,int,int} because we wouldn't return the second int. We
1998 // have to do this analysis on the source type because we can't depend on
1999 // unions being lowered a specific way etc.
2000 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
Derek Schuffbabaf312012-10-11 15:52:22 +00002001 IRType->isIntegerTy(32) ||
2002 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
2003 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
2004 cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002005
Chris Lattnere2962be2010-07-29 07:30:00 +00002006 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
2007 SourceOffset*8+64, getContext()))
2008 return IRType;
2009 }
2010 }
Chris Lattner49382de2010-07-28 22:44:07 +00002011
Chris Lattner2acc6e32011-07-18 04:24:23 +00002012 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00002013 // If this is a struct, recurse into the field at the specified offset.
Micah Villmow25a6a842012-10-08 16:25:52 +00002014 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00002015 if (IROffset < SL->getSizeInBytes()) {
2016 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
2017 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002018
Chris Lattner0d2656d2010-07-29 17:40:35 +00002019 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
2020 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002021 }
Chris Lattner49382de2010-07-28 22:44:07 +00002022 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002023
Chris Lattner2acc6e32011-07-18 04:24:23 +00002024 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002025 llvm::Type *EltTy = ATy->getElementType();
Micah Villmow25a6a842012-10-08 16:25:52 +00002026 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
Chris Lattner021c3a32010-07-29 07:43:55 +00002027 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00002028 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
2029 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00002030 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002031
Chris Lattner49382de2010-07-28 22:44:07 +00002032 // Okay, we don't have any better idea of what to pass, so we pass this in an
2033 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00002034 unsigned TySizeInBytes =
2035 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00002036
Chris Lattner9e45a3d2010-07-29 17:34:39 +00002037 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002038
Chris Lattner49382de2010-07-28 22:44:07 +00002039 // It is always safe to classify this as an integer type up to i64 that
2040 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00002041 return llvm::IntegerType::get(getVMContext(),
2042 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00002043}
2044
Chris Lattner66e7b682010-09-01 00:50:20 +00002045
2046/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
2047/// be used as elements of a two register pair to pass or return, return a
2048/// first class aggregate to represent them. For example, if the low part of
2049/// a by-value argument should be passed as i32* and the high part as float,
2050/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002051static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00002052GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Micah Villmow25a6a842012-10-08 16:25:52 +00002053 const llvm::DataLayout &TD) {
Chris Lattner66e7b682010-09-01 00:50:20 +00002054 // In order to correctly satisfy the ABI, we need to the high part to start
2055 // at offset 8. If the high and low parts we inferred are both 4-byte types
2056 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
2057 // the second element at offset 8. Check for this:
2058 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
2059 unsigned HiAlign = TD.getABITypeAlignment(Hi);
Micah Villmow25a6a842012-10-08 16:25:52 +00002060 unsigned HiStart = llvm::DataLayout::RoundUpAlignment(LoSize, HiAlign);
Chris Lattner66e7b682010-09-01 00:50:20 +00002061 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002062
Chris Lattner66e7b682010-09-01 00:50:20 +00002063 // To handle this, we have to increase the size of the low part so that the
2064 // second element will start at an 8 byte offset. We can't increase the size
2065 // of the second element because it might make us access off the end of the
2066 // struct.
2067 if (HiStart != 8) {
2068 // There are only two sorts of types the ABI generation code can produce for
2069 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
2070 // Promote these to a larger type.
2071 if (Lo->isFloatTy())
2072 Lo = llvm::Type::getDoubleTy(Lo->getContext());
2073 else {
2074 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
2075 Lo = llvm::Type::getInt64Ty(Lo->getContext());
2076 }
2077 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002078
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002079 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002080
2081
Chris Lattner66e7b682010-09-01 00:50:20 +00002082 // Verify that the second element is at an 8-byte offset.
2083 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
2084 "Invalid x86-64 argument pair!");
2085 return Result;
2086}
2087
Chris Lattner519f68c2010-07-28 23:06:14 +00002088ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00002089classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00002090 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
2091 // classification algorithm.
2092 X86_64ABIInfo::Class Lo, Hi;
Eli Friedman7a1b5862013-06-12 00:13:45 +00002093 classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true);
Chris Lattner519f68c2010-07-28 23:06:14 +00002094
2095 // Check some invariants.
2096 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002097 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2098
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002099 llvm::Type *ResType = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002100 switch (Lo) {
2101 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002102 if (Hi == NoClass)
2103 return ABIArgInfo::getIgnore();
2104 // If the low part is just padding, it takes no register, leave ResType
2105 // null.
2106 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2107 "Unknown missing lo part");
2108 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002109
2110 case SSEUp:
2111 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002112 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002113
2114 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
2115 // hidden argument.
2116 case Memory:
2117 return getIndirectReturnResult(RetTy);
2118
2119 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
2120 // available register of the sequence %rax, %rdx is used.
2121 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002122 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002123
Chris Lattnereb518b42010-07-29 21:42:50 +00002124 // If we have a sign or zero extended integer, make sure to return Extend
2125 // so that the parameter gets the right LLVM IR attributes.
2126 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2127 // Treat an enum type as its underlying type.
2128 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2129 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002130
Chris Lattnereb518b42010-07-29 21:42:50 +00002131 if (RetTy->isIntegralOrEnumerationType() &&
2132 RetTy->isPromotableIntegerType())
2133 return ABIArgInfo::getExtend();
2134 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002135 break;
2136
2137 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
2138 // available SSE register of the sequence %xmm0, %xmm1 is used.
2139 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002140 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00002141 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002142
2143 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
2144 // returned on the X87 stack in %st0 as 80-bit x87 number.
2145 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00002146 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00002147 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002148
2149 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
2150 // part of the value is returned in %st0 and the imaginary part in
2151 // %st1.
2152 case ComplexX87:
2153 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00002154 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00002155 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00002156 NULL);
2157 break;
2158 }
2159
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002160 llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002161 switch (Hi) {
2162 // Memory was handled previously and X87 should
2163 // never occur as a hi class.
2164 case Memory:
2165 case X87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002166 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002167
2168 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00002169 case NoClass:
2170 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002171
Chris Lattner3db4dde2010-09-01 00:20:33 +00002172 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002173 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002174 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2175 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002176 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00002177 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002178 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002179 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2180 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002181 break;
2182
2183 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002184 // is passed in the next available eightbyte chunk if the last used
2185 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00002186 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002187 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00002188 case SSEUp:
2189 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002190 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00002191 break;
2192
2193 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
2194 // returned together with the previous X87 value in %st0.
2195 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002196 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00002197 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002198 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00002199 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00002200 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002201 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002202 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2203 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00002204 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002205 break;
2206 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002207
Chris Lattner3db4dde2010-09-01 00:20:33 +00002208 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00002209 // known to pass in the high eightbyte of the result. We do this by forming a
2210 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00002211 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002212 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Chris Lattner519f68c2010-07-28 23:06:14 +00002213
Chris Lattnereb518b42010-07-29 21:42:50 +00002214 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00002215}
2216
Daniel Dunbaredfac032012-03-10 01:03:58 +00002217ABIArgInfo X86_64ABIInfo::classifyArgumentType(
Eli Friedman7a1b5862013-06-12 00:13:45 +00002218 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE,
2219 bool isNamedArg)
Daniel Dunbaredfac032012-03-10 01:03:58 +00002220 const
2221{
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002222 X86_64ABIInfo::Class Lo, Hi;
Eli Friedman7a1b5862013-06-12 00:13:45 +00002223 classify(Ty, 0, Lo, Hi, isNamedArg);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002224
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002225 // Check some invariants.
2226 // FIXME: Enforce these by construction.
2227 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002228 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2229
2230 neededInt = 0;
2231 neededSSE = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002232 llvm::Type *ResType = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002233 switch (Lo) {
2234 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002235 if (Hi == NoClass)
2236 return ABIArgInfo::getIgnore();
2237 // If the low part is just padding, it takes no register, leave ResType
2238 // null.
2239 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2240 "Unknown missing lo part");
2241 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002242
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002243 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
2244 // on the stack.
2245 case Memory:
2246
2247 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
2248 // COMPLEX_X87, it is passed in memory.
2249 case X87:
2250 case ComplexX87:
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002251 if (getRecordArgABI(Ty, CGT) == CGCXXABI::RAA_Indirect)
Eli Friedmanded137f2011-06-29 07:04:55 +00002252 ++neededInt;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002253 return getIndirectResult(Ty, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002254
2255 case SSEUp:
2256 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002257 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002258
2259 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
2260 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
2261 // and %r9 is used.
2262 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00002263 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002264
Chris Lattner49382de2010-07-28 22:44:07 +00002265 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002266 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00002267
2268 // If we have a sign or zero extended integer, make sure to return Extend
2269 // so that the parameter gets the right LLVM IR attributes.
2270 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2271 // Treat an enum type as its underlying type.
2272 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2273 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002274
Chris Lattnereb518b42010-07-29 21:42:50 +00002275 if (Ty->isIntegralOrEnumerationType() &&
2276 Ty->isPromotableIntegerType())
2277 return ABIArgInfo::getExtend();
2278 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002279
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002280 break;
2281
2282 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
2283 // available SSE register is used, the registers are taken in the
2284 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00002285 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002286 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00002287 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00002288 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002289 break;
2290 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00002291 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002292
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002293 llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002294 switch (Hi) {
2295 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002296 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002297 // which is passed in memory.
2298 case Memory:
2299 case X87:
2300 case ComplexX87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002301 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002302
2303 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002304
Chris Lattner645406a2010-09-01 00:24:35 +00002305 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002306 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00002307 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002308 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002309
Chris Lattner645406a2010-09-01 00:24:35 +00002310 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2311 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002312 break;
2313
2314 // X87Up generally doesn't occur here (long double is passed in
2315 // memory), except in situations involving unions.
2316 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00002317 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002318 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002319
Chris Lattner645406a2010-09-01 00:24:35 +00002320 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2321 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00002322
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002323 ++neededSSE;
2324 break;
2325
2326 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
2327 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002328 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002329 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00002330 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002331 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002332 break;
2333 }
2334
Chris Lattner645406a2010-09-01 00:24:35 +00002335 // If a high part was specified, merge it together with the low part. It is
2336 // known to pass in the high eightbyte of the result. We do this by forming a
2337 // first class struct aggregate with the high and low part: {low, high}
2338 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002339 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002340
Chris Lattnereb518b42010-07-29 21:42:50 +00002341 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002342}
2343
Chris Lattneree5dcd02010-07-29 02:31:05 +00002344void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002345
Chris Lattnera3c109b2010-07-29 02:16:43 +00002346 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002347
2348 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00002349 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002350
2351 // If the return value is indirect, then the hidden argument is consuming one
2352 // integer register.
2353 if (FI.getReturnInfo().isIndirect())
2354 --freeIntRegs;
2355
Eli Friedman7a1b5862013-06-12 00:13:45 +00002356 bool isVariadic = FI.isVariadic();
2357 unsigned numRequiredArgs = 0;
2358 if (isVariadic)
2359 numRequiredArgs = FI.getRequiredArgs().getNumRequiredArgs();
2360
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002361 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2362 // get assigned (in left-to-right order) for passing as follows...
2363 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2364 it != ie; ++it) {
Eli Friedman7a1b5862013-06-12 00:13:45 +00002365 bool isNamedArg = true;
2366 if (isVariadic)
Aaron Ballmaneba7d2f2013-06-12 15:03:45 +00002367 isNamedArg = (it - FI.arg_begin()) <
2368 static_cast<signed>(numRequiredArgs);
Eli Friedman7a1b5862013-06-12 00:13:45 +00002369
Bill Wendling99aaae82010-10-18 23:51:38 +00002370 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002371 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
Eli Friedman7a1b5862013-06-12 00:13:45 +00002372 neededSSE, isNamedArg);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002373
2374 // AMD64-ABI 3.2.3p3: If there are no registers available for any
2375 // eightbyte of an argument, the whole argument is passed on the
2376 // stack. If registers have already been assigned for some
2377 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00002378 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002379 freeIntRegs -= neededInt;
2380 freeSSERegs -= neededSSE;
2381 } else {
Daniel Dunbaredfac032012-03-10 01:03:58 +00002382 it->info = getIndirectResult(it->type, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002383 }
2384 }
2385}
2386
2387static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2388 QualType Ty,
2389 CodeGenFunction &CGF) {
2390 llvm::Value *overflow_arg_area_p =
2391 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2392 llvm::Value *overflow_arg_area =
2393 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2394
2395 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2396 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedman8d2fe422011-11-18 02:44:19 +00002397 // It isn't stated explicitly in the standard, but in practice we use
2398 // alignment greater than 16 where necessary.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002399 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2400 if (Align > 8) {
Eli Friedman8d2fe422011-11-18 02:44:19 +00002401 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
Owen Anderson0032b272009-08-13 21:57:51 +00002402 llvm::Value *Offset =
Eli Friedman8d2fe422011-11-18 02:44:19 +00002403 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002404 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2405 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00002406 CGF.Int64Ty);
Eli Friedman8d2fe422011-11-18 02:44:19 +00002407 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002408 overflow_arg_area =
2409 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2410 overflow_arg_area->getType(),
2411 "overflow_arg_area.align");
2412 }
2413
2414 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002415 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002416 llvm::Value *Res =
2417 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002418 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002419
2420 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2421 // l->overflow_arg_area + sizeof(type).
2422 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2423 // an 8 byte boundary.
2424
2425 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00002426 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00002427 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002428 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2429 "overflow_arg_area.next");
2430 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2431
2432 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2433 return Res;
2434}
2435
2436llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2437 CodeGenFunction &CGF) const {
2438 // Assume that va_list type is correct; should be pointer to LLVM type:
2439 // struct {
2440 // i32 gp_offset;
2441 // i32 fp_offset;
2442 // i8* overflow_arg_area;
2443 // i8* reg_save_area;
2444 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00002445 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002446
Chris Lattnera14db752010-03-11 18:19:55 +00002447 Ty = CGF.getContext().getCanonicalType(Ty);
Eli Friedman7a1b5862013-06-12 00:13:45 +00002448 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE,
2449 /*isNamedArg*/false);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002450
2451 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2452 // in the registers. If not go to step 7.
2453 if (!neededInt && !neededSSE)
2454 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2455
2456 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2457 // general purpose registers needed to pass type and num_fp to hold
2458 // the number of floating point registers needed.
2459
2460 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2461 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2462 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2463 //
2464 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2465 // register save space).
2466
2467 llvm::Value *InRegs = 0;
2468 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2469 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2470 if (neededInt) {
2471 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2472 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002473 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2474 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002475 }
2476
2477 if (neededSSE) {
2478 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2479 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2480 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00002481 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2482 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002483 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2484 }
2485
2486 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2487 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2488 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2489 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2490
2491 // Emit code to load the value if it was passed in registers.
2492
2493 CGF.EmitBlock(InRegBlock);
2494
2495 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2496 // an offset of l->gp_offset and/or l->fp_offset. This may require
2497 // copying to a temporary location in case the parameter is passed
2498 // in different register classes or requires an alignment greater
2499 // than 8 for general purpose registers and 16 for XMM registers.
2500 //
2501 // FIXME: This really results in shameful code when we end up needing to
2502 // collect arguments from different places; often what should result in a
2503 // simple assembling of a structure from scattered addresses has many more
2504 // loads than necessary. Can we clean this up?
Chris Lattner2acc6e32011-07-18 04:24:23 +00002505 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002506 llvm::Value *RegAddr =
2507 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2508 "reg_save_area");
2509 if (neededInt && neededSSE) {
2510 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002511 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002512 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Eli Friedmaneeb00622013-06-07 23:20:55 +00002513 llvm::Value *Tmp = CGF.CreateMemTemp(Ty);
2514 Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002515 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002516 llvm::Type *TyLo = ST->getElementType(0);
2517 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002518 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002519 "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002520 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2521 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002522 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2523 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002524 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2525 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002526 llvm::Value *V =
2527 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2528 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2529 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2530 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2531
Owen Andersona1cf15f2009-07-14 23:10:40 +00002532 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002533 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002534 } else if (neededInt) {
2535 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2536 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002537 llvm::PointerType::getUnqual(LTy));
Eli Friedmaneeb00622013-06-07 23:20:55 +00002538
2539 // Copy to a temporary if necessary to ensure the appropriate alignment.
2540 std::pair<CharUnits, CharUnits> SizeAlign =
2541 CGF.getContext().getTypeInfoInChars(Ty);
2542 uint64_t TySize = SizeAlign.first.getQuantity();
2543 unsigned TyAlign = SizeAlign.second.getQuantity();
2544 if (TyAlign > 8) {
Eli Friedmaneeb00622013-06-07 23:20:55 +00002545 llvm::Value *Tmp = CGF.CreateMemTemp(Ty);
2546 CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, 8, false);
2547 RegAddr = Tmp;
2548 }
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002549 } else if (neededSSE == 1) {
2550 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2551 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2552 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002553 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002554 assert(neededSSE == 2 && "Invalid number of needed registers!");
2555 // SSE registers are spaced 16 bytes apart in the register save
2556 // area, we need to collect the two eightbytes together.
2557 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002558 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner8b418682012-02-07 00:39:47 +00002559 llvm::Type *DoubleTy = CGF.DoubleTy;
Chris Lattner2acc6e32011-07-18 04:24:23 +00002560 llvm::Type *DblPtrTy =
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002561 llvm::PointerType::getUnqual(DoubleTy);
Eli Friedmaneeb00622013-06-07 23:20:55 +00002562 llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, NULL);
2563 llvm::Value *V, *Tmp = CGF.CreateMemTemp(Ty);
2564 Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo());
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002565 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2566 DblPtrTy));
2567 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2568 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2569 DblPtrTy));
2570 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2571 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2572 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002573 }
2574
2575 // AMD64-ABI 3.5.7p5: Step 5. Set:
2576 // l->gp_offset = l->gp_offset + num_gp * 8
2577 // l->fp_offset = l->fp_offset + num_fp * 16.
2578 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002579 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002580 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2581 gp_offset_p);
2582 }
2583 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002584 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002585 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2586 fp_offset_p);
2587 }
2588 CGF.EmitBranch(ContBlock);
2589
2590 // Emit code to load the value if it was passed in memory.
2591
2592 CGF.EmitBlock(InMemBlock);
2593 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2594
2595 // Return the appropriate result.
2596
2597 CGF.EmitBlock(ContBlock);
Jay Foadbbf3bac2011-03-30 11:28:58 +00002598 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002599 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002600 ResAddr->addIncoming(RegAddr, InRegBlock);
2601 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002602 return ResAddr;
2603}
2604
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002605ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, bool IsReturnType) const {
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002606
2607 if (Ty->isVoidType())
2608 return ABIArgInfo::getIgnore();
2609
2610 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2611 Ty = EnumTy->getDecl()->getIntegerType();
2612
2613 uint64_t Size = getContext().getTypeSize(Ty);
2614
2615 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002616 if (IsReturnType) {
2617 if (isRecordReturnIndirect(RT, CGT))
2618 return ABIArgInfo::getIndirect(0, false);
2619 } else {
2620 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, CGT))
2621 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
2622 }
2623
2624 if (RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002625 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2626
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002627 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
John McCall64aa4b32013-04-16 22:48:15 +00002628 if (Size == 128 && getTarget().getTriple().getOS() == llvm::Triple::MinGW32)
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002629 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2630 Size));
2631
2632 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2633 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2634 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002635 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002636 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2637 Size));
2638
2639 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2640 }
2641
2642 if (Ty->isPromotableIntegerType())
2643 return ABIArgInfo::getExtend();
2644
2645 return ABIArgInfo::getDirect();
2646}
2647
2648void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2649
2650 QualType RetTy = FI.getReturnType();
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002651 FI.getReturnInfo() = classify(RetTy, true);
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002652
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002653 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2654 it != ie; ++it)
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002655 it->info = classify(it->type, false);
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002656}
2657
Chris Lattnerf13721d2010-08-31 16:44:54 +00002658llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2659 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002660 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002661
Chris Lattnerf13721d2010-08-31 16:44:54 +00002662 CGBuilderTy &Builder = CGF.Builder;
2663 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2664 "ap");
2665 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2666 llvm::Type *PTy =
2667 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2668 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2669
2670 uint64_t Offset =
2671 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2672 llvm::Value *NextAddr =
2673 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2674 "ap.next");
2675 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2676
2677 return AddrTyped;
2678}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002679
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00002680namespace {
2681
Derek Schuff263366f2012-10-16 22:30:41 +00002682class NaClX86_64ABIInfo : public ABIInfo {
2683 public:
2684 NaClX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2685 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, HasAVX) {}
2686 virtual void computeInfo(CGFunctionInfo &FI) const;
2687 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2688 CodeGenFunction &CGF) const;
2689 private:
2690 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
2691 X86_64ABIInfo NInfo; // Used for everything else.
2692};
2693
2694class NaClX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2695 public:
2696 NaClX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2697 : TargetCodeGenInfo(new NaClX86_64ABIInfo(CGT, HasAVX)) {}
2698};
2699
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00002700}
2701
Derek Schuff263366f2012-10-16 22:30:41 +00002702void NaClX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2703 if (FI.getASTCallingConvention() == CC_PnaclCall)
2704 PInfo.computeInfo(FI);
2705 else
2706 NInfo.computeInfo(FI);
2707}
2708
2709llvm::Value *NaClX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2710 CodeGenFunction &CGF) const {
2711 // Always use the native convention; calling pnacl-style varargs functions
2712 // is unuspported.
2713 return NInfo.EmitVAArg(VAListAddr, Ty, CGF);
2714}
2715
2716
John McCallec853ba2010-03-11 00:10:12 +00002717// PowerPC-32
2718
2719namespace {
2720class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2721public:
Chris Lattnerea044322010-07-29 02:01:43 +00002722 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002723
John McCallec853ba2010-03-11 00:10:12 +00002724 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2725 // This is recovered from gcc output.
2726 return 1; // r1 is the dedicated stack pointer
2727 }
2728
2729 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002730 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002731};
2732
2733}
2734
2735bool
2736PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2737 llvm::Value *Address) const {
2738 // This is calculated from the LLVM and GCC tables and verified
2739 // against gcc output. AFAIK all ABIs use the same encoding.
2740
2741 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallec853ba2010-03-11 00:10:12 +00002742
Chris Lattner8b418682012-02-07 00:39:47 +00002743 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallec853ba2010-03-11 00:10:12 +00002744 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2745 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2746 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2747
2748 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002749 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002750
2751 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002752 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002753
2754 // 64-76 are various 4-byte special-purpose registers:
2755 // 64: mq
2756 // 65: lr
2757 // 66: ctr
2758 // 67: ap
2759 // 68-75 cr0-7
2760 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002761 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002762
2763 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002764 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002765
2766 // 109: vrsave
2767 // 110: vscr
2768 // 111: spe_acc
2769 // 112: spefscr
2770 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002771 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002772
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002773 return false;
John McCallec853ba2010-03-11 00:10:12 +00002774}
2775
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002776// PowerPC-64
2777
2778namespace {
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002779/// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
2780class PPC64_SVR4_ABIInfo : public DefaultABIInfo {
2781
2782public:
2783 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
2784
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002785 bool isPromotableTypeForABI(QualType Ty) const;
2786
2787 ABIArgInfo classifyReturnType(QualType RetTy) const;
2788 ABIArgInfo classifyArgumentType(QualType Ty) const;
2789
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00002790 // TODO: We can add more logic to computeInfo to improve performance.
2791 // Example: For aggregate arguments that fit in a register, we could
2792 // use getDirectInReg (as is done below for structs containing a single
2793 // floating-point value) to avoid pushing them to memory on function
2794 // entry. This would require changing the logic in PPCISelLowering
2795 // when lowering the parameters in the caller and args in the callee.
2796 virtual void computeInfo(CGFunctionInfo &FI) const {
2797 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2798 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2799 it != ie; ++it) {
2800 // We rely on the default argument classification for the most part.
2801 // One exception: An aggregate containing a single floating-point
2802 // item must be passed in a register if one is available.
2803 const Type *T = isSingleElementStruct(it->type, getContext());
2804 if (T) {
2805 const BuiltinType *BT = T->getAs<BuiltinType>();
2806 if (BT && BT->isFloatingPoint()) {
2807 QualType QT(T, 0);
2808 it->info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
2809 continue;
2810 }
2811 }
2812 it->info = classifyArgumentType(it->type);
2813 }
2814 }
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002815
2816 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr,
2817 QualType Ty,
2818 CodeGenFunction &CGF) const;
2819};
2820
2821class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
2822public:
2823 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT)
2824 : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT)) {}
2825
2826 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2827 // This is recovered from gcc output.
2828 return 1; // r1 is the dedicated stack pointer
2829 }
2830
2831 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2832 llvm::Value *Address) const;
2833};
2834
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002835class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2836public:
2837 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
2838
2839 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2840 // This is recovered from gcc output.
2841 return 1; // r1 is the dedicated stack pointer
2842 }
2843
2844 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2845 llvm::Value *Address) const;
2846};
2847
2848}
2849
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002850// Return true if the ABI requires Ty to be passed sign- or zero-
2851// extended to 64 bits.
2852bool
2853PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
2854 // Treat an enum type as its underlying type.
2855 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2856 Ty = EnumTy->getDecl()->getIntegerType();
2857
2858 // Promotable integer types are required to be promoted by the ABI.
2859 if (Ty->isPromotableIntegerType())
2860 return true;
2861
2862 // In addition to the usual promotable integer types, we also need to
2863 // extend all 32-bit types, since the ABI requires promotion to 64 bits.
2864 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2865 switch (BT->getKind()) {
2866 case BuiltinType::Int:
2867 case BuiltinType::UInt:
2868 return true;
2869 default:
2870 break;
2871 }
2872
2873 return false;
2874}
2875
2876ABIArgInfo
2877PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
Bill Schmidtc9715fc2012-11-27 02:46:43 +00002878 if (Ty->isAnyComplexType())
2879 return ABIArgInfo::getDirect();
2880
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002881 if (isAggregateTypeForABI(Ty)) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002882 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
2883 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002884
2885 return ABIArgInfo::getIndirect(0);
2886 }
2887
2888 return (isPromotableTypeForABI(Ty) ?
2889 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2890}
2891
2892ABIArgInfo
2893PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
2894 if (RetTy->isVoidType())
2895 return ABIArgInfo::getIgnore();
2896
Bill Schmidt9e6111a2012-12-17 04:20:17 +00002897 if (RetTy->isAnyComplexType())
2898 return ABIArgInfo::getDirect();
2899
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002900 if (isAggregateTypeForABI(RetTy))
2901 return ABIArgInfo::getIndirect(0);
2902
2903 return (isPromotableTypeForABI(RetTy) ?
2904 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2905}
2906
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002907// Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
2908llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr,
2909 QualType Ty,
2910 CodeGenFunction &CGF) const {
2911 llvm::Type *BP = CGF.Int8PtrTy;
2912 llvm::Type *BPP = CGF.Int8PtrPtrTy;
2913
2914 CGBuilderTy &Builder = CGF.Builder;
2915 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
2916 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2917
Bill Schmidt19f8e852013-01-14 17:45:36 +00002918 // Update the va_list pointer. The pointer should be bumped by the
2919 // size of the object. We can trust getTypeSize() except for a complex
2920 // type whose base type is smaller than a doubleword. For these, the
2921 // size of the object is 16 bytes; see below for further explanation.
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002922 unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8;
Bill Schmidt19f8e852013-01-14 17:45:36 +00002923 QualType BaseTy;
2924 unsigned CplxBaseSize = 0;
2925
2926 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
2927 BaseTy = CTy->getElementType();
2928 CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8;
2929 if (CplxBaseSize < 8)
2930 SizeInBytes = 16;
2931 }
2932
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002933 unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8);
2934 llvm::Value *NextAddr =
2935 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset),
2936 "ap.next");
2937 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2938
Bill Schmidt19f8e852013-01-14 17:45:36 +00002939 // If we have a complex type and the base type is smaller than 8 bytes,
2940 // the ABI calls for the real and imaginary parts to be right-adjusted
2941 // in separate doublewords. However, Clang expects us to produce a
2942 // pointer to a structure with the two parts packed tightly. So generate
2943 // loads of the real and imaginary parts relative to the va_list pointer,
2944 // and store them to a temporary structure.
2945 if (CplxBaseSize && CplxBaseSize < 8) {
2946 llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2947 llvm::Value *ImagAddr = RealAddr;
2948 RealAddr = Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize));
2949 ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize));
2950 llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy));
2951 RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy);
2952 ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy);
2953 llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal");
2954 llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag");
2955 llvm::Value *Ptr = CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty),
2956 "vacplx");
2957 llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, ".real");
2958 llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, ".imag");
2959 Builder.CreateStore(Real, RealPtr, false);
2960 Builder.CreateStore(Imag, ImagPtr, false);
2961 return Ptr;
2962 }
2963
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002964 // If the argument is smaller than 8 bytes, it is right-adjusted in
2965 // its doubleword slot. Adjust the pointer to pick it up from the
2966 // correct offset.
2967 if (SizeInBytes < 8) {
2968 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2969 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes));
2970 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2971 }
2972
2973 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2974 return Builder.CreateBitCast(Addr, PTy);
2975}
2976
2977static bool
2978PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2979 llvm::Value *Address) {
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002980 // This is calculated from the LLVM and GCC tables and verified
2981 // against gcc output. AFAIK all ABIs use the same encoding.
2982
2983 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2984
2985 llvm::IntegerType *i8 = CGF.Int8Ty;
2986 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2987 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2988 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2989
2990 // 0-31: r0-31, the 8-byte general-purpose registers
2991 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
2992
2993 // 32-63: fp0-31, the 8-byte floating-point registers
2994 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
2995
2996 // 64-76 are various 4-byte special-purpose registers:
2997 // 64: mq
2998 // 65: lr
2999 // 66: ctr
3000 // 67: ap
3001 // 68-75 cr0-7
3002 // 76: xer
3003 AssignToArrayRange(Builder, Address, Four8, 64, 76);
3004
3005 // 77-108: v0-31, the 16-byte vector registers
3006 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
3007
3008 // 109: vrsave
3009 // 110: vscr
3010 // 111: spe_acc
3011 // 112: spefscr
3012 // 113: sfp
3013 AssignToArrayRange(Builder, Address, Four8, 109, 113);
3014
3015 return false;
3016}
John McCallec853ba2010-03-11 00:10:12 +00003017
Bill Schmidt2fc107f2012-10-03 19:18:57 +00003018bool
3019PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
3020 CodeGen::CodeGenFunction &CGF,
3021 llvm::Value *Address) const {
3022
3023 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
3024}
3025
3026bool
3027PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3028 llvm::Value *Address) const {
3029
3030 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
3031}
3032
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003033//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003034// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003035//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003036
3037namespace {
3038
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003039class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003040public:
3041 enum ABIKind {
3042 APCS = 0,
3043 AAPCS = 1,
3044 AAPCS_VFP
3045 };
3046
3047private:
3048 ABIKind Kind;
3049
3050public:
John McCallbd7370a2013-02-28 19:01:20 +00003051 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {
3052 setRuntimeCC();
3053 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003054
John McCall49e34be2011-08-30 01:42:09 +00003055 bool isEABI() const {
John McCall64aa4b32013-04-16 22:48:15 +00003056 StringRef Env = getTarget().getTriple().getEnvironmentName();
Logan Chien94a71422012-09-02 09:30:11 +00003057 return (Env == "gnueabi" || Env == "eabi" ||
3058 Env == "android" || Env == "androideabi");
John McCall49e34be2011-08-30 01:42:09 +00003059 }
3060
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003061private:
3062 ABIKind getABIKind() const { return Kind; }
3063
Chris Lattnera3c109b2010-07-29 02:16:43 +00003064 ABIArgInfo classifyReturnType(QualType RetTy) const;
Manman Ren710c5172012-10-31 19:02:26 +00003065 ABIArgInfo classifyArgumentType(QualType RetTy, int *VFPRegs,
3066 unsigned &AllocatedVFP,
Manman Renb3fa55f2012-10-30 23:21:41 +00003067 bool &IsHA) const;
Manman Ren97f81572012-10-16 19:18:39 +00003068 bool isIllegalVectorType(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003069
Chris Lattneree5dcd02010-07-29 02:31:05 +00003070 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003071
3072 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3073 CodeGenFunction &CGF) const;
John McCallbd7370a2013-02-28 19:01:20 +00003074
3075 llvm::CallingConv::ID getLLVMDefaultCC() const;
3076 llvm::CallingConv::ID getABIDefaultCC() const;
3077 void setRuntimeCC();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003078};
3079
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003080class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
3081public:
Chris Lattnerea044322010-07-29 02:01:43 +00003082 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
3083 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00003084
John McCall49e34be2011-08-30 01:42:09 +00003085 const ARMABIInfo &getABIInfo() const {
3086 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
3087 }
3088
John McCall6374c332010-03-06 00:35:14 +00003089 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3090 return 13;
3091 }
Roman Divacky09345d12011-05-18 19:36:54 +00003092
Chris Lattner5f9e2722011-07-23 10:55:15 +00003093 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCallf85e1932011-06-15 23:02:42 +00003094 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
3095 }
3096
Roman Divacky09345d12011-05-18 19:36:54 +00003097 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3098 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003099 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divacky09345d12011-05-18 19:36:54 +00003100
3101 // 0-15 are the 16 integer registers.
Chris Lattner8b418682012-02-07 00:39:47 +00003102 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divacky09345d12011-05-18 19:36:54 +00003103 return false;
3104 }
John McCall49e34be2011-08-30 01:42:09 +00003105
3106 unsigned getSizeOfUnwindException() const {
3107 if (getABIInfo().isEABI()) return 88;
3108 return TargetCodeGenInfo::getSizeOfUnwindException();
3109 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003110};
3111
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003112}
3113
Chris Lattneree5dcd02010-07-29 02:31:05 +00003114void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Manman Renb3fa55f2012-10-30 23:21:41 +00003115 // To correctly handle Homogeneous Aggregate, we need to keep track of the
Manman Ren710c5172012-10-31 19:02:26 +00003116 // VFP registers allocated so far.
Manman Renb3fa55f2012-10-30 23:21:41 +00003117 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
3118 // VFP registers of the appropriate type unallocated then the argument is
3119 // allocated to the lowest-numbered sequence of such registers.
3120 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
3121 // unallocated are marked as unavailable.
3122 unsigned AllocatedVFP = 0;
Manman Ren710c5172012-10-31 19:02:26 +00003123 int VFPRegs[16] = { 0 };
Chris Lattnera3c109b2010-07-29 02:16:43 +00003124 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003125 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Manman Renb3fa55f2012-10-30 23:21:41 +00003126 it != ie; ++it) {
3127 unsigned PreAllocation = AllocatedVFP;
3128 bool IsHA = false;
3129 // 6.1.2.3 There is one VFP co-processor register class using registers
3130 // s0-s15 (d0-d7) for passing arguments.
3131 const unsigned NumVFPs = 16;
Manman Ren710c5172012-10-31 19:02:26 +00003132 it->info = classifyArgumentType(it->type, VFPRegs, AllocatedVFP, IsHA);
Manman Renb3fa55f2012-10-30 23:21:41 +00003133 // If we do not have enough VFP registers for the HA, any VFP registers
3134 // that are unallocated are marked as unavailable. To achieve this, we add
3135 // padding of (NumVFPs - PreAllocation) floats.
3136 if (IsHA && AllocatedVFP > NumVFPs && PreAllocation < NumVFPs) {
3137 llvm::Type *PaddingTy = llvm::ArrayType::get(
3138 llvm::Type::getFloatTy(getVMContext()), NumVFPs - PreAllocation);
3139 it->info = ABIArgInfo::getExpandWithPadding(false, PaddingTy);
3140 }
3141 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003142
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003143 // Always honor user-specified calling convention.
3144 if (FI.getCallingConvention() != llvm::CallingConv::C)
3145 return;
3146
John McCallbd7370a2013-02-28 19:01:20 +00003147 llvm::CallingConv::ID cc = getRuntimeCC();
3148 if (cc != llvm::CallingConv::C)
3149 FI.setEffectiveCallingConvention(cc);
3150}
Rafael Espindola25117ab2010-06-16 16:13:39 +00003151
John McCallbd7370a2013-02-28 19:01:20 +00003152/// Return the default calling convention that LLVM will use.
3153llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
3154 // The default calling convention that LLVM will infer.
John McCall64aa4b32013-04-16 22:48:15 +00003155 if (getTarget().getTriple().getEnvironmentName()=="gnueabihf")
John McCallbd7370a2013-02-28 19:01:20 +00003156 return llvm::CallingConv::ARM_AAPCS_VFP;
3157 else if (isEABI())
3158 return llvm::CallingConv::ARM_AAPCS;
3159 else
3160 return llvm::CallingConv::ARM_APCS;
3161}
3162
3163/// Return the calling convention that our ABI would like us to use
3164/// as the C calling convention.
3165llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003166 switch (getABIKind()) {
John McCallbd7370a2013-02-28 19:01:20 +00003167 case APCS: return llvm::CallingConv::ARM_APCS;
3168 case AAPCS: return llvm::CallingConv::ARM_AAPCS;
3169 case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003170 }
John McCallbd7370a2013-02-28 19:01:20 +00003171 llvm_unreachable("bad ABI kind");
3172}
3173
3174void ARMABIInfo::setRuntimeCC() {
3175 assert(getRuntimeCC() == llvm::CallingConv::C);
3176
3177 // Don't muddy up the IR with a ton of explicit annotations if
3178 // they'd just match what LLVM will infer from the triple.
3179 llvm::CallingConv::ID abiCC = getABIDefaultCC();
3180 if (abiCC != getLLVMDefaultCC())
3181 RuntimeCC = abiCC;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003182}
3183
Bob Wilson194f06a2011-08-03 05:58:22 +00003184/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
3185/// aggregate. If HAMembers is non-null, the number of base elements
3186/// contained in the type is returned through it; this is used for the
3187/// recursive calls that check aggregate component types.
3188static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
3189 ASTContext &Context,
3190 uint64_t *HAMembers = 0) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003191 uint64_t Members = 0;
Bob Wilson194f06a2011-08-03 05:58:22 +00003192 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
3193 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
3194 return false;
3195 Members *= AT->getSize().getZExtValue();
3196 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
3197 const RecordDecl *RD = RT->getDecl();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003198 if (RD->hasFlexibleArrayMember())
Bob Wilson194f06a2011-08-03 05:58:22 +00003199 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003200
Bob Wilson194f06a2011-08-03 05:58:22 +00003201 Members = 0;
3202 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3203 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00003204 const FieldDecl *FD = *i;
Bob Wilson194f06a2011-08-03 05:58:22 +00003205 uint64_t FldMembers;
3206 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
3207 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003208
3209 Members = (RD->isUnion() ?
3210 std::max(Members, FldMembers) : Members + FldMembers);
Bob Wilson194f06a2011-08-03 05:58:22 +00003211 }
3212 } else {
3213 Members = 1;
3214 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
3215 Members = 2;
3216 Ty = CT->getElementType();
3217 }
3218
3219 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
3220 // double, or 64-bit or 128-bit vectors.
3221 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3222 if (BT->getKind() != BuiltinType::Float &&
Tim Northoveradfa45f2012-07-20 22:29:29 +00003223 BT->getKind() != BuiltinType::Double &&
3224 BT->getKind() != BuiltinType::LongDouble)
Bob Wilson194f06a2011-08-03 05:58:22 +00003225 return false;
3226 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
3227 unsigned VecSize = Context.getTypeSize(VT);
3228 if (VecSize != 64 && VecSize != 128)
3229 return false;
3230 } else {
3231 return false;
3232 }
3233
3234 // The base type must be the same for all members. Vector types of the
3235 // same total size are treated as being equivalent here.
3236 const Type *TyPtr = Ty.getTypePtr();
3237 if (!Base)
3238 Base = TyPtr;
3239 if (Base != TyPtr &&
3240 (!Base->isVectorType() || !TyPtr->isVectorType() ||
3241 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
3242 return false;
3243 }
3244
3245 // Homogeneous Aggregates can have at most 4 members of the base type.
3246 if (HAMembers)
3247 *HAMembers = Members;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003248
3249 return (Members > 0 && Members <= 4);
Bob Wilson194f06a2011-08-03 05:58:22 +00003250}
3251
Manman Ren710c5172012-10-31 19:02:26 +00003252/// markAllocatedVFPs - update VFPRegs according to the alignment and
3253/// number of VFP registers (unit is S register) requested.
3254static void markAllocatedVFPs(int *VFPRegs, unsigned &AllocatedVFP,
3255 unsigned Alignment,
3256 unsigned NumRequired) {
3257 // Early Exit.
3258 if (AllocatedVFP >= 16)
3259 return;
3260 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
3261 // VFP registers of the appropriate type unallocated then the argument is
3262 // allocated to the lowest-numbered sequence of such registers.
3263 for (unsigned I = 0; I < 16; I += Alignment) {
3264 bool FoundSlot = true;
3265 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3266 if (J >= 16 || VFPRegs[J]) {
3267 FoundSlot = false;
3268 break;
3269 }
3270 if (FoundSlot) {
3271 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3272 VFPRegs[J] = 1;
3273 AllocatedVFP += NumRequired;
3274 return;
3275 }
3276 }
3277 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
3278 // unallocated are marked as unavailable.
3279 for (unsigned I = 0; I < 16; I++)
3280 VFPRegs[I] = 1;
3281 AllocatedVFP = 17; // We do not have enough VFP registers.
3282}
3283
3284ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, int *VFPRegs,
3285 unsigned &AllocatedVFP,
Manman Renb3fa55f2012-10-30 23:21:41 +00003286 bool &IsHA) const {
3287 // We update number of allocated VFPs according to
3288 // 6.1.2.1 The following argument types are VFP CPRCs:
3289 // A single-precision floating-point type (including promoted
3290 // half-precision types); A double-precision floating-point type;
3291 // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
3292 // with a Base Type of a single- or double-precision floating-point type,
3293 // 64-bit containerized vectors or 128-bit containerized vectors with one
3294 // to four Elements.
3295
Manman Ren97f81572012-10-16 19:18:39 +00003296 // Handle illegal vector types here.
3297 if (isIllegalVectorType(Ty)) {
3298 uint64_t Size = getContext().getTypeSize(Ty);
3299 if (Size <= 32) {
3300 llvm::Type *ResType =
3301 llvm::Type::getInt32Ty(getVMContext());
3302 return ABIArgInfo::getDirect(ResType);
3303 }
3304 if (Size == 64) {
3305 llvm::Type *ResType = llvm::VectorType::get(
3306 llvm::Type::getInt32Ty(getVMContext()), 2);
Manman Ren710c5172012-10-31 19:02:26 +00003307 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
Manman Ren97f81572012-10-16 19:18:39 +00003308 return ABIArgInfo::getDirect(ResType);
3309 }
3310 if (Size == 128) {
3311 llvm::Type *ResType = llvm::VectorType::get(
3312 llvm::Type::getInt32Ty(getVMContext()), 4);
Manman Ren710c5172012-10-31 19:02:26 +00003313 markAllocatedVFPs(VFPRegs, AllocatedVFP, 4, 4);
Manman Ren97f81572012-10-16 19:18:39 +00003314 return ABIArgInfo::getDirect(ResType);
3315 }
3316 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3317 }
Manman Ren710c5172012-10-31 19:02:26 +00003318 // Update VFPRegs for legal vector types.
Manman Renb3fa55f2012-10-30 23:21:41 +00003319 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3320 uint64_t Size = getContext().getTypeSize(VT);
3321 // Size of a legal vector should be power of 2 and above 64.
Manman Ren710c5172012-10-31 19:02:26 +00003322 markAllocatedVFPs(VFPRegs, AllocatedVFP, Size >= 128 ? 4 : 2, Size / 32);
Manman Renb3fa55f2012-10-30 23:21:41 +00003323 }
Manman Ren710c5172012-10-31 19:02:26 +00003324 // Update VFPRegs for floating point types.
Manman Renb3fa55f2012-10-30 23:21:41 +00003325 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3326 if (BT->getKind() == BuiltinType::Half ||
3327 BT->getKind() == BuiltinType::Float)
Manman Ren710c5172012-10-31 19:02:26 +00003328 markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, 1);
Manman Renb3fa55f2012-10-30 23:21:41 +00003329 if (BT->getKind() == BuiltinType::Double ||
Manman Ren710c5172012-10-31 19:02:26 +00003330 BT->getKind() == BuiltinType::LongDouble)
3331 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
Manman Renb3fa55f2012-10-30 23:21:41 +00003332 }
Manman Ren97f81572012-10-16 19:18:39 +00003333
John McCalld608cdb2010-08-22 10:59:02 +00003334 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003335 // Treat an enum type as its underlying type.
3336 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3337 Ty = EnumTy->getDecl()->getIntegerType();
3338
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003339 return (Ty->isPromotableIntegerType() ?
3340 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003341 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003342
Tim Northoverf5c3a252013-06-21 22:49:34 +00003343 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
3344 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
3345
Daniel Dunbar42025572009-09-14 21:54:03 +00003346 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003347 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00003348 return ABIArgInfo::getIgnore();
3349
Bob Wilson194f06a2011-08-03 05:58:22 +00003350 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
Manman Renb3fa55f2012-10-30 23:21:41 +00003351 // Homogeneous Aggregates need to be expanded when we can fit the aggregate
3352 // into VFP registers.
Bob Wilson194f06a2011-08-03 05:58:22 +00003353 const Type *Base = 0;
Manman Renb3fa55f2012-10-30 23:21:41 +00003354 uint64_t Members = 0;
3355 if (isHomogeneousAggregate(Ty, Base, getContext(), &Members)) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003356 assert(Base && "Base class should be set for homogeneous aggregate");
Manman Renb3fa55f2012-10-30 23:21:41 +00003357 // Base can be a floating-point or a vector.
3358 if (Base->isVectorType()) {
3359 // ElementSize is in number of floats.
3360 unsigned ElementSize = getContext().getTypeSize(Base) == 64 ? 2 : 4;
Manman Rencb489dd2012-11-06 19:05:29 +00003361 markAllocatedVFPs(VFPRegs, AllocatedVFP, ElementSize,
3362 Members * ElementSize);
Manman Renb3fa55f2012-10-30 23:21:41 +00003363 } else if (Base->isSpecificBuiltinType(BuiltinType::Float))
Manman Ren710c5172012-10-31 19:02:26 +00003364 markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, Members);
Manman Renb3fa55f2012-10-30 23:21:41 +00003365 else {
3366 assert(Base->isSpecificBuiltinType(BuiltinType::Double) ||
3367 Base->isSpecificBuiltinType(BuiltinType::LongDouble));
Manman Ren710c5172012-10-31 19:02:26 +00003368 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, Members * 2);
Manman Renb3fa55f2012-10-30 23:21:41 +00003369 }
3370 IsHA = true;
Bob Wilson194f06a2011-08-03 05:58:22 +00003371 return ABIArgInfo::getExpand();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003372 }
Bob Wilson194f06a2011-08-03 05:58:22 +00003373 }
3374
Manman Ren634b3d22012-08-13 21:23:55 +00003375 // Support byval for ARM.
Manman Rencb489dd2012-11-06 19:05:29 +00003376 // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
3377 // most 8-byte. We realign the indirect argument if type alignment is bigger
3378 // than ABI alignment.
Manman Renfd1ba912012-11-05 22:42:46 +00003379 uint64_t ABIAlign = 4;
3380 uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8;
3381 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3382 getABIKind() == ARMABIInfo::AAPCS)
3383 ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
Manman Ren885ad692012-11-06 04:58:01 +00003384 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
3385 return ABIArgInfo::getIndirect(0, /*ByVal=*/true,
Manman Rencb489dd2012-11-06 19:05:29 +00003386 /*Realign=*/TyAlign > ABIAlign);
Eli Friedman79f30982012-08-09 00:31:40 +00003387 }
3388
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00003389 // Otherwise, pass by coercing to a structure of the appropriate size.
Chris Lattner2acc6e32011-07-18 04:24:23 +00003390 llvm::Type* ElemTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003391 unsigned SizeRegs;
Eli Friedman79f30982012-08-09 00:31:40 +00003392 // FIXME: Try to match the types of the arguments more accurately where
3393 // we can.
3394 if (getContext().getTypeAlign(Ty) <= 32) {
Bob Wilson53fc1a62011-08-01 23:39:04 +00003395 ElemTy = llvm::Type::getInt32Ty(getVMContext());
3396 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Manman Ren78eb76e2012-06-25 22:04:00 +00003397 } else {
Manman Ren78eb76e2012-06-25 22:04:00 +00003398 ElemTy = llvm::Type::getInt64Ty(getVMContext());
3399 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastings67d097e2011-04-27 17:24:02 +00003400 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003401
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003402 llvm::Type *STy =
Chris Lattner7650d952011-06-18 22:49:11 +00003403 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003404 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003405}
3406
Chris Lattnera3c109b2010-07-29 02:16:43 +00003407static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00003408 llvm::LLVMContext &VMContext) {
3409 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
3410 // is called integer-like if its size is less than or equal to one word, and
3411 // the offset of each of its addressable sub-fields is zero.
3412
3413 uint64_t Size = Context.getTypeSize(Ty);
3414
3415 // Check that the type fits in a word.
3416 if (Size > 32)
3417 return false;
3418
3419 // FIXME: Handle vector types!
3420 if (Ty->isVectorType())
3421 return false;
3422
Daniel Dunbarb0d58192009-09-14 02:20:34 +00003423 // Float types are never treated as "integer like".
3424 if (Ty->isRealFloatingType())
3425 return false;
3426
Daniel Dunbar98303b92009-09-13 08:03:58 +00003427 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00003428 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00003429 return true;
3430
Daniel Dunbar45815812010-02-01 23:31:26 +00003431 // Small complex integer types are "integer like".
3432 if (const ComplexType *CT = Ty->getAs<ComplexType>())
3433 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003434
3435 // Single element and zero sized arrays should be allowed, by the definition
3436 // above, but they are not.
3437
3438 // Otherwise, it must be a record type.
3439 const RecordType *RT = Ty->getAs<RecordType>();
3440 if (!RT) return false;
3441
3442 // Ignore records with flexible arrays.
3443 const RecordDecl *RD = RT->getDecl();
3444 if (RD->hasFlexibleArrayMember())
3445 return false;
3446
3447 // Check that all sub-fields are at offset 0, and are themselves "integer
3448 // like".
3449 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3450
3451 bool HadField = false;
3452 unsigned idx = 0;
3453 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3454 i != e; ++i, ++idx) {
David Blaikie581deb32012-06-06 20:45:41 +00003455 const FieldDecl *FD = *i;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003456
Daniel Dunbar679855a2010-01-29 03:22:29 +00003457 // Bit-fields are not addressable, we only need to verify they are "integer
3458 // like". We still have to disallow a subsequent non-bitfield, for example:
3459 // struct { int : 0; int x }
3460 // is non-integer like according to gcc.
3461 if (FD->isBitField()) {
3462 if (!RD->isUnion())
3463 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003464
Daniel Dunbar679855a2010-01-29 03:22:29 +00003465 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3466 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003467
Daniel Dunbar679855a2010-01-29 03:22:29 +00003468 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003469 }
3470
Daniel Dunbar679855a2010-01-29 03:22:29 +00003471 // Check if this field is at offset 0.
3472 if (Layout.getFieldOffset(idx) != 0)
3473 return false;
3474
Daniel Dunbar98303b92009-09-13 08:03:58 +00003475 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3476 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003477
Daniel Dunbar679855a2010-01-29 03:22:29 +00003478 // Only allow at most one field in a structure. This doesn't match the
3479 // wording above, but follows gcc in situations with a field following an
3480 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00003481 if (!RD->isUnion()) {
3482 if (HadField)
3483 return false;
3484
3485 HadField = true;
3486 }
3487 }
3488
3489 return true;
3490}
3491
Chris Lattnera3c109b2010-07-29 02:16:43 +00003492ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003493 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003494 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00003495
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00003496 // Large vector types should be returned via memory.
3497 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
3498 return ABIArgInfo::getIndirect(0);
3499
John McCalld608cdb2010-08-22 10:59:02 +00003500 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003501 // Treat an enum type as its underlying type.
3502 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3503 RetTy = EnumTy->getDecl()->getIntegerType();
3504
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003505 return (RetTy->isPromotableIntegerType() ?
3506 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003507 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003508
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003509 // Structures with either a non-trivial destructor or a non-trivial
3510 // copy constructor are always indirect.
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00003511 if (isRecordReturnIndirect(RetTy, CGT))
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003512 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3513
Daniel Dunbar98303b92009-09-13 08:03:58 +00003514 // Are we following APCS?
3515 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00003516 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00003517 return ABIArgInfo::getIgnore();
3518
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003519 // Complex types are all returned as packed integers.
3520 //
3521 // FIXME: Consider using 2 x vector types if the back end handles them
3522 // correctly.
3523 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00003524 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00003525 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003526
Daniel Dunbar98303b92009-09-13 08:03:58 +00003527 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003528 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003529 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003530 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003531 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003532 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003533 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003534 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3535 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003536 }
3537
3538 // Otherwise return in memory.
3539 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003540 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003541
3542 // Otherwise this is an AAPCS variant.
3543
Chris Lattnera3c109b2010-07-29 02:16:43 +00003544 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00003545 return ABIArgInfo::getIgnore();
3546
Bob Wilson3b694fa2011-11-02 04:51:36 +00003547 // Check for homogeneous aggregates with AAPCS-VFP.
3548 if (getABIKind() == AAPCS_VFP) {
3549 const Type *Base = 0;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003550 if (isHomogeneousAggregate(RetTy, Base, getContext())) {
3551 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson3b694fa2011-11-02 04:51:36 +00003552 // Homogeneous Aggregates are returned directly.
3553 return ABIArgInfo::getDirect();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003554 }
Bob Wilson3b694fa2011-11-02 04:51:36 +00003555 }
3556
Daniel Dunbar98303b92009-09-13 08:03:58 +00003557 // Aggregates <= 4 bytes are returned in r0; other aggregates
3558 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003559 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00003560 if (Size <= 32) {
3561 // Return in the smallest viable integer type.
3562 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003563 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003564 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003565 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3566 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003567 }
3568
Daniel Dunbar98303b92009-09-13 08:03:58 +00003569 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003570}
3571
Manman Ren97f81572012-10-16 19:18:39 +00003572/// isIllegalVector - check whether Ty is an illegal vector type.
3573bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
3574 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3575 // Check whether VT is legal.
3576 unsigned NumElements = VT->getNumElements();
3577 uint64_t Size = getContext().getTypeSize(VT);
3578 // NumElements should be power of 2.
3579 if ((NumElements & (NumElements - 1)) != 0)
3580 return true;
3581 // Size should be greater than 32 bits.
3582 return Size <= 32;
3583 }
3584 return false;
3585}
3586
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003587llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00003588 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003589 llvm::Type *BP = CGF.Int8PtrTy;
3590 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003591
3592 CGBuilderTy &Builder = CGF.Builder;
Chris Lattner8b418682012-02-07 00:39:47 +00003593 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003594 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Manman Rend105e732012-10-16 19:01:37 +00003595
Tim Northover373ac0a2013-06-21 23:05:33 +00003596 if (isEmptyRecord(getContext(), Ty, true)) {
3597 // These are ignored for parameter passing purposes.
3598 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3599 return Builder.CreateBitCast(Addr, PTy);
3600 }
3601
Manman Rend105e732012-10-16 19:01:37 +00003602 uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8;
Rafael Espindolae164c182011-08-02 22:33:37 +00003603 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
Manman Ren97f81572012-10-16 19:18:39 +00003604 bool IsIndirect = false;
Manman Rend105e732012-10-16 19:01:37 +00003605
3606 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
3607 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
Manman Ren93371022012-10-16 19:51:48 +00003608 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3609 getABIKind() == ARMABIInfo::AAPCS)
3610 TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
3611 else
3612 TyAlign = 4;
Manman Ren97f81572012-10-16 19:18:39 +00003613 // Use indirect if size of the illegal vector is bigger than 16 bytes.
3614 if (isIllegalVectorType(Ty) && Size > 16) {
3615 IsIndirect = true;
3616 Size = 4;
3617 TyAlign = 4;
3618 }
Manman Rend105e732012-10-16 19:01:37 +00003619
3620 // Handle address alignment for ABI alignment > 4 bytes.
Rafael Espindolae164c182011-08-02 22:33:37 +00003621 if (TyAlign > 4) {
3622 assert((TyAlign & (TyAlign - 1)) == 0 &&
3623 "Alignment is not power of 2!");
3624 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3625 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
3626 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
Manman Rend105e732012-10-16 19:01:37 +00003627 Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align");
Rafael Espindolae164c182011-08-02 22:33:37 +00003628 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003629
3630 uint64_t Offset =
Manman Rend105e732012-10-16 19:01:37 +00003631 llvm::RoundUpToAlignment(Size, 4);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003632 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00003633 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003634 "ap.next");
3635 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3636
Manman Ren97f81572012-10-16 19:18:39 +00003637 if (IsIndirect)
3638 Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP));
Manman Ren93371022012-10-16 19:51:48 +00003639 else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) {
Manman Rend105e732012-10-16 19:01:37 +00003640 // We can't directly cast ap.cur to pointer to a vector type, since ap.cur
3641 // may not be correctly aligned for the vector type. We create an aligned
3642 // temporary space and copy the content over from ap.cur to the temporary
3643 // space. This is necessary if the natural alignment of the type is greater
3644 // than the ABI alignment.
3645 llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
3646 CharUnits CharSize = getContext().getTypeSizeInChars(Ty);
3647 llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty),
3648 "var.align");
3649 llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
3650 llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy);
3651 Builder.CreateMemCpy(Dst, Src,
3652 llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()),
3653 TyAlign, false);
3654 Addr = AlignedTemp; //The content is in aligned location.
3655 }
3656 llvm::Type *PTy =
3657 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3658 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3659
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003660 return AddrTyped;
3661}
3662
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00003663namespace {
3664
Derek Schuff263366f2012-10-16 22:30:41 +00003665class NaClARMABIInfo : public ABIInfo {
3666 public:
3667 NaClARMABIInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3668 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, Kind) {}
3669 virtual void computeInfo(CGFunctionInfo &FI) const;
3670 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3671 CodeGenFunction &CGF) const;
3672 private:
3673 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
3674 ARMABIInfo NInfo; // Used for everything else.
3675};
3676
3677class NaClARMTargetCodeGenInfo : public TargetCodeGenInfo {
3678 public:
3679 NaClARMTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3680 : TargetCodeGenInfo(new NaClARMABIInfo(CGT, Kind)) {}
3681};
3682
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00003683}
3684
Derek Schuff263366f2012-10-16 22:30:41 +00003685void NaClARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
3686 if (FI.getASTCallingConvention() == CC_PnaclCall)
3687 PInfo.computeInfo(FI);
3688 else
3689 static_cast<const ABIInfo&>(NInfo).computeInfo(FI);
3690}
3691
3692llvm::Value *NaClARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3693 CodeGenFunction &CGF) const {
3694 // Always use the native convention; calling pnacl-style varargs functions
3695 // is unsupported.
3696 return static_cast<const ABIInfo&>(NInfo).EmitVAArg(VAListAddr, Ty, CGF);
3697}
3698
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003699//===----------------------------------------------------------------------===//
Tim Northoverc264e162013-01-31 12:13:10 +00003700// AArch64 ABI Implementation
3701//===----------------------------------------------------------------------===//
3702
3703namespace {
3704
3705class AArch64ABIInfo : public ABIInfo {
3706public:
3707 AArch64ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3708
3709private:
3710 // The AArch64 PCS is explicit about return types and argument types being
3711 // handled identically, so we don't need to draw a distinction between
3712 // Argument and Return classification.
3713 ABIArgInfo classifyGenericType(QualType Ty, int &FreeIntRegs,
3714 int &FreeVFPRegs) const;
3715
3716 ABIArgInfo tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded, bool IsInt,
3717 llvm::Type *DirectTy = 0) const;
3718
3719 virtual void computeInfo(CGFunctionInfo &FI) const;
3720
3721 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3722 CodeGenFunction &CGF) const;
3723};
3724
3725class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
3726public:
3727 AArch64TargetCodeGenInfo(CodeGenTypes &CGT)
3728 :TargetCodeGenInfo(new AArch64ABIInfo(CGT)) {}
3729
3730 const AArch64ABIInfo &getABIInfo() const {
3731 return static_cast<const AArch64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
3732 }
3733
3734 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3735 return 31;
3736 }
3737
3738 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3739 llvm::Value *Address) const {
3740 // 0-31 are x0-x30 and sp: 8 bytes each
3741 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
3742 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 31);
3743
3744 // 64-95 are v0-v31: 16 bytes each
3745 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
3746 AssignToArrayRange(CGF.Builder, Address, Sixteen8, 64, 95);
3747
3748 return false;
3749 }
3750
3751};
3752
3753}
3754
3755void AArch64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
3756 int FreeIntRegs = 8, FreeVFPRegs = 8;
3757
3758 FI.getReturnInfo() = classifyGenericType(FI.getReturnType(),
3759 FreeIntRegs, FreeVFPRegs);
3760
3761 FreeIntRegs = FreeVFPRegs = 8;
3762 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3763 it != ie; ++it) {
3764 it->info = classifyGenericType(it->type, FreeIntRegs, FreeVFPRegs);
3765
3766 }
3767}
3768
3769ABIArgInfo
3770AArch64ABIInfo::tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded,
3771 bool IsInt, llvm::Type *DirectTy) const {
3772 if (FreeRegs >= RegsNeeded) {
3773 FreeRegs -= RegsNeeded;
3774 return ABIArgInfo::getDirect(DirectTy);
3775 }
3776
3777 llvm::Type *Padding = 0;
3778
3779 // We need padding so that later arguments don't get filled in anyway. That
3780 // wouldn't happen if only ByVal arguments followed in the same category, but
3781 // a large structure will simply seem to be a pointer as far as LLVM is
3782 // concerned.
3783 if (FreeRegs > 0) {
3784 if (IsInt)
3785 Padding = llvm::Type::getInt64Ty(getVMContext());
3786 else
3787 Padding = llvm::Type::getFloatTy(getVMContext());
3788
3789 // Either [N x i64] or [N x float].
3790 Padding = llvm::ArrayType::get(Padding, FreeRegs);
3791 FreeRegs = 0;
3792 }
3793
3794 return ABIArgInfo::getIndirect(getContext().getTypeAlign(Ty) / 8,
3795 /*IsByVal=*/ true, /*Realign=*/ false,
3796 Padding);
3797}
3798
3799
3800ABIArgInfo AArch64ABIInfo::classifyGenericType(QualType Ty,
3801 int &FreeIntRegs,
3802 int &FreeVFPRegs) const {
3803 // Can only occurs for return, but harmless otherwise.
3804 if (Ty->isVoidType())
3805 return ABIArgInfo::getIgnore();
3806
3807 // Large vector types should be returned via memory. There's no such concept
3808 // in the ABI, but they'd be over 16 bytes anyway so no matter how they're
3809 // classified they'd go into memory (see B.3).
3810 if (Ty->isVectorType() && getContext().getTypeSize(Ty) > 128) {
3811 if (FreeIntRegs > 0)
3812 --FreeIntRegs;
3813 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3814 }
3815
3816 // All non-aggregate LLVM types have a concrete ABI representation so they can
3817 // be passed directly. After this block we're guaranteed to be in a
3818 // complicated case.
3819 if (!isAggregateTypeForABI(Ty)) {
3820 // Treat an enum type as its underlying type.
3821 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3822 Ty = EnumTy->getDecl()->getIntegerType();
3823
3824 if (Ty->isFloatingType() || Ty->isVectorType())
3825 return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ false);
3826
3827 assert(getContext().getTypeSize(Ty) <= 128 &&
3828 "unexpectedly large scalar type");
3829
3830 int RegsNeeded = getContext().getTypeSize(Ty) > 64 ? 2 : 1;
3831
3832 // If the type may need padding registers to ensure "alignment", we must be
3833 // careful when this is accounted for. Increasing the effective size covers
3834 // all cases.
3835 if (getContext().getTypeAlign(Ty) == 128)
3836 RegsNeeded += FreeIntRegs % 2 != 0;
3837
3838 return tryUseRegs(Ty, FreeIntRegs, RegsNeeded, /*IsInt=*/ true);
3839 }
3840
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00003841 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) {
3842 if (FreeIntRegs > 0 && RAA == CGCXXABI::RAA_Indirect)
Tim Northoverc264e162013-01-31 12:13:10 +00003843 --FreeIntRegs;
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00003844 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Tim Northoverc264e162013-01-31 12:13:10 +00003845 }
3846
3847 if (isEmptyRecord(getContext(), Ty, true)) {
3848 if (!getContext().getLangOpts().CPlusPlus) {
3849 // Empty structs outside C++ mode are a GNU extension, so no ABI can
3850 // possibly tell us what to do. It turns out (I believe) that GCC ignores
3851 // the object for parameter-passsing purposes.
3852 return ABIArgInfo::getIgnore();
3853 }
3854
3855 // The combination of C++98 9p5 (sizeof(struct) != 0) and the pseudocode
3856 // description of va_arg in the PCS require that an empty struct does
3857 // actually occupy space for parameter-passing. I'm hoping for a
3858 // clarification giving an explicit paragraph to point to in future.
3859 return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ true,
3860 llvm::Type::getInt8Ty(getVMContext()));
3861 }
3862
3863 // Homogeneous vector aggregates get passed in registers or on the stack.
3864 const Type *Base = 0;
3865 uint64_t NumMembers = 0;
3866 if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)) {
3867 assert(Base && "Base class should be set for homogeneous aggregate");
3868 // Homogeneous aggregates are passed and returned directly.
3869 return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ NumMembers,
3870 /*IsInt=*/ false);
3871 }
3872
3873 uint64_t Size = getContext().getTypeSize(Ty);
3874 if (Size <= 128) {
3875 // Small structs can use the same direct type whether they're in registers
3876 // or on the stack.
3877 llvm::Type *BaseTy;
3878 unsigned NumBases;
3879 int SizeInRegs = (Size + 63) / 64;
3880
3881 if (getContext().getTypeAlign(Ty) == 128) {
3882 BaseTy = llvm::Type::getIntNTy(getVMContext(), 128);
3883 NumBases = 1;
3884
3885 // If the type may need padding registers to ensure "alignment", we must
3886 // be careful when this is accounted for. Increasing the effective size
3887 // covers all cases.
3888 SizeInRegs += FreeIntRegs % 2 != 0;
3889 } else {
3890 BaseTy = llvm::Type::getInt64Ty(getVMContext());
3891 NumBases = SizeInRegs;
3892 }
3893 llvm::Type *DirectTy = llvm::ArrayType::get(BaseTy, NumBases);
3894
3895 return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ SizeInRegs,
3896 /*IsInt=*/ true, DirectTy);
3897 }
3898
3899 // If the aggregate is > 16 bytes, it's passed and returned indirectly. In
3900 // LLVM terms the return uses an "sret" pointer, but that's handled elsewhere.
3901 --FreeIntRegs;
3902 return ABIArgInfo::getIndirect(0, /* byVal = */ false);
3903}
3904
3905llvm::Value *AArch64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3906 CodeGenFunction &CGF) const {
3907 // The AArch64 va_list type and handling is specified in the Procedure Call
3908 // Standard, section B.4:
3909 //
3910 // struct {
3911 // void *__stack;
3912 // void *__gr_top;
3913 // void *__vr_top;
3914 // int __gr_offs;
3915 // int __vr_offs;
3916 // };
3917
3918 assert(!CGF.CGM.getDataLayout().isBigEndian()
3919 && "va_arg not implemented for big-endian AArch64");
3920
3921 int FreeIntRegs = 8, FreeVFPRegs = 8;
3922 Ty = CGF.getContext().getCanonicalType(Ty);
3923 ABIArgInfo AI = classifyGenericType(Ty, FreeIntRegs, FreeVFPRegs);
3924
3925 llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
3926 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
3927 llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
3928 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
3929
3930 llvm::Value *reg_offs_p = 0, *reg_offs = 0;
3931 int reg_top_index;
3932 int RegSize;
3933 if (FreeIntRegs < 8) {
3934 assert(FreeVFPRegs == 8 && "Arguments never split between int & VFP regs");
3935 // 3 is the field number of __gr_offs
3936 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p");
3937 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
3938 reg_top_index = 1; // field number for __gr_top
3939 RegSize = 8 * (8 - FreeIntRegs);
3940 } else {
3941 assert(FreeVFPRegs < 8 && "Argument must go in VFP or int regs");
3942 // 4 is the field number of __vr_offs.
3943 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p");
3944 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
3945 reg_top_index = 2; // field number for __vr_top
3946 RegSize = 16 * (8 - FreeVFPRegs);
3947 }
3948
3949 //=======================================
3950 // Find out where argument was passed
3951 //=======================================
3952
3953 // If reg_offs >= 0 we're already using the stack for this type of
3954 // argument. We don't want to keep updating reg_offs (in case it overflows,
3955 // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
3956 // whatever they get).
3957 llvm::Value *UsingStack = 0;
3958 UsingStack = CGF.Builder.CreateICmpSGE(reg_offs,
3959 llvm::ConstantInt::get(CGF.Int32Ty, 0));
3960
3961 CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
3962
3963 // Otherwise, at least some kind of argument could go in these registers, the
3964 // quesiton is whether this particular type is too big.
3965 CGF.EmitBlock(MaybeRegBlock);
3966
3967 // Integer arguments may need to correct register alignment (for example a
3968 // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
3969 // align __gr_offs to calculate the potential address.
3970 if (FreeIntRegs < 8 && AI.isDirect() && getContext().getTypeAlign(Ty) > 64) {
3971 int Align = getContext().getTypeAlign(Ty) / 8;
3972
3973 reg_offs = CGF.Builder.CreateAdd(reg_offs,
3974 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
3975 "align_regoffs");
3976 reg_offs = CGF.Builder.CreateAnd(reg_offs,
3977 llvm::ConstantInt::get(CGF.Int32Ty, -Align),
3978 "aligned_regoffs");
3979 }
3980
3981 // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
3982 llvm::Value *NewOffset = 0;
3983 NewOffset = CGF.Builder.CreateAdd(reg_offs,
3984 llvm::ConstantInt::get(CGF.Int32Ty, RegSize),
3985 "new_reg_offs");
3986 CGF.Builder.CreateStore(NewOffset, reg_offs_p);
3987
3988 // Now we're in a position to decide whether this argument really was in
3989 // registers or not.
3990 llvm::Value *InRegs = 0;
3991 InRegs = CGF.Builder.CreateICmpSLE(NewOffset,
3992 llvm::ConstantInt::get(CGF.Int32Ty, 0),
3993 "inreg");
3994
3995 CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
3996
3997 //=======================================
3998 // Argument was in registers
3999 //=======================================
4000
4001 // Now we emit the code for if the argument was originally passed in
4002 // registers. First start the appropriate block:
4003 CGF.EmitBlock(InRegBlock);
4004
4005 llvm::Value *reg_top_p = 0, *reg_top = 0;
4006 reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p");
4007 reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
4008 llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs);
4009 llvm::Value *RegAddr = 0;
4010 llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
4011
4012 if (!AI.isDirect()) {
4013 // If it's been passed indirectly (actually a struct), whatever we find from
4014 // stored registers or on the stack will actually be a struct **.
4015 MemTy = llvm::PointerType::getUnqual(MemTy);
4016 }
4017
4018 const Type *Base = 0;
4019 uint64_t NumMembers;
4020 if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)
4021 && NumMembers > 1) {
4022 // Homogeneous aggregates passed in registers will have their elements split
4023 // and stored 16-bytes apart regardless of size (they're notionally in qN,
4024 // qN+1, ...). We reload and store into a temporary local variable
4025 // contiguously.
4026 assert(AI.isDirect() && "Homogeneous aggregates should be passed directly");
4027 llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
4028 llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
4029 llvm::Value *Tmp = CGF.CreateTempAlloca(HFATy);
4030
4031 for (unsigned i = 0; i < NumMembers; ++i) {
4032 llvm::Value *BaseOffset = llvm::ConstantInt::get(CGF.Int32Ty, 16 * i);
4033 llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset);
4034 LoadAddr = CGF.Builder.CreateBitCast(LoadAddr,
4035 llvm::PointerType::getUnqual(BaseTy));
4036 llvm::Value *StoreAddr = CGF.Builder.CreateStructGEP(Tmp, i);
4037
4038 llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
4039 CGF.Builder.CreateStore(Elem, StoreAddr);
4040 }
4041
4042 RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy);
4043 } else {
4044 // Otherwise the object is contiguous in memory
4045 RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy);
4046 }
4047
4048 CGF.EmitBranch(ContBlock);
4049
4050 //=======================================
4051 // Argument was on the stack
4052 //=======================================
4053 CGF.EmitBlock(OnStackBlock);
4054
4055 llvm::Value *stack_p = 0, *OnStackAddr = 0;
4056 stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p");
4057 OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack");
4058
4059 // Again, stack arguments may need realigmnent. In this case both integer and
4060 // floating-point ones might be affected.
4061 if (AI.isDirect() && getContext().getTypeAlign(Ty) > 64) {
4062 int Align = getContext().getTypeAlign(Ty) / 8;
4063
4064 OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty);
4065
4066 OnStackAddr = CGF.Builder.CreateAdd(OnStackAddr,
4067 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
4068 "align_stack");
4069 OnStackAddr = CGF.Builder.CreateAnd(OnStackAddr,
4070 llvm::ConstantInt::get(CGF.Int64Ty, -Align),
4071 "align_stack");
4072
4073 OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy);
4074 }
4075
4076 uint64_t StackSize;
4077 if (AI.isDirect())
4078 StackSize = getContext().getTypeSize(Ty) / 8;
4079 else
4080 StackSize = 8;
4081
4082 // All stack slots are 8 bytes
4083 StackSize = llvm::RoundUpToAlignment(StackSize, 8);
4084
4085 llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize);
4086 llvm::Value *NewStack = CGF.Builder.CreateGEP(OnStackAddr, StackSizeC,
4087 "new_stack");
4088
4089 // Write the new value of __stack for the next call to va_arg
4090 CGF.Builder.CreateStore(NewStack, stack_p);
4091
4092 OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy);
4093
4094 CGF.EmitBranch(ContBlock);
4095
4096 //=======================================
4097 // Tidy up
4098 //=======================================
4099 CGF.EmitBlock(ContBlock);
4100
4101 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr");
4102 ResAddr->addIncoming(RegAddr, InRegBlock);
4103 ResAddr->addIncoming(OnStackAddr, OnStackBlock);
4104
4105 if (AI.isDirect())
4106 return ResAddr;
4107
4108 return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr");
4109}
4110
4111//===----------------------------------------------------------------------===//
Justin Holewinski2c585b92012-05-24 17:43:12 +00004112// NVPTX ABI Implementation
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004113//===----------------------------------------------------------------------===//
4114
4115namespace {
4116
Justin Holewinski2c585b92012-05-24 17:43:12 +00004117class NVPTXABIInfo : public ABIInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004118public:
Justin Holewinskidca8f332013-03-30 14:38:24 +00004119 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004120
4121 ABIArgInfo classifyReturnType(QualType RetTy) const;
4122 ABIArgInfo classifyArgumentType(QualType Ty) const;
4123
4124 virtual void computeInfo(CGFunctionInfo &FI) const;
4125 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4126 CodeGenFunction &CFG) const;
4127};
4128
Justin Holewinski2c585b92012-05-24 17:43:12 +00004129class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004130public:
Justin Holewinski2c585b92012-05-24 17:43:12 +00004131 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
4132 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
Justin Holewinski818eafb2011-10-05 17:58:44 +00004133
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004134 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4135 CodeGen::CodeGenModule &M) const;
Justin Holewinskidca8f332013-03-30 14:38:24 +00004136private:
4137 static void addKernelMetadata(llvm::Function *F);
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004138};
4139
Justin Holewinski2c585b92012-05-24 17:43:12 +00004140ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004141 if (RetTy->isVoidType())
4142 return ABIArgInfo::getIgnore();
4143 if (isAggregateTypeForABI(RetTy))
4144 return ABIArgInfo::getIndirect(0);
4145 return ABIArgInfo::getDirect();
4146}
4147
Justin Holewinski2c585b92012-05-24 17:43:12 +00004148ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004149 if (isAggregateTypeForABI(Ty))
4150 return ABIArgInfo::getIndirect(0);
4151
4152 return ABIArgInfo::getDirect();
4153}
4154
Justin Holewinski2c585b92012-05-24 17:43:12 +00004155void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004156 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4157 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4158 it != ie; ++it)
4159 it->info = classifyArgumentType(it->type);
4160
4161 // Always honor user-specified calling convention.
4162 if (FI.getCallingConvention() != llvm::CallingConv::C)
4163 return;
4164
John McCallbd7370a2013-02-28 19:01:20 +00004165 FI.setEffectiveCallingConvention(getRuntimeCC());
4166}
4167
Justin Holewinski2c585b92012-05-24 17:43:12 +00004168llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4169 CodeGenFunction &CFG) const {
4170 llvm_unreachable("NVPTX does not support varargs");
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004171}
4172
Justin Holewinski2c585b92012-05-24 17:43:12 +00004173void NVPTXTargetCodeGenInfo::
4174SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4175 CodeGen::CodeGenModule &M) const{
Justin Holewinski818eafb2011-10-05 17:58:44 +00004176 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4177 if (!FD) return;
4178
4179 llvm::Function *F = cast<llvm::Function>(GV);
4180
4181 // Perform special handling in OpenCL mode
David Blaikie4e4d0842012-03-11 07:00:24 +00004182 if (M.getLangOpts().OpenCL) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00004183 // Use OpenCL function attributes to check for kernel functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00004184 // By default, all functions are device functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00004185 if (FD->hasAttr<OpenCLKernelAttr>()) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00004186 // OpenCL __kernel functions get kernel metadata
4187 addKernelMetadata(F);
Justin Holewinski818eafb2011-10-05 17:58:44 +00004188 // And kernel functions are not subject to inlining
Bill Wendling72390b32012-12-20 19:27:06 +00004189 F->addFnAttr(llvm::Attribute::NoInline);
Justin Holewinski818eafb2011-10-05 17:58:44 +00004190 }
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004191 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00004192
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004193 // Perform special handling in CUDA mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00004194 if (M.getLangOpts().CUDA) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00004195 // CUDA __global__ functions get a kernel metadata entry. Since
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004196 // __global__ functions cannot be called from the device, we do not
4197 // need to set the noinline attribute.
4198 if (FD->getAttr<CUDAGlobalAttr>())
Justin Holewinskidca8f332013-03-30 14:38:24 +00004199 addKernelMetadata(F);
Justin Holewinski818eafb2011-10-05 17:58:44 +00004200 }
4201}
4202
Justin Holewinskidca8f332013-03-30 14:38:24 +00004203void NVPTXTargetCodeGenInfo::addKernelMetadata(llvm::Function *F) {
4204 llvm::Module *M = F->getParent();
4205 llvm::LLVMContext &Ctx = M->getContext();
4206
4207 // Get "nvvm.annotations" metadata node
4208 llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
4209
4210 // Create !{<func-ref>, metadata !"kernel", i32 1} node
4211 llvm::SmallVector<llvm::Value *, 3> MDVals;
4212 MDVals.push_back(F);
4213 MDVals.push_back(llvm::MDString::get(Ctx, "kernel"));
4214 MDVals.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), 1));
4215
4216 // Append metadata to nvvm.annotations
4217 MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
4218}
4219
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004220}
4221
4222//===----------------------------------------------------------------------===//
Ulrich Weigandb8409212013-05-06 16:26:41 +00004223// SystemZ ABI Implementation
4224//===----------------------------------------------------------------------===//
4225
4226namespace {
4227
4228class SystemZABIInfo : public ABIInfo {
4229public:
4230 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
4231
4232 bool isPromotableIntegerType(QualType Ty) const;
4233 bool isCompoundType(QualType Ty) const;
4234 bool isFPArgumentType(QualType Ty) const;
4235
4236 ABIArgInfo classifyReturnType(QualType RetTy) const;
4237 ABIArgInfo classifyArgumentType(QualType ArgTy) const;
4238
4239 virtual void computeInfo(CGFunctionInfo &FI) const {
4240 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4241 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4242 it != ie; ++it)
4243 it->info = classifyArgumentType(it->type);
4244 }
4245
4246 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4247 CodeGenFunction &CGF) const;
4248};
4249
4250class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
4251public:
4252 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
4253 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
4254};
4255
4256}
4257
4258bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
4259 // Treat an enum type as its underlying type.
4260 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4261 Ty = EnumTy->getDecl()->getIntegerType();
4262
4263 // Promotable integer types are required to be promoted by the ABI.
4264 if (Ty->isPromotableIntegerType())
4265 return true;
4266
4267 // 32-bit values must also be promoted.
4268 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4269 switch (BT->getKind()) {
4270 case BuiltinType::Int:
4271 case BuiltinType::UInt:
4272 return true;
4273 default:
4274 return false;
4275 }
4276 return false;
4277}
4278
4279bool SystemZABIInfo::isCompoundType(QualType Ty) const {
4280 return Ty->isAnyComplexType() || isAggregateTypeForABI(Ty);
4281}
4282
4283bool SystemZABIInfo::isFPArgumentType(QualType Ty) const {
4284 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4285 switch (BT->getKind()) {
4286 case BuiltinType::Float:
4287 case BuiltinType::Double:
4288 return true;
4289 default:
4290 return false;
4291 }
4292
4293 if (const RecordType *RT = Ty->getAsStructureType()) {
4294 const RecordDecl *RD = RT->getDecl();
4295 bool Found = false;
4296
4297 // If this is a C++ record, check the bases first.
4298 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
4299 for (CXXRecordDecl::base_class_const_iterator I = CXXRD->bases_begin(),
4300 E = CXXRD->bases_end(); I != E; ++I) {
4301 QualType Base = I->getType();
4302
4303 // Empty bases don't affect things either way.
4304 if (isEmptyRecord(getContext(), Base, true))
4305 continue;
4306
4307 if (Found)
4308 return false;
4309 Found = isFPArgumentType(Base);
4310 if (!Found)
4311 return false;
4312 }
4313
4314 // Check the fields.
4315 for (RecordDecl::field_iterator I = RD->field_begin(),
4316 E = RD->field_end(); I != E; ++I) {
4317 const FieldDecl *FD = *I;
4318
4319 // Empty bitfields don't affect things either way.
4320 // Unlike isSingleElementStruct(), empty structure and array fields
4321 // do count. So do anonymous bitfields that aren't zero-sized.
4322 if (FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
4323 return true;
4324
4325 // Unlike isSingleElementStruct(), arrays do not count.
4326 // Nested isFPArgumentType structures still do though.
4327 if (Found)
4328 return false;
4329 Found = isFPArgumentType(FD->getType());
4330 if (!Found)
4331 return false;
4332 }
4333
4334 // Unlike isSingleElementStruct(), trailing padding is allowed.
4335 // An 8-byte aligned struct s { float f; } is passed as a double.
4336 return Found;
4337 }
4338
4339 return false;
4340}
4341
4342llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4343 CodeGenFunction &CGF) const {
4344 // Assume that va_list type is correct; should be pointer to LLVM type:
4345 // struct {
4346 // i64 __gpr;
4347 // i64 __fpr;
4348 // i8 *__overflow_arg_area;
4349 // i8 *__reg_save_area;
4350 // };
4351
4352 // Every argument occupies 8 bytes and is passed by preference in either
4353 // GPRs or FPRs.
4354 Ty = CGF.getContext().getCanonicalType(Ty);
4355 ABIArgInfo AI = classifyArgumentType(Ty);
4356 bool InFPRs = isFPArgumentType(Ty);
4357
4358 llvm::Type *APTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
4359 bool IsIndirect = AI.isIndirect();
4360 unsigned UnpaddedBitSize;
4361 if (IsIndirect) {
4362 APTy = llvm::PointerType::getUnqual(APTy);
4363 UnpaddedBitSize = 64;
4364 } else
4365 UnpaddedBitSize = getContext().getTypeSize(Ty);
4366 unsigned PaddedBitSize = 64;
4367 assert((UnpaddedBitSize <= PaddedBitSize) && "Invalid argument size.");
4368
4369 unsigned PaddedSize = PaddedBitSize / 8;
4370 unsigned Padding = (PaddedBitSize - UnpaddedBitSize) / 8;
4371
4372 unsigned MaxRegs, RegCountField, RegSaveIndex, RegPadding;
4373 if (InFPRs) {
4374 MaxRegs = 4; // Maximum of 4 FPR arguments
4375 RegCountField = 1; // __fpr
4376 RegSaveIndex = 16; // save offset for f0
4377 RegPadding = 0; // floats are passed in the high bits of an FPR
4378 } else {
4379 MaxRegs = 5; // Maximum of 5 GPR arguments
4380 RegCountField = 0; // __gpr
4381 RegSaveIndex = 2; // save offset for r2
4382 RegPadding = Padding; // values are passed in the low bits of a GPR
4383 }
4384
4385 llvm::Value *RegCountPtr =
4386 CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr");
4387 llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count");
4388 llvm::Type *IndexTy = RegCount->getType();
4389 llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs);
4390 llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV,
4391 "fits_in_regs");
4392
4393 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
4394 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
4395 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
4396 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
4397
4398 // Emit code to load the value if it was passed in registers.
4399 CGF.EmitBlock(InRegBlock);
4400
4401 // Work out the address of an argument register.
4402 llvm::Value *PaddedSizeV = llvm::ConstantInt::get(IndexTy, PaddedSize);
4403 llvm::Value *ScaledRegCount =
4404 CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count");
4405 llvm::Value *RegBase =
4406 llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize + RegPadding);
4407 llvm::Value *RegOffset =
4408 CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset");
4409 llvm::Value *RegSaveAreaPtr =
4410 CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr");
4411 llvm::Value *RegSaveArea =
4412 CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area");
4413 llvm::Value *RawRegAddr =
4414 CGF.Builder.CreateGEP(RegSaveArea, RegOffset, "raw_reg_addr");
4415 llvm::Value *RegAddr =
4416 CGF.Builder.CreateBitCast(RawRegAddr, APTy, "reg_addr");
4417
4418 // Update the register count
4419 llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1);
4420 llvm::Value *NewRegCount =
4421 CGF.Builder.CreateAdd(RegCount, One, "reg_count");
4422 CGF.Builder.CreateStore(NewRegCount, RegCountPtr);
4423 CGF.EmitBranch(ContBlock);
4424
4425 // Emit code to load the value if it was passed in memory.
4426 CGF.EmitBlock(InMemBlock);
4427
4428 // Work out the address of a stack argument.
4429 llvm::Value *OverflowArgAreaPtr =
4430 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr");
4431 llvm::Value *OverflowArgArea =
4432 CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area");
4433 llvm::Value *PaddingV = llvm::ConstantInt::get(IndexTy, Padding);
4434 llvm::Value *RawMemAddr =
4435 CGF.Builder.CreateGEP(OverflowArgArea, PaddingV, "raw_mem_addr");
4436 llvm::Value *MemAddr =
4437 CGF.Builder.CreateBitCast(RawMemAddr, APTy, "mem_addr");
4438
4439 // Update overflow_arg_area_ptr pointer
4440 llvm::Value *NewOverflowArgArea =
4441 CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area");
4442 CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
4443 CGF.EmitBranch(ContBlock);
4444
4445 // Return the appropriate result.
4446 CGF.EmitBlock(ContBlock);
4447 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(APTy, 2, "va_arg.addr");
4448 ResAddr->addIncoming(RegAddr, InRegBlock);
4449 ResAddr->addIncoming(MemAddr, InMemBlock);
4450
4451 if (IsIndirect)
4452 return CGF.Builder.CreateLoad(ResAddr, "indirect_arg");
4453
4454 return ResAddr;
4455}
4456
John McCallb8b52972013-06-18 02:46:29 +00004457bool X86_32TargetCodeGenInfo::isStructReturnInRegABI(
4458 const llvm::Triple &Triple, const CodeGenOptions &Opts) {
4459 assert(Triple.getArch() == llvm::Triple::x86);
4460
4461 switch (Opts.getStructReturnConvention()) {
4462 case CodeGenOptions::SRCK_Default:
4463 break;
4464 case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return
4465 return false;
4466 case CodeGenOptions::SRCK_InRegs: // -freg-struct-return
4467 return true;
4468 }
4469
4470 if (Triple.isOSDarwin())
4471 return true;
4472
4473 switch (Triple.getOS()) {
4474 case llvm::Triple::Cygwin:
4475 case llvm::Triple::MinGW32:
4476 case llvm::Triple::AuroraUX:
4477 case llvm::Triple::DragonFly:
4478 case llvm::Triple::FreeBSD:
4479 case llvm::Triple::OpenBSD:
4480 case llvm::Triple::Bitrig:
4481 case llvm::Triple::Win32:
4482 return true;
4483 default:
4484 return false;
4485 }
4486}
Ulrich Weigandb8409212013-05-06 16:26:41 +00004487
4488ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
4489 if (RetTy->isVoidType())
4490 return ABIArgInfo::getIgnore();
4491 if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64)
4492 return ABIArgInfo::getIndirect(0);
4493 return (isPromotableIntegerType(RetTy) ?
4494 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4495}
4496
4497ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
4498 // Handle the generic C++ ABI.
4499 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
4500 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
4501
4502 // Integers and enums are extended to full register width.
4503 if (isPromotableIntegerType(Ty))
4504 return ABIArgInfo::getExtend();
4505
4506 // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly.
4507 uint64_t Size = getContext().getTypeSize(Ty);
4508 if (Size != 8 && Size != 16 && Size != 32 && Size != 64)
4509 return ABIArgInfo::getIndirect(0);
4510
4511 // Handle small structures.
4512 if (const RecordType *RT = Ty->getAs<RecordType>()) {
4513 // Structures with flexible arrays have variable length, so really
4514 // fail the size test above.
4515 const RecordDecl *RD = RT->getDecl();
4516 if (RD->hasFlexibleArrayMember())
4517 return ABIArgInfo::getIndirect(0);
4518
4519 // The structure is passed as an unextended integer, a float, or a double.
4520 llvm::Type *PassTy;
4521 if (isFPArgumentType(Ty)) {
4522 assert(Size == 32 || Size == 64);
4523 if (Size == 32)
4524 PassTy = llvm::Type::getFloatTy(getVMContext());
4525 else
4526 PassTy = llvm::Type::getDoubleTy(getVMContext());
4527 } else
4528 PassTy = llvm::IntegerType::get(getVMContext(), Size);
4529 return ABIArgInfo::getDirect(PassTy);
4530 }
4531
4532 // Non-structure compounds are passed indirectly.
4533 if (isCompoundType(Ty))
4534 return ABIArgInfo::getIndirect(0);
4535
4536 return ABIArgInfo::getDirect(0);
4537}
4538
4539//===----------------------------------------------------------------------===//
Wesley Peck276fdf42010-12-19 19:57:51 +00004540// MBlaze ABI Implementation
4541//===----------------------------------------------------------------------===//
4542
4543namespace {
4544
4545class MBlazeABIInfo : public ABIInfo {
4546public:
4547 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
4548
4549 bool isPromotableIntegerType(QualType Ty) const;
4550
4551 ABIArgInfo classifyReturnType(QualType RetTy) const;
4552 ABIArgInfo classifyArgumentType(QualType RetTy) const;
4553
4554 virtual void computeInfo(CGFunctionInfo &FI) const {
4555 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4556 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4557 it != ie; ++it)
4558 it->info = classifyArgumentType(it->type);
4559 }
4560
4561 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4562 CodeGenFunction &CGF) const;
4563};
4564
4565class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
4566public:
4567 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
4568 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
4569 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4570 CodeGen::CodeGenModule &M) const;
4571};
4572
4573}
4574
4575bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
4576 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
4577 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4578 switch (BT->getKind()) {
4579 case BuiltinType::Bool:
4580 case BuiltinType::Char_S:
4581 case BuiltinType::Char_U:
4582 case BuiltinType::SChar:
4583 case BuiltinType::UChar:
4584 case BuiltinType::Short:
4585 case BuiltinType::UShort:
4586 return true;
4587 default:
4588 return false;
4589 }
4590 return false;
4591}
4592
4593llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4594 CodeGenFunction &CGF) const {
4595 // FIXME: Implement
4596 return 0;
4597}
4598
4599
4600ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
4601 if (RetTy->isVoidType())
4602 return ABIArgInfo::getIgnore();
4603 if (isAggregateTypeForABI(RetTy))
4604 return ABIArgInfo::getIndirect(0);
4605
4606 return (isPromotableIntegerType(RetTy) ?
4607 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4608}
4609
4610ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
4611 if (isAggregateTypeForABI(Ty))
4612 return ABIArgInfo::getIndirect(0);
4613
4614 return (isPromotableIntegerType(Ty) ?
4615 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4616}
4617
4618void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4619 llvm::GlobalValue *GV,
4620 CodeGen::CodeGenModule &M)
4621 const {
4622 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4623 if (!FD) return;
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00004624
Wesley Peck276fdf42010-12-19 19:57:51 +00004625 llvm::CallingConv::ID CC = llvm::CallingConv::C;
4626 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
4627 CC = llvm::CallingConv::MBLAZE_INTR;
4628 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
4629 CC = llvm::CallingConv::MBLAZE_SVOL;
4630
4631 if (CC != llvm::CallingConv::C) {
4632 // Handle 'interrupt_handler' attribute:
4633 llvm::Function *F = cast<llvm::Function>(GV);
4634
4635 // Step 1: Set ISR calling convention.
4636 F->setCallingConv(CC);
4637
4638 // Step 2: Add attributes goodness.
Bill Wendling72390b32012-12-20 19:27:06 +00004639 F->addFnAttr(llvm::Attribute::NoInline);
Wesley Peck276fdf42010-12-19 19:57:51 +00004640 }
4641
4642 // Step 3: Emit _interrupt_handler alias.
4643 if (CC == llvm::CallingConv::MBLAZE_INTR)
4644 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
4645 "_interrupt_handler", GV, &M.getModule());
4646}
4647
4648
4649//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004650// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004651//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004652
4653namespace {
4654
4655class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
4656public:
Chris Lattnerea044322010-07-29 02:01:43 +00004657 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
4658 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004659 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4660 CodeGen::CodeGenModule &M) const;
4661};
4662
4663}
4664
4665void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4666 llvm::GlobalValue *GV,
4667 CodeGen::CodeGenModule &M) const {
4668 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4669 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
4670 // Handle 'interrupt' attribute:
4671 llvm::Function *F = cast<llvm::Function>(GV);
4672
4673 // Step 1: Set ISR calling convention.
4674 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
4675
4676 // Step 2: Add attributes goodness.
Bill Wendling72390b32012-12-20 19:27:06 +00004677 F->addFnAttr(llvm::Attribute::NoInline);
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004678
4679 // Step 3: Emit ISR vector alias.
Anton Korobeynikovf419a852012-11-26 18:59:10 +00004680 unsigned Num = attr->getNumber() / 2;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004681 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Anton Korobeynikovf419a852012-11-26 18:59:10 +00004682 "__isr_" + Twine(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004683 GV, &M.getModule());
4684 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004685 }
4686}
4687
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004688//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00004689// MIPS ABI Implementation. This works for both little-endian and
4690// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004691//===----------------------------------------------------------------------===//
4692
John McCallaeeb7012010-05-27 06:19:26 +00004693namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00004694class MipsABIInfo : public ABIInfo {
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004695 bool IsO32;
Akira Hatanakac359f202012-07-03 19:24:06 +00004696 unsigned MinABIStackAlignInBytes, StackAlignInBytes;
4697 void CoerceToIntArgs(uint64_t TySize,
4698 SmallVector<llvm::Type*, 8> &ArgList) const;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004699 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004700 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004701 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004702public:
Akira Hatanakab551dd32011-11-03 00:05:50 +00004703 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
Akira Hatanakac359f202012-07-03 19:24:06 +00004704 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
4705 StackAlignInBytes(IsO32 ? 8 : 16) {}
Akira Hatanaka619e8872011-06-02 00:09:17 +00004706
4707 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004708 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004709 virtual void computeInfo(CGFunctionInfo &FI) const;
4710 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4711 CodeGenFunction &CGF) const;
4712};
4713
John McCallaeeb7012010-05-27 06:19:26 +00004714class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanakae624fa02011-09-20 18:23:28 +00004715 unsigned SizeOfUnwindException;
John McCallaeeb7012010-05-27 06:19:26 +00004716public:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004717 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
4718 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
4719 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCallaeeb7012010-05-27 06:19:26 +00004720
4721 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
4722 return 29;
4723 }
4724
Reed Kotler7dfd1822013-01-16 17:10:28 +00004725 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4726 CodeGen::CodeGenModule &CGM) const {
Reed Kotlerad4b8b42013-03-13 20:40:30 +00004727 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4728 if (!FD) return;
Rafael Espindolad8e6d6d2013-03-19 14:32:23 +00004729 llvm::Function *Fn = cast<llvm::Function>(GV);
Reed Kotlerad4b8b42013-03-13 20:40:30 +00004730 if (FD->hasAttr<Mips16Attr>()) {
4731 Fn->addFnAttr("mips16");
4732 }
4733 else if (FD->hasAttr<NoMips16Attr>()) {
4734 Fn->addFnAttr("nomips16");
4735 }
Reed Kotler7dfd1822013-01-16 17:10:28 +00004736 }
Reed Kotlerad4b8b42013-03-13 20:40:30 +00004737
John McCallaeeb7012010-05-27 06:19:26 +00004738 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00004739 llvm::Value *Address) const;
John McCall49e34be2011-08-30 01:42:09 +00004740
4741 unsigned getSizeOfUnwindException() const {
Akira Hatanakae624fa02011-09-20 18:23:28 +00004742 return SizeOfUnwindException;
John McCall49e34be2011-08-30 01:42:09 +00004743 }
John McCallaeeb7012010-05-27 06:19:26 +00004744};
4745}
4746
Akira Hatanakac359f202012-07-03 19:24:06 +00004747void MipsABIInfo::CoerceToIntArgs(uint64_t TySize,
4748 SmallVector<llvm::Type*, 8> &ArgList) const {
4749 llvm::IntegerType *IntTy =
4750 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004751
4752 // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
4753 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
4754 ArgList.push_back(IntTy);
4755
4756 // If necessary, add one more integer type to ArgList.
4757 unsigned R = TySize % (MinABIStackAlignInBytes * 8);
4758
4759 if (R)
4760 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004761}
4762
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004763// In N32/64, an aligned double precision floating point field is passed in
4764// a register.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004765llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
Akira Hatanakac359f202012-07-03 19:24:06 +00004766 SmallVector<llvm::Type*, 8> ArgList, IntArgList;
4767
4768 if (IsO32) {
4769 CoerceToIntArgs(TySize, ArgList);
4770 return llvm::StructType::get(getVMContext(), ArgList);
4771 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004772
Akira Hatanaka2afd23d2012-01-12 00:52:17 +00004773 if (Ty->isComplexType())
4774 return CGT.ConvertType(Ty);
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00004775
Akira Hatanakaa34e9212012-02-09 19:54:16 +00004776 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004777
Akira Hatanakac359f202012-07-03 19:24:06 +00004778 // Unions/vectors are passed in integer registers.
4779 if (!RT || !RT->isStructureOrClassType()) {
4780 CoerceToIntArgs(TySize, ArgList);
4781 return llvm::StructType::get(getVMContext(), ArgList);
4782 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004783
4784 const RecordDecl *RD = RT->getDecl();
4785 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004786 assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004787
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004788 uint64_t LastOffset = 0;
4789 unsigned idx = 0;
4790 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
4791
Akira Hatanakaa34e9212012-02-09 19:54:16 +00004792 // Iterate over fields in the struct/class and check if there are any aligned
4793 // double fields.
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004794 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
4795 i != e; ++i, ++idx) {
David Blaikie262bc182012-04-30 02:36:29 +00004796 const QualType Ty = i->getType();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004797 const BuiltinType *BT = Ty->getAs<BuiltinType>();
4798
4799 if (!BT || BT->getKind() != BuiltinType::Double)
4800 continue;
4801
4802 uint64_t Offset = Layout.getFieldOffset(idx);
4803 if (Offset % 64) // Ignore doubles that are not aligned.
4804 continue;
4805
4806 // Add ((Offset - LastOffset) / 64) args of type i64.
4807 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
4808 ArgList.push_back(I64);
4809
4810 // Add double type.
4811 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
4812 LastOffset = Offset + 64;
4813 }
4814
Akira Hatanakac359f202012-07-03 19:24:06 +00004815 CoerceToIntArgs(TySize - LastOffset, IntArgList);
4816 ArgList.append(IntArgList.begin(), IntArgList.end());
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004817
4818 return llvm::StructType::get(getVMContext(), ArgList);
4819}
4820
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004821llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004822 assert((Offset % MinABIStackAlignInBytes) == 0);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004823
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004824 if ((Align - 1) & Offset)
4825 return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
4826
4827 return 0;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004828}
Akira Hatanaka9659d592012-01-10 22:44:52 +00004829
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004830ABIArgInfo
4831MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004832 uint64_t OrigOffset = Offset;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004833 uint64_t TySize = getContext().getTypeSize(Ty);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004834 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004835
Akira Hatanakac359f202012-07-03 19:24:06 +00004836 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
4837 (uint64_t)StackAlignInBytes);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004838 Offset = llvm::RoundUpToAlignment(Offset, Align);
4839 Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004840
Akira Hatanakac359f202012-07-03 19:24:06 +00004841 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
Akira Hatanaka619e8872011-06-02 00:09:17 +00004842 // Ignore empty aggregates.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004843 if (TySize == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00004844 return ABIArgInfo::getIgnore();
4845
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00004846 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004847 Offset = OrigOffset + MinABIStackAlignInBytes;
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00004848 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004849 }
Akira Hatanaka511949b2011-08-01 18:09:58 +00004850
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004851 // If we have reached here, aggregates are passed directly by coercing to
4852 // another structure type. Padding is inserted if the offset of the
4853 // aggregate is unaligned.
4854 return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
4855 getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00004856 }
4857
4858 // Treat an enum type as its underlying type.
4859 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4860 Ty = EnumTy->getDecl()->getIntegerType();
4861
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004862 if (Ty->isPromotableIntegerType())
4863 return ABIArgInfo::getExtend();
4864
Akira Hatanaka4055cfc2013-01-24 21:47:33 +00004865 return ABIArgInfo::getDirect(0, 0,
4866 IsO32 ? 0 : getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00004867}
4868
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004869llvm::Type*
4870MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakada54ff32012-02-09 18:49:26 +00004871 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakac359f202012-07-03 19:24:06 +00004872 SmallVector<llvm::Type*, 8> RTList;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004873
Akira Hatanakada54ff32012-02-09 18:49:26 +00004874 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004875 const RecordDecl *RD = RT->getDecl();
Akira Hatanakada54ff32012-02-09 18:49:26 +00004876 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
4877 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004878
Akira Hatanakada54ff32012-02-09 18:49:26 +00004879 // N32/64 returns struct/classes in floating point registers if the
4880 // following conditions are met:
4881 // 1. The size of the struct/class is no larger than 128-bit.
4882 // 2. The struct/class has one or two fields all of which are floating
4883 // point types.
4884 // 3. The offset of the first field is zero (this follows what gcc does).
4885 //
4886 // Any other composite results are returned in integer registers.
4887 //
4888 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
4889 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
4890 for (; b != e; ++b) {
David Blaikie262bc182012-04-30 02:36:29 +00004891 const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004892
Akira Hatanakada54ff32012-02-09 18:49:26 +00004893 if (!BT || !BT->isFloatingPoint())
4894 break;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004895
David Blaikie262bc182012-04-30 02:36:29 +00004896 RTList.push_back(CGT.ConvertType(b->getType()));
Akira Hatanakada54ff32012-02-09 18:49:26 +00004897 }
4898
4899 if (b == e)
4900 return llvm::StructType::get(getVMContext(), RTList,
4901 RD->hasAttr<PackedAttr>());
4902
4903 RTList.clear();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004904 }
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004905 }
4906
Akira Hatanakac359f202012-07-03 19:24:06 +00004907 CoerceToIntArgs(Size, RTList);
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004908 return llvm::StructType::get(getVMContext(), RTList);
4909}
4910
Akira Hatanaka619e8872011-06-02 00:09:17 +00004911ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanakaa8536c02012-01-23 23:18:57 +00004912 uint64_t Size = getContext().getTypeSize(RetTy);
4913
4914 if (RetTy->isVoidType() || Size == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00004915 return ABIArgInfo::getIgnore();
4916
Akira Hatanaka8aeb1472012-05-11 21:01:17 +00004917 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00004918 if (isRecordReturnIndirect(RetTy, CGT))
4919 return ABIArgInfo::getIndirect(0);
4920
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004921 if (Size <= 128) {
4922 if (RetTy->isAnyComplexType())
4923 return ABIArgInfo::getDirect();
4924
Akira Hatanakac359f202012-07-03 19:24:06 +00004925 // O32 returns integer vectors in registers.
4926 if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())
4927 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
4928
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00004929 if (!IsO32)
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004930 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
4931 }
Akira Hatanaka619e8872011-06-02 00:09:17 +00004932
4933 return ABIArgInfo::getIndirect(0);
4934 }
4935
4936 // Treat an enum type as its underlying type.
4937 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4938 RetTy = EnumTy->getDecl()->getIntegerType();
4939
4940 return (RetTy->isPromotableIntegerType() ?
4941 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4942}
4943
4944void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanakacc662542012-01-12 01:10:09 +00004945 ABIArgInfo &RetInfo = FI.getReturnInfo();
4946 RetInfo = classifyReturnType(FI.getReturnType());
4947
4948 // Check if a pointer to an aggregate is passed as a hidden argument.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004949 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
Akira Hatanakacc662542012-01-12 01:10:09 +00004950
Akira Hatanaka619e8872011-06-02 00:09:17 +00004951 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4952 it != ie; ++it)
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004953 it->info = classifyArgumentType(it->type, Offset);
Akira Hatanaka619e8872011-06-02 00:09:17 +00004954}
4955
4956llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4957 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00004958 llvm::Type *BP = CGF.Int8PtrTy;
4959 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004960
4961 CGBuilderTy &Builder = CGF.Builder;
4962 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
4963 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004964 int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004965 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
4966 llvm::Value *AddrTyped;
John McCall64aa4b32013-04-16 22:48:15 +00004967 unsigned PtrWidth = getTarget().getPointerWidth(0);
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004968 llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004969
4970 if (TypeAlign > MinABIStackAlignInBytes) {
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004971 llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
4972 llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
4973 llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
4974 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004975 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
4976 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
4977 }
4978 else
4979 AddrTyped = Builder.CreateBitCast(Addr, PTy);
4980
4981 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004982 TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004983 uint64_t Offset =
4984 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
4985 llvm::Value *NextAddr =
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004986 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004987 "ap.next");
4988 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
4989
4990 return AddrTyped;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004991}
4992
John McCallaeeb7012010-05-27 06:19:26 +00004993bool
4994MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4995 llvm::Value *Address) const {
4996 // This information comes from gcc's implementation, which seems to
4997 // as canonical as it gets.
4998
John McCallaeeb7012010-05-27 06:19:26 +00004999 // Everything on MIPS is 4 bytes. Double-precision FP registers
5000 // are aliased to pairs of single-precision FP registers.
Chris Lattner8b418682012-02-07 00:39:47 +00005001 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCallaeeb7012010-05-27 06:19:26 +00005002
5003 // 0-31 are the general purpose registers, $0 - $31.
5004 // 32-63 are the floating-point registers, $f0 - $f31.
5005 // 64 and 65 are the multiply/divide registers, $hi and $lo.
5006 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattner8b418682012-02-07 00:39:47 +00005007 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCallaeeb7012010-05-27 06:19:26 +00005008
5009 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
5010 // They are one bit wide and ignored here.
5011
5012 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
5013 // (coprocessor 1 is the FP unit)
5014 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
5015 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
5016 // 176-181 are the DSP accumulator registers.
Chris Lattner8b418682012-02-07 00:39:47 +00005017 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCallaeeb7012010-05-27 06:19:26 +00005018 return false;
5019}
5020
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00005021//===----------------------------------------------------------------------===//
5022// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
5023// Currently subclassed only to implement custom OpenCL C function attribute
5024// handling.
5025//===----------------------------------------------------------------------===//
5026
5027namespace {
5028
5029class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
5030public:
5031 TCETargetCodeGenInfo(CodeGenTypes &CGT)
5032 : DefaultTargetCodeGenInfo(CGT) {}
5033
5034 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
5035 CodeGen::CodeGenModule &M) const;
5036};
5037
5038void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
5039 llvm::GlobalValue *GV,
5040 CodeGen::CodeGenModule &M) const {
5041 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
5042 if (!FD) return;
5043
5044 llvm::Function *F = cast<llvm::Function>(GV);
5045
David Blaikie4e4d0842012-03-11 07:00:24 +00005046 if (M.getLangOpts().OpenCL) {
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00005047 if (FD->hasAttr<OpenCLKernelAttr>()) {
5048 // OpenCL C Kernel functions are not subject to inlining
Bill Wendling72390b32012-12-20 19:27:06 +00005049 F->addFnAttr(llvm::Attribute::NoInline);
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00005050
5051 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
5052
5053 // Convert the reqd_work_group_size() attributes to metadata.
5054 llvm::LLVMContext &Context = F->getContext();
5055 llvm::NamedMDNode *OpenCLMetadata =
5056 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
5057
5058 SmallVector<llvm::Value*, 5> Operands;
5059 Operands.push_back(F);
5060
Chris Lattner8b418682012-02-07 00:39:47 +00005061 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
5062 llvm::APInt(32,
5063 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
5064 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
5065 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00005066 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
Chris Lattner8b418682012-02-07 00:39:47 +00005067 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
5068 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00005069 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
5070
5071 // Add a boolean constant operand for "required" (true) or "hint" (false)
5072 // for implementing the work_group_size_hint attr later. Currently
5073 // always true as the hint is not yet implemented.
Chris Lattner8b418682012-02-07 00:39:47 +00005074 Operands.push_back(llvm::ConstantInt::getTrue(Context));
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00005075 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
5076 }
5077 }
5078 }
5079}
5080
5081}
John McCallaeeb7012010-05-27 06:19:26 +00005082
Tony Linthicum96319392011-12-12 21:14:55 +00005083//===----------------------------------------------------------------------===//
5084// Hexagon ABI Implementation
5085//===----------------------------------------------------------------------===//
5086
5087namespace {
5088
5089class HexagonABIInfo : public ABIInfo {
5090
5091
5092public:
5093 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
5094
5095private:
5096
5097 ABIArgInfo classifyReturnType(QualType RetTy) const;
5098 ABIArgInfo classifyArgumentType(QualType RetTy) const;
5099
5100 virtual void computeInfo(CGFunctionInfo &FI) const;
5101
5102 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5103 CodeGenFunction &CGF) const;
5104};
5105
5106class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
5107public:
5108 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
5109 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
5110
5111 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
5112 return 29;
5113 }
5114};
5115
5116}
5117
5118void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
5119 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
5120 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
5121 it != ie; ++it)
5122 it->info = classifyArgumentType(it->type);
5123}
5124
5125ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
5126 if (!isAggregateTypeForABI(Ty)) {
5127 // Treat an enum type as its underlying type.
5128 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5129 Ty = EnumTy->getDecl()->getIntegerType();
5130
5131 return (Ty->isPromotableIntegerType() ?
5132 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
5133 }
5134
5135 // Ignore empty records.
5136 if (isEmptyRecord(getContext(), Ty, true))
5137 return ABIArgInfo::getIgnore();
5138
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00005139 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
5140 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Tony Linthicum96319392011-12-12 21:14:55 +00005141
5142 uint64_t Size = getContext().getTypeSize(Ty);
5143 if (Size > 64)
5144 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
5145 // Pass in the smallest viable integer type.
5146 else if (Size > 32)
5147 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
5148 else if (Size > 16)
5149 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
5150 else if (Size > 8)
5151 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5152 else
5153 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
5154}
5155
5156ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
5157 if (RetTy->isVoidType())
5158 return ABIArgInfo::getIgnore();
5159
5160 // Large vector types should be returned via memory.
5161 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
5162 return ABIArgInfo::getIndirect(0);
5163
5164 if (!isAggregateTypeForABI(RetTy)) {
5165 // Treat an enum type as its underlying type.
5166 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5167 RetTy = EnumTy->getDecl()->getIntegerType();
5168
5169 return (RetTy->isPromotableIntegerType() ?
5170 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
5171 }
5172
5173 // Structures with either a non-trivial destructor or a non-trivial
5174 // copy constructor are always indirect.
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00005175 if (isRecordReturnIndirect(RetTy, CGT))
Tony Linthicum96319392011-12-12 21:14:55 +00005176 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
5177
5178 if (isEmptyRecord(getContext(), RetTy, true))
5179 return ABIArgInfo::getIgnore();
5180
5181 // Aggregates <= 8 bytes are returned in r0; other aggregates
5182 // are returned indirectly.
5183 uint64_t Size = getContext().getTypeSize(RetTy);
5184 if (Size <= 64) {
5185 // Return in the smallest viable integer type.
5186 if (Size <= 8)
5187 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
5188 if (Size <= 16)
5189 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5190 if (Size <= 32)
5191 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
5192 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
5193 }
5194
5195 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
5196}
5197
5198llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner8b418682012-02-07 00:39:47 +00005199 CodeGenFunction &CGF) const {
Tony Linthicum96319392011-12-12 21:14:55 +00005200 // FIXME: Need to handle alignment
Chris Lattner8b418682012-02-07 00:39:47 +00005201 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Tony Linthicum96319392011-12-12 21:14:55 +00005202
5203 CGBuilderTy &Builder = CGF.Builder;
5204 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
5205 "ap");
5206 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
5207 llvm::Type *PTy =
5208 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
5209 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
5210
5211 uint64_t Offset =
5212 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
5213 llvm::Value *NextAddr =
5214 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
5215 "ap.next");
5216 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
5217
5218 return AddrTyped;
5219}
5220
5221
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00005222//===----------------------------------------------------------------------===//
5223// SPARC v9 ABI Implementation.
5224// Based on the SPARC Compliance Definition version 2.4.1.
5225//
5226// Function arguments a mapped to a nominal "parameter array" and promoted to
5227// registers depending on their type. Each argument occupies 8 or 16 bytes in
5228// the array, structs larger than 16 bytes are passed indirectly.
5229//
5230// One case requires special care:
5231//
5232// struct mixed {
5233// int i;
5234// float f;
5235// };
5236//
5237// When a struct mixed is passed by value, it only occupies 8 bytes in the
5238// parameter array, but the int is passed in an integer register, and the float
5239// is passed in a floating point register. This is represented as two arguments
5240// with the LLVM IR inreg attribute:
5241//
5242// declare void f(i32 inreg %i, float inreg %f)
5243//
5244// The code generator will only allocate 4 bytes from the parameter array for
5245// the inreg arguments. All other arguments are allocated a multiple of 8
5246// bytes.
5247//
5248namespace {
5249class SparcV9ABIInfo : public ABIInfo {
5250public:
5251 SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
5252
5253private:
5254 ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const;
5255 virtual void computeInfo(CGFunctionInfo &FI) const;
5256 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5257 CodeGenFunction &CGF) const;
Jakob Stoklund Olesenfc782fb2013-05-28 04:57:37 +00005258
5259 // Coercion type builder for structs passed in registers. The coercion type
5260 // serves two purposes:
5261 //
5262 // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned'
5263 // in registers.
5264 // 2. Expose aligned floating point elements as first-level elements, so the
5265 // code generator knows to pass them in floating point registers.
5266 //
5267 // We also compute the InReg flag which indicates that the struct contains
5268 // aligned 32-bit floats.
5269 //
5270 struct CoerceBuilder {
5271 llvm::LLVMContext &Context;
5272 const llvm::DataLayout &DL;
5273 SmallVector<llvm::Type*, 8> Elems;
5274 uint64_t Size;
5275 bool InReg;
5276
5277 CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl)
5278 : Context(c), DL(dl), Size(0), InReg(false) {}
5279
5280 // Pad Elems with integers until Size is ToSize.
5281 void pad(uint64_t ToSize) {
5282 assert(ToSize >= Size && "Cannot remove elements");
5283 if (ToSize == Size)
5284 return;
5285
5286 // Finish the current 64-bit word.
5287 uint64_t Aligned = llvm::RoundUpToAlignment(Size, 64);
5288 if (Aligned > Size && Aligned <= ToSize) {
5289 Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size));
5290 Size = Aligned;
5291 }
5292
5293 // Add whole 64-bit words.
5294 while (Size + 64 <= ToSize) {
5295 Elems.push_back(llvm::Type::getInt64Ty(Context));
5296 Size += 64;
5297 }
5298
5299 // Final in-word padding.
5300 if (Size < ToSize) {
5301 Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size));
5302 Size = ToSize;
5303 }
5304 }
5305
5306 // Add a floating point element at Offset.
5307 void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) {
5308 // Unaligned floats are treated as integers.
5309 if (Offset % Bits)
5310 return;
5311 // The InReg flag is only required if there are any floats < 64 bits.
5312 if (Bits < 64)
5313 InReg = true;
5314 pad(Offset);
5315 Elems.push_back(Ty);
5316 Size = Offset + Bits;
5317 }
5318
5319 // Add a struct type to the coercion type, starting at Offset (in bits).
5320 void addStruct(uint64_t Offset, llvm::StructType *StrTy) {
5321 const llvm::StructLayout *Layout = DL.getStructLayout(StrTy);
5322 for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) {
5323 llvm::Type *ElemTy = StrTy->getElementType(i);
5324 uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i);
5325 switch (ElemTy->getTypeID()) {
5326 case llvm::Type::StructTyID:
5327 addStruct(ElemOffset, cast<llvm::StructType>(ElemTy));
5328 break;
5329 case llvm::Type::FloatTyID:
5330 addFloat(ElemOffset, ElemTy, 32);
5331 break;
5332 case llvm::Type::DoubleTyID:
5333 addFloat(ElemOffset, ElemTy, 64);
5334 break;
5335 case llvm::Type::FP128TyID:
5336 addFloat(ElemOffset, ElemTy, 128);
5337 break;
5338 case llvm::Type::PointerTyID:
5339 if (ElemOffset % 64 == 0) {
5340 pad(ElemOffset);
5341 Elems.push_back(ElemTy);
5342 Size += 64;
5343 }
5344 break;
5345 default:
5346 break;
5347 }
5348 }
5349 }
5350
5351 // Check if Ty is a usable substitute for the coercion type.
5352 bool isUsableType(llvm::StructType *Ty) const {
5353 if (Ty->getNumElements() != Elems.size())
5354 return false;
5355 for (unsigned i = 0, e = Elems.size(); i != e; ++i)
5356 if (Elems[i] != Ty->getElementType(i))
5357 return false;
5358 return true;
5359 }
5360
5361 // Get the coercion type as a literal struct type.
5362 llvm::Type *getType() const {
5363 if (Elems.size() == 1)
5364 return Elems.front();
5365 else
5366 return llvm::StructType::get(Context, Elems);
5367 }
5368 };
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00005369};
5370} // end anonymous namespace
5371
5372ABIArgInfo
5373SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const {
5374 if (Ty->isVoidType())
5375 return ABIArgInfo::getIgnore();
5376
5377 uint64_t Size = getContext().getTypeSize(Ty);
5378
5379 // Anything too big to fit in registers is passed with an explicit indirect
5380 // pointer / sret pointer.
5381 if (Size > SizeLimit)
5382 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
5383
5384 // Treat an enum type as its underlying type.
5385 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5386 Ty = EnumTy->getDecl()->getIntegerType();
5387
5388 // Integer types smaller than a register are extended.
5389 if (Size < 64 && Ty->isIntegerType())
5390 return ABIArgInfo::getExtend();
5391
5392 // Other non-aggregates go in registers.
5393 if (!isAggregateTypeForABI(Ty))
5394 return ABIArgInfo::getDirect();
5395
5396 // This is a small aggregate type that should be passed in registers.
Jakob Stoklund Olesenfc782fb2013-05-28 04:57:37 +00005397 // Build a coercion type from the LLVM struct type.
5398 llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty));
5399 if (!StrTy)
5400 return ABIArgInfo::getDirect();
5401
5402 CoerceBuilder CB(getVMContext(), getDataLayout());
5403 CB.addStruct(0, StrTy);
5404 CB.pad(llvm::RoundUpToAlignment(CB.DL.getTypeSizeInBits(StrTy), 64));
5405
5406 // Try to use the original type for coercion.
5407 llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();
5408
5409 if (CB.InReg)
5410 return ABIArgInfo::getDirectInReg(CoerceTy);
5411 else
5412 return ABIArgInfo::getDirect(CoerceTy);
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00005413}
5414
5415llvm::Value *SparcV9ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5416 CodeGenFunction &CGF) const {
Jakob Stoklund Olesena4b56d32013-06-05 03:00:18 +00005417 ABIArgInfo AI = classifyType(Ty, 16 * 8);
5418 llvm::Type *ArgTy = CGT.ConvertType(Ty);
5419 if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
5420 AI.setCoerceToType(ArgTy);
5421
5422 llvm::Type *BPP = CGF.Int8PtrPtrTy;
5423 CGBuilderTy &Builder = CGF.Builder;
5424 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
5425 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
5426 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
5427 llvm::Value *ArgAddr;
5428 unsigned Stride;
5429
5430 switch (AI.getKind()) {
5431 case ABIArgInfo::Expand:
5432 llvm_unreachable("Unsupported ABI kind for va_arg");
5433
5434 case ABIArgInfo::Extend:
5435 Stride = 8;
5436 ArgAddr = Builder
5437 .CreateConstGEP1_32(Addr, 8 - getDataLayout().getTypeAllocSize(ArgTy),
5438 "extend");
5439 break;
5440
5441 case ABIArgInfo::Direct:
5442 Stride = getDataLayout().getTypeAllocSize(AI.getCoerceToType());
5443 ArgAddr = Addr;
5444 break;
5445
5446 case ABIArgInfo::Indirect:
5447 Stride = 8;
5448 ArgAddr = Builder.CreateBitCast(Addr,
5449 llvm::PointerType::getUnqual(ArgPtrTy),
5450 "indirect");
5451 ArgAddr = Builder.CreateLoad(ArgAddr, "indirect.arg");
5452 break;
5453
5454 case ABIArgInfo::Ignore:
5455 return llvm::UndefValue::get(ArgPtrTy);
5456 }
5457
5458 // Update VAList.
5459 Addr = Builder.CreateConstGEP1_32(Addr, Stride, "ap.next");
5460 Builder.CreateStore(Addr, VAListAddrAsBPP);
5461
5462 return Builder.CreatePointerCast(ArgAddr, ArgPtrTy, "arg.addr");
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00005463}
5464
5465void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const {
5466 FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8);
5467 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
5468 it != ie; ++it)
5469 it->info = classifyType(it->type, 16 * 8);
5470}
5471
5472namespace {
5473class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo {
5474public:
5475 SparcV9TargetCodeGenInfo(CodeGenTypes &CGT)
5476 : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {}
5477};
5478} // end anonymous namespace
5479
5480
Chris Lattnerea044322010-07-29 02:01:43 +00005481const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005482 if (TheTargetCodeGenInfo)
5483 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005484
John McCall64aa4b32013-04-16 22:48:15 +00005485 const llvm::Triple &Triple = getTarget().getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00005486 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00005487 default:
Chris Lattnerea044322010-07-29 02:01:43 +00005488 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00005489
Derek Schuff9ed63f82012-09-06 17:37:28 +00005490 case llvm::Triple::le32:
5491 return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
John McCallaeeb7012010-05-27 06:19:26 +00005492 case llvm::Triple::mips:
5493 case llvm::Triple::mipsel:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00005494 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCallaeeb7012010-05-27 06:19:26 +00005495
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00005496 case llvm::Triple::mips64:
5497 case llvm::Triple::mips64el:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00005498 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00005499
Tim Northoverc264e162013-01-31 12:13:10 +00005500 case llvm::Triple::aarch64:
5501 return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types));
5502
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00005503 case llvm::Triple::arm:
5504 case llvm::Triple::thumb:
Sandeep Patel34c1af82011-04-05 00:23:47 +00005505 {
5506 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
John McCall64aa4b32013-04-16 22:48:15 +00005507 if (strcmp(getTarget().getABI(), "apcs-gnu") == 0)
Sandeep Patel34c1af82011-04-05 00:23:47 +00005508 Kind = ARMABIInfo::APCS;
David Tweedb16abb12012-10-25 13:33:01 +00005509 else if (CodeGenOpts.FloatABI == "hard" ||
John McCall64aa4b32013-04-16 22:48:15 +00005510 (CodeGenOpts.FloatABI != "soft" &&
5511 Triple.getEnvironment() == llvm::Triple::GNUEABIHF))
Sandeep Patel34c1af82011-04-05 00:23:47 +00005512 Kind = ARMABIInfo::AAPCS_VFP;
5513
Derek Schuff263366f2012-10-16 22:30:41 +00005514 switch (Triple.getOS()) {
Eli Bendersky441d9f72012-12-04 18:38:10 +00005515 case llvm::Triple::NaCl:
Derek Schuff263366f2012-10-16 22:30:41 +00005516 return *(TheTargetCodeGenInfo =
5517 new NaClARMTargetCodeGenInfo(Types, Kind));
5518 default:
5519 return *(TheTargetCodeGenInfo =
5520 new ARMTargetCodeGenInfo(Types, Kind));
5521 }
Sandeep Patel34c1af82011-04-05 00:23:47 +00005522 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00005523
John McCallec853ba2010-03-11 00:10:12 +00005524 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00005525 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
Roman Divacky0fbc4b92012-05-09 18:22:46 +00005526 case llvm::Triple::ppc64:
Bill Schmidt2fc107f2012-10-03 19:18:57 +00005527 if (Triple.isOSBinFormatELF())
5528 return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types));
5529 else
5530 return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00005531
Peter Collingbourneedb66f32012-05-20 23:28:41 +00005532 case llvm::Triple::nvptx:
5533 case llvm::Triple::nvptx64:
Justin Holewinski2c585b92012-05-24 17:43:12 +00005534 return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types));
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005535
Wesley Peck276fdf42010-12-19 19:57:51 +00005536 case llvm::Triple::mblaze:
5537 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
5538
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005539 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00005540 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00005541
Ulrich Weigandb8409212013-05-06 16:26:41 +00005542 case llvm::Triple::systemz:
5543 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
5544
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00005545 case llvm::Triple::tce:
5546 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
5547
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00005548 case llvm::Triple::x86: {
John McCallb8b52972013-06-18 02:46:29 +00005549 bool IsDarwinVectorABI = Triple.isOSDarwin();
5550 bool IsSmallStructInRegABI =
5551 X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
5552 bool IsWin32FloatStructABI = (Triple.getOS() == llvm::Triple::Win32);
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00005553
John McCallb8b52972013-06-18 02:46:29 +00005554 if (Triple.getOS() == llvm::Triple::Win32) {
Eli Friedman55fc7e22012-01-25 22:46:34 +00005555 return *(TheTargetCodeGenInfo =
Reid Kleckner3190ca92013-05-08 13:44:39 +00005556 new WinX86_32TargetCodeGenInfo(Types,
John McCallb8b52972013-06-18 02:46:29 +00005557 IsDarwinVectorABI, IsSmallStructInRegABI,
5558 IsWin32FloatStructABI,
Reid Kleckner3190ca92013-05-08 13:44:39 +00005559 CodeGenOpts.NumRegisterParameters));
John McCallb8b52972013-06-18 02:46:29 +00005560 } else {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005561 return *(TheTargetCodeGenInfo =
John McCallb8b52972013-06-18 02:46:29 +00005562 new X86_32TargetCodeGenInfo(Types,
5563 IsDarwinVectorABI, IsSmallStructInRegABI,
5564 IsWin32FloatStructABI,
Rafael Espindolab48280b2012-07-31 02:44:24 +00005565 CodeGenOpts.NumRegisterParameters));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005566 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00005567 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005568
Eli Friedmanee1ad992011-12-02 00:11:43 +00005569 case llvm::Triple::x86_64: {
John McCall64aa4b32013-04-16 22:48:15 +00005570 bool HasAVX = strcmp(getTarget().getABI(), "avx") == 0;
Eli Friedmanee1ad992011-12-02 00:11:43 +00005571
Chris Lattnerf13721d2010-08-31 16:44:54 +00005572 switch (Triple.getOS()) {
5573 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00005574 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00005575 case llvm::Triple::Cygwin:
5576 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
Eli Bendersky441d9f72012-12-04 18:38:10 +00005577 case llvm::Triple::NaCl:
John McCall64aa4b32013-04-16 22:48:15 +00005578 return *(TheTargetCodeGenInfo = new NaClX86_64TargetCodeGenInfo(Types,
5579 HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00005580 default:
Eli Friedmanee1ad992011-12-02 00:11:43 +00005581 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
5582 HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00005583 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00005584 }
Tony Linthicum96319392011-12-12 21:14:55 +00005585 case llvm::Triple::hexagon:
5586 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00005587 case llvm::Triple::sparcv9:
5588 return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types));
Eli Friedmanee1ad992011-12-02 00:11:43 +00005589 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005590}