blob: 981a38d7ff343dd8fd9afc3dc4a88dbc46b24f0b [file] [log] [blame]
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000016#include "ABIInfo.h"
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +000017#include "CGCXXABI.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000018#include "CodeGenFunction.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000019#include "clang/AST/RecordLayout.h"
Sandeep Patel34c1af82011-04-05 00:23:47 +000020#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbar2c0843f2009-08-24 08:52:16 +000021#include "llvm/ADT/Triple.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000022#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/Type.h"
Daniel Dunbar28df7a52009-12-03 09:13:49 +000024#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000025using namespace clang;
26using namespace CodeGen;
27
John McCallaeeb7012010-05-27 06:19:26 +000028static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
29 llvm::Value *Array,
30 llvm::Value *Value,
31 unsigned FirstIndex,
32 unsigned LastIndex) {
33 // Alternatively, we could emit this as a loop in the source.
34 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
35 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
36 Builder.CreateStore(Value, Cell);
37 }
38}
39
John McCalld608cdb2010-08-22 10:59:02 +000040static bool isAggregateTypeForABI(QualType T) {
John McCall9d232c82013-03-07 21:37:08 +000041 return !CodeGenFunction::hasScalarEvaluationKind(T) ||
John McCalld608cdb2010-08-22 10:59:02 +000042 T->isMemberFunctionPointerType();
43}
44
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000045ABIInfo::~ABIInfo() {}
46
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +000047static bool isRecordReturnIndirect(const RecordType *RT, CodeGen::CodeGenTypes &CGT) {
48 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
49 if (!RD)
50 return false;
51 return CGT.CGM.getCXXABI().isReturnTypeIndirect(RD);
52}
53
54
55static bool isRecordReturnIndirect(QualType T, CodeGen::CodeGenTypes &CGT) {
56 const RecordType *RT = T->getAs<RecordType>();
57 if (!RT)
58 return false;
59 return isRecordReturnIndirect(RT, CGT);
60}
61
62static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT,
63 CodeGen::CodeGenTypes &CGT) {
64 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
65 if (!RD)
66 return CGCXXABI::RAA_Default;
67 return CGT.CGM.getCXXABI().getRecordArgABI(RD);
68}
69
70static CGCXXABI::RecordArgABI getRecordArgABI(QualType T,
71 CodeGen::CodeGenTypes &CGT) {
72 const RecordType *RT = T->getAs<RecordType>();
73 if (!RT)
74 return CGCXXABI::RAA_Default;
75 return getRecordArgABI(RT, CGT);
76}
77
Chris Lattnerea044322010-07-29 02:01:43 +000078ASTContext &ABIInfo::getContext() const {
79 return CGT.getContext();
80}
81
82llvm::LLVMContext &ABIInfo::getVMContext() const {
83 return CGT.getLLVMContext();
84}
85
Micah Villmow25a6a842012-10-08 16:25:52 +000086const llvm::DataLayout &ABIInfo::getDataLayout() const {
87 return CGT.getDataLayout();
Chris Lattnerea044322010-07-29 02:01:43 +000088}
89
John McCall64aa4b32013-04-16 22:48:15 +000090const TargetInfo &ABIInfo::getTarget() const {
91 return CGT.getTarget();
92}
Chris Lattnerea044322010-07-29 02:01:43 +000093
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000094void ABIArgInfo::dump() const {
Chris Lattner5f9e2722011-07-23 10:55:15 +000095 raw_ostream &OS = llvm::errs();
Daniel Dunbar28df7a52009-12-03 09:13:49 +000096 OS << "(ABIArgInfo Kind=";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000097 switch (TheKind) {
98 case Direct:
Chris Lattner800588f2010-07-29 06:26:06 +000099 OS << "Direct Type=";
Chris Lattner2acc6e32011-07-18 04:24:23 +0000100 if (llvm::Type *Ty = getCoerceToType())
Chris Lattner800588f2010-07-29 06:26:06 +0000101 Ty->print(OS);
102 else
103 OS << "null";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000104 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000105 case Extend:
Daniel Dunbar28df7a52009-12-03 09:13:49 +0000106 OS << "Extend";
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000107 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000108 case Ignore:
Daniel Dunbar28df7a52009-12-03 09:13:49 +0000109 OS << "Ignore";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000110 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000111 case Indirect:
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000112 OS << "Indirect Align=" << getIndirectAlign()
Joerg Sonnenbergere9b5d772011-07-15 18:23:44 +0000113 << " ByVal=" << getIndirectByVal()
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000114 << " Realign=" << getIndirectRealign();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000115 break;
116 case Expand:
Daniel Dunbar28df7a52009-12-03 09:13:49 +0000117 OS << "Expand";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000118 break;
119 }
Daniel Dunbar28df7a52009-12-03 09:13:49 +0000120 OS << ")\n";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000121}
122
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000123TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
124
John McCall49e34be2011-08-30 01:42:09 +0000125// If someone can figure out a general rule for this, that would be great.
126// It's probably just doomed to be platform-dependent, though.
127unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
128 // Verified for:
129 // x86-64 FreeBSD, Linux, Darwin
130 // x86-32 FreeBSD, Linux, Darwin
131 // PowerPC Linux, Darwin
132 // ARM Darwin (*not* EABI)
Tim Northoverc264e162013-01-31 12:13:10 +0000133 // AArch64 Linux
John McCall49e34be2011-08-30 01:42:09 +0000134 return 32;
135}
136
John McCallde5d3c72012-02-17 03:33:10 +0000137bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
138 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +0000139 // The following conventions are known to require this to be false:
140 // x86_stdcall
141 // MIPS
142 // For everything else, we just prefer false unless we opt out.
143 return false;
144}
145
Reid Kleckner3190ca92013-05-08 13:44:39 +0000146void
147TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib,
148 llvm::SmallString<24> &Opt) const {
149 // This assumes the user is passing a library name like "rt" instead of a
150 // filename like "librt.a/so", and that they don't care whether it's static or
151 // dynamic.
152 Opt = "-l";
153 Opt += Lib;
154}
155
Daniel Dunbar98303b92009-09-13 08:03:58 +0000156static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000157
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000158/// isEmptyField - Return true iff a the field is "empty", that is it
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000159/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +0000160static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
161 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000162 if (FD->isUnnamedBitfield())
163 return true;
164
165 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000166
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000167 // Constant arrays of empty records count as empty, strip them off.
168 // Constant arrays of zero length always count as empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000169 if (AllowArrays)
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000170 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
171 if (AT->getSize() == 0)
172 return true;
Daniel Dunbar98303b92009-09-13 08:03:58 +0000173 FT = AT->getElementType();
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000174 }
Daniel Dunbar98303b92009-09-13 08:03:58 +0000175
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000176 const RecordType *RT = FT->getAs<RecordType>();
177 if (!RT)
178 return false;
179
180 // C++ record fields are never empty, at least in the Itanium ABI.
181 //
182 // FIXME: We should use a predicate for whether this behavior is true in the
183 // current ABI.
184 if (isa<CXXRecordDecl>(RT->getDecl()))
185 return false;
186
Daniel Dunbar98303b92009-09-13 08:03:58 +0000187 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000188}
189
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000190/// isEmptyRecord - Return true iff a structure contains only empty
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000191/// fields. Note that a structure with a flexible array member is not
192/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000193static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000194 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000195 if (!RT)
196 return 0;
197 const RecordDecl *RD = RT->getDecl();
198 if (RD->hasFlexibleArrayMember())
199 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000200
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000201 // If this is a C++ record, check the bases first.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000202 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000203 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
204 e = CXXRD->bases_end(); i != e; ++i)
205 if (!isEmptyRecord(Context, i->getType(), true))
206 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000207
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000208 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
209 i != e; ++i)
David Blaikie581deb32012-06-06 20:45:41 +0000210 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000211 return false;
212 return true;
213}
214
215/// isSingleElementStruct - Determine if a structure is a "single
216/// element struct", i.e. it has exactly one non-empty field or
217/// exactly one field which is itself a single element
218/// struct. Structures with flexible array members are never
219/// considered single element structs.
220///
221/// \return The field declaration for the single non-empty field, if
222/// it exists.
223static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
224 const RecordType *RT = T->getAsStructureType();
225 if (!RT)
226 return 0;
227
228 const RecordDecl *RD = RT->getDecl();
229 if (RD->hasFlexibleArrayMember())
230 return 0;
231
232 const Type *Found = 0;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000233
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000234 // If this is a C++ record, check the bases first.
235 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
236 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
237 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000238 // Ignore empty records.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000239 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000240 continue;
241
242 // If we already found an element then this isn't a single-element struct.
243 if (Found)
244 return 0;
245
246 // If this is non-empty and not a single element struct, the composite
247 // cannot be a single element struct.
248 Found = isSingleElementStruct(i->getType(), Context);
249 if (!Found)
250 return 0;
251 }
252 }
253
254 // Check for single element.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000255 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
256 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000257 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000258 QualType FT = FD->getType();
259
260 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000261 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000262 continue;
263
264 // If we already found an element then this isn't a single-element
265 // struct.
266 if (Found)
267 return 0;
268
269 // Treat single element arrays as the element.
270 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
271 if (AT->getSize().getZExtValue() != 1)
272 break;
273 FT = AT->getElementType();
274 }
275
John McCalld608cdb2010-08-22 10:59:02 +0000276 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000277 Found = FT.getTypePtr();
278 } else {
279 Found = isSingleElementStruct(FT, Context);
280 if (!Found)
281 return 0;
282 }
283 }
284
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000285 // We don't consider a struct a single-element struct if it has
286 // padding beyond the element type.
287 if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
288 return 0;
289
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000290 return Found;
291}
292
293static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Eli Friedmandb748a32012-11-29 23:21:04 +0000294 // Treat complex types as the element type.
295 if (const ComplexType *CTy = Ty->getAs<ComplexType>())
296 Ty = CTy->getElementType();
297
298 // Check for a type which we know has a simple scalar argument-passing
299 // convention without any padding. (We're specifically looking for 32
300 // and 64-bit integer and integer-equivalents, float, and double.)
Daniel Dunbara1842d32010-05-14 03:40:53 +0000301 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Eli Friedmandb748a32012-11-29 23:21:04 +0000302 !Ty->isEnumeralType() && !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000303 return false;
304
305 uint64_t Size = Context.getTypeSize(Ty);
306 return Size == 32 || Size == 64;
307}
308
Daniel Dunbar53012f42009-11-09 01:33:53 +0000309/// canExpandIndirectArgument - Test whether an argument type which is to be
310/// passed indirectly (on the stack) would have the equivalent layout if it was
311/// expanded into separate arguments. If so, we prefer to do the latter to avoid
312/// inhibiting optimizations.
313///
314// FIXME: This predicate is missing many cases, currently it just follows
315// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
316// should probably make this smarter, or better yet make the LLVM backend
317// capable of handling it.
318static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
319 // We can only expand structure types.
320 const RecordType *RT = Ty->getAs<RecordType>();
321 if (!RT)
322 return false;
323
324 // We can only expand (C) structures.
325 //
326 // FIXME: This needs to be generalized to handle classes as well.
327 const RecordDecl *RD = RT->getDecl();
328 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
329 return false;
330
Eli Friedman506d4e32011-11-18 01:32:26 +0000331 uint64_t Size = 0;
332
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000333 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
334 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000335 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000336
337 if (!is32Or64BitBasicType(FD->getType(), Context))
338 return false;
339
340 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
341 // how to expand them yet, and the predicate for telling if a bitfield still
342 // counts as "basic" is more complicated than what we were doing previously.
343 if (FD->isBitField())
344 return false;
Eli Friedman506d4e32011-11-18 01:32:26 +0000345
346 Size += Context.getTypeSize(FD->getType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000347 }
348
Eli Friedman506d4e32011-11-18 01:32:26 +0000349 // Make sure there are not any holes in the struct.
350 if (Size != Context.getTypeSize(Ty))
351 return false;
352
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000353 return true;
354}
355
356namespace {
357/// DefaultABIInfo - The default implementation for ABI specific
358/// details. This implementation provides information which results in
359/// self-consistent and sensible LLVM IR generation, but does not
360/// conform to any particular ABI.
361class DefaultABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +0000362public:
363 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000364
Chris Lattnera3c109b2010-07-29 02:16:43 +0000365 ABIArgInfo classifyReturnType(QualType RetTy) const;
366 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000367
Chris Lattneree5dcd02010-07-29 02:31:05 +0000368 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000369 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000370 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
371 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000372 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000373 }
374
375 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
376 CodeGenFunction &CGF) const;
377};
378
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000379class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
380public:
Chris Lattnerea044322010-07-29 02:01:43 +0000381 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
382 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000383};
384
385llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
386 CodeGenFunction &CGF) const {
387 return 0;
388}
389
Chris Lattnera3c109b2010-07-29 02:16:43 +0000390ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
Jan Wen Voung90306932011-11-03 00:59:44 +0000391 if (isAggregateTypeForABI(Ty)) {
392 // Records with non trivial destructors/constructors should not be passed
393 // by value.
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000394 if (isRecordReturnIndirect(Ty, CGT))
Jan Wen Voung90306932011-11-03 00:59:44 +0000395 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
396
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000397 return ABIArgInfo::getIndirect(0);
Jan Wen Voung90306932011-11-03 00:59:44 +0000398 }
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000399
Chris Lattnera14db752010-03-11 18:19:55 +0000400 // Treat an enum type as its underlying type.
401 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
402 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000403
Chris Lattnera14db752010-03-11 18:19:55 +0000404 return (Ty->isPromotableIntegerType() ?
405 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000406}
407
Bob Wilson0024f942011-01-10 23:54:17 +0000408ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
409 if (RetTy->isVoidType())
410 return ABIArgInfo::getIgnore();
411
412 if (isAggregateTypeForABI(RetTy))
413 return ABIArgInfo::getIndirect(0);
414
415 // Treat an enum type as its underlying type.
416 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
417 RetTy = EnumTy->getDecl()->getIntegerType();
418
419 return (RetTy->isPromotableIntegerType() ?
420 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
421}
422
Derek Schuff9ed63f82012-09-06 17:37:28 +0000423//===----------------------------------------------------------------------===//
424// le32/PNaCl bitcode ABI Implementation
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000425//
426// This is a simplified version of the x86_32 ABI. Arguments and return values
427// are always passed on the stack.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000428//===----------------------------------------------------------------------===//
429
430class PNaClABIInfo : public ABIInfo {
431 public:
432 PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
433
434 ABIArgInfo classifyReturnType(QualType RetTy) const;
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000435 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Derek Schuff9ed63f82012-09-06 17:37:28 +0000436
437 virtual void computeInfo(CGFunctionInfo &FI) const;
438 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
439 CodeGenFunction &CGF) const;
440};
441
442class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
443 public:
444 PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
445 : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {}
446};
447
448void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
449 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
450
Derek Schuff9ed63f82012-09-06 17:37:28 +0000451 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
452 it != ie; ++it)
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000453 it->info = classifyArgumentType(it->type);
Derek Schuff9ed63f82012-09-06 17:37:28 +0000454 }
455
456llvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
457 CodeGenFunction &CGF) const {
458 return 0;
459}
460
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000461/// \brief Classify argument of given type \p Ty.
462ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const {
Derek Schuff9ed63f82012-09-06 17:37:28 +0000463 if (isAggregateTypeForABI(Ty)) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000464 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
465 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Derek Schuff9ed63f82012-09-06 17:37:28 +0000466 return ABIArgInfo::getIndirect(0);
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000467 } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
468 // Treat an enum type as its underlying type.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000469 Ty = EnumTy->getDecl()->getIntegerType();
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000470 } else if (Ty->isFloatingType()) {
471 // Floating-point types don't go inreg.
472 return ABIArgInfo::getDirect();
Derek Schuff9ed63f82012-09-06 17:37:28 +0000473 }
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000474
475 return (Ty->isPromotableIntegerType() ?
476 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Derek Schuff9ed63f82012-09-06 17:37:28 +0000477}
478
479ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
480 if (RetTy->isVoidType())
481 return ABIArgInfo::getIgnore();
482
Eli Benderskye45dfd12013-04-04 22:49:35 +0000483 // In the PNaCl ABI we always return records/structures on the stack.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000484 if (isAggregateTypeForABI(RetTy))
485 return ABIArgInfo::getIndirect(0);
486
487 // Treat an enum type as its underlying type.
488 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
489 RetTy = EnumTy->getDecl()->getIntegerType();
490
491 return (RetTy->isPromotableIntegerType() ?
492 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
493}
494
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000495/// IsX86_MMXType - Return true if this is an MMX type.
496bool IsX86_MMXType(llvm::Type *IRType) {
497 // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>.
Bill Wendlingbb465d72010-10-18 03:41:31 +0000498 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
499 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
500 IRType->getScalarSizeInBits() != 64;
501}
502
Jay Foadef6de3d2011-07-11 09:56:20 +0000503static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000504 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000505 llvm::Type* Ty) {
Tim Northover1bea6532013-06-07 00:04:50 +0000506 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) {
507 if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) {
508 // Invalid MMX constraint
509 return 0;
510 }
511
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000512 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
Tim Northover1bea6532013-06-07 00:04:50 +0000513 }
514
515 // No operation needed
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000516 return Ty;
517}
518
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000519//===----------------------------------------------------------------------===//
520// X86-32 ABI Implementation
521//===----------------------------------------------------------------------===//
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000522
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000523/// X86_32ABIInfo - The X86-32 ABI information.
524class X86_32ABIInfo : public ABIInfo {
Rafael Espindolab48280b2012-07-31 02:44:24 +0000525 enum Class {
526 Integer,
527 Float
528 };
529
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000530 static const unsigned MinABIStackAlignInBytes = 4;
531
David Chisnall1e4249c2009-08-17 23:08:21 +0000532 bool IsDarwinVectorABI;
533 bool IsSmallStructInRegABI;
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000534 bool IsWin32StructABI;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000535 unsigned DefaultNumRegisterParameters;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000536
537 static bool isRegisterSize(unsigned Size) {
538 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
539 }
540
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000541 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context,
542 unsigned callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000543
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000544 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
545 /// such that the argument will be passed in memory.
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000546 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal,
547 unsigned &FreeRegs) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000548
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000549 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbare59d8582010-09-16 20:42:06 +0000550 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000551
Rafael Espindolab48280b2012-07-31 02:44:24 +0000552 Class classify(QualType Ty) const;
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000553 ABIArgInfo classifyReturnType(QualType RetTy,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000554 unsigned callingConvention) const;
Rafael Espindolab6932692012-10-24 01:58:58 +0000555 ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &FreeRegs,
556 bool IsFastCall) const;
557 bool shouldUseInReg(QualType Ty, unsigned &FreeRegs,
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000558 bool IsFastCall, bool &NeedsPadding) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000559
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000560public:
561
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000562 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000563 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
564 CodeGenFunction &CGF) const;
565
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000566 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool w,
Rafael Espindolab48280b2012-07-31 02:44:24 +0000567 unsigned r)
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000568 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000569 IsWin32StructABI(w), DefaultNumRegisterParameters(r) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000570};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000571
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000572class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
573public:
Eli Friedman55fc7e22012-01-25 22:46:34 +0000574 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000575 bool d, bool p, bool w, unsigned r)
576 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, w, r)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000577
578 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
579 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000580
581 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
582 // Darwin uses different dwarf register numbers for EH.
John McCall64aa4b32013-04-16 22:48:15 +0000583 if (CGM.getTarget().getTriple().isOSDarwin()) return 5;
John McCall6374c332010-03-06 00:35:14 +0000584 return 4;
585 }
586
587 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
588 llvm::Value *Address) const;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000589
Jay Foadef6de3d2011-07-11 09:56:20 +0000590 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000591 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000592 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000593 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
594 }
595
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000596};
597
598}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000599
600/// shouldReturnTypeInRegister - Determine if the given type should be
601/// passed in a register (for the Darwin ABI).
602bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000603 ASTContext &Context,
604 unsigned callingConvention) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000605 uint64_t Size = Context.getTypeSize(Ty);
606
607 // Type must be register sized.
608 if (!isRegisterSize(Size))
609 return false;
610
611 if (Ty->isVectorType()) {
612 // 64- and 128- bit vectors inside structures are not returned in
613 // registers.
614 if (Size == 64 || Size == 128)
615 return false;
616
617 return true;
618 }
619
Daniel Dunbar77115232010-05-15 00:00:30 +0000620 // If this is a builtin, pointer, enum, complex type, member pointer, or
621 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000622 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000623 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000624 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000625 return true;
626
627 // Arrays are treated like records.
628 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000629 return shouldReturnTypeInRegister(AT->getElementType(), Context,
630 callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000631
632 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000633 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000634 if (!RT) return false;
635
Anders Carlssona8874232010-01-27 03:25:19 +0000636 // FIXME: Traverse bases here too.
637
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000638 // For thiscall conventions, structures will never be returned in
639 // a register. This is for compatibility with the MSVC ABI
640 if (callingConvention == llvm::CallingConv::X86_ThisCall &&
641 RT->isStructureType()) {
642 return false;
643 }
644
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000645 // Structure types are passed in register if all fields would be
646 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000647 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
648 e = RT->getDecl()->field_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000649 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000650
651 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000652 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000653 continue;
654
655 // Check fields recursively.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000656 if (!shouldReturnTypeInRegister(FD->getType(), Context,
657 callingConvention))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000658 return false;
659 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000660 return true;
661}
662
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000663ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
664 unsigned callingConvention) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000665 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000666 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000667
Chris Lattnera3c109b2010-07-29 02:16:43 +0000668 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000669 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000670 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000671 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000672
673 // 128-bit vectors are a special case; they are returned in
674 // registers and we need to make sure to pick a type the LLVM
675 // backend will like.
676 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +0000677 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000678 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000679
680 // Always return in register if it fits in a general purpose
681 // register, or if it is 64 bits and has a single element.
682 if ((Size == 8 || Size == 16 || Size == 32) ||
683 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +0000684 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000685 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000686
687 return ABIArgInfo::getIndirect(0);
688 }
689
690 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000691 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000692
John McCalld608cdb2010-08-22 10:59:02 +0000693 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000694 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000695 if (isRecordReturnIndirect(RT, CGT))
Anders Carlsson40092972009-10-20 22:07:59 +0000696 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000697
Anders Carlsson40092972009-10-20 22:07:59 +0000698 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000699 if (RT->getDecl()->hasFlexibleArrayMember())
700 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000701 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000702
David Chisnall1e4249c2009-08-17 23:08:21 +0000703 // If specified, structs and unions are always indirect.
704 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000705 return ABIArgInfo::getIndirect(0);
706
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000707 // Small structures which are register sized are generally returned
708 // in a register.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000709 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(),
710 callingConvention)) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000711 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000712
713 // As a special-case, if the struct is a "single-element" struct, and
714 // the field is of type "float" or "double", return it in a
Eli Friedman55fc7e22012-01-25 22:46:34 +0000715 // floating-point register. (MSVC does not apply this special case.)
716 // We apply a similar transformation for pointer types to improve the
717 // quality of the generated IR.
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000718 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000719 if ((!IsWin32StructABI && SeltTy->isRealFloatingType())
Eli Friedman55fc7e22012-01-25 22:46:34 +0000720 || SeltTy->hasPointerRepresentation())
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000721 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
722
723 // FIXME: We should be able to narrow this integer in cases with dead
724 // padding.
Chris Lattner800588f2010-07-29 06:26:06 +0000725 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000726 }
727
728 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000729 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000730
Chris Lattnera3c109b2010-07-29 02:16:43 +0000731 // Treat an enum type as its underlying type.
732 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
733 RetTy = EnumTy->getDecl()->getIntegerType();
734
735 return (RetTy->isPromotableIntegerType() ?
736 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000737}
738
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000739static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
740 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
741}
742
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000743static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
744 const RecordType *RT = Ty->getAs<RecordType>();
745 if (!RT)
746 return 0;
747 const RecordDecl *RD = RT->getDecl();
748
749 // If this is a C++ record, check the bases first.
750 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
751 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
752 e = CXXRD->bases_end(); i != e; ++i)
753 if (!isRecordWithSSEVectorType(Context, i->getType()))
754 return false;
755
756 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
757 i != e; ++i) {
758 QualType FT = i->getType();
759
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000760 if (isSSEVectorType(Context, FT))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000761 return true;
762
763 if (isRecordWithSSEVectorType(Context, FT))
764 return true;
765 }
766
767 return false;
768}
769
Daniel Dunbare59d8582010-09-16 20:42:06 +0000770unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
771 unsigned Align) const {
772 // Otherwise, if the alignment is less than or equal to the minimum ABI
773 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000774 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +0000775 return 0; // Use default alignment.
776
777 // On non-Darwin, the stack type alignment is always 4.
778 if (!IsDarwinVectorABI) {
779 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000780 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +0000781 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000782
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000783 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000784 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
785 isRecordWithSSEVectorType(getContext(), Ty)))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000786 return 16;
787
788 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000789}
790
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000791ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
792 unsigned &FreeRegs) const {
793 if (!ByVal) {
794 if (FreeRegs) {
795 --FreeRegs; // Non byval indirects just use one pointer.
796 return ABIArgInfo::getIndirectInReg(0, false);
797 }
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000798 return ABIArgInfo::getIndirect(0, false);
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000799 }
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000800
Daniel Dunbare59d8582010-09-16 20:42:06 +0000801 // Compute the byval alignment.
802 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
803 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
804 if (StackAlign == 0)
Chris Lattnerde92d732011-05-22 23:35:00 +0000805 return ABIArgInfo::getIndirect(4);
Daniel Dunbare59d8582010-09-16 20:42:06 +0000806
807 // If the stack alignment is less than the type alignment, realign the
808 // argument.
809 if (StackAlign < TypeAlign)
810 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
811 /*Realign=*/true);
812
813 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000814}
815
Rafael Espindolab48280b2012-07-31 02:44:24 +0000816X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
817 const Type *T = isSingleElementStruct(Ty, getContext());
818 if (!T)
819 T = Ty.getTypePtr();
820
821 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
822 BuiltinType::Kind K = BT->getKind();
823 if (K == BuiltinType::Float || K == BuiltinType::Double)
824 return Float;
825 }
826 return Integer;
827}
828
Rafael Espindolab6932692012-10-24 01:58:58 +0000829bool X86_32ABIInfo::shouldUseInReg(QualType Ty, unsigned &FreeRegs,
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000830 bool IsFastCall, bool &NeedsPadding) const {
831 NeedsPadding = false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000832 Class C = classify(Ty);
833 if (C == Float)
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000834 return false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000835
Rafael Espindolab6932692012-10-24 01:58:58 +0000836 unsigned Size = getContext().getTypeSize(Ty);
837 unsigned SizeInRegs = (Size + 31) / 32;
Rafael Espindola5f14fcb2012-10-23 02:04:01 +0000838
839 if (SizeInRegs == 0)
840 return false;
841
Rafael Espindolab48280b2012-07-31 02:44:24 +0000842 if (SizeInRegs > FreeRegs) {
843 FreeRegs = 0;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000844 return false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000845 }
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000846
Rafael Espindolab48280b2012-07-31 02:44:24 +0000847 FreeRegs -= SizeInRegs;
Rafael Espindolab6932692012-10-24 01:58:58 +0000848
849 if (IsFastCall) {
850 if (Size > 32)
851 return false;
852
853 if (Ty->isIntegralOrEnumerationType())
854 return true;
855
856 if (Ty->isPointerType())
857 return true;
858
859 if (Ty->isReferenceType())
860 return true;
861
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000862 if (FreeRegs)
863 NeedsPadding = true;
864
Rafael Espindolab6932692012-10-24 01:58:58 +0000865 return false;
866 }
867
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000868 return true;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000869}
870
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000871ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Rafael Espindolab6932692012-10-24 01:58:58 +0000872 unsigned &FreeRegs,
873 bool IsFastCall) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000874 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000875 if (isAggregateTypeForABI(Ty)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000876 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000877 if (IsWin32StructABI)
878 return getIndirectResult(Ty, true, FreeRegs);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000879
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000880 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, CGT))
881 return getIndirectResult(Ty, RAA == CGCXXABI::RAA_DirectInMemory, FreeRegs);
882
883 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000884 if (RT->getDecl()->hasFlexibleArrayMember())
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000885 return getIndirectResult(Ty, true, FreeRegs);
Anders Carlssona8874232010-01-27 03:25:19 +0000886 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000887
Eli Friedman5a4d3522011-11-18 00:28:11 +0000888 // Ignore empty structs/unions.
Eli Friedman5a1ac892011-11-18 04:01:36 +0000889 if (isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000890 return ABIArgInfo::getIgnore();
891
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000892 llvm::LLVMContext &LLVMContext = getVMContext();
893 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
894 bool NeedsPadding;
895 if (shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding)) {
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000896 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000897 SmallVector<llvm::Type*, 3> Elements;
898 for (unsigned I = 0; I < SizeInRegs; ++I)
899 Elements.push_back(Int32);
900 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
901 return ABIArgInfo::getDirectInReg(Result);
902 }
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000903 llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : 0;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000904
Daniel Dunbar53012f42009-11-09 01:33:53 +0000905 // Expand small (<= 128-bit) record types when we know that the stack layout
906 // of those arguments will match the struct. This is important because the
907 // LLVM backend isn't smart enough to remove byval, which inhibits many
908 // optimizations.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000909 if (getContext().getTypeSize(Ty) <= 4*32 &&
910 canExpandIndirectArgument(Ty, getContext()))
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000911 return ABIArgInfo::getExpandWithPadding(IsFastCall, PaddingType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000912
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000913 return getIndirectResult(Ty, true, FreeRegs);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000914 }
915
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000916 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +0000917 // On Darwin, some vectors are passed in memory, we handle this by passing
918 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000919 if (IsDarwinVectorABI) {
920 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000921 if ((Size == 8 || Size == 16 || Size == 32) ||
922 (Size == 64 && VT->getNumElements() == 1))
923 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
924 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000925 }
Bill Wendlingbb465d72010-10-18 03:41:31 +0000926
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000927 if (IsX86_MMXType(CGT.ConvertType(Ty)))
928 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64));
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000929
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000930 return ABIArgInfo::getDirect();
931 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000932
933
Chris Lattnera3c109b2010-07-29 02:16:43 +0000934 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
935 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000936
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000937 bool NeedsPadding;
938 bool InReg = shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding);
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000939
940 if (Ty->isPromotableIntegerType()) {
941 if (InReg)
942 return ABIArgInfo::getExtendInReg();
943 return ABIArgInfo::getExtend();
944 }
945 if (InReg)
946 return ABIArgInfo::getDirectInReg();
947 return ABIArgInfo::getDirect();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000948}
949
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000950void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
951 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
952 FI.getCallingConvention());
Rafael Espindolab48280b2012-07-31 02:44:24 +0000953
Rafael Espindolab6932692012-10-24 01:58:58 +0000954 unsigned CC = FI.getCallingConvention();
955 bool IsFastCall = CC == llvm::CallingConv::X86_FastCall;
956 unsigned FreeRegs;
957 if (IsFastCall)
958 FreeRegs = 2;
959 else if (FI.getHasRegParm())
960 FreeRegs = FI.getRegParm();
961 else
962 FreeRegs = DefaultNumRegisterParameters;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000963
964 // If the return value is indirect, then the hidden argument is consuming one
965 // integer register.
966 if (FI.getReturnInfo().isIndirect() && FreeRegs) {
967 --FreeRegs;
968 ABIArgInfo &Old = FI.getReturnInfo();
969 Old = ABIArgInfo::getIndirectInReg(Old.getIndirectAlign(),
970 Old.getIndirectByVal(),
971 Old.getIndirectRealign());
972 }
973
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000974 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
975 it != ie; ++it)
Rafael Espindolab6932692012-10-24 01:58:58 +0000976 it->info = classifyArgumentType(it->type, FreeRegs, IsFastCall);
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000977}
978
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000979llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
980 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +0000981 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000982
983 CGBuilderTy &Builder = CGF.Builder;
984 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
985 "ap");
986 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Eli Friedman7b1fb812011-11-18 02:12:09 +0000987
988 // Compute if the address needs to be aligned
989 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
990 Align = getTypeStackAlignInBytes(Ty, Align);
991 Align = std::max(Align, 4U);
992 if (Align > 4) {
993 // addr = (addr + align - 1) & -align;
994 llvm::Value *Offset =
995 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
996 Addr = CGF.Builder.CreateGEP(Addr, Offset);
997 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
998 CGF.Int32Ty);
999 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
1000 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1001 Addr->getType(),
1002 "ap.cur.aligned");
1003 }
1004
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001005 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00001006 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001007 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1008
1009 uint64_t Offset =
Eli Friedman7b1fb812011-11-18 02:12:09 +00001010 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001011 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00001012 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001013 "ap.next");
1014 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1015
1016 return AddrTyped;
1017}
1018
Charles Davis74f72932010-02-13 15:54:06 +00001019void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
1020 llvm::GlobalValue *GV,
1021 CodeGen::CodeGenModule &CGM) const {
1022 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1023 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
1024 // Get the LLVM function.
1025 llvm::Function *Fn = cast<llvm::Function>(GV);
1026
1027 // Now add the 'alignstack' attribute with a value of 16.
Bill Wendling0d583392012-10-15 20:36:26 +00001028 llvm::AttrBuilder B;
Bill Wendlinge91e9ec2012-10-14 03:28:14 +00001029 B.addStackAlignmentAttr(16);
Bill Wendling909b6de2013-01-23 00:21:06 +00001030 Fn->addAttributes(llvm::AttributeSet::FunctionIndex,
1031 llvm::AttributeSet::get(CGM.getLLVMContext(),
1032 llvm::AttributeSet::FunctionIndex,
1033 B));
Charles Davis74f72932010-02-13 15:54:06 +00001034 }
1035 }
1036}
1037
John McCall6374c332010-03-06 00:35:14 +00001038bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
1039 CodeGen::CodeGenFunction &CGF,
1040 llvm::Value *Address) const {
1041 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCall6374c332010-03-06 00:35:14 +00001042
Chris Lattner8b418682012-02-07 00:39:47 +00001043 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001044
John McCall6374c332010-03-06 00:35:14 +00001045 // 0-7 are the eight integer registers; the order is different
1046 // on Darwin (for EH), but the range is the same.
1047 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +00001048 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +00001049
John McCall64aa4b32013-04-16 22:48:15 +00001050 if (CGF.CGM.getTarget().getTriple().isOSDarwin()) {
John McCall6374c332010-03-06 00:35:14 +00001051 // 12-16 are st(0..4). Not sure why we stop at 4.
1052 // These have size 16, which is sizeof(long double) on
1053 // platforms with 8-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001054 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCallaeeb7012010-05-27 06:19:26 +00001055 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001056
John McCall6374c332010-03-06 00:35:14 +00001057 } else {
1058 // 9 is %eflags, which doesn't get a size on Darwin for some
1059 // reason.
1060 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
1061
1062 // 11-16 are st(0..5). Not sure why we stop at 5.
1063 // These have size 12, which is sizeof(long double) on
1064 // platforms with 4-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001065 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCallaeeb7012010-05-27 06:19:26 +00001066 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1067 }
John McCall6374c332010-03-06 00:35:14 +00001068
1069 return false;
1070}
1071
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001072//===----------------------------------------------------------------------===//
1073// X86-64 ABI Implementation
1074//===----------------------------------------------------------------------===//
1075
1076
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001077namespace {
1078/// X86_64ABIInfo - The X86_64 ABI information.
1079class X86_64ABIInfo : public ABIInfo {
1080 enum Class {
1081 Integer = 0,
1082 SSE,
1083 SSEUp,
1084 X87,
1085 X87Up,
1086 ComplexX87,
1087 NoClass,
1088 Memory
1089 };
1090
1091 /// merge - Implement the X86_64 ABI merging algorithm.
1092 ///
1093 /// Merge an accumulating classification \arg Accum with a field
1094 /// classification \arg Field.
1095 ///
1096 /// \param Accum - The accumulating classification. This should
1097 /// always be either NoClass or the result of a previous merge
1098 /// call. In addition, this should never be Memory (the caller
1099 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001100 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001101
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001102 /// postMerge - Implement the X86_64 ABI post merging algorithm.
1103 ///
1104 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
1105 /// final MEMORY or SSE classes when necessary.
1106 ///
1107 /// \param AggregateSize - The size of the current aggregate in
1108 /// the classification process.
1109 ///
1110 /// \param Lo - The classification for the parts of the type
1111 /// residing in the low word of the containing object.
1112 ///
1113 /// \param Hi - The classification for the parts of the type
1114 /// residing in the higher words of the containing object.
1115 ///
1116 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
1117
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001118 /// classify - Determine the x86_64 register classes in which the
1119 /// given type T should be passed.
1120 ///
1121 /// \param Lo - The classification for the parts of the type
1122 /// residing in the low word of the containing object.
1123 ///
1124 /// \param Hi - The classification for the parts of the type
1125 /// residing in the high word of the containing object.
1126 ///
1127 /// \param OffsetBase - The bit offset of this type in the
1128 /// containing object. Some parameters are classified different
1129 /// depending on whether they straddle an eightbyte boundary.
1130 ///
Eli Friedman7a1b5862013-06-12 00:13:45 +00001131 /// \param isNamedArg - Whether the argument in question is a "named"
1132 /// argument, as used in AMD64-ABI 3.5.7.
1133 ///
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001134 /// If a word is unused its result will be NoClass; if a type should
1135 /// be passed in Memory then at least the classification of \arg Lo
1136 /// will be Memory.
1137 ///
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001138 /// The \arg Lo class will be NoClass iff the argument is ignored.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001139 ///
1140 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
1141 /// also be ComplexX87.
Eli Friedman7a1b5862013-06-12 00:13:45 +00001142 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi,
1143 bool isNamedArg) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001144
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001145 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001146 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
1147 unsigned IROffset, QualType SourceTy,
1148 unsigned SourceOffset) const;
1149 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
1150 unsigned IROffset, QualType SourceTy,
1151 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001152
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001153 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001154 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +00001155 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001156
1157 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001158 /// such that the argument will be passed in memory.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001159 ///
1160 /// \param freeIntRegs - The number of free integer registers remaining
1161 /// available.
1162 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001163
Chris Lattnera3c109b2010-07-29 02:16:43 +00001164 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001165
Bill Wendlingbb465d72010-10-18 03:41:31 +00001166 ABIArgInfo classifyArgumentType(QualType Ty,
Daniel Dunbaredfac032012-03-10 01:03:58 +00001167 unsigned freeIntRegs,
Bill Wendlingbb465d72010-10-18 03:41:31 +00001168 unsigned &neededInt,
Eli Friedman7a1b5862013-06-12 00:13:45 +00001169 unsigned &neededSSE,
1170 bool isNamedArg) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001171
Eli Friedmanee1ad992011-12-02 00:11:43 +00001172 bool IsIllegalVectorType(QualType Ty) const;
1173
John McCall67a57732011-04-21 01:20:55 +00001174 /// The 0.98 ABI revision clarified a lot of ambiguities,
1175 /// unfortunately in ways that were not always consistent with
1176 /// certain previous compilers. In particular, platforms which
1177 /// required strict binary compatibility with older versions of GCC
1178 /// may need to exempt themselves.
1179 bool honorsRevision0_98() const {
John McCall64aa4b32013-04-16 22:48:15 +00001180 return !getTarget().getTriple().isOSDarwin();
John McCall67a57732011-04-21 01:20:55 +00001181 }
1182
Eli Friedmanee1ad992011-12-02 00:11:43 +00001183 bool HasAVX;
Derek Schuffbabaf312012-10-11 15:52:22 +00001184 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
1185 // 64-bit hardware.
1186 bool Has64BitPointers;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001187
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001188public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001189 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
Derek Schuffbabaf312012-10-11 15:52:22 +00001190 ABIInfo(CGT), HasAVX(hasavx),
Derek Schuff90da80c2012-10-11 18:21:13 +00001191 Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001192 }
Chris Lattner9c254f02010-06-29 06:01:59 +00001193
John McCallde5d3c72012-02-17 03:33:10 +00001194 bool isPassedUsingAVXType(QualType type) const {
1195 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00001196 // The freeIntRegs argument doesn't matter here.
Eli Friedman7a1b5862013-06-12 00:13:45 +00001197 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE,
1198 /*isNamedArg*/true);
John McCallde5d3c72012-02-17 03:33:10 +00001199 if (info.isDirect()) {
1200 llvm::Type *ty = info.getCoerceToType();
1201 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
1202 return (vectorTy->getBitWidth() > 128);
1203 }
1204 return false;
1205 }
1206
Chris Lattneree5dcd02010-07-29 02:31:05 +00001207 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001208
1209 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1210 CodeGenFunction &CGF) const;
1211};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001212
Chris Lattnerf13721d2010-08-31 16:44:54 +00001213/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001214class WinX86_64ABIInfo : public ABIInfo {
1215
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00001216 ABIArgInfo classify(QualType Ty, bool IsReturnType) const;
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001217
Chris Lattnerf13721d2010-08-31 16:44:54 +00001218public:
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001219 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
1220
1221 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattnerf13721d2010-08-31 16:44:54 +00001222
1223 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1224 CodeGenFunction &CGF) const;
1225};
1226
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001227class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1228public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001229 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
Derek Schuffbabaf312012-10-11 15:52:22 +00001230 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
John McCall6374c332010-03-06 00:35:14 +00001231
John McCallde5d3c72012-02-17 03:33:10 +00001232 const X86_64ABIInfo &getABIInfo() const {
1233 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
1234 }
1235
John McCall6374c332010-03-06 00:35:14 +00001236 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1237 return 7;
1238 }
1239
1240 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1241 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001242 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001243
John McCallaeeb7012010-05-27 06:19:26 +00001244 // 0-15 are the 16 integer registers.
1245 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001246 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +00001247 return false;
1248 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001249
Jay Foadef6de3d2011-07-11 09:56:20 +00001250 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001251 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +00001252 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001253 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1254 }
1255
John McCallde5d3c72012-02-17 03:33:10 +00001256 bool isNoProtoCallVariadic(const CallArgList &args,
1257 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +00001258 // The default CC on x86-64 sets %al to the number of SSA
1259 // registers used, and GCC sets this when calling an unprototyped
Eli Friedman3ed79032011-12-01 04:53:19 +00001260 // function, so we override the default behavior. However, don't do
Eli Friedman68805fe2011-12-06 03:08:26 +00001261 // that when AVX types are involved: the ABI explicitly states it is
1262 // undefined, and it doesn't work in practice because of how the ABI
1263 // defines varargs anyway.
John McCallde5d3c72012-02-17 03:33:10 +00001264 if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) {
Eli Friedman3ed79032011-12-01 04:53:19 +00001265 bool HasAVXType = false;
John McCallde5d3c72012-02-17 03:33:10 +00001266 for (CallArgList::const_iterator
1267 it = args.begin(), ie = args.end(); it != ie; ++it) {
1268 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1269 HasAVXType = true;
1270 break;
Eli Friedman3ed79032011-12-01 04:53:19 +00001271 }
1272 }
John McCallde5d3c72012-02-17 03:33:10 +00001273
Eli Friedman3ed79032011-12-01 04:53:19 +00001274 if (!HasAVXType)
1275 return true;
1276 }
John McCall01f151e2011-09-21 08:08:30 +00001277
John McCallde5d3c72012-02-17 03:33:10 +00001278 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
John McCall01f151e2011-09-21 08:08:30 +00001279 }
1280
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001281};
1282
Aaron Ballman89735b92013-05-24 15:06:56 +00001283static std::string qualifyWindowsLibrary(llvm::StringRef Lib) {
1284 // If the argument does not end in .lib, automatically add the suffix. This
1285 // matches the behavior of MSVC.
1286 std::string ArgStr = Lib;
1287 if (Lib.size() <= 4 ||
1288 Lib.substr(Lib.size() - 4).compare_lower(".lib") != 0) {
1289 ArgStr += ".lib";
1290 }
1291 return ArgStr;
1292}
1293
Reid Kleckner3190ca92013-05-08 13:44:39 +00001294class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo {
1295public:
1296 WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned RegParms)
1297 : X86_32TargetCodeGenInfo(CGT, false, true, true, RegParms) {}
1298
1299 void getDependentLibraryOption(llvm::StringRef Lib,
1300 llvm::SmallString<24> &Opt) const {
1301 Opt = "/DEFAULTLIB:";
Aaron Ballman89735b92013-05-24 15:06:56 +00001302 Opt += qualifyWindowsLibrary(Lib);
Reid Kleckner3190ca92013-05-08 13:44:39 +00001303 }
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00001304
1305 void getDetectMismatchOption(llvm::StringRef Name,
1306 llvm::StringRef Value,
1307 llvm::SmallString<32> &Opt) const {
Eli Friedman572ac322013-06-07 22:42:22 +00001308 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00001309 }
Reid Kleckner3190ca92013-05-08 13:44:39 +00001310};
1311
Chris Lattnerf13721d2010-08-31 16:44:54 +00001312class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1313public:
1314 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1315 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1316
1317 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1318 return 7;
1319 }
1320
1321 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1322 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001323 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001324
Chris Lattnerf13721d2010-08-31 16:44:54 +00001325 // 0-15 are the 16 integer registers.
1326 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001327 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattnerf13721d2010-08-31 16:44:54 +00001328 return false;
1329 }
Reid Kleckner3190ca92013-05-08 13:44:39 +00001330
1331 void getDependentLibraryOption(llvm::StringRef Lib,
1332 llvm::SmallString<24> &Opt) const {
1333 Opt = "/DEFAULTLIB:";
Aaron Ballman89735b92013-05-24 15:06:56 +00001334 Opt += qualifyWindowsLibrary(Lib);
Reid Kleckner3190ca92013-05-08 13:44:39 +00001335 }
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00001336
1337 void getDetectMismatchOption(llvm::StringRef Name,
1338 llvm::StringRef Value,
1339 llvm::SmallString<32> &Opt) const {
Eli Friedman572ac322013-06-07 22:42:22 +00001340 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00001341 }
Chris Lattnerf13721d2010-08-31 16:44:54 +00001342};
1343
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001344}
1345
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001346void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1347 Class &Hi) const {
1348 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1349 //
1350 // (a) If one of the classes is Memory, the whole argument is passed in
1351 // memory.
1352 //
1353 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1354 // memory.
1355 //
1356 // (c) If the size of the aggregate exceeds two eightbytes and the first
1357 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1358 // argument is passed in memory. NOTE: This is necessary to keep the
1359 // ABI working for processors that don't support the __m256 type.
1360 //
1361 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1362 //
1363 // Some of these are enforced by the merging logic. Others can arise
1364 // only with unions; for example:
1365 // union { _Complex double; unsigned; }
1366 //
1367 // Note that clauses (b) and (c) were added in 0.98.
1368 //
1369 if (Hi == Memory)
1370 Lo = Memory;
1371 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1372 Lo = Memory;
1373 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1374 Lo = Memory;
1375 if (Hi == SSEUp && Lo != SSE)
1376 Hi = SSE;
1377}
1378
Chris Lattner1090a9b2010-06-28 21:43:59 +00001379X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001380 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1381 // classified recursively so that always two fields are
1382 // considered. The resulting class is calculated according to
1383 // the classes of the fields in the eightbyte:
1384 //
1385 // (a) If both classes are equal, this is the resulting class.
1386 //
1387 // (b) If one of the classes is NO_CLASS, the resulting class is
1388 // the other class.
1389 //
1390 // (c) If one of the classes is MEMORY, the result is the MEMORY
1391 // class.
1392 //
1393 // (d) If one of the classes is INTEGER, the result is the
1394 // INTEGER.
1395 //
1396 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1397 // MEMORY is used as class.
1398 //
1399 // (f) Otherwise class SSE is used.
1400
1401 // Accum should never be memory (we should have returned) or
1402 // ComplexX87 (because this cannot be passed in a structure).
1403 assert((Accum != Memory && Accum != ComplexX87) &&
1404 "Invalid accumulated classification during merge.");
1405 if (Accum == Field || Field == NoClass)
1406 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001407 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001408 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001409 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001410 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001411 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001412 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001413 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1414 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001415 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001416 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001417}
1418
Chris Lattnerbcaedae2010-06-30 19:14:05 +00001419void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Eli Friedman7a1b5862013-06-12 00:13:45 +00001420 Class &Lo, Class &Hi, bool isNamedArg) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001421 // FIXME: This code can be simplified by introducing a simple value class for
1422 // Class pairs with appropriate constructor methods for the various
1423 // situations.
1424
1425 // FIXME: Some of the split computations are wrong; unaligned vectors
1426 // shouldn't be passed in registers for example, so there is no chance they
1427 // can straddle an eightbyte. Verify & simplify.
1428
1429 Lo = Hi = NoClass;
1430
1431 Class &Current = OffsetBase < 64 ? Lo : Hi;
1432 Current = Memory;
1433
John McCall183700f2009-09-21 23:43:11 +00001434 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001435 BuiltinType::Kind k = BT->getKind();
1436
1437 if (k == BuiltinType::Void) {
1438 Current = NoClass;
1439 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1440 Lo = Integer;
1441 Hi = Integer;
1442 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1443 Current = Integer;
Derek Schuff7da46f92012-10-11 16:55:58 +00001444 } else if ((k == BuiltinType::Float || k == BuiltinType::Double) ||
1445 (k == BuiltinType::LongDouble &&
John McCall64aa4b32013-04-16 22:48:15 +00001446 getTarget().getTriple().getOS() == llvm::Triple::NaCl)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001447 Current = SSE;
1448 } else if (k == BuiltinType::LongDouble) {
1449 Lo = X87;
1450 Hi = X87Up;
1451 }
1452 // FIXME: _Decimal32 and _Decimal64 are SSE.
1453 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001454 return;
1455 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001456
Chris Lattner1090a9b2010-06-28 21:43:59 +00001457 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001458 // Classify the underlying integer type.
Eli Friedman7a1b5862013-06-12 00:13:45 +00001459 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001460 return;
1461 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001462
Chris Lattner1090a9b2010-06-28 21:43:59 +00001463 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001464 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001465 return;
1466 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001467
Chris Lattner1090a9b2010-06-28 21:43:59 +00001468 if (Ty->isMemberPointerType()) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001469 if (Ty->isMemberFunctionPointerType() && Has64BitPointers)
Daniel Dunbar67d438d2010-05-15 00:00:37 +00001470 Lo = Hi = Integer;
1471 else
1472 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001473 return;
1474 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001475
Chris Lattner1090a9b2010-06-28 21:43:59 +00001476 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001477 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001478 if (Size == 32) {
1479 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1480 // float> as integer.
1481 Current = Integer;
1482
1483 // If this type crosses an eightbyte boundary, it should be
1484 // split.
1485 uint64_t EB_Real = (OffsetBase) / 64;
1486 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1487 if (EB_Real != EB_Imag)
1488 Hi = Lo;
1489 } else if (Size == 64) {
1490 // gcc passes <1 x double> in memory. :(
1491 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1492 return;
1493
1494 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001495 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001496 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1497 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1498 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001499 Current = Integer;
1500 else
1501 Current = SSE;
1502
1503 // If this type crosses an eightbyte boundary, it should be
1504 // split.
1505 if (OffsetBase && OffsetBase != 64)
1506 Hi = Lo;
Eli Friedman7a1b5862013-06-12 00:13:45 +00001507 } else if (Size == 128 || (HasAVX && isNamedArg && Size == 256)) {
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001508 // Arguments of 256-bits are split into four eightbyte chunks. The
1509 // least significant one belongs to class SSE and all the others to class
1510 // SSEUP. The original Lo and Hi design considers that types can't be
1511 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1512 // This design isn't correct for 256-bits, but since there're no cases
1513 // where the upper parts would need to be inspected, avoid adding
1514 // complexity and just consider Hi to match the 64-256 part.
Eli Friedman7a1b5862013-06-12 00:13:45 +00001515 //
1516 // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in
1517 // registers if they are "named", i.e. not part of the "..." of a
1518 // variadic function.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001519 Lo = SSE;
1520 Hi = SSEUp;
1521 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001522 return;
1523 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001524
Chris Lattner1090a9b2010-06-28 21:43:59 +00001525 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001526 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001527
Chris Lattnerea044322010-07-29 02:01:43 +00001528 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001529 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001530 if (Size <= 64)
1531 Current = Integer;
1532 else if (Size <= 128)
1533 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001534 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001535 Current = SSE;
Derek Schuff7da46f92012-10-11 16:55:58 +00001536 else if (ET == getContext().DoubleTy ||
1537 (ET == getContext().LongDoubleTy &&
John McCall64aa4b32013-04-16 22:48:15 +00001538 getTarget().getTriple().getOS() == llvm::Triple::NaCl))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001539 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001540 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001541 Current = ComplexX87;
1542
1543 // If this complex type crosses an eightbyte boundary then it
1544 // should be split.
1545 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001546 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001547 if (Hi == NoClass && EB_Real != EB_Imag)
1548 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001549
Chris Lattner1090a9b2010-06-28 21:43:59 +00001550 return;
1551 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001552
Chris Lattnerea044322010-07-29 02:01:43 +00001553 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001554 // Arrays are treated like structures.
1555
Chris Lattnerea044322010-07-29 02:01:43 +00001556 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001557
1558 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001559 // than four eightbytes, ..., it has class MEMORY.
1560 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001561 return;
1562
1563 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1564 // fields, it has class MEMORY.
1565 //
1566 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001567 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001568 return;
1569
1570 // Otherwise implement simplified merge. We could be smarter about
1571 // this, but it isn't worth it and would be harder to verify.
1572 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001573 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001574 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes089d8922011-07-12 01:27:38 +00001575
1576 // The only case a 256-bit wide vector could be used is when the array
1577 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1578 // to work for sizes wider than 128, early check and fallback to memory.
1579 if (Size > 128 && EltSize != 256)
1580 return;
1581
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001582 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1583 Class FieldLo, FieldHi;
Eli Friedman7a1b5862013-06-12 00:13:45 +00001584 classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001585 Lo = merge(Lo, FieldLo);
1586 Hi = merge(Hi, FieldHi);
1587 if (Lo == Memory || Hi == Memory)
1588 break;
1589 }
1590
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001591 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001592 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001593 return;
1594 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001595
Chris Lattner1090a9b2010-06-28 21:43:59 +00001596 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001597 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001598
1599 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001600 // than four eightbytes, ..., it has class MEMORY.
1601 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001602 return;
1603
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001604 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1605 // copy constructor or a non-trivial destructor, it is passed by invisible
1606 // reference.
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00001607 if (getRecordArgABI(RT, CGT))
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001608 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001609
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001610 const RecordDecl *RD = RT->getDecl();
1611
1612 // Assume variable sized types are passed in memory.
1613 if (RD->hasFlexibleArrayMember())
1614 return;
1615
Chris Lattnerea044322010-07-29 02:01:43 +00001616 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001617
1618 // Reset Lo class, this will be recomputed.
1619 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001620
1621 // If this is a C++ record, classify the bases first.
1622 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1623 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1624 e = CXXRD->bases_end(); i != e; ++i) {
1625 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1626 "Unexpected base class!");
1627 const CXXRecordDecl *Base =
1628 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1629
1630 // Classify this field.
1631 //
1632 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1633 // single eightbyte, each is classified separately. Each eightbyte gets
1634 // initialized to class NO_CLASS.
1635 Class FieldLo, FieldHi;
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001636 uint64_t Offset =
1637 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
Eli Friedman7a1b5862013-06-12 00:13:45 +00001638 classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001639 Lo = merge(Lo, FieldLo);
1640 Hi = merge(Hi, FieldHi);
1641 if (Lo == Memory || Hi == Memory)
1642 break;
1643 }
1644 }
1645
1646 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001647 unsigned idx = 0;
Bruno Cardoso Lopes548e4782011-07-12 22:30:58 +00001648 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001649 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001650 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1651 bool BitField = i->isBitField();
1652
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001653 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1654 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001655 //
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001656 // The only case a 256-bit wide vector could be used is when the struct
1657 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1658 // to work for sizes wider than 128, early check and fallback to memory.
1659 //
1660 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1661 Lo = Memory;
1662 return;
1663 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001664 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001665 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001666 Lo = Memory;
1667 return;
1668 }
1669
1670 // Classify this field.
1671 //
1672 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1673 // exceeds a single eightbyte, each is classified
1674 // separately. Each eightbyte gets initialized to class
1675 // NO_CLASS.
1676 Class FieldLo, FieldHi;
1677
1678 // Bit-fields require special handling, they do not force the
1679 // structure to be passed in memory even if unaligned, and
1680 // therefore they can straddle an eightbyte.
1681 if (BitField) {
1682 // Ignore padding bit-fields.
1683 if (i->isUnnamedBitfield())
1684 continue;
1685
1686 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001687 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001688
1689 uint64_t EB_Lo = Offset / 64;
1690 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1691 FieldLo = FieldHi = NoClass;
1692 if (EB_Lo) {
1693 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1694 FieldLo = NoClass;
1695 FieldHi = Integer;
1696 } else {
1697 FieldLo = Integer;
1698 FieldHi = EB_Hi ? Integer : NoClass;
1699 }
1700 } else
Eli Friedman7a1b5862013-06-12 00:13:45 +00001701 classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001702 Lo = merge(Lo, FieldLo);
1703 Hi = merge(Hi, FieldHi);
1704 if (Lo == Memory || Hi == Memory)
1705 break;
1706 }
1707
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001708 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001709 }
1710}
1711
Chris Lattner9c254f02010-06-29 06:01:59 +00001712ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001713 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1714 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001715 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001716 // Treat an enum type as its underlying type.
1717 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1718 Ty = EnumTy->getDecl()->getIntegerType();
1719
1720 return (Ty->isPromotableIntegerType() ?
1721 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1722 }
1723
1724 return ABIArgInfo::getIndirect(0);
1725}
1726
Eli Friedmanee1ad992011-12-02 00:11:43 +00001727bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1728 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1729 uint64_t Size = getContext().getTypeSize(VecTy);
1730 unsigned LargestVector = HasAVX ? 256 : 128;
1731 if (Size <= 64 || Size > LargestVector)
1732 return true;
1733 }
1734
1735 return false;
1736}
1737
Daniel Dunbaredfac032012-03-10 01:03:58 +00001738ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1739 unsigned freeIntRegs) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001740 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1741 // place naturally.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001742 //
1743 // This assumption is optimistic, as there could be free registers available
1744 // when we need to pass this argument in memory, and LLVM could try to pass
1745 // the argument in the free register. This does not seem to happen currently,
1746 // but this code would be much safer if we could mark the argument with
1747 // 'onstack'. See PR12193.
Eli Friedmanee1ad992011-12-02 00:11:43 +00001748 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001749 // Treat an enum type as its underlying type.
1750 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1751 Ty = EnumTy->getDecl()->getIntegerType();
1752
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001753 return (Ty->isPromotableIntegerType() ?
1754 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001755 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001756
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00001757 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
1758 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001759
Chris Lattner855d2272011-05-22 23:21:23 +00001760 // Compute the byval alignment. We specify the alignment of the byval in all
1761 // cases so that the mid-level optimizer knows the alignment of the byval.
1762 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
Daniel Dunbaredfac032012-03-10 01:03:58 +00001763
1764 // Attempt to avoid passing indirect results using byval when possible. This
1765 // is important for good codegen.
1766 //
1767 // We do this by coercing the value into a scalar type which the backend can
1768 // handle naturally (i.e., without using byval).
1769 //
1770 // For simplicity, we currently only do this when we have exhausted all of the
1771 // free integer registers. Doing this when there are free integer registers
1772 // would require more care, as we would have to ensure that the coerced value
1773 // did not claim the unused register. That would require either reording the
1774 // arguments to the function (so that any subsequent inreg values came first),
1775 // or only doing this optimization when there were no following arguments that
1776 // might be inreg.
1777 //
1778 // We currently expect it to be rare (particularly in well written code) for
1779 // arguments to be passed on the stack when there are still free integer
1780 // registers available (this would typically imply large structs being passed
1781 // by value), so this seems like a fair tradeoff for now.
1782 //
1783 // We can revisit this if the backend grows support for 'onstack' parameter
1784 // attributes. See PR12193.
1785 if (freeIntRegs == 0) {
1786 uint64_t Size = getContext().getTypeSize(Ty);
1787
1788 // If this type fits in an eightbyte, coerce it into the matching integral
1789 // type, which will end up on the stack (with alignment 8).
1790 if (Align == 8 && Size <= 64)
1791 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1792 Size));
1793 }
1794
Chris Lattner855d2272011-05-22 23:21:23 +00001795 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001796}
1797
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001798/// GetByteVectorType - The ABI specifies that a value should be passed in an
1799/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner0f408f52010-07-29 04:56:46 +00001800/// vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001801llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001802 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001803
Chris Lattner15842bd2010-07-29 05:02:29 +00001804 // Wrapper structs that just contain vectors are passed just like vectors,
1805 // strip them off if present.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001806 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner15842bd2010-07-29 05:02:29 +00001807 while (STy && STy->getNumElements() == 1) {
1808 IRType = STy->getElementType(0);
1809 STy = dyn_cast<llvm::StructType>(IRType);
1810 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001811
Bruno Cardoso Lopes528a8c72011-07-08 22:57:35 +00001812 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001813 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1814 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001815 unsigned BitWidth = VT->getBitWidth();
Tanya Lattnerce275672011-11-28 23:18:11 +00001816 if ((BitWidth >= 128 && BitWidth <= 256) &&
Chris Lattner0f408f52010-07-29 04:56:46 +00001817 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1818 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1819 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1820 EltTy->isIntegerTy(128)))
1821 return VT;
1822 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001823
Chris Lattner0f408f52010-07-29 04:56:46 +00001824 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1825}
1826
Chris Lattnere2962be2010-07-29 07:30:00 +00001827/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1828/// is known to either be off the end of the specified type or being in
1829/// alignment padding. The user type specified is known to be at most 128 bits
1830/// in size, and have passed through X86_64ABIInfo::classify with a successful
1831/// classification that put one of the two halves in the INTEGER class.
1832///
1833/// It is conservatively correct to return false.
1834static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1835 unsigned EndBit, ASTContext &Context) {
1836 // If the bytes being queried are off the end of the type, there is no user
1837 // data hiding here. This handles analysis of builtins, vectors and other
1838 // types that don't contain interesting padding.
1839 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1840 if (TySize <= StartBit)
1841 return true;
1842
Chris Lattner021c3a32010-07-29 07:43:55 +00001843 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1844 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1845 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1846
1847 // Check each element to see if the element overlaps with the queried range.
1848 for (unsigned i = 0; i != NumElts; ++i) {
1849 // If the element is after the span we care about, then we're done..
1850 unsigned EltOffset = i*EltSize;
1851 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001852
Chris Lattner021c3a32010-07-29 07:43:55 +00001853 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1854 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1855 EndBit-EltOffset, Context))
1856 return false;
1857 }
1858 // If it overlaps no elements, then it is safe to process as padding.
1859 return true;
1860 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001861
Chris Lattnere2962be2010-07-29 07:30:00 +00001862 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1863 const RecordDecl *RD = RT->getDecl();
1864 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001865
Chris Lattnere2962be2010-07-29 07:30:00 +00001866 // If this is a C++ record, check the bases first.
1867 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1868 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1869 e = CXXRD->bases_end(); i != e; ++i) {
1870 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1871 "Unexpected base class!");
1872 const CXXRecordDecl *Base =
1873 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001874
Chris Lattnere2962be2010-07-29 07:30:00 +00001875 // If the base is after the span we care about, ignore it.
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001876 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
Chris Lattnere2962be2010-07-29 07:30:00 +00001877 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001878
Chris Lattnere2962be2010-07-29 07:30:00 +00001879 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1880 if (!BitsContainNoUserData(i->getType(), BaseStart,
1881 EndBit-BaseOffset, Context))
1882 return false;
1883 }
1884 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001885
Chris Lattnere2962be2010-07-29 07:30:00 +00001886 // Verify that no field has data that overlaps the region of interest. Yes
1887 // this could be sped up a lot by being smarter about queried fields,
1888 // however we're only looking at structs up to 16 bytes, so we don't care
1889 // much.
1890 unsigned idx = 0;
1891 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1892 i != e; ++i, ++idx) {
1893 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001894
Chris Lattnere2962be2010-07-29 07:30:00 +00001895 // If we found a field after the region we care about, then we're done.
1896 if (FieldOffset >= EndBit) break;
1897
1898 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1899 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1900 Context))
1901 return false;
1902 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001903
Chris Lattnere2962be2010-07-29 07:30:00 +00001904 // If nothing in this record overlapped the area of interest, then we're
1905 // clean.
1906 return true;
1907 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001908
Chris Lattnere2962be2010-07-29 07:30:00 +00001909 return false;
1910}
1911
Chris Lattner0b362002010-07-29 18:39:32 +00001912/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1913/// float member at the specified offset. For example, {int,{float}} has a
1914/// float at offset 4. It is conservatively correct for this routine to return
1915/// false.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001916static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Micah Villmow25a6a842012-10-08 16:25:52 +00001917 const llvm::DataLayout &TD) {
Chris Lattner0b362002010-07-29 18:39:32 +00001918 // Base case if we find a float.
1919 if (IROffset == 0 && IRType->isFloatTy())
1920 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001921
Chris Lattner0b362002010-07-29 18:39:32 +00001922 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001923 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner0b362002010-07-29 18:39:32 +00001924 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1925 unsigned Elt = SL->getElementContainingOffset(IROffset);
1926 IROffset -= SL->getElementOffset(Elt);
1927 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1928 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001929
Chris Lattner0b362002010-07-29 18:39:32 +00001930 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001931 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1932 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner0b362002010-07-29 18:39:32 +00001933 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1934 IROffset -= IROffset/EltSize*EltSize;
1935 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1936 }
1937
1938 return false;
1939}
1940
Chris Lattnerf47c9442010-07-29 18:13:09 +00001941
1942/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1943/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001944llvm::Type *X86_64ABIInfo::
1945GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00001946 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001947 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001948 // pass as float if the last 4 bytes is just padding. This happens for
1949 // structs that contain 3 floats.
1950 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1951 SourceOffset*8+64, getContext()))
1952 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001953
Chris Lattner0b362002010-07-29 18:39:32 +00001954 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1955 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1956 // case.
Micah Villmow25a6a842012-10-08 16:25:52 +00001957 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
1958 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001959 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001960
Chris Lattnerf47c9442010-07-29 18:13:09 +00001961 return llvm::Type::getDoubleTy(getVMContext());
1962}
1963
1964
Chris Lattner0d2656d2010-07-29 17:40:35 +00001965/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1966/// an 8-byte GPR. This means that we either have a scalar or we are talking
1967/// about the high or low part of an up-to-16-byte struct. This routine picks
1968/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001969/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1970/// etc).
1971///
1972/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1973/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1974/// the 8-byte value references. PrefType may be null.
1975///
1976/// SourceTy is the source level type for the entire argument. SourceOffset is
1977/// an offset into this that we're processing (which is always either 0 or 8).
1978///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001979llvm::Type *X86_64ABIInfo::
1980GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00001981 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001982 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1983 // returning an 8-byte unit starting with it. See if we can safely use it.
1984 if (IROffset == 0) {
1985 // Pointers and int64's always fill the 8-byte unit.
Derek Schuffbabaf312012-10-11 15:52:22 +00001986 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
1987 IRType->isIntegerTy(64))
Chris Lattnere2962be2010-07-29 07:30:00 +00001988 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001989
Chris Lattnere2962be2010-07-29 07:30:00 +00001990 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1991 // goodness in the source type is just tail padding. This is allowed to
1992 // kick in for struct {double,int} on the int, but not on
1993 // struct{double,int,int} because we wouldn't return the second int. We
1994 // have to do this analysis on the source type because we can't depend on
1995 // unions being lowered a specific way etc.
1996 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
Derek Schuffbabaf312012-10-11 15:52:22 +00001997 IRType->isIntegerTy(32) ||
1998 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
1999 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
2000 cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002001
Chris Lattnere2962be2010-07-29 07:30:00 +00002002 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
2003 SourceOffset*8+64, getContext()))
2004 return IRType;
2005 }
2006 }
Chris Lattner49382de2010-07-28 22:44:07 +00002007
Chris Lattner2acc6e32011-07-18 04:24:23 +00002008 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00002009 // If this is a struct, recurse into the field at the specified offset.
Micah Villmow25a6a842012-10-08 16:25:52 +00002010 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00002011 if (IROffset < SL->getSizeInBytes()) {
2012 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
2013 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002014
Chris Lattner0d2656d2010-07-29 17:40:35 +00002015 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
2016 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002017 }
Chris Lattner49382de2010-07-28 22:44:07 +00002018 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002019
Chris Lattner2acc6e32011-07-18 04:24:23 +00002020 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002021 llvm::Type *EltTy = ATy->getElementType();
Micah Villmow25a6a842012-10-08 16:25:52 +00002022 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
Chris Lattner021c3a32010-07-29 07:43:55 +00002023 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00002024 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
2025 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00002026 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002027
Chris Lattner49382de2010-07-28 22:44:07 +00002028 // Okay, we don't have any better idea of what to pass, so we pass this in an
2029 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00002030 unsigned TySizeInBytes =
2031 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00002032
Chris Lattner9e45a3d2010-07-29 17:34:39 +00002033 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002034
Chris Lattner49382de2010-07-28 22:44:07 +00002035 // It is always safe to classify this as an integer type up to i64 that
2036 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00002037 return llvm::IntegerType::get(getVMContext(),
2038 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00002039}
2040
Chris Lattner66e7b682010-09-01 00:50:20 +00002041
2042/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
2043/// be used as elements of a two register pair to pass or return, return a
2044/// first class aggregate to represent them. For example, if the low part of
2045/// a by-value argument should be passed as i32* and the high part as float,
2046/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002047static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00002048GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Micah Villmow25a6a842012-10-08 16:25:52 +00002049 const llvm::DataLayout &TD) {
Chris Lattner66e7b682010-09-01 00:50:20 +00002050 // In order to correctly satisfy the ABI, we need to the high part to start
2051 // at offset 8. If the high and low parts we inferred are both 4-byte types
2052 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
2053 // the second element at offset 8. Check for this:
2054 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
2055 unsigned HiAlign = TD.getABITypeAlignment(Hi);
Micah Villmow25a6a842012-10-08 16:25:52 +00002056 unsigned HiStart = llvm::DataLayout::RoundUpAlignment(LoSize, HiAlign);
Chris Lattner66e7b682010-09-01 00:50:20 +00002057 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002058
Chris Lattner66e7b682010-09-01 00:50:20 +00002059 // To handle this, we have to increase the size of the low part so that the
2060 // second element will start at an 8 byte offset. We can't increase the size
2061 // of the second element because it might make us access off the end of the
2062 // struct.
2063 if (HiStart != 8) {
2064 // There are only two sorts of types the ABI generation code can produce for
2065 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
2066 // Promote these to a larger type.
2067 if (Lo->isFloatTy())
2068 Lo = llvm::Type::getDoubleTy(Lo->getContext());
2069 else {
2070 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
2071 Lo = llvm::Type::getInt64Ty(Lo->getContext());
2072 }
2073 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002074
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002075 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002076
2077
Chris Lattner66e7b682010-09-01 00:50:20 +00002078 // Verify that the second element is at an 8-byte offset.
2079 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
2080 "Invalid x86-64 argument pair!");
2081 return Result;
2082}
2083
Chris Lattner519f68c2010-07-28 23:06:14 +00002084ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00002085classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00002086 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
2087 // classification algorithm.
2088 X86_64ABIInfo::Class Lo, Hi;
Eli Friedman7a1b5862013-06-12 00:13:45 +00002089 classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true);
Chris Lattner519f68c2010-07-28 23:06:14 +00002090
2091 // Check some invariants.
2092 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002093 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2094
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002095 llvm::Type *ResType = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002096 switch (Lo) {
2097 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002098 if (Hi == NoClass)
2099 return ABIArgInfo::getIgnore();
2100 // If the low part is just padding, it takes no register, leave ResType
2101 // null.
2102 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2103 "Unknown missing lo part");
2104 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002105
2106 case SSEUp:
2107 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002108 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002109
2110 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
2111 // hidden argument.
2112 case Memory:
2113 return getIndirectReturnResult(RetTy);
2114
2115 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
2116 // available register of the sequence %rax, %rdx is used.
2117 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002118 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002119
Chris Lattnereb518b42010-07-29 21:42:50 +00002120 // If we have a sign or zero extended integer, make sure to return Extend
2121 // so that the parameter gets the right LLVM IR attributes.
2122 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2123 // Treat an enum type as its underlying type.
2124 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2125 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002126
Chris Lattnereb518b42010-07-29 21:42:50 +00002127 if (RetTy->isIntegralOrEnumerationType() &&
2128 RetTy->isPromotableIntegerType())
2129 return ABIArgInfo::getExtend();
2130 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002131 break;
2132
2133 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
2134 // available SSE register of the sequence %xmm0, %xmm1 is used.
2135 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002136 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00002137 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002138
2139 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
2140 // returned on the X87 stack in %st0 as 80-bit x87 number.
2141 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00002142 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00002143 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002144
2145 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
2146 // part of the value is returned in %st0 and the imaginary part in
2147 // %st1.
2148 case ComplexX87:
2149 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00002150 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00002151 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00002152 NULL);
2153 break;
2154 }
2155
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002156 llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002157 switch (Hi) {
2158 // Memory was handled previously and X87 should
2159 // never occur as a hi class.
2160 case Memory:
2161 case X87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002162 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002163
2164 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00002165 case NoClass:
2166 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002167
Chris Lattner3db4dde2010-09-01 00:20:33 +00002168 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002169 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002170 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2171 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002172 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00002173 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002174 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002175 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2176 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002177 break;
2178
2179 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002180 // is passed in the next available eightbyte chunk if the last used
2181 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00002182 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002183 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00002184 case SSEUp:
2185 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002186 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00002187 break;
2188
2189 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
2190 // returned together with the previous X87 value in %st0.
2191 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002192 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00002193 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002194 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00002195 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00002196 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002197 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002198 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2199 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00002200 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002201 break;
2202 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002203
Chris Lattner3db4dde2010-09-01 00:20:33 +00002204 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00002205 // known to pass in the high eightbyte of the result. We do this by forming a
2206 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00002207 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002208 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Chris Lattner519f68c2010-07-28 23:06:14 +00002209
Chris Lattnereb518b42010-07-29 21:42:50 +00002210 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00002211}
2212
Daniel Dunbaredfac032012-03-10 01:03:58 +00002213ABIArgInfo X86_64ABIInfo::classifyArgumentType(
Eli Friedman7a1b5862013-06-12 00:13:45 +00002214 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE,
2215 bool isNamedArg)
Daniel Dunbaredfac032012-03-10 01:03:58 +00002216 const
2217{
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002218 X86_64ABIInfo::Class Lo, Hi;
Eli Friedman7a1b5862013-06-12 00:13:45 +00002219 classify(Ty, 0, Lo, Hi, isNamedArg);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002220
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002221 // Check some invariants.
2222 // FIXME: Enforce these by construction.
2223 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002224 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2225
2226 neededInt = 0;
2227 neededSSE = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002228 llvm::Type *ResType = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002229 switch (Lo) {
2230 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002231 if (Hi == NoClass)
2232 return ABIArgInfo::getIgnore();
2233 // If the low part is just padding, it takes no register, leave ResType
2234 // null.
2235 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2236 "Unknown missing lo part");
2237 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002238
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002239 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
2240 // on the stack.
2241 case Memory:
2242
2243 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
2244 // COMPLEX_X87, it is passed in memory.
2245 case X87:
2246 case ComplexX87:
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002247 if (getRecordArgABI(Ty, CGT) == CGCXXABI::RAA_Indirect)
Eli Friedmanded137f2011-06-29 07:04:55 +00002248 ++neededInt;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002249 return getIndirectResult(Ty, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002250
2251 case SSEUp:
2252 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002253 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002254
2255 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
2256 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
2257 // and %r9 is used.
2258 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00002259 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002260
Chris Lattner49382de2010-07-28 22:44:07 +00002261 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002262 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00002263
2264 // If we have a sign or zero extended integer, make sure to return Extend
2265 // so that the parameter gets the right LLVM IR attributes.
2266 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2267 // Treat an enum type as its underlying type.
2268 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2269 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002270
Chris Lattnereb518b42010-07-29 21:42:50 +00002271 if (Ty->isIntegralOrEnumerationType() &&
2272 Ty->isPromotableIntegerType())
2273 return ABIArgInfo::getExtend();
2274 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002275
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002276 break;
2277
2278 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
2279 // available SSE register is used, the registers are taken in the
2280 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00002281 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002282 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00002283 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00002284 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002285 break;
2286 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00002287 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002288
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002289 llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002290 switch (Hi) {
2291 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002292 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002293 // which is passed in memory.
2294 case Memory:
2295 case X87:
2296 case ComplexX87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002297 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002298
2299 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002300
Chris Lattner645406a2010-09-01 00:24:35 +00002301 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002302 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00002303 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002304 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002305
Chris Lattner645406a2010-09-01 00:24:35 +00002306 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2307 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002308 break;
2309
2310 // X87Up generally doesn't occur here (long double is passed in
2311 // memory), except in situations involving unions.
2312 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00002313 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002314 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002315
Chris Lattner645406a2010-09-01 00:24:35 +00002316 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2317 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00002318
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002319 ++neededSSE;
2320 break;
2321
2322 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
2323 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002324 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002325 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00002326 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002327 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002328 break;
2329 }
2330
Chris Lattner645406a2010-09-01 00:24:35 +00002331 // If a high part was specified, merge it together with the low part. It is
2332 // known to pass in the high eightbyte of the result. We do this by forming a
2333 // first class struct aggregate with the high and low part: {low, high}
2334 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002335 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002336
Chris Lattnereb518b42010-07-29 21:42:50 +00002337 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002338}
2339
Chris Lattneree5dcd02010-07-29 02:31:05 +00002340void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002341
Chris Lattnera3c109b2010-07-29 02:16:43 +00002342 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002343
2344 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00002345 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002346
2347 // If the return value is indirect, then the hidden argument is consuming one
2348 // integer register.
2349 if (FI.getReturnInfo().isIndirect())
2350 --freeIntRegs;
2351
Eli Friedman7a1b5862013-06-12 00:13:45 +00002352 bool isVariadic = FI.isVariadic();
2353 unsigned numRequiredArgs = 0;
2354 if (isVariadic)
2355 numRequiredArgs = FI.getRequiredArgs().getNumRequiredArgs();
2356
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002357 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2358 // get assigned (in left-to-right order) for passing as follows...
2359 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2360 it != ie; ++it) {
Eli Friedman7a1b5862013-06-12 00:13:45 +00002361 bool isNamedArg = true;
2362 if (isVariadic)
2363 isNamedArg = (it - FI.arg_begin()) < numRequiredArgs;
2364
Bill Wendling99aaae82010-10-18 23:51:38 +00002365 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002366 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
Eli Friedman7a1b5862013-06-12 00:13:45 +00002367 neededSSE, isNamedArg);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002368
2369 // AMD64-ABI 3.2.3p3: If there are no registers available for any
2370 // eightbyte of an argument, the whole argument is passed on the
2371 // stack. If registers have already been assigned for some
2372 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00002373 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002374 freeIntRegs -= neededInt;
2375 freeSSERegs -= neededSSE;
2376 } else {
Daniel Dunbaredfac032012-03-10 01:03:58 +00002377 it->info = getIndirectResult(it->type, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002378 }
2379 }
2380}
2381
2382static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2383 QualType Ty,
2384 CodeGenFunction &CGF) {
2385 llvm::Value *overflow_arg_area_p =
2386 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2387 llvm::Value *overflow_arg_area =
2388 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2389
2390 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2391 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedman8d2fe422011-11-18 02:44:19 +00002392 // It isn't stated explicitly in the standard, but in practice we use
2393 // alignment greater than 16 where necessary.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002394 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2395 if (Align > 8) {
Eli Friedman8d2fe422011-11-18 02:44:19 +00002396 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
Owen Anderson0032b272009-08-13 21:57:51 +00002397 llvm::Value *Offset =
Eli Friedman8d2fe422011-11-18 02:44:19 +00002398 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002399 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2400 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00002401 CGF.Int64Ty);
Eli Friedman8d2fe422011-11-18 02:44:19 +00002402 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002403 overflow_arg_area =
2404 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2405 overflow_arg_area->getType(),
2406 "overflow_arg_area.align");
2407 }
2408
2409 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002410 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002411 llvm::Value *Res =
2412 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002413 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002414
2415 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2416 // l->overflow_arg_area + sizeof(type).
2417 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2418 // an 8 byte boundary.
2419
2420 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00002421 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00002422 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002423 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2424 "overflow_arg_area.next");
2425 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2426
2427 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2428 return Res;
2429}
2430
2431llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2432 CodeGenFunction &CGF) const {
2433 // Assume that va_list type is correct; should be pointer to LLVM type:
2434 // struct {
2435 // i32 gp_offset;
2436 // i32 fp_offset;
2437 // i8* overflow_arg_area;
2438 // i8* reg_save_area;
2439 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00002440 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002441
Chris Lattnera14db752010-03-11 18:19:55 +00002442 Ty = CGF.getContext().getCanonicalType(Ty);
Eli Friedman7a1b5862013-06-12 00:13:45 +00002443 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE,
2444 /*isNamedArg*/false);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002445
2446 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2447 // in the registers. If not go to step 7.
2448 if (!neededInt && !neededSSE)
2449 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2450
2451 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2452 // general purpose registers needed to pass type and num_fp to hold
2453 // the number of floating point registers needed.
2454
2455 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2456 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2457 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2458 //
2459 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2460 // register save space).
2461
2462 llvm::Value *InRegs = 0;
2463 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2464 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2465 if (neededInt) {
2466 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2467 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002468 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2469 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002470 }
2471
2472 if (neededSSE) {
2473 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2474 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2475 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00002476 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2477 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002478 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2479 }
2480
2481 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2482 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2483 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2484 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2485
2486 // Emit code to load the value if it was passed in registers.
2487
2488 CGF.EmitBlock(InRegBlock);
2489
2490 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2491 // an offset of l->gp_offset and/or l->fp_offset. This may require
2492 // copying to a temporary location in case the parameter is passed
2493 // in different register classes or requires an alignment greater
2494 // than 8 for general purpose registers and 16 for XMM registers.
2495 //
2496 // FIXME: This really results in shameful code when we end up needing to
2497 // collect arguments from different places; often what should result in a
2498 // simple assembling of a structure from scattered addresses has many more
2499 // loads than necessary. Can we clean this up?
Chris Lattner2acc6e32011-07-18 04:24:23 +00002500 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002501 llvm::Value *RegAddr =
2502 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2503 "reg_save_area");
2504 if (neededInt && neededSSE) {
2505 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002506 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002507 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Eli Friedmaneeb00622013-06-07 23:20:55 +00002508 llvm::Value *Tmp = CGF.CreateMemTemp(Ty);
2509 Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002510 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002511 llvm::Type *TyLo = ST->getElementType(0);
2512 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002513 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002514 "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002515 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2516 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002517 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2518 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002519 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2520 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002521 llvm::Value *V =
2522 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2523 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2524 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2525 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2526
Owen Andersona1cf15f2009-07-14 23:10:40 +00002527 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002528 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002529 } else if (neededInt) {
2530 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2531 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002532 llvm::PointerType::getUnqual(LTy));
Eli Friedmaneeb00622013-06-07 23:20:55 +00002533
2534 // Copy to a temporary if necessary to ensure the appropriate alignment.
2535 std::pair<CharUnits, CharUnits> SizeAlign =
2536 CGF.getContext().getTypeInfoInChars(Ty);
2537 uint64_t TySize = SizeAlign.first.getQuantity();
2538 unsigned TyAlign = SizeAlign.second.getQuantity();
2539 if (TyAlign > 8) {
Eli Friedmaneeb00622013-06-07 23:20:55 +00002540 llvm::Value *Tmp = CGF.CreateMemTemp(Ty);
2541 CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, 8, false);
2542 RegAddr = Tmp;
2543 }
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002544 } else if (neededSSE == 1) {
2545 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2546 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2547 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002548 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002549 assert(neededSSE == 2 && "Invalid number of needed registers!");
2550 // SSE registers are spaced 16 bytes apart in the register save
2551 // area, we need to collect the two eightbytes together.
2552 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002553 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner8b418682012-02-07 00:39:47 +00002554 llvm::Type *DoubleTy = CGF.DoubleTy;
Chris Lattner2acc6e32011-07-18 04:24:23 +00002555 llvm::Type *DblPtrTy =
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002556 llvm::PointerType::getUnqual(DoubleTy);
Eli Friedmaneeb00622013-06-07 23:20:55 +00002557 llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, NULL);
2558 llvm::Value *V, *Tmp = CGF.CreateMemTemp(Ty);
2559 Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo());
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002560 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2561 DblPtrTy));
2562 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2563 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2564 DblPtrTy));
2565 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2566 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2567 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002568 }
2569
2570 // AMD64-ABI 3.5.7p5: Step 5. Set:
2571 // l->gp_offset = l->gp_offset + num_gp * 8
2572 // l->fp_offset = l->fp_offset + num_fp * 16.
2573 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002574 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002575 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2576 gp_offset_p);
2577 }
2578 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002579 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002580 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2581 fp_offset_p);
2582 }
2583 CGF.EmitBranch(ContBlock);
2584
2585 // Emit code to load the value if it was passed in memory.
2586
2587 CGF.EmitBlock(InMemBlock);
2588 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2589
2590 // Return the appropriate result.
2591
2592 CGF.EmitBlock(ContBlock);
Jay Foadbbf3bac2011-03-30 11:28:58 +00002593 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002594 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002595 ResAddr->addIncoming(RegAddr, InRegBlock);
2596 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002597 return ResAddr;
2598}
2599
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002600ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, bool IsReturnType) const {
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002601
2602 if (Ty->isVoidType())
2603 return ABIArgInfo::getIgnore();
2604
2605 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2606 Ty = EnumTy->getDecl()->getIntegerType();
2607
2608 uint64_t Size = getContext().getTypeSize(Ty);
2609
2610 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002611 if (IsReturnType) {
2612 if (isRecordReturnIndirect(RT, CGT))
2613 return ABIArgInfo::getIndirect(0, false);
2614 } else {
2615 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, CGT))
2616 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
2617 }
2618
2619 if (RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002620 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2621
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002622 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
John McCall64aa4b32013-04-16 22:48:15 +00002623 if (Size == 128 && getTarget().getTriple().getOS() == llvm::Triple::MinGW32)
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002624 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2625 Size));
2626
2627 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2628 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2629 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002630 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002631 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2632 Size));
2633
2634 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2635 }
2636
2637 if (Ty->isPromotableIntegerType())
2638 return ABIArgInfo::getExtend();
2639
2640 return ABIArgInfo::getDirect();
2641}
2642
2643void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2644
2645 QualType RetTy = FI.getReturnType();
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002646 FI.getReturnInfo() = classify(RetTy, true);
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002647
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002648 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2649 it != ie; ++it)
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002650 it->info = classify(it->type, false);
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002651}
2652
Chris Lattnerf13721d2010-08-31 16:44:54 +00002653llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2654 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002655 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002656
Chris Lattnerf13721d2010-08-31 16:44:54 +00002657 CGBuilderTy &Builder = CGF.Builder;
2658 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2659 "ap");
2660 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2661 llvm::Type *PTy =
2662 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2663 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2664
2665 uint64_t Offset =
2666 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2667 llvm::Value *NextAddr =
2668 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2669 "ap.next");
2670 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2671
2672 return AddrTyped;
2673}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002674
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00002675namespace {
2676
Derek Schuff263366f2012-10-16 22:30:41 +00002677class NaClX86_64ABIInfo : public ABIInfo {
2678 public:
2679 NaClX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2680 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, HasAVX) {}
2681 virtual void computeInfo(CGFunctionInfo &FI) const;
2682 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2683 CodeGenFunction &CGF) const;
2684 private:
2685 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
2686 X86_64ABIInfo NInfo; // Used for everything else.
2687};
2688
2689class NaClX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2690 public:
2691 NaClX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2692 : TargetCodeGenInfo(new NaClX86_64ABIInfo(CGT, HasAVX)) {}
2693};
2694
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00002695}
2696
Derek Schuff263366f2012-10-16 22:30:41 +00002697void NaClX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2698 if (FI.getASTCallingConvention() == CC_PnaclCall)
2699 PInfo.computeInfo(FI);
2700 else
2701 NInfo.computeInfo(FI);
2702}
2703
2704llvm::Value *NaClX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2705 CodeGenFunction &CGF) const {
2706 // Always use the native convention; calling pnacl-style varargs functions
2707 // is unuspported.
2708 return NInfo.EmitVAArg(VAListAddr, Ty, CGF);
2709}
2710
2711
John McCallec853ba2010-03-11 00:10:12 +00002712// PowerPC-32
2713
2714namespace {
2715class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2716public:
Chris Lattnerea044322010-07-29 02:01:43 +00002717 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002718
John McCallec853ba2010-03-11 00:10:12 +00002719 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2720 // This is recovered from gcc output.
2721 return 1; // r1 is the dedicated stack pointer
2722 }
2723
2724 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002725 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002726};
2727
2728}
2729
2730bool
2731PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2732 llvm::Value *Address) const {
2733 // This is calculated from the LLVM and GCC tables and verified
2734 // against gcc output. AFAIK all ABIs use the same encoding.
2735
2736 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallec853ba2010-03-11 00:10:12 +00002737
Chris Lattner8b418682012-02-07 00:39:47 +00002738 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallec853ba2010-03-11 00:10:12 +00002739 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2740 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2741 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2742
2743 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002744 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002745
2746 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002747 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002748
2749 // 64-76 are various 4-byte special-purpose registers:
2750 // 64: mq
2751 // 65: lr
2752 // 66: ctr
2753 // 67: ap
2754 // 68-75 cr0-7
2755 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002756 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002757
2758 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002759 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002760
2761 // 109: vrsave
2762 // 110: vscr
2763 // 111: spe_acc
2764 // 112: spefscr
2765 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002766 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002767
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002768 return false;
John McCallec853ba2010-03-11 00:10:12 +00002769}
2770
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002771// PowerPC-64
2772
2773namespace {
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002774/// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
2775class PPC64_SVR4_ABIInfo : public DefaultABIInfo {
2776
2777public:
2778 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
2779
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002780 bool isPromotableTypeForABI(QualType Ty) const;
2781
2782 ABIArgInfo classifyReturnType(QualType RetTy) const;
2783 ABIArgInfo classifyArgumentType(QualType Ty) const;
2784
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00002785 // TODO: We can add more logic to computeInfo to improve performance.
2786 // Example: For aggregate arguments that fit in a register, we could
2787 // use getDirectInReg (as is done below for structs containing a single
2788 // floating-point value) to avoid pushing them to memory on function
2789 // entry. This would require changing the logic in PPCISelLowering
2790 // when lowering the parameters in the caller and args in the callee.
2791 virtual void computeInfo(CGFunctionInfo &FI) const {
2792 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2793 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2794 it != ie; ++it) {
2795 // We rely on the default argument classification for the most part.
2796 // One exception: An aggregate containing a single floating-point
2797 // item must be passed in a register if one is available.
2798 const Type *T = isSingleElementStruct(it->type, getContext());
2799 if (T) {
2800 const BuiltinType *BT = T->getAs<BuiltinType>();
2801 if (BT && BT->isFloatingPoint()) {
2802 QualType QT(T, 0);
2803 it->info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
2804 continue;
2805 }
2806 }
2807 it->info = classifyArgumentType(it->type);
2808 }
2809 }
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002810
2811 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr,
2812 QualType Ty,
2813 CodeGenFunction &CGF) const;
2814};
2815
2816class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
2817public:
2818 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT)
2819 : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT)) {}
2820
2821 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2822 // This is recovered from gcc output.
2823 return 1; // r1 is the dedicated stack pointer
2824 }
2825
2826 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2827 llvm::Value *Address) const;
2828};
2829
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002830class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2831public:
2832 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
2833
2834 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2835 // This is recovered from gcc output.
2836 return 1; // r1 is the dedicated stack pointer
2837 }
2838
2839 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2840 llvm::Value *Address) const;
2841};
2842
2843}
2844
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002845// Return true if the ABI requires Ty to be passed sign- or zero-
2846// extended to 64 bits.
2847bool
2848PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
2849 // Treat an enum type as its underlying type.
2850 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2851 Ty = EnumTy->getDecl()->getIntegerType();
2852
2853 // Promotable integer types are required to be promoted by the ABI.
2854 if (Ty->isPromotableIntegerType())
2855 return true;
2856
2857 // In addition to the usual promotable integer types, we also need to
2858 // extend all 32-bit types, since the ABI requires promotion to 64 bits.
2859 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2860 switch (BT->getKind()) {
2861 case BuiltinType::Int:
2862 case BuiltinType::UInt:
2863 return true;
2864 default:
2865 break;
2866 }
2867
2868 return false;
2869}
2870
2871ABIArgInfo
2872PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
Bill Schmidtc9715fc2012-11-27 02:46:43 +00002873 if (Ty->isAnyComplexType())
2874 return ABIArgInfo::getDirect();
2875
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002876 if (isAggregateTypeForABI(Ty)) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00002877 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
2878 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002879
2880 return ABIArgInfo::getIndirect(0);
2881 }
2882
2883 return (isPromotableTypeForABI(Ty) ?
2884 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2885}
2886
2887ABIArgInfo
2888PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
2889 if (RetTy->isVoidType())
2890 return ABIArgInfo::getIgnore();
2891
Bill Schmidt9e6111a2012-12-17 04:20:17 +00002892 if (RetTy->isAnyComplexType())
2893 return ABIArgInfo::getDirect();
2894
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002895 if (isAggregateTypeForABI(RetTy))
2896 return ABIArgInfo::getIndirect(0);
2897
2898 return (isPromotableTypeForABI(RetTy) ?
2899 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2900}
2901
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002902// Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
2903llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr,
2904 QualType Ty,
2905 CodeGenFunction &CGF) const {
2906 llvm::Type *BP = CGF.Int8PtrTy;
2907 llvm::Type *BPP = CGF.Int8PtrPtrTy;
2908
2909 CGBuilderTy &Builder = CGF.Builder;
2910 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
2911 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2912
Bill Schmidt19f8e852013-01-14 17:45:36 +00002913 // Update the va_list pointer. The pointer should be bumped by the
2914 // size of the object. We can trust getTypeSize() except for a complex
2915 // type whose base type is smaller than a doubleword. For these, the
2916 // size of the object is 16 bytes; see below for further explanation.
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002917 unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8;
Bill Schmidt19f8e852013-01-14 17:45:36 +00002918 QualType BaseTy;
2919 unsigned CplxBaseSize = 0;
2920
2921 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
2922 BaseTy = CTy->getElementType();
2923 CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8;
2924 if (CplxBaseSize < 8)
2925 SizeInBytes = 16;
2926 }
2927
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002928 unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8);
2929 llvm::Value *NextAddr =
2930 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset),
2931 "ap.next");
2932 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2933
Bill Schmidt19f8e852013-01-14 17:45:36 +00002934 // If we have a complex type and the base type is smaller than 8 bytes,
2935 // the ABI calls for the real and imaginary parts to be right-adjusted
2936 // in separate doublewords. However, Clang expects us to produce a
2937 // pointer to a structure with the two parts packed tightly. So generate
2938 // loads of the real and imaginary parts relative to the va_list pointer,
2939 // and store them to a temporary structure.
2940 if (CplxBaseSize && CplxBaseSize < 8) {
2941 llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2942 llvm::Value *ImagAddr = RealAddr;
2943 RealAddr = Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize));
2944 ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize));
2945 llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy));
2946 RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy);
2947 ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy);
2948 llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal");
2949 llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag");
2950 llvm::Value *Ptr = CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty),
2951 "vacplx");
2952 llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, ".real");
2953 llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, ".imag");
2954 Builder.CreateStore(Real, RealPtr, false);
2955 Builder.CreateStore(Imag, ImagPtr, false);
2956 return Ptr;
2957 }
2958
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002959 // If the argument is smaller than 8 bytes, it is right-adjusted in
2960 // its doubleword slot. Adjust the pointer to pick it up from the
2961 // correct offset.
2962 if (SizeInBytes < 8) {
2963 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2964 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes));
2965 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2966 }
2967
2968 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2969 return Builder.CreateBitCast(Addr, PTy);
2970}
2971
2972static bool
2973PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2974 llvm::Value *Address) {
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002975 // This is calculated from the LLVM and GCC tables and verified
2976 // against gcc output. AFAIK all ABIs use the same encoding.
2977
2978 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2979
2980 llvm::IntegerType *i8 = CGF.Int8Ty;
2981 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2982 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2983 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2984
2985 // 0-31: r0-31, the 8-byte general-purpose registers
2986 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
2987
2988 // 32-63: fp0-31, the 8-byte floating-point registers
2989 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
2990
2991 // 64-76 are various 4-byte special-purpose registers:
2992 // 64: mq
2993 // 65: lr
2994 // 66: ctr
2995 // 67: ap
2996 // 68-75 cr0-7
2997 // 76: xer
2998 AssignToArrayRange(Builder, Address, Four8, 64, 76);
2999
3000 // 77-108: v0-31, the 16-byte vector registers
3001 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
3002
3003 // 109: vrsave
3004 // 110: vscr
3005 // 111: spe_acc
3006 // 112: spefscr
3007 // 113: sfp
3008 AssignToArrayRange(Builder, Address, Four8, 109, 113);
3009
3010 return false;
3011}
John McCallec853ba2010-03-11 00:10:12 +00003012
Bill Schmidt2fc107f2012-10-03 19:18:57 +00003013bool
3014PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
3015 CodeGen::CodeGenFunction &CGF,
3016 llvm::Value *Address) const {
3017
3018 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
3019}
3020
3021bool
3022PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3023 llvm::Value *Address) const {
3024
3025 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
3026}
3027
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003028//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003029// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003030//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003031
3032namespace {
3033
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003034class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003035public:
3036 enum ABIKind {
3037 APCS = 0,
3038 AAPCS = 1,
3039 AAPCS_VFP
3040 };
3041
3042private:
3043 ABIKind Kind;
3044
3045public:
John McCallbd7370a2013-02-28 19:01:20 +00003046 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {
3047 setRuntimeCC();
3048 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003049
John McCall49e34be2011-08-30 01:42:09 +00003050 bool isEABI() const {
John McCall64aa4b32013-04-16 22:48:15 +00003051 StringRef Env = getTarget().getTriple().getEnvironmentName();
Logan Chien94a71422012-09-02 09:30:11 +00003052 return (Env == "gnueabi" || Env == "eabi" ||
3053 Env == "android" || Env == "androideabi");
John McCall49e34be2011-08-30 01:42:09 +00003054 }
3055
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003056private:
3057 ABIKind getABIKind() const { return Kind; }
3058
Chris Lattnera3c109b2010-07-29 02:16:43 +00003059 ABIArgInfo classifyReturnType(QualType RetTy) const;
Manman Ren710c5172012-10-31 19:02:26 +00003060 ABIArgInfo classifyArgumentType(QualType RetTy, int *VFPRegs,
3061 unsigned &AllocatedVFP,
Manman Renb3fa55f2012-10-30 23:21:41 +00003062 bool &IsHA) const;
Manman Ren97f81572012-10-16 19:18:39 +00003063 bool isIllegalVectorType(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003064
Chris Lattneree5dcd02010-07-29 02:31:05 +00003065 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003066
3067 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3068 CodeGenFunction &CGF) const;
John McCallbd7370a2013-02-28 19:01:20 +00003069
3070 llvm::CallingConv::ID getLLVMDefaultCC() const;
3071 llvm::CallingConv::ID getABIDefaultCC() const;
3072 void setRuntimeCC();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003073};
3074
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003075class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
3076public:
Chris Lattnerea044322010-07-29 02:01:43 +00003077 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
3078 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00003079
John McCall49e34be2011-08-30 01:42:09 +00003080 const ARMABIInfo &getABIInfo() const {
3081 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
3082 }
3083
John McCall6374c332010-03-06 00:35:14 +00003084 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3085 return 13;
3086 }
Roman Divacky09345d12011-05-18 19:36:54 +00003087
Chris Lattner5f9e2722011-07-23 10:55:15 +00003088 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCallf85e1932011-06-15 23:02:42 +00003089 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
3090 }
3091
Roman Divacky09345d12011-05-18 19:36:54 +00003092 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3093 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003094 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divacky09345d12011-05-18 19:36:54 +00003095
3096 // 0-15 are the 16 integer registers.
Chris Lattner8b418682012-02-07 00:39:47 +00003097 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divacky09345d12011-05-18 19:36:54 +00003098 return false;
3099 }
John McCall49e34be2011-08-30 01:42:09 +00003100
3101 unsigned getSizeOfUnwindException() const {
3102 if (getABIInfo().isEABI()) return 88;
3103 return TargetCodeGenInfo::getSizeOfUnwindException();
3104 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003105};
3106
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003107}
3108
Chris Lattneree5dcd02010-07-29 02:31:05 +00003109void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Manman Renb3fa55f2012-10-30 23:21:41 +00003110 // To correctly handle Homogeneous Aggregate, we need to keep track of the
Manman Ren710c5172012-10-31 19:02:26 +00003111 // VFP registers allocated so far.
Manman Renb3fa55f2012-10-30 23:21:41 +00003112 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
3113 // VFP registers of the appropriate type unallocated then the argument is
3114 // allocated to the lowest-numbered sequence of such registers.
3115 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
3116 // unallocated are marked as unavailable.
3117 unsigned AllocatedVFP = 0;
Manman Ren710c5172012-10-31 19:02:26 +00003118 int VFPRegs[16] = { 0 };
Chris Lattnera3c109b2010-07-29 02:16:43 +00003119 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003120 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Manman Renb3fa55f2012-10-30 23:21:41 +00003121 it != ie; ++it) {
3122 unsigned PreAllocation = AllocatedVFP;
3123 bool IsHA = false;
3124 // 6.1.2.3 There is one VFP co-processor register class using registers
3125 // s0-s15 (d0-d7) for passing arguments.
3126 const unsigned NumVFPs = 16;
Manman Ren710c5172012-10-31 19:02:26 +00003127 it->info = classifyArgumentType(it->type, VFPRegs, AllocatedVFP, IsHA);
Manman Renb3fa55f2012-10-30 23:21:41 +00003128 // If we do not have enough VFP registers for the HA, any VFP registers
3129 // that are unallocated are marked as unavailable. To achieve this, we add
3130 // padding of (NumVFPs - PreAllocation) floats.
3131 if (IsHA && AllocatedVFP > NumVFPs && PreAllocation < NumVFPs) {
3132 llvm::Type *PaddingTy = llvm::ArrayType::get(
3133 llvm::Type::getFloatTy(getVMContext()), NumVFPs - PreAllocation);
3134 it->info = ABIArgInfo::getExpandWithPadding(false, PaddingTy);
3135 }
3136 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003137
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003138 // Always honor user-specified calling convention.
3139 if (FI.getCallingConvention() != llvm::CallingConv::C)
3140 return;
3141
John McCallbd7370a2013-02-28 19:01:20 +00003142 llvm::CallingConv::ID cc = getRuntimeCC();
3143 if (cc != llvm::CallingConv::C)
3144 FI.setEffectiveCallingConvention(cc);
3145}
Rafael Espindola25117ab2010-06-16 16:13:39 +00003146
John McCallbd7370a2013-02-28 19:01:20 +00003147/// Return the default calling convention that LLVM will use.
3148llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
3149 // The default calling convention that LLVM will infer.
John McCall64aa4b32013-04-16 22:48:15 +00003150 if (getTarget().getTriple().getEnvironmentName()=="gnueabihf")
John McCallbd7370a2013-02-28 19:01:20 +00003151 return llvm::CallingConv::ARM_AAPCS_VFP;
3152 else if (isEABI())
3153 return llvm::CallingConv::ARM_AAPCS;
3154 else
3155 return llvm::CallingConv::ARM_APCS;
3156}
3157
3158/// Return the calling convention that our ABI would like us to use
3159/// as the C calling convention.
3160llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003161 switch (getABIKind()) {
John McCallbd7370a2013-02-28 19:01:20 +00003162 case APCS: return llvm::CallingConv::ARM_APCS;
3163 case AAPCS: return llvm::CallingConv::ARM_AAPCS;
3164 case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003165 }
John McCallbd7370a2013-02-28 19:01:20 +00003166 llvm_unreachable("bad ABI kind");
3167}
3168
3169void ARMABIInfo::setRuntimeCC() {
3170 assert(getRuntimeCC() == llvm::CallingConv::C);
3171
3172 // Don't muddy up the IR with a ton of explicit annotations if
3173 // they'd just match what LLVM will infer from the triple.
3174 llvm::CallingConv::ID abiCC = getABIDefaultCC();
3175 if (abiCC != getLLVMDefaultCC())
3176 RuntimeCC = abiCC;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003177}
3178
Bob Wilson194f06a2011-08-03 05:58:22 +00003179/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
3180/// aggregate. If HAMembers is non-null, the number of base elements
3181/// contained in the type is returned through it; this is used for the
3182/// recursive calls that check aggregate component types.
3183static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
3184 ASTContext &Context,
3185 uint64_t *HAMembers = 0) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003186 uint64_t Members = 0;
Bob Wilson194f06a2011-08-03 05:58:22 +00003187 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
3188 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
3189 return false;
3190 Members *= AT->getSize().getZExtValue();
3191 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
3192 const RecordDecl *RD = RT->getDecl();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003193 if (RD->hasFlexibleArrayMember())
Bob Wilson194f06a2011-08-03 05:58:22 +00003194 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003195
Bob Wilson194f06a2011-08-03 05:58:22 +00003196 Members = 0;
3197 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3198 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00003199 const FieldDecl *FD = *i;
Bob Wilson194f06a2011-08-03 05:58:22 +00003200 uint64_t FldMembers;
3201 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
3202 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003203
3204 Members = (RD->isUnion() ?
3205 std::max(Members, FldMembers) : Members + FldMembers);
Bob Wilson194f06a2011-08-03 05:58:22 +00003206 }
3207 } else {
3208 Members = 1;
3209 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
3210 Members = 2;
3211 Ty = CT->getElementType();
3212 }
3213
3214 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
3215 // double, or 64-bit or 128-bit vectors.
3216 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3217 if (BT->getKind() != BuiltinType::Float &&
Tim Northoveradfa45f2012-07-20 22:29:29 +00003218 BT->getKind() != BuiltinType::Double &&
3219 BT->getKind() != BuiltinType::LongDouble)
Bob Wilson194f06a2011-08-03 05:58:22 +00003220 return false;
3221 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
3222 unsigned VecSize = Context.getTypeSize(VT);
3223 if (VecSize != 64 && VecSize != 128)
3224 return false;
3225 } else {
3226 return false;
3227 }
3228
3229 // The base type must be the same for all members. Vector types of the
3230 // same total size are treated as being equivalent here.
3231 const Type *TyPtr = Ty.getTypePtr();
3232 if (!Base)
3233 Base = TyPtr;
3234 if (Base != TyPtr &&
3235 (!Base->isVectorType() || !TyPtr->isVectorType() ||
3236 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
3237 return false;
3238 }
3239
3240 // Homogeneous Aggregates can have at most 4 members of the base type.
3241 if (HAMembers)
3242 *HAMembers = Members;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003243
3244 return (Members > 0 && Members <= 4);
Bob Wilson194f06a2011-08-03 05:58:22 +00003245}
3246
Manman Ren710c5172012-10-31 19:02:26 +00003247/// markAllocatedVFPs - update VFPRegs according to the alignment and
3248/// number of VFP registers (unit is S register) requested.
3249static void markAllocatedVFPs(int *VFPRegs, unsigned &AllocatedVFP,
3250 unsigned Alignment,
3251 unsigned NumRequired) {
3252 // Early Exit.
3253 if (AllocatedVFP >= 16)
3254 return;
3255 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
3256 // VFP registers of the appropriate type unallocated then the argument is
3257 // allocated to the lowest-numbered sequence of such registers.
3258 for (unsigned I = 0; I < 16; I += Alignment) {
3259 bool FoundSlot = true;
3260 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3261 if (J >= 16 || VFPRegs[J]) {
3262 FoundSlot = false;
3263 break;
3264 }
3265 if (FoundSlot) {
3266 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3267 VFPRegs[J] = 1;
3268 AllocatedVFP += NumRequired;
3269 return;
3270 }
3271 }
3272 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
3273 // unallocated are marked as unavailable.
3274 for (unsigned I = 0; I < 16; I++)
3275 VFPRegs[I] = 1;
3276 AllocatedVFP = 17; // We do not have enough VFP registers.
3277}
3278
3279ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, int *VFPRegs,
3280 unsigned &AllocatedVFP,
Manman Renb3fa55f2012-10-30 23:21:41 +00003281 bool &IsHA) const {
3282 // We update number of allocated VFPs according to
3283 // 6.1.2.1 The following argument types are VFP CPRCs:
3284 // A single-precision floating-point type (including promoted
3285 // half-precision types); A double-precision floating-point type;
3286 // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
3287 // with a Base Type of a single- or double-precision floating-point type,
3288 // 64-bit containerized vectors or 128-bit containerized vectors with one
3289 // to four Elements.
3290
Manman Ren97f81572012-10-16 19:18:39 +00003291 // Handle illegal vector types here.
3292 if (isIllegalVectorType(Ty)) {
3293 uint64_t Size = getContext().getTypeSize(Ty);
3294 if (Size <= 32) {
3295 llvm::Type *ResType =
3296 llvm::Type::getInt32Ty(getVMContext());
3297 return ABIArgInfo::getDirect(ResType);
3298 }
3299 if (Size == 64) {
3300 llvm::Type *ResType = llvm::VectorType::get(
3301 llvm::Type::getInt32Ty(getVMContext()), 2);
Manman Ren710c5172012-10-31 19:02:26 +00003302 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
Manman Ren97f81572012-10-16 19:18:39 +00003303 return ABIArgInfo::getDirect(ResType);
3304 }
3305 if (Size == 128) {
3306 llvm::Type *ResType = llvm::VectorType::get(
3307 llvm::Type::getInt32Ty(getVMContext()), 4);
Manman Ren710c5172012-10-31 19:02:26 +00003308 markAllocatedVFPs(VFPRegs, AllocatedVFP, 4, 4);
Manman Ren97f81572012-10-16 19:18:39 +00003309 return ABIArgInfo::getDirect(ResType);
3310 }
3311 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3312 }
Manman Ren710c5172012-10-31 19:02:26 +00003313 // Update VFPRegs for legal vector types.
Manman Renb3fa55f2012-10-30 23:21:41 +00003314 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3315 uint64_t Size = getContext().getTypeSize(VT);
3316 // Size of a legal vector should be power of 2 and above 64.
Manman Ren710c5172012-10-31 19:02:26 +00003317 markAllocatedVFPs(VFPRegs, AllocatedVFP, Size >= 128 ? 4 : 2, Size / 32);
Manman Renb3fa55f2012-10-30 23:21:41 +00003318 }
Manman Ren710c5172012-10-31 19:02:26 +00003319 // Update VFPRegs for floating point types.
Manman Renb3fa55f2012-10-30 23:21:41 +00003320 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3321 if (BT->getKind() == BuiltinType::Half ||
3322 BT->getKind() == BuiltinType::Float)
Manman Ren710c5172012-10-31 19:02:26 +00003323 markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, 1);
Manman Renb3fa55f2012-10-30 23:21:41 +00003324 if (BT->getKind() == BuiltinType::Double ||
Manman Ren710c5172012-10-31 19:02:26 +00003325 BT->getKind() == BuiltinType::LongDouble)
3326 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
Manman Renb3fa55f2012-10-30 23:21:41 +00003327 }
Manman Ren97f81572012-10-16 19:18:39 +00003328
John McCalld608cdb2010-08-22 10:59:02 +00003329 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003330 // Treat an enum type as its underlying type.
3331 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3332 Ty = EnumTy->getDecl()->getIntegerType();
3333
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003334 return (Ty->isPromotableIntegerType() ?
3335 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003336 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003337
Daniel Dunbar42025572009-09-14 21:54:03 +00003338 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003339 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00003340 return ABIArgInfo::getIgnore();
3341
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00003342 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
3343 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003344
Bob Wilson194f06a2011-08-03 05:58:22 +00003345 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
Manman Renb3fa55f2012-10-30 23:21:41 +00003346 // Homogeneous Aggregates need to be expanded when we can fit the aggregate
3347 // into VFP registers.
Bob Wilson194f06a2011-08-03 05:58:22 +00003348 const Type *Base = 0;
Manman Renb3fa55f2012-10-30 23:21:41 +00003349 uint64_t Members = 0;
3350 if (isHomogeneousAggregate(Ty, Base, getContext(), &Members)) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003351 assert(Base && "Base class should be set for homogeneous aggregate");
Manman Renb3fa55f2012-10-30 23:21:41 +00003352 // Base can be a floating-point or a vector.
3353 if (Base->isVectorType()) {
3354 // ElementSize is in number of floats.
3355 unsigned ElementSize = getContext().getTypeSize(Base) == 64 ? 2 : 4;
Manman Rencb489dd2012-11-06 19:05:29 +00003356 markAllocatedVFPs(VFPRegs, AllocatedVFP, ElementSize,
3357 Members * ElementSize);
Manman Renb3fa55f2012-10-30 23:21:41 +00003358 } else if (Base->isSpecificBuiltinType(BuiltinType::Float))
Manman Ren710c5172012-10-31 19:02:26 +00003359 markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, Members);
Manman Renb3fa55f2012-10-30 23:21:41 +00003360 else {
3361 assert(Base->isSpecificBuiltinType(BuiltinType::Double) ||
3362 Base->isSpecificBuiltinType(BuiltinType::LongDouble));
Manman Ren710c5172012-10-31 19:02:26 +00003363 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, Members * 2);
Manman Renb3fa55f2012-10-30 23:21:41 +00003364 }
3365 IsHA = true;
Bob Wilson194f06a2011-08-03 05:58:22 +00003366 return ABIArgInfo::getExpand();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003367 }
Bob Wilson194f06a2011-08-03 05:58:22 +00003368 }
3369
Manman Ren634b3d22012-08-13 21:23:55 +00003370 // Support byval for ARM.
Manman Rencb489dd2012-11-06 19:05:29 +00003371 // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
3372 // most 8-byte. We realign the indirect argument if type alignment is bigger
3373 // than ABI alignment.
Manman Renfd1ba912012-11-05 22:42:46 +00003374 uint64_t ABIAlign = 4;
3375 uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8;
3376 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3377 getABIKind() == ARMABIInfo::AAPCS)
3378 ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
Manman Ren885ad692012-11-06 04:58:01 +00003379 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
3380 return ABIArgInfo::getIndirect(0, /*ByVal=*/true,
Manman Rencb489dd2012-11-06 19:05:29 +00003381 /*Realign=*/TyAlign > ABIAlign);
Eli Friedman79f30982012-08-09 00:31:40 +00003382 }
3383
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00003384 // Otherwise, pass by coercing to a structure of the appropriate size.
Chris Lattner2acc6e32011-07-18 04:24:23 +00003385 llvm::Type* ElemTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003386 unsigned SizeRegs;
Eli Friedman79f30982012-08-09 00:31:40 +00003387 // FIXME: Try to match the types of the arguments more accurately where
3388 // we can.
3389 if (getContext().getTypeAlign(Ty) <= 32) {
Bob Wilson53fc1a62011-08-01 23:39:04 +00003390 ElemTy = llvm::Type::getInt32Ty(getVMContext());
3391 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Manman Ren78eb76e2012-06-25 22:04:00 +00003392 } else {
Manman Ren78eb76e2012-06-25 22:04:00 +00003393 ElemTy = llvm::Type::getInt64Ty(getVMContext());
3394 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastings67d097e2011-04-27 17:24:02 +00003395 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003396
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003397 llvm::Type *STy =
Chris Lattner7650d952011-06-18 22:49:11 +00003398 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003399 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003400}
3401
Chris Lattnera3c109b2010-07-29 02:16:43 +00003402static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00003403 llvm::LLVMContext &VMContext) {
3404 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
3405 // is called integer-like if its size is less than or equal to one word, and
3406 // the offset of each of its addressable sub-fields is zero.
3407
3408 uint64_t Size = Context.getTypeSize(Ty);
3409
3410 // Check that the type fits in a word.
3411 if (Size > 32)
3412 return false;
3413
3414 // FIXME: Handle vector types!
3415 if (Ty->isVectorType())
3416 return false;
3417
Daniel Dunbarb0d58192009-09-14 02:20:34 +00003418 // Float types are never treated as "integer like".
3419 if (Ty->isRealFloatingType())
3420 return false;
3421
Daniel Dunbar98303b92009-09-13 08:03:58 +00003422 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00003423 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00003424 return true;
3425
Daniel Dunbar45815812010-02-01 23:31:26 +00003426 // Small complex integer types are "integer like".
3427 if (const ComplexType *CT = Ty->getAs<ComplexType>())
3428 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003429
3430 // Single element and zero sized arrays should be allowed, by the definition
3431 // above, but they are not.
3432
3433 // Otherwise, it must be a record type.
3434 const RecordType *RT = Ty->getAs<RecordType>();
3435 if (!RT) return false;
3436
3437 // Ignore records with flexible arrays.
3438 const RecordDecl *RD = RT->getDecl();
3439 if (RD->hasFlexibleArrayMember())
3440 return false;
3441
3442 // Check that all sub-fields are at offset 0, and are themselves "integer
3443 // like".
3444 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3445
3446 bool HadField = false;
3447 unsigned idx = 0;
3448 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3449 i != e; ++i, ++idx) {
David Blaikie581deb32012-06-06 20:45:41 +00003450 const FieldDecl *FD = *i;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003451
Daniel Dunbar679855a2010-01-29 03:22:29 +00003452 // Bit-fields are not addressable, we only need to verify they are "integer
3453 // like". We still have to disallow a subsequent non-bitfield, for example:
3454 // struct { int : 0; int x }
3455 // is non-integer like according to gcc.
3456 if (FD->isBitField()) {
3457 if (!RD->isUnion())
3458 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003459
Daniel Dunbar679855a2010-01-29 03:22:29 +00003460 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3461 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003462
Daniel Dunbar679855a2010-01-29 03:22:29 +00003463 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003464 }
3465
Daniel Dunbar679855a2010-01-29 03:22:29 +00003466 // Check if this field is at offset 0.
3467 if (Layout.getFieldOffset(idx) != 0)
3468 return false;
3469
Daniel Dunbar98303b92009-09-13 08:03:58 +00003470 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3471 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003472
Daniel Dunbar679855a2010-01-29 03:22:29 +00003473 // Only allow at most one field in a structure. This doesn't match the
3474 // wording above, but follows gcc in situations with a field following an
3475 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00003476 if (!RD->isUnion()) {
3477 if (HadField)
3478 return false;
3479
3480 HadField = true;
3481 }
3482 }
3483
3484 return true;
3485}
3486
Chris Lattnera3c109b2010-07-29 02:16:43 +00003487ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003488 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003489 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00003490
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00003491 // Large vector types should be returned via memory.
3492 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
3493 return ABIArgInfo::getIndirect(0);
3494
John McCalld608cdb2010-08-22 10:59:02 +00003495 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003496 // Treat an enum type as its underlying type.
3497 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3498 RetTy = EnumTy->getDecl()->getIntegerType();
3499
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003500 return (RetTy->isPromotableIntegerType() ?
3501 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003502 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003503
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003504 // Structures with either a non-trivial destructor or a non-trivial
3505 // copy constructor are always indirect.
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00003506 if (isRecordReturnIndirect(RetTy, CGT))
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003507 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3508
Daniel Dunbar98303b92009-09-13 08:03:58 +00003509 // Are we following APCS?
3510 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00003511 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00003512 return ABIArgInfo::getIgnore();
3513
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003514 // Complex types are all returned as packed integers.
3515 //
3516 // FIXME: Consider using 2 x vector types if the back end handles them
3517 // correctly.
3518 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00003519 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00003520 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003521
Daniel Dunbar98303b92009-09-13 08:03:58 +00003522 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003523 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003524 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003525 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003526 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003527 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003528 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003529 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3530 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003531 }
3532
3533 // Otherwise return in memory.
3534 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003535 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003536
3537 // Otherwise this is an AAPCS variant.
3538
Chris Lattnera3c109b2010-07-29 02:16:43 +00003539 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00003540 return ABIArgInfo::getIgnore();
3541
Bob Wilson3b694fa2011-11-02 04:51:36 +00003542 // Check for homogeneous aggregates with AAPCS-VFP.
3543 if (getABIKind() == AAPCS_VFP) {
3544 const Type *Base = 0;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003545 if (isHomogeneousAggregate(RetTy, Base, getContext())) {
3546 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson3b694fa2011-11-02 04:51:36 +00003547 // Homogeneous Aggregates are returned directly.
3548 return ABIArgInfo::getDirect();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003549 }
Bob Wilson3b694fa2011-11-02 04:51:36 +00003550 }
3551
Daniel Dunbar98303b92009-09-13 08:03:58 +00003552 // Aggregates <= 4 bytes are returned in r0; other aggregates
3553 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003554 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00003555 if (Size <= 32) {
3556 // Return in the smallest viable integer type.
3557 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003558 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003559 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003560 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3561 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003562 }
3563
Daniel Dunbar98303b92009-09-13 08:03:58 +00003564 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003565}
3566
Manman Ren97f81572012-10-16 19:18:39 +00003567/// isIllegalVector - check whether Ty is an illegal vector type.
3568bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
3569 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3570 // Check whether VT is legal.
3571 unsigned NumElements = VT->getNumElements();
3572 uint64_t Size = getContext().getTypeSize(VT);
3573 // NumElements should be power of 2.
3574 if ((NumElements & (NumElements - 1)) != 0)
3575 return true;
3576 // Size should be greater than 32 bits.
3577 return Size <= 32;
3578 }
3579 return false;
3580}
3581
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003582llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00003583 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003584 llvm::Type *BP = CGF.Int8PtrTy;
3585 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003586
3587 CGBuilderTy &Builder = CGF.Builder;
Chris Lattner8b418682012-02-07 00:39:47 +00003588 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003589 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Manman Rend105e732012-10-16 19:01:37 +00003590
3591 uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8;
Rafael Espindolae164c182011-08-02 22:33:37 +00003592 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
Manman Ren97f81572012-10-16 19:18:39 +00003593 bool IsIndirect = false;
Manman Rend105e732012-10-16 19:01:37 +00003594
3595 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
3596 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
Manman Ren93371022012-10-16 19:51:48 +00003597 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3598 getABIKind() == ARMABIInfo::AAPCS)
3599 TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
3600 else
3601 TyAlign = 4;
Manman Ren97f81572012-10-16 19:18:39 +00003602 // Use indirect if size of the illegal vector is bigger than 16 bytes.
3603 if (isIllegalVectorType(Ty) && Size > 16) {
3604 IsIndirect = true;
3605 Size = 4;
3606 TyAlign = 4;
3607 }
Manman Rend105e732012-10-16 19:01:37 +00003608
3609 // Handle address alignment for ABI alignment > 4 bytes.
Rafael Espindolae164c182011-08-02 22:33:37 +00003610 if (TyAlign > 4) {
3611 assert((TyAlign & (TyAlign - 1)) == 0 &&
3612 "Alignment is not power of 2!");
3613 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3614 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
3615 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
Manman Rend105e732012-10-16 19:01:37 +00003616 Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align");
Rafael Espindolae164c182011-08-02 22:33:37 +00003617 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003618
3619 uint64_t Offset =
Manman Rend105e732012-10-16 19:01:37 +00003620 llvm::RoundUpToAlignment(Size, 4);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003621 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00003622 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003623 "ap.next");
3624 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3625
Manman Ren97f81572012-10-16 19:18:39 +00003626 if (IsIndirect)
3627 Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP));
Manman Ren93371022012-10-16 19:51:48 +00003628 else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) {
Manman Rend105e732012-10-16 19:01:37 +00003629 // We can't directly cast ap.cur to pointer to a vector type, since ap.cur
3630 // may not be correctly aligned for the vector type. We create an aligned
3631 // temporary space and copy the content over from ap.cur to the temporary
3632 // space. This is necessary if the natural alignment of the type is greater
3633 // than the ABI alignment.
3634 llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
3635 CharUnits CharSize = getContext().getTypeSizeInChars(Ty);
3636 llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty),
3637 "var.align");
3638 llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
3639 llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy);
3640 Builder.CreateMemCpy(Dst, Src,
3641 llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()),
3642 TyAlign, false);
3643 Addr = AlignedTemp; //The content is in aligned location.
3644 }
3645 llvm::Type *PTy =
3646 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3647 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3648
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003649 return AddrTyped;
3650}
3651
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00003652namespace {
3653
Derek Schuff263366f2012-10-16 22:30:41 +00003654class NaClARMABIInfo : public ABIInfo {
3655 public:
3656 NaClARMABIInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3657 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, Kind) {}
3658 virtual void computeInfo(CGFunctionInfo &FI) const;
3659 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3660 CodeGenFunction &CGF) const;
3661 private:
3662 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
3663 ARMABIInfo NInfo; // Used for everything else.
3664};
3665
3666class NaClARMTargetCodeGenInfo : public TargetCodeGenInfo {
3667 public:
3668 NaClARMTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3669 : TargetCodeGenInfo(new NaClARMABIInfo(CGT, Kind)) {}
3670};
3671
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00003672}
3673
Derek Schuff263366f2012-10-16 22:30:41 +00003674void NaClARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
3675 if (FI.getASTCallingConvention() == CC_PnaclCall)
3676 PInfo.computeInfo(FI);
3677 else
3678 static_cast<const ABIInfo&>(NInfo).computeInfo(FI);
3679}
3680
3681llvm::Value *NaClARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3682 CodeGenFunction &CGF) const {
3683 // Always use the native convention; calling pnacl-style varargs functions
3684 // is unsupported.
3685 return static_cast<const ABIInfo&>(NInfo).EmitVAArg(VAListAddr, Ty, CGF);
3686}
3687
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003688//===----------------------------------------------------------------------===//
Tim Northoverc264e162013-01-31 12:13:10 +00003689// AArch64 ABI Implementation
3690//===----------------------------------------------------------------------===//
3691
3692namespace {
3693
3694class AArch64ABIInfo : public ABIInfo {
3695public:
3696 AArch64ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3697
3698private:
3699 // The AArch64 PCS is explicit about return types and argument types being
3700 // handled identically, so we don't need to draw a distinction between
3701 // Argument and Return classification.
3702 ABIArgInfo classifyGenericType(QualType Ty, int &FreeIntRegs,
3703 int &FreeVFPRegs) const;
3704
3705 ABIArgInfo tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded, bool IsInt,
3706 llvm::Type *DirectTy = 0) const;
3707
3708 virtual void computeInfo(CGFunctionInfo &FI) const;
3709
3710 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3711 CodeGenFunction &CGF) const;
3712};
3713
3714class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
3715public:
3716 AArch64TargetCodeGenInfo(CodeGenTypes &CGT)
3717 :TargetCodeGenInfo(new AArch64ABIInfo(CGT)) {}
3718
3719 const AArch64ABIInfo &getABIInfo() const {
3720 return static_cast<const AArch64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
3721 }
3722
3723 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3724 return 31;
3725 }
3726
3727 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3728 llvm::Value *Address) const {
3729 // 0-31 are x0-x30 and sp: 8 bytes each
3730 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
3731 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 31);
3732
3733 // 64-95 are v0-v31: 16 bytes each
3734 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
3735 AssignToArrayRange(CGF.Builder, Address, Sixteen8, 64, 95);
3736
3737 return false;
3738 }
3739
3740};
3741
3742}
3743
3744void AArch64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
3745 int FreeIntRegs = 8, FreeVFPRegs = 8;
3746
3747 FI.getReturnInfo() = classifyGenericType(FI.getReturnType(),
3748 FreeIntRegs, FreeVFPRegs);
3749
3750 FreeIntRegs = FreeVFPRegs = 8;
3751 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3752 it != ie; ++it) {
3753 it->info = classifyGenericType(it->type, FreeIntRegs, FreeVFPRegs);
3754
3755 }
3756}
3757
3758ABIArgInfo
3759AArch64ABIInfo::tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded,
3760 bool IsInt, llvm::Type *DirectTy) const {
3761 if (FreeRegs >= RegsNeeded) {
3762 FreeRegs -= RegsNeeded;
3763 return ABIArgInfo::getDirect(DirectTy);
3764 }
3765
3766 llvm::Type *Padding = 0;
3767
3768 // We need padding so that later arguments don't get filled in anyway. That
3769 // wouldn't happen if only ByVal arguments followed in the same category, but
3770 // a large structure will simply seem to be a pointer as far as LLVM is
3771 // concerned.
3772 if (FreeRegs > 0) {
3773 if (IsInt)
3774 Padding = llvm::Type::getInt64Ty(getVMContext());
3775 else
3776 Padding = llvm::Type::getFloatTy(getVMContext());
3777
3778 // Either [N x i64] or [N x float].
3779 Padding = llvm::ArrayType::get(Padding, FreeRegs);
3780 FreeRegs = 0;
3781 }
3782
3783 return ABIArgInfo::getIndirect(getContext().getTypeAlign(Ty) / 8,
3784 /*IsByVal=*/ true, /*Realign=*/ false,
3785 Padding);
3786}
3787
3788
3789ABIArgInfo AArch64ABIInfo::classifyGenericType(QualType Ty,
3790 int &FreeIntRegs,
3791 int &FreeVFPRegs) const {
3792 // Can only occurs for return, but harmless otherwise.
3793 if (Ty->isVoidType())
3794 return ABIArgInfo::getIgnore();
3795
3796 // Large vector types should be returned via memory. There's no such concept
3797 // in the ABI, but they'd be over 16 bytes anyway so no matter how they're
3798 // classified they'd go into memory (see B.3).
3799 if (Ty->isVectorType() && getContext().getTypeSize(Ty) > 128) {
3800 if (FreeIntRegs > 0)
3801 --FreeIntRegs;
3802 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3803 }
3804
3805 // All non-aggregate LLVM types have a concrete ABI representation so they can
3806 // be passed directly. After this block we're guaranteed to be in a
3807 // complicated case.
3808 if (!isAggregateTypeForABI(Ty)) {
3809 // Treat an enum type as its underlying type.
3810 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3811 Ty = EnumTy->getDecl()->getIntegerType();
3812
3813 if (Ty->isFloatingType() || Ty->isVectorType())
3814 return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ false);
3815
3816 assert(getContext().getTypeSize(Ty) <= 128 &&
3817 "unexpectedly large scalar type");
3818
3819 int RegsNeeded = getContext().getTypeSize(Ty) > 64 ? 2 : 1;
3820
3821 // If the type may need padding registers to ensure "alignment", we must be
3822 // careful when this is accounted for. Increasing the effective size covers
3823 // all cases.
3824 if (getContext().getTypeAlign(Ty) == 128)
3825 RegsNeeded += FreeIntRegs % 2 != 0;
3826
3827 return tryUseRegs(Ty, FreeIntRegs, RegsNeeded, /*IsInt=*/ true);
3828 }
3829
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00003830 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) {
3831 if (FreeIntRegs > 0 && RAA == CGCXXABI::RAA_Indirect)
Tim Northoverc264e162013-01-31 12:13:10 +00003832 --FreeIntRegs;
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00003833 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Tim Northoverc264e162013-01-31 12:13:10 +00003834 }
3835
3836 if (isEmptyRecord(getContext(), Ty, true)) {
3837 if (!getContext().getLangOpts().CPlusPlus) {
3838 // Empty structs outside C++ mode are a GNU extension, so no ABI can
3839 // possibly tell us what to do. It turns out (I believe) that GCC ignores
3840 // the object for parameter-passsing purposes.
3841 return ABIArgInfo::getIgnore();
3842 }
3843
3844 // The combination of C++98 9p5 (sizeof(struct) != 0) and the pseudocode
3845 // description of va_arg in the PCS require that an empty struct does
3846 // actually occupy space for parameter-passing. I'm hoping for a
3847 // clarification giving an explicit paragraph to point to in future.
3848 return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ true,
3849 llvm::Type::getInt8Ty(getVMContext()));
3850 }
3851
3852 // Homogeneous vector aggregates get passed in registers or on the stack.
3853 const Type *Base = 0;
3854 uint64_t NumMembers = 0;
3855 if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)) {
3856 assert(Base && "Base class should be set for homogeneous aggregate");
3857 // Homogeneous aggregates are passed and returned directly.
3858 return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ NumMembers,
3859 /*IsInt=*/ false);
3860 }
3861
3862 uint64_t Size = getContext().getTypeSize(Ty);
3863 if (Size <= 128) {
3864 // Small structs can use the same direct type whether they're in registers
3865 // or on the stack.
3866 llvm::Type *BaseTy;
3867 unsigned NumBases;
3868 int SizeInRegs = (Size + 63) / 64;
3869
3870 if (getContext().getTypeAlign(Ty) == 128) {
3871 BaseTy = llvm::Type::getIntNTy(getVMContext(), 128);
3872 NumBases = 1;
3873
3874 // If the type may need padding registers to ensure "alignment", we must
3875 // be careful when this is accounted for. Increasing the effective size
3876 // covers all cases.
3877 SizeInRegs += FreeIntRegs % 2 != 0;
3878 } else {
3879 BaseTy = llvm::Type::getInt64Ty(getVMContext());
3880 NumBases = SizeInRegs;
3881 }
3882 llvm::Type *DirectTy = llvm::ArrayType::get(BaseTy, NumBases);
3883
3884 return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ SizeInRegs,
3885 /*IsInt=*/ true, DirectTy);
3886 }
3887
3888 // If the aggregate is > 16 bytes, it's passed and returned indirectly. In
3889 // LLVM terms the return uses an "sret" pointer, but that's handled elsewhere.
3890 --FreeIntRegs;
3891 return ABIArgInfo::getIndirect(0, /* byVal = */ false);
3892}
3893
3894llvm::Value *AArch64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3895 CodeGenFunction &CGF) const {
3896 // The AArch64 va_list type and handling is specified in the Procedure Call
3897 // Standard, section B.4:
3898 //
3899 // struct {
3900 // void *__stack;
3901 // void *__gr_top;
3902 // void *__vr_top;
3903 // int __gr_offs;
3904 // int __vr_offs;
3905 // };
3906
3907 assert(!CGF.CGM.getDataLayout().isBigEndian()
3908 && "va_arg not implemented for big-endian AArch64");
3909
3910 int FreeIntRegs = 8, FreeVFPRegs = 8;
3911 Ty = CGF.getContext().getCanonicalType(Ty);
3912 ABIArgInfo AI = classifyGenericType(Ty, FreeIntRegs, FreeVFPRegs);
3913
3914 llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
3915 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
3916 llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
3917 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
3918
3919 llvm::Value *reg_offs_p = 0, *reg_offs = 0;
3920 int reg_top_index;
3921 int RegSize;
3922 if (FreeIntRegs < 8) {
3923 assert(FreeVFPRegs == 8 && "Arguments never split between int & VFP regs");
3924 // 3 is the field number of __gr_offs
3925 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p");
3926 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
3927 reg_top_index = 1; // field number for __gr_top
3928 RegSize = 8 * (8 - FreeIntRegs);
3929 } else {
3930 assert(FreeVFPRegs < 8 && "Argument must go in VFP or int regs");
3931 // 4 is the field number of __vr_offs.
3932 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p");
3933 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
3934 reg_top_index = 2; // field number for __vr_top
3935 RegSize = 16 * (8 - FreeVFPRegs);
3936 }
3937
3938 //=======================================
3939 // Find out where argument was passed
3940 //=======================================
3941
3942 // If reg_offs >= 0 we're already using the stack for this type of
3943 // argument. We don't want to keep updating reg_offs (in case it overflows,
3944 // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
3945 // whatever they get).
3946 llvm::Value *UsingStack = 0;
3947 UsingStack = CGF.Builder.CreateICmpSGE(reg_offs,
3948 llvm::ConstantInt::get(CGF.Int32Ty, 0));
3949
3950 CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
3951
3952 // Otherwise, at least some kind of argument could go in these registers, the
3953 // quesiton is whether this particular type is too big.
3954 CGF.EmitBlock(MaybeRegBlock);
3955
3956 // Integer arguments may need to correct register alignment (for example a
3957 // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
3958 // align __gr_offs to calculate the potential address.
3959 if (FreeIntRegs < 8 && AI.isDirect() && getContext().getTypeAlign(Ty) > 64) {
3960 int Align = getContext().getTypeAlign(Ty) / 8;
3961
3962 reg_offs = CGF.Builder.CreateAdd(reg_offs,
3963 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
3964 "align_regoffs");
3965 reg_offs = CGF.Builder.CreateAnd(reg_offs,
3966 llvm::ConstantInt::get(CGF.Int32Ty, -Align),
3967 "aligned_regoffs");
3968 }
3969
3970 // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
3971 llvm::Value *NewOffset = 0;
3972 NewOffset = CGF.Builder.CreateAdd(reg_offs,
3973 llvm::ConstantInt::get(CGF.Int32Ty, RegSize),
3974 "new_reg_offs");
3975 CGF.Builder.CreateStore(NewOffset, reg_offs_p);
3976
3977 // Now we're in a position to decide whether this argument really was in
3978 // registers or not.
3979 llvm::Value *InRegs = 0;
3980 InRegs = CGF.Builder.CreateICmpSLE(NewOffset,
3981 llvm::ConstantInt::get(CGF.Int32Ty, 0),
3982 "inreg");
3983
3984 CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
3985
3986 //=======================================
3987 // Argument was in registers
3988 //=======================================
3989
3990 // Now we emit the code for if the argument was originally passed in
3991 // registers. First start the appropriate block:
3992 CGF.EmitBlock(InRegBlock);
3993
3994 llvm::Value *reg_top_p = 0, *reg_top = 0;
3995 reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p");
3996 reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
3997 llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs);
3998 llvm::Value *RegAddr = 0;
3999 llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
4000
4001 if (!AI.isDirect()) {
4002 // If it's been passed indirectly (actually a struct), whatever we find from
4003 // stored registers or on the stack will actually be a struct **.
4004 MemTy = llvm::PointerType::getUnqual(MemTy);
4005 }
4006
4007 const Type *Base = 0;
4008 uint64_t NumMembers;
4009 if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)
4010 && NumMembers > 1) {
4011 // Homogeneous aggregates passed in registers will have their elements split
4012 // and stored 16-bytes apart regardless of size (they're notionally in qN,
4013 // qN+1, ...). We reload and store into a temporary local variable
4014 // contiguously.
4015 assert(AI.isDirect() && "Homogeneous aggregates should be passed directly");
4016 llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
4017 llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
4018 llvm::Value *Tmp = CGF.CreateTempAlloca(HFATy);
4019
4020 for (unsigned i = 0; i < NumMembers; ++i) {
4021 llvm::Value *BaseOffset = llvm::ConstantInt::get(CGF.Int32Ty, 16 * i);
4022 llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset);
4023 LoadAddr = CGF.Builder.CreateBitCast(LoadAddr,
4024 llvm::PointerType::getUnqual(BaseTy));
4025 llvm::Value *StoreAddr = CGF.Builder.CreateStructGEP(Tmp, i);
4026
4027 llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
4028 CGF.Builder.CreateStore(Elem, StoreAddr);
4029 }
4030
4031 RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy);
4032 } else {
4033 // Otherwise the object is contiguous in memory
4034 RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy);
4035 }
4036
4037 CGF.EmitBranch(ContBlock);
4038
4039 //=======================================
4040 // Argument was on the stack
4041 //=======================================
4042 CGF.EmitBlock(OnStackBlock);
4043
4044 llvm::Value *stack_p = 0, *OnStackAddr = 0;
4045 stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p");
4046 OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack");
4047
4048 // Again, stack arguments may need realigmnent. In this case both integer and
4049 // floating-point ones might be affected.
4050 if (AI.isDirect() && getContext().getTypeAlign(Ty) > 64) {
4051 int Align = getContext().getTypeAlign(Ty) / 8;
4052
4053 OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty);
4054
4055 OnStackAddr = CGF.Builder.CreateAdd(OnStackAddr,
4056 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
4057 "align_stack");
4058 OnStackAddr = CGF.Builder.CreateAnd(OnStackAddr,
4059 llvm::ConstantInt::get(CGF.Int64Ty, -Align),
4060 "align_stack");
4061
4062 OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy);
4063 }
4064
4065 uint64_t StackSize;
4066 if (AI.isDirect())
4067 StackSize = getContext().getTypeSize(Ty) / 8;
4068 else
4069 StackSize = 8;
4070
4071 // All stack slots are 8 bytes
4072 StackSize = llvm::RoundUpToAlignment(StackSize, 8);
4073
4074 llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize);
4075 llvm::Value *NewStack = CGF.Builder.CreateGEP(OnStackAddr, StackSizeC,
4076 "new_stack");
4077
4078 // Write the new value of __stack for the next call to va_arg
4079 CGF.Builder.CreateStore(NewStack, stack_p);
4080
4081 OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy);
4082
4083 CGF.EmitBranch(ContBlock);
4084
4085 //=======================================
4086 // Tidy up
4087 //=======================================
4088 CGF.EmitBlock(ContBlock);
4089
4090 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr");
4091 ResAddr->addIncoming(RegAddr, InRegBlock);
4092 ResAddr->addIncoming(OnStackAddr, OnStackBlock);
4093
4094 if (AI.isDirect())
4095 return ResAddr;
4096
4097 return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr");
4098}
4099
4100//===----------------------------------------------------------------------===//
Justin Holewinski2c585b92012-05-24 17:43:12 +00004101// NVPTX ABI Implementation
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004102//===----------------------------------------------------------------------===//
4103
4104namespace {
4105
Justin Holewinski2c585b92012-05-24 17:43:12 +00004106class NVPTXABIInfo : public ABIInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004107public:
Justin Holewinskidca8f332013-03-30 14:38:24 +00004108 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004109
4110 ABIArgInfo classifyReturnType(QualType RetTy) const;
4111 ABIArgInfo classifyArgumentType(QualType Ty) const;
4112
4113 virtual void computeInfo(CGFunctionInfo &FI) const;
4114 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4115 CodeGenFunction &CFG) const;
4116};
4117
Justin Holewinski2c585b92012-05-24 17:43:12 +00004118class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004119public:
Justin Holewinski2c585b92012-05-24 17:43:12 +00004120 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
4121 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
Justin Holewinski818eafb2011-10-05 17:58:44 +00004122
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004123 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4124 CodeGen::CodeGenModule &M) const;
Justin Holewinskidca8f332013-03-30 14:38:24 +00004125private:
4126 static void addKernelMetadata(llvm::Function *F);
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004127};
4128
Justin Holewinski2c585b92012-05-24 17:43:12 +00004129ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004130 if (RetTy->isVoidType())
4131 return ABIArgInfo::getIgnore();
4132 if (isAggregateTypeForABI(RetTy))
4133 return ABIArgInfo::getIndirect(0);
4134 return ABIArgInfo::getDirect();
4135}
4136
Justin Holewinski2c585b92012-05-24 17:43:12 +00004137ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004138 if (isAggregateTypeForABI(Ty))
4139 return ABIArgInfo::getIndirect(0);
4140
4141 return ABIArgInfo::getDirect();
4142}
4143
Justin Holewinski2c585b92012-05-24 17:43:12 +00004144void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004145 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4146 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4147 it != ie; ++it)
4148 it->info = classifyArgumentType(it->type);
4149
4150 // Always honor user-specified calling convention.
4151 if (FI.getCallingConvention() != llvm::CallingConv::C)
4152 return;
4153
John McCallbd7370a2013-02-28 19:01:20 +00004154 FI.setEffectiveCallingConvention(getRuntimeCC());
4155}
4156
Justin Holewinski2c585b92012-05-24 17:43:12 +00004157llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4158 CodeGenFunction &CFG) const {
4159 llvm_unreachable("NVPTX does not support varargs");
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004160}
4161
Justin Holewinski2c585b92012-05-24 17:43:12 +00004162void NVPTXTargetCodeGenInfo::
4163SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4164 CodeGen::CodeGenModule &M) const{
Justin Holewinski818eafb2011-10-05 17:58:44 +00004165 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4166 if (!FD) return;
4167
4168 llvm::Function *F = cast<llvm::Function>(GV);
4169
4170 // Perform special handling in OpenCL mode
David Blaikie4e4d0842012-03-11 07:00:24 +00004171 if (M.getLangOpts().OpenCL) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00004172 // Use OpenCL function attributes to check for kernel functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00004173 // By default, all functions are device functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00004174 if (FD->hasAttr<OpenCLKernelAttr>()) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00004175 // OpenCL __kernel functions get kernel metadata
4176 addKernelMetadata(F);
Justin Holewinski818eafb2011-10-05 17:58:44 +00004177 // And kernel functions are not subject to inlining
Bill Wendling72390b32012-12-20 19:27:06 +00004178 F->addFnAttr(llvm::Attribute::NoInline);
Justin Holewinski818eafb2011-10-05 17:58:44 +00004179 }
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004180 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00004181
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004182 // Perform special handling in CUDA mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00004183 if (M.getLangOpts().CUDA) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00004184 // CUDA __global__ functions get a kernel metadata entry. Since
Peter Collingbourne744d90b2011-10-06 16:49:54 +00004185 // __global__ functions cannot be called from the device, we do not
4186 // need to set the noinline attribute.
4187 if (FD->getAttr<CUDAGlobalAttr>())
Justin Holewinskidca8f332013-03-30 14:38:24 +00004188 addKernelMetadata(F);
Justin Holewinski818eafb2011-10-05 17:58:44 +00004189 }
4190}
4191
Justin Holewinskidca8f332013-03-30 14:38:24 +00004192void NVPTXTargetCodeGenInfo::addKernelMetadata(llvm::Function *F) {
4193 llvm::Module *M = F->getParent();
4194 llvm::LLVMContext &Ctx = M->getContext();
4195
4196 // Get "nvvm.annotations" metadata node
4197 llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
4198
4199 // Create !{<func-ref>, metadata !"kernel", i32 1} node
4200 llvm::SmallVector<llvm::Value *, 3> MDVals;
4201 MDVals.push_back(F);
4202 MDVals.push_back(llvm::MDString::get(Ctx, "kernel"));
4203 MDVals.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), 1));
4204
4205 // Append metadata to nvvm.annotations
4206 MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
4207}
4208
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004209}
4210
4211//===----------------------------------------------------------------------===//
Ulrich Weigandb8409212013-05-06 16:26:41 +00004212// SystemZ ABI Implementation
4213//===----------------------------------------------------------------------===//
4214
4215namespace {
4216
4217class SystemZABIInfo : public ABIInfo {
4218public:
4219 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
4220
4221 bool isPromotableIntegerType(QualType Ty) const;
4222 bool isCompoundType(QualType Ty) const;
4223 bool isFPArgumentType(QualType Ty) const;
4224
4225 ABIArgInfo classifyReturnType(QualType RetTy) const;
4226 ABIArgInfo classifyArgumentType(QualType ArgTy) const;
4227
4228 virtual void computeInfo(CGFunctionInfo &FI) const {
4229 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4230 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4231 it != ie; ++it)
4232 it->info = classifyArgumentType(it->type);
4233 }
4234
4235 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4236 CodeGenFunction &CGF) const;
4237};
4238
4239class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
4240public:
4241 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
4242 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
4243};
4244
4245}
4246
4247bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
4248 // Treat an enum type as its underlying type.
4249 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4250 Ty = EnumTy->getDecl()->getIntegerType();
4251
4252 // Promotable integer types are required to be promoted by the ABI.
4253 if (Ty->isPromotableIntegerType())
4254 return true;
4255
4256 // 32-bit values must also be promoted.
4257 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4258 switch (BT->getKind()) {
4259 case BuiltinType::Int:
4260 case BuiltinType::UInt:
4261 return true;
4262 default:
4263 return false;
4264 }
4265 return false;
4266}
4267
4268bool SystemZABIInfo::isCompoundType(QualType Ty) const {
4269 return Ty->isAnyComplexType() || isAggregateTypeForABI(Ty);
4270}
4271
4272bool SystemZABIInfo::isFPArgumentType(QualType Ty) const {
4273 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4274 switch (BT->getKind()) {
4275 case BuiltinType::Float:
4276 case BuiltinType::Double:
4277 return true;
4278 default:
4279 return false;
4280 }
4281
4282 if (const RecordType *RT = Ty->getAsStructureType()) {
4283 const RecordDecl *RD = RT->getDecl();
4284 bool Found = false;
4285
4286 // If this is a C++ record, check the bases first.
4287 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
4288 for (CXXRecordDecl::base_class_const_iterator I = CXXRD->bases_begin(),
4289 E = CXXRD->bases_end(); I != E; ++I) {
4290 QualType Base = I->getType();
4291
4292 // Empty bases don't affect things either way.
4293 if (isEmptyRecord(getContext(), Base, true))
4294 continue;
4295
4296 if (Found)
4297 return false;
4298 Found = isFPArgumentType(Base);
4299 if (!Found)
4300 return false;
4301 }
4302
4303 // Check the fields.
4304 for (RecordDecl::field_iterator I = RD->field_begin(),
4305 E = RD->field_end(); I != E; ++I) {
4306 const FieldDecl *FD = *I;
4307
4308 // Empty bitfields don't affect things either way.
4309 // Unlike isSingleElementStruct(), empty structure and array fields
4310 // do count. So do anonymous bitfields that aren't zero-sized.
4311 if (FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
4312 return true;
4313
4314 // Unlike isSingleElementStruct(), arrays do not count.
4315 // Nested isFPArgumentType structures still do though.
4316 if (Found)
4317 return false;
4318 Found = isFPArgumentType(FD->getType());
4319 if (!Found)
4320 return false;
4321 }
4322
4323 // Unlike isSingleElementStruct(), trailing padding is allowed.
4324 // An 8-byte aligned struct s { float f; } is passed as a double.
4325 return Found;
4326 }
4327
4328 return false;
4329}
4330
4331llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4332 CodeGenFunction &CGF) const {
4333 // Assume that va_list type is correct; should be pointer to LLVM type:
4334 // struct {
4335 // i64 __gpr;
4336 // i64 __fpr;
4337 // i8 *__overflow_arg_area;
4338 // i8 *__reg_save_area;
4339 // };
4340
4341 // Every argument occupies 8 bytes and is passed by preference in either
4342 // GPRs or FPRs.
4343 Ty = CGF.getContext().getCanonicalType(Ty);
4344 ABIArgInfo AI = classifyArgumentType(Ty);
4345 bool InFPRs = isFPArgumentType(Ty);
4346
4347 llvm::Type *APTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
4348 bool IsIndirect = AI.isIndirect();
4349 unsigned UnpaddedBitSize;
4350 if (IsIndirect) {
4351 APTy = llvm::PointerType::getUnqual(APTy);
4352 UnpaddedBitSize = 64;
4353 } else
4354 UnpaddedBitSize = getContext().getTypeSize(Ty);
4355 unsigned PaddedBitSize = 64;
4356 assert((UnpaddedBitSize <= PaddedBitSize) && "Invalid argument size.");
4357
4358 unsigned PaddedSize = PaddedBitSize / 8;
4359 unsigned Padding = (PaddedBitSize - UnpaddedBitSize) / 8;
4360
4361 unsigned MaxRegs, RegCountField, RegSaveIndex, RegPadding;
4362 if (InFPRs) {
4363 MaxRegs = 4; // Maximum of 4 FPR arguments
4364 RegCountField = 1; // __fpr
4365 RegSaveIndex = 16; // save offset for f0
4366 RegPadding = 0; // floats are passed in the high bits of an FPR
4367 } else {
4368 MaxRegs = 5; // Maximum of 5 GPR arguments
4369 RegCountField = 0; // __gpr
4370 RegSaveIndex = 2; // save offset for r2
4371 RegPadding = Padding; // values are passed in the low bits of a GPR
4372 }
4373
4374 llvm::Value *RegCountPtr =
4375 CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr");
4376 llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count");
4377 llvm::Type *IndexTy = RegCount->getType();
4378 llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs);
4379 llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV,
4380 "fits_in_regs");
4381
4382 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
4383 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
4384 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
4385 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
4386
4387 // Emit code to load the value if it was passed in registers.
4388 CGF.EmitBlock(InRegBlock);
4389
4390 // Work out the address of an argument register.
4391 llvm::Value *PaddedSizeV = llvm::ConstantInt::get(IndexTy, PaddedSize);
4392 llvm::Value *ScaledRegCount =
4393 CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count");
4394 llvm::Value *RegBase =
4395 llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize + RegPadding);
4396 llvm::Value *RegOffset =
4397 CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset");
4398 llvm::Value *RegSaveAreaPtr =
4399 CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr");
4400 llvm::Value *RegSaveArea =
4401 CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area");
4402 llvm::Value *RawRegAddr =
4403 CGF.Builder.CreateGEP(RegSaveArea, RegOffset, "raw_reg_addr");
4404 llvm::Value *RegAddr =
4405 CGF.Builder.CreateBitCast(RawRegAddr, APTy, "reg_addr");
4406
4407 // Update the register count
4408 llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1);
4409 llvm::Value *NewRegCount =
4410 CGF.Builder.CreateAdd(RegCount, One, "reg_count");
4411 CGF.Builder.CreateStore(NewRegCount, RegCountPtr);
4412 CGF.EmitBranch(ContBlock);
4413
4414 // Emit code to load the value if it was passed in memory.
4415 CGF.EmitBlock(InMemBlock);
4416
4417 // Work out the address of a stack argument.
4418 llvm::Value *OverflowArgAreaPtr =
4419 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr");
4420 llvm::Value *OverflowArgArea =
4421 CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area");
4422 llvm::Value *PaddingV = llvm::ConstantInt::get(IndexTy, Padding);
4423 llvm::Value *RawMemAddr =
4424 CGF.Builder.CreateGEP(OverflowArgArea, PaddingV, "raw_mem_addr");
4425 llvm::Value *MemAddr =
4426 CGF.Builder.CreateBitCast(RawMemAddr, APTy, "mem_addr");
4427
4428 // Update overflow_arg_area_ptr pointer
4429 llvm::Value *NewOverflowArgArea =
4430 CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area");
4431 CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
4432 CGF.EmitBranch(ContBlock);
4433
4434 // Return the appropriate result.
4435 CGF.EmitBlock(ContBlock);
4436 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(APTy, 2, "va_arg.addr");
4437 ResAddr->addIncoming(RegAddr, InRegBlock);
4438 ResAddr->addIncoming(MemAddr, InMemBlock);
4439
4440 if (IsIndirect)
4441 return CGF.Builder.CreateLoad(ResAddr, "indirect_arg");
4442
4443 return ResAddr;
4444}
4445
4446
4447ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
4448 if (RetTy->isVoidType())
4449 return ABIArgInfo::getIgnore();
4450 if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64)
4451 return ABIArgInfo::getIndirect(0);
4452 return (isPromotableIntegerType(RetTy) ?
4453 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4454}
4455
4456ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
4457 // Handle the generic C++ ABI.
4458 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
4459 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
4460
4461 // Integers and enums are extended to full register width.
4462 if (isPromotableIntegerType(Ty))
4463 return ABIArgInfo::getExtend();
4464
4465 // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly.
4466 uint64_t Size = getContext().getTypeSize(Ty);
4467 if (Size != 8 && Size != 16 && Size != 32 && Size != 64)
4468 return ABIArgInfo::getIndirect(0);
4469
4470 // Handle small structures.
4471 if (const RecordType *RT = Ty->getAs<RecordType>()) {
4472 // Structures with flexible arrays have variable length, so really
4473 // fail the size test above.
4474 const RecordDecl *RD = RT->getDecl();
4475 if (RD->hasFlexibleArrayMember())
4476 return ABIArgInfo::getIndirect(0);
4477
4478 // The structure is passed as an unextended integer, a float, or a double.
4479 llvm::Type *PassTy;
4480 if (isFPArgumentType(Ty)) {
4481 assert(Size == 32 || Size == 64);
4482 if (Size == 32)
4483 PassTy = llvm::Type::getFloatTy(getVMContext());
4484 else
4485 PassTy = llvm::Type::getDoubleTy(getVMContext());
4486 } else
4487 PassTy = llvm::IntegerType::get(getVMContext(), Size);
4488 return ABIArgInfo::getDirect(PassTy);
4489 }
4490
4491 // Non-structure compounds are passed indirectly.
4492 if (isCompoundType(Ty))
4493 return ABIArgInfo::getIndirect(0);
4494
4495 return ABIArgInfo::getDirect(0);
4496}
4497
4498//===----------------------------------------------------------------------===//
Wesley Peck276fdf42010-12-19 19:57:51 +00004499// MBlaze ABI Implementation
4500//===----------------------------------------------------------------------===//
4501
4502namespace {
4503
4504class MBlazeABIInfo : public ABIInfo {
4505public:
4506 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
4507
4508 bool isPromotableIntegerType(QualType Ty) const;
4509
4510 ABIArgInfo classifyReturnType(QualType RetTy) const;
4511 ABIArgInfo classifyArgumentType(QualType RetTy) const;
4512
4513 virtual void computeInfo(CGFunctionInfo &FI) const {
4514 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4515 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4516 it != ie; ++it)
4517 it->info = classifyArgumentType(it->type);
4518 }
4519
4520 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4521 CodeGenFunction &CGF) const;
4522};
4523
4524class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
4525public:
4526 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
4527 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
4528 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4529 CodeGen::CodeGenModule &M) const;
4530};
4531
4532}
4533
4534bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
4535 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
4536 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4537 switch (BT->getKind()) {
4538 case BuiltinType::Bool:
4539 case BuiltinType::Char_S:
4540 case BuiltinType::Char_U:
4541 case BuiltinType::SChar:
4542 case BuiltinType::UChar:
4543 case BuiltinType::Short:
4544 case BuiltinType::UShort:
4545 return true;
4546 default:
4547 return false;
4548 }
4549 return false;
4550}
4551
4552llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4553 CodeGenFunction &CGF) const {
4554 // FIXME: Implement
4555 return 0;
4556}
4557
4558
4559ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
4560 if (RetTy->isVoidType())
4561 return ABIArgInfo::getIgnore();
4562 if (isAggregateTypeForABI(RetTy))
4563 return ABIArgInfo::getIndirect(0);
4564
4565 return (isPromotableIntegerType(RetTy) ?
4566 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4567}
4568
4569ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
4570 if (isAggregateTypeForABI(Ty))
4571 return ABIArgInfo::getIndirect(0);
4572
4573 return (isPromotableIntegerType(Ty) ?
4574 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4575}
4576
4577void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4578 llvm::GlobalValue *GV,
4579 CodeGen::CodeGenModule &M)
4580 const {
4581 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4582 if (!FD) return;
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00004583
Wesley Peck276fdf42010-12-19 19:57:51 +00004584 llvm::CallingConv::ID CC = llvm::CallingConv::C;
4585 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
4586 CC = llvm::CallingConv::MBLAZE_INTR;
4587 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
4588 CC = llvm::CallingConv::MBLAZE_SVOL;
4589
4590 if (CC != llvm::CallingConv::C) {
4591 // Handle 'interrupt_handler' attribute:
4592 llvm::Function *F = cast<llvm::Function>(GV);
4593
4594 // Step 1: Set ISR calling convention.
4595 F->setCallingConv(CC);
4596
4597 // Step 2: Add attributes goodness.
Bill Wendling72390b32012-12-20 19:27:06 +00004598 F->addFnAttr(llvm::Attribute::NoInline);
Wesley Peck276fdf42010-12-19 19:57:51 +00004599 }
4600
4601 // Step 3: Emit _interrupt_handler alias.
4602 if (CC == llvm::CallingConv::MBLAZE_INTR)
4603 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
4604 "_interrupt_handler", GV, &M.getModule());
4605}
4606
4607
4608//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004609// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004610//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004611
4612namespace {
4613
4614class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
4615public:
Chris Lattnerea044322010-07-29 02:01:43 +00004616 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
4617 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004618 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4619 CodeGen::CodeGenModule &M) const;
4620};
4621
4622}
4623
4624void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4625 llvm::GlobalValue *GV,
4626 CodeGen::CodeGenModule &M) const {
4627 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4628 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
4629 // Handle 'interrupt' attribute:
4630 llvm::Function *F = cast<llvm::Function>(GV);
4631
4632 // Step 1: Set ISR calling convention.
4633 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
4634
4635 // Step 2: Add attributes goodness.
Bill Wendling72390b32012-12-20 19:27:06 +00004636 F->addFnAttr(llvm::Attribute::NoInline);
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004637
4638 // Step 3: Emit ISR vector alias.
Anton Korobeynikovf419a852012-11-26 18:59:10 +00004639 unsigned Num = attr->getNumber() / 2;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004640 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Anton Korobeynikovf419a852012-11-26 18:59:10 +00004641 "__isr_" + Twine(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004642 GV, &M.getModule());
4643 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004644 }
4645}
4646
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004647//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00004648// MIPS ABI Implementation. This works for both little-endian and
4649// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004650//===----------------------------------------------------------------------===//
4651
John McCallaeeb7012010-05-27 06:19:26 +00004652namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00004653class MipsABIInfo : public ABIInfo {
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004654 bool IsO32;
Akira Hatanakac359f202012-07-03 19:24:06 +00004655 unsigned MinABIStackAlignInBytes, StackAlignInBytes;
4656 void CoerceToIntArgs(uint64_t TySize,
4657 SmallVector<llvm::Type*, 8> &ArgList) const;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004658 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004659 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004660 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004661public:
Akira Hatanakab551dd32011-11-03 00:05:50 +00004662 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
Akira Hatanakac359f202012-07-03 19:24:06 +00004663 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
4664 StackAlignInBytes(IsO32 ? 8 : 16) {}
Akira Hatanaka619e8872011-06-02 00:09:17 +00004665
4666 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004667 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004668 virtual void computeInfo(CGFunctionInfo &FI) const;
4669 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4670 CodeGenFunction &CGF) const;
4671};
4672
John McCallaeeb7012010-05-27 06:19:26 +00004673class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanakae624fa02011-09-20 18:23:28 +00004674 unsigned SizeOfUnwindException;
John McCallaeeb7012010-05-27 06:19:26 +00004675public:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004676 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
4677 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
4678 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCallaeeb7012010-05-27 06:19:26 +00004679
4680 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
4681 return 29;
4682 }
4683
Reed Kotler7dfd1822013-01-16 17:10:28 +00004684 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4685 CodeGen::CodeGenModule &CGM) const {
Reed Kotlerad4b8b42013-03-13 20:40:30 +00004686 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4687 if (!FD) return;
Rafael Espindolad8e6d6d2013-03-19 14:32:23 +00004688 llvm::Function *Fn = cast<llvm::Function>(GV);
Reed Kotlerad4b8b42013-03-13 20:40:30 +00004689 if (FD->hasAttr<Mips16Attr>()) {
4690 Fn->addFnAttr("mips16");
4691 }
4692 else if (FD->hasAttr<NoMips16Attr>()) {
4693 Fn->addFnAttr("nomips16");
4694 }
Reed Kotler7dfd1822013-01-16 17:10:28 +00004695 }
Reed Kotlerad4b8b42013-03-13 20:40:30 +00004696
John McCallaeeb7012010-05-27 06:19:26 +00004697 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00004698 llvm::Value *Address) const;
John McCall49e34be2011-08-30 01:42:09 +00004699
4700 unsigned getSizeOfUnwindException() const {
Akira Hatanakae624fa02011-09-20 18:23:28 +00004701 return SizeOfUnwindException;
John McCall49e34be2011-08-30 01:42:09 +00004702 }
John McCallaeeb7012010-05-27 06:19:26 +00004703};
4704}
4705
Akira Hatanakac359f202012-07-03 19:24:06 +00004706void MipsABIInfo::CoerceToIntArgs(uint64_t TySize,
4707 SmallVector<llvm::Type*, 8> &ArgList) const {
4708 llvm::IntegerType *IntTy =
4709 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004710
4711 // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
4712 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
4713 ArgList.push_back(IntTy);
4714
4715 // If necessary, add one more integer type to ArgList.
4716 unsigned R = TySize % (MinABIStackAlignInBytes * 8);
4717
4718 if (R)
4719 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004720}
4721
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004722// In N32/64, an aligned double precision floating point field is passed in
4723// a register.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004724llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
Akira Hatanakac359f202012-07-03 19:24:06 +00004725 SmallVector<llvm::Type*, 8> ArgList, IntArgList;
4726
4727 if (IsO32) {
4728 CoerceToIntArgs(TySize, ArgList);
4729 return llvm::StructType::get(getVMContext(), ArgList);
4730 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004731
Akira Hatanaka2afd23d2012-01-12 00:52:17 +00004732 if (Ty->isComplexType())
4733 return CGT.ConvertType(Ty);
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00004734
Akira Hatanakaa34e9212012-02-09 19:54:16 +00004735 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004736
Akira Hatanakac359f202012-07-03 19:24:06 +00004737 // Unions/vectors are passed in integer registers.
4738 if (!RT || !RT->isStructureOrClassType()) {
4739 CoerceToIntArgs(TySize, ArgList);
4740 return llvm::StructType::get(getVMContext(), ArgList);
4741 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004742
4743 const RecordDecl *RD = RT->getDecl();
4744 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004745 assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004746
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004747 uint64_t LastOffset = 0;
4748 unsigned idx = 0;
4749 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
4750
Akira Hatanakaa34e9212012-02-09 19:54:16 +00004751 // Iterate over fields in the struct/class and check if there are any aligned
4752 // double fields.
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004753 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
4754 i != e; ++i, ++idx) {
David Blaikie262bc182012-04-30 02:36:29 +00004755 const QualType Ty = i->getType();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004756 const BuiltinType *BT = Ty->getAs<BuiltinType>();
4757
4758 if (!BT || BT->getKind() != BuiltinType::Double)
4759 continue;
4760
4761 uint64_t Offset = Layout.getFieldOffset(idx);
4762 if (Offset % 64) // Ignore doubles that are not aligned.
4763 continue;
4764
4765 // Add ((Offset - LastOffset) / 64) args of type i64.
4766 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
4767 ArgList.push_back(I64);
4768
4769 // Add double type.
4770 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
4771 LastOffset = Offset + 64;
4772 }
4773
Akira Hatanakac359f202012-07-03 19:24:06 +00004774 CoerceToIntArgs(TySize - LastOffset, IntArgList);
4775 ArgList.append(IntArgList.begin(), IntArgList.end());
Akira Hatanakad5a257f2011-11-02 23:54:49 +00004776
4777 return llvm::StructType::get(getVMContext(), ArgList);
4778}
4779
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004780llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004781 assert((Offset % MinABIStackAlignInBytes) == 0);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004782
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004783 if ((Align - 1) & Offset)
4784 return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
4785
4786 return 0;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004787}
Akira Hatanaka9659d592012-01-10 22:44:52 +00004788
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004789ABIArgInfo
4790MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004791 uint64_t OrigOffset = Offset;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004792 uint64_t TySize = getContext().getTypeSize(Ty);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004793 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004794
Akira Hatanakac359f202012-07-03 19:24:06 +00004795 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
4796 (uint64_t)StackAlignInBytes);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004797 Offset = llvm::RoundUpToAlignment(Offset, Align);
4798 Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004799
Akira Hatanakac359f202012-07-03 19:24:06 +00004800 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
Akira Hatanaka619e8872011-06-02 00:09:17 +00004801 // Ignore empty aggregates.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004802 if (TySize == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00004803 return ABIArgInfo::getIgnore();
4804
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00004805 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004806 Offset = OrigOffset + MinABIStackAlignInBytes;
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00004807 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004808 }
Akira Hatanaka511949b2011-08-01 18:09:58 +00004809
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004810 // If we have reached here, aggregates are passed directly by coercing to
4811 // another structure type. Padding is inserted if the offset of the
4812 // aggregate is unaligned.
4813 return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
4814 getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00004815 }
4816
4817 // Treat an enum type as its underlying type.
4818 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4819 Ty = EnumTy->getDecl()->getIntegerType();
4820
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004821 if (Ty->isPromotableIntegerType())
4822 return ABIArgInfo::getExtend();
4823
Akira Hatanaka4055cfc2013-01-24 21:47:33 +00004824 return ABIArgInfo::getDirect(0, 0,
4825 IsO32 ? 0 : getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00004826}
4827
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004828llvm::Type*
4829MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakada54ff32012-02-09 18:49:26 +00004830 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakac359f202012-07-03 19:24:06 +00004831 SmallVector<llvm::Type*, 8> RTList;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004832
Akira Hatanakada54ff32012-02-09 18:49:26 +00004833 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004834 const RecordDecl *RD = RT->getDecl();
Akira Hatanakada54ff32012-02-09 18:49:26 +00004835 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
4836 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004837
Akira Hatanakada54ff32012-02-09 18:49:26 +00004838 // N32/64 returns struct/classes in floating point registers if the
4839 // following conditions are met:
4840 // 1. The size of the struct/class is no larger than 128-bit.
4841 // 2. The struct/class has one or two fields all of which are floating
4842 // point types.
4843 // 3. The offset of the first field is zero (this follows what gcc does).
4844 //
4845 // Any other composite results are returned in integer registers.
4846 //
4847 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
4848 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
4849 for (; b != e; ++b) {
David Blaikie262bc182012-04-30 02:36:29 +00004850 const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004851
Akira Hatanakada54ff32012-02-09 18:49:26 +00004852 if (!BT || !BT->isFloatingPoint())
4853 break;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004854
David Blaikie262bc182012-04-30 02:36:29 +00004855 RTList.push_back(CGT.ConvertType(b->getType()));
Akira Hatanakada54ff32012-02-09 18:49:26 +00004856 }
4857
4858 if (b == e)
4859 return llvm::StructType::get(getVMContext(), RTList,
4860 RD->hasAttr<PackedAttr>());
4861
4862 RTList.clear();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004863 }
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004864 }
4865
Akira Hatanakac359f202012-07-03 19:24:06 +00004866 CoerceToIntArgs(Size, RTList);
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004867 return llvm::StructType::get(getVMContext(), RTList);
4868}
4869
Akira Hatanaka619e8872011-06-02 00:09:17 +00004870ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanakaa8536c02012-01-23 23:18:57 +00004871 uint64_t Size = getContext().getTypeSize(RetTy);
4872
4873 if (RetTy->isVoidType() || Size == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00004874 return ABIArgInfo::getIgnore();
4875
Akira Hatanaka8aeb1472012-05-11 21:01:17 +00004876 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00004877 if (isRecordReturnIndirect(RetTy, CGT))
4878 return ABIArgInfo::getIndirect(0);
4879
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004880 if (Size <= 128) {
4881 if (RetTy->isAnyComplexType())
4882 return ABIArgInfo::getDirect();
4883
Akira Hatanakac359f202012-07-03 19:24:06 +00004884 // O32 returns integer vectors in registers.
4885 if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())
4886 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
4887
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00004888 if (!IsO32)
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004889 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
4890 }
Akira Hatanaka619e8872011-06-02 00:09:17 +00004891
4892 return ABIArgInfo::getIndirect(0);
4893 }
4894
4895 // Treat an enum type as its underlying type.
4896 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4897 RetTy = EnumTy->getDecl()->getIntegerType();
4898
4899 return (RetTy->isPromotableIntegerType() ?
4900 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4901}
4902
4903void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanakacc662542012-01-12 01:10:09 +00004904 ABIArgInfo &RetInfo = FI.getReturnInfo();
4905 RetInfo = classifyReturnType(FI.getReturnType());
4906
4907 // Check if a pointer to an aggregate is passed as a hidden argument.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004908 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
Akira Hatanakacc662542012-01-12 01:10:09 +00004909
Akira Hatanaka619e8872011-06-02 00:09:17 +00004910 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4911 it != ie; ++it)
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004912 it->info = classifyArgumentType(it->type, Offset);
Akira Hatanaka619e8872011-06-02 00:09:17 +00004913}
4914
4915llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4916 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00004917 llvm::Type *BP = CGF.Int8PtrTy;
4918 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004919
4920 CGBuilderTy &Builder = CGF.Builder;
4921 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
4922 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004923 int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004924 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
4925 llvm::Value *AddrTyped;
John McCall64aa4b32013-04-16 22:48:15 +00004926 unsigned PtrWidth = getTarget().getPointerWidth(0);
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004927 llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004928
4929 if (TypeAlign > MinABIStackAlignInBytes) {
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004930 llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
4931 llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
4932 llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
4933 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004934 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
4935 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
4936 }
4937 else
4938 AddrTyped = Builder.CreateBitCast(Addr, PTy);
4939
4940 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004941 TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004942 uint64_t Offset =
4943 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
4944 llvm::Value *NextAddr =
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004945 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004946 "ap.next");
4947 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
4948
4949 return AddrTyped;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004950}
4951
John McCallaeeb7012010-05-27 06:19:26 +00004952bool
4953MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4954 llvm::Value *Address) const {
4955 // This information comes from gcc's implementation, which seems to
4956 // as canonical as it gets.
4957
John McCallaeeb7012010-05-27 06:19:26 +00004958 // Everything on MIPS is 4 bytes. Double-precision FP registers
4959 // are aliased to pairs of single-precision FP registers.
Chris Lattner8b418682012-02-07 00:39:47 +00004960 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCallaeeb7012010-05-27 06:19:26 +00004961
4962 // 0-31 are the general purpose registers, $0 - $31.
4963 // 32-63 are the floating-point registers, $f0 - $f31.
4964 // 64 and 65 are the multiply/divide registers, $hi and $lo.
4965 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattner8b418682012-02-07 00:39:47 +00004966 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCallaeeb7012010-05-27 06:19:26 +00004967
4968 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
4969 // They are one bit wide and ignored here.
4970
4971 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
4972 // (coprocessor 1 is the FP unit)
4973 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
4974 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
4975 // 176-181 are the DSP accumulator registers.
Chris Lattner8b418682012-02-07 00:39:47 +00004976 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCallaeeb7012010-05-27 06:19:26 +00004977 return false;
4978}
4979
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004980//===----------------------------------------------------------------------===//
4981// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
4982// Currently subclassed only to implement custom OpenCL C function attribute
4983// handling.
4984//===----------------------------------------------------------------------===//
4985
4986namespace {
4987
4988class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
4989public:
4990 TCETargetCodeGenInfo(CodeGenTypes &CGT)
4991 : DefaultTargetCodeGenInfo(CGT) {}
4992
4993 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4994 CodeGen::CodeGenModule &M) const;
4995};
4996
4997void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4998 llvm::GlobalValue *GV,
4999 CodeGen::CodeGenModule &M) const {
5000 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
5001 if (!FD) return;
5002
5003 llvm::Function *F = cast<llvm::Function>(GV);
5004
David Blaikie4e4d0842012-03-11 07:00:24 +00005005 if (M.getLangOpts().OpenCL) {
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00005006 if (FD->hasAttr<OpenCLKernelAttr>()) {
5007 // OpenCL C Kernel functions are not subject to inlining
Bill Wendling72390b32012-12-20 19:27:06 +00005008 F->addFnAttr(llvm::Attribute::NoInline);
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00005009
5010 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
5011
5012 // Convert the reqd_work_group_size() attributes to metadata.
5013 llvm::LLVMContext &Context = F->getContext();
5014 llvm::NamedMDNode *OpenCLMetadata =
5015 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
5016
5017 SmallVector<llvm::Value*, 5> Operands;
5018 Operands.push_back(F);
5019
Chris Lattner8b418682012-02-07 00:39:47 +00005020 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
5021 llvm::APInt(32,
5022 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
5023 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
5024 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00005025 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
Chris Lattner8b418682012-02-07 00:39:47 +00005026 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
5027 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00005028 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
5029
5030 // Add a boolean constant operand for "required" (true) or "hint" (false)
5031 // for implementing the work_group_size_hint attr later. Currently
5032 // always true as the hint is not yet implemented.
Chris Lattner8b418682012-02-07 00:39:47 +00005033 Operands.push_back(llvm::ConstantInt::getTrue(Context));
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00005034 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
5035 }
5036 }
5037 }
5038}
5039
5040}
John McCallaeeb7012010-05-27 06:19:26 +00005041
Tony Linthicum96319392011-12-12 21:14:55 +00005042//===----------------------------------------------------------------------===//
5043// Hexagon ABI Implementation
5044//===----------------------------------------------------------------------===//
5045
5046namespace {
5047
5048class HexagonABIInfo : public ABIInfo {
5049
5050
5051public:
5052 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
5053
5054private:
5055
5056 ABIArgInfo classifyReturnType(QualType RetTy) const;
5057 ABIArgInfo classifyArgumentType(QualType RetTy) const;
5058
5059 virtual void computeInfo(CGFunctionInfo &FI) const;
5060
5061 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5062 CodeGenFunction &CGF) const;
5063};
5064
5065class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
5066public:
5067 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
5068 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
5069
5070 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
5071 return 29;
5072 }
5073};
5074
5075}
5076
5077void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
5078 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
5079 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
5080 it != ie; ++it)
5081 it->info = classifyArgumentType(it->type);
5082}
5083
5084ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
5085 if (!isAggregateTypeForABI(Ty)) {
5086 // Treat an enum type as its underlying type.
5087 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5088 Ty = EnumTy->getDecl()->getIntegerType();
5089
5090 return (Ty->isPromotableIntegerType() ?
5091 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
5092 }
5093
5094 // Ignore empty records.
5095 if (isEmptyRecord(getContext(), Ty, true))
5096 return ABIArgInfo::getIgnore();
5097
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00005098 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
5099 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Tony Linthicum96319392011-12-12 21:14:55 +00005100
5101 uint64_t Size = getContext().getTypeSize(Ty);
5102 if (Size > 64)
5103 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
5104 // Pass in the smallest viable integer type.
5105 else if (Size > 32)
5106 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
5107 else if (Size > 16)
5108 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
5109 else if (Size > 8)
5110 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5111 else
5112 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
5113}
5114
5115ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
5116 if (RetTy->isVoidType())
5117 return ABIArgInfo::getIgnore();
5118
5119 // Large vector types should be returned via memory.
5120 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
5121 return ABIArgInfo::getIndirect(0);
5122
5123 if (!isAggregateTypeForABI(RetTy)) {
5124 // Treat an enum type as its underlying type.
5125 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5126 RetTy = EnumTy->getDecl()->getIntegerType();
5127
5128 return (RetTy->isPromotableIntegerType() ?
5129 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
5130 }
5131
5132 // Structures with either a non-trivial destructor or a non-trivial
5133 // copy constructor are always indirect.
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00005134 if (isRecordReturnIndirect(RetTy, CGT))
Tony Linthicum96319392011-12-12 21:14:55 +00005135 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
5136
5137 if (isEmptyRecord(getContext(), RetTy, true))
5138 return ABIArgInfo::getIgnore();
5139
5140 // Aggregates <= 8 bytes are returned in r0; other aggregates
5141 // are returned indirectly.
5142 uint64_t Size = getContext().getTypeSize(RetTy);
5143 if (Size <= 64) {
5144 // Return in the smallest viable integer type.
5145 if (Size <= 8)
5146 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
5147 if (Size <= 16)
5148 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5149 if (Size <= 32)
5150 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
5151 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
5152 }
5153
5154 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
5155}
5156
5157llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner8b418682012-02-07 00:39:47 +00005158 CodeGenFunction &CGF) const {
Tony Linthicum96319392011-12-12 21:14:55 +00005159 // FIXME: Need to handle alignment
Chris Lattner8b418682012-02-07 00:39:47 +00005160 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Tony Linthicum96319392011-12-12 21:14:55 +00005161
5162 CGBuilderTy &Builder = CGF.Builder;
5163 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
5164 "ap");
5165 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
5166 llvm::Type *PTy =
5167 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
5168 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
5169
5170 uint64_t Offset =
5171 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
5172 llvm::Value *NextAddr =
5173 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
5174 "ap.next");
5175 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
5176
5177 return AddrTyped;
5178}
5179
5180
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00005181//===----------------------------------------------------------------------===//
5182// SPARC v9 ABI Implementation.
5183// Based on the SPARC Compliance Definition version 2.4.1.
5184//
5185// Function arguments a mapped to a nominal "parameter array" and promoted to
5186// registers depending on their type. Each argument occupies 8 or 16 bytes in
5187// the array, structs larger than 16 bytes are passed indirectly.
5188//
5189// One case requires special care:
5190//
5191// struct mixed {
5192// int i;
5193// float f;
5194// };
5195//
5196// When a struct mixed is passed by value, it only occupies 8 bytes in the
5197// parameter array, but the int is passed in an integer register, and the float
5198// is passed in a floating point register. This is represented as two arguments
5199// with the LLVM IR inreg attribute:
5200//
5201// declare void f(i32 inreg %i, float inreg %f)
5202//
5203// The code generator will only allocate 4 bytes from the parameter array for
5204// the inreg arguments. All other arguments are allocated a multiple of 8
5205// bytes.
5206//
5207namespace {
5208class SparcV9ABIInfo : public ABIInfo {
5209public:
5210 SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
5211
5212private:
5213 ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const;
5214 virtual void computeInfo(CGFunctionInfo &FI) const;
5215 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5216 CodeGenFunction &CGF) const;
Jakob Stoklund Olesenfc782fb2013-05-28 04:57:37 +00005217
5218 // Coercion type builder for structs passed in registers. The coercion type
5219 // serves two purposes:
5220 //
5221 // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned'
5222 // in registers.
5223 // 2. Expose aligned floating point elements as first-level elements, so the
5224 // code generator knows to pass them in floating point registers.
5225 //
5226 // We also compute the InReg flag which indicates that the struct contains
5227 // aligned 32-bit floats.
5228 //
5229 struct CoerceBuilder {
5230 llvm::LLVMContext &Context;
5231 const llvm::DataLayout &DL;
5232 SmallVector<llvm::Type*, 8> Elems;
5233 uint64_t Size;
5234 bool InReg;
5235
5236 CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl)
5237 : Context(c), DL(dl), Size(0), InReg(false) {}
5238
5239 // Pad Elems with integers until Size is ToSize.
5240 void pad(uint64_t ToSize) {
5241 assert(ToSize >= Size && "Cannot remove elements");
5242 if (ToSize == Size)
5243 return;
5244
5245 // Finish the current 64-bit word.
5246 uint64_t Aligned = llvm::RoundUpToAlignment(Size, 64);
5247 if (Aligned > Size && Aligned <= ToSize) {
5248 Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size));
5249 Size = Aligned;
5250 }
5251
5252 // Add whole 64-bit words.
5253 while (Size + 64 <= ToSize) {
5254 Elems.push_back(llvm::Type::getInt64Ty(Context));
5255 Size += 64;
5256 }
5257
5258 // Final in-word padding.
5259 if (Size < ToSize) {
5260 Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size));
5261 Size = ToSize;
5262 }
5263 }
5264
5265 // Add a floating point element at Offset.
5266 void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) {
5267 // Unaligned floats are treated as integers.
5268 if (Offset % Bits)
5269 return;
5270 // The InReg flag is only required if there are any floats < 64 bits.
5271 if (Bits < 64)
5272 InReg = true;
5273 pad(Offset);
5274 Elems.push_back(Ty);
5275 Size = Offset + Bits;
5276 }
5277
5278 // Add a struct type to the coercion type, starting at Offset (in bits).
5279 void addStruct(uint64_t Offset, llvm::StructType *StrTy) {
5280 const llvm::StructLayout *Layout = DL.getStructLayout(StrTy);
5281 for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) {
5282 llvm::Type *ElemTy = StrTy->getElementType(i);
5283 uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i);
5284 switch (ElemTy->getTypeID()) {
5285 case llvm::Type::StructTyID:
5286 addStruct(ElemOffset, cast<llvm::StructType>(ElemTy));
5287 break;
5288 case llvm::Type::FloatTyID:
5289 addFloat(ElemOffset, ElemTy, 32);
5290 break;
5291 case llvm::Type::DoubleTyID:
5292 addFloat(ElemOffset, ElemTy, 64);
5293 break;
5294 case llvm::Type::FP128TyID:
5295 addFloat(ElemOffset, ElemTy, 128);
5296 break;
5297 case llvm::Type::PointerTyID:
5298 if (ElemOffset % 64 == 0) {
5299 pad(ElemOffset);
5300 Elems.push_back(ElemTy);
5301 Size += 64;
5302 }
5303 break;
5304 default:
5305 break;
5306 }
5307 }
5308 }
5309
5310 // Check if Ty is a usable substitute for the coercion type.
5311 bool isUsableType(llvm::StructType *Ty) const {
5312 if (Ty->getNumElements() != Elems.size())
5313 return false;
5314 for (unsigned i = 0, e = Elems.size(); i != e; ++i)
5315 if (Elems[i] != Ty->getElementType(i))
5316 return false;
5317 return true;
5318 }
5319
5320 // Get the coercion type as a literal struct type.
5321 llvm::Type *getType() const {
5322 if (Elems.size() == 1)
5323 return Elems.front();
5324 else
5325 return llvm::StructType::get(Context, Elems);
5326 }
5327 };
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00005328};
5329} // end anonymous namespace
5330
5331ABIArgInfo
5332SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const {
5333 if (Ty->isVoidType())
5334 return ABIArgInfo::getIgnore();
5335
5336 uint64_t Size = getContext().getTypeSize(Ty);
5337
5338 // Anything too big to fit in registers is passed with an explicit indirect
5339 // pointer / sret pointer.
5340 if (Size > SizeLimit)
5341 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
5342
5343 // Treat an enum type as its underlying type.
5344 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5345 Ty = EnumTy->getDecl()->getIntegerType();
5346
5347 // Integer types smaller than a register are extended.
5348 if (Size < 64 && Ty->isIntegerType())
5349 return ABIArgInfo::getExtend();
5350
5351 // Other non-aggregates go in registers.
5352 if (!isAggregateTypeForABI(Ty))
5353 return ABIArgInfo::getDirect();
5354
5355 // This is a small aggregate type that should be passed in registers.
Jakob Stoklund Olesenfc782fb2013-05-28 04:57:37 +00005356 // Build a coercion type from the LLVM struct type.
5357 llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty));
5358 if (!StrTy)
5359 return ABIArgInfo::getDirect();
5360
5361 CoerceBuilder CB(getVMContext(), getDataLayout());
5362 CB.addStruct(0, StrTy);
5363 CB.pad(llvm::RoundUpToAlignment(CB.DL.getTypeSizeInBits(StrTy), 64));
5364
5365 // Try to use the original type for coercion.
5366 llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();
5367
5368 if (CB.InReg)
5369 return ABIArgInfo::getDirectInReg(CoerceTy);
5370 else
5371 return ABIArgInfo::getDirect(CoerceTy);
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00005372}
5373
5374llvm::Value *SparcV9ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5375 CodeGenFunction &CGF) const {
Jakob Stoklund Olesena4b56d32013-06-05 03:00:18 +00005376 ABIArgInfo AI = classifyType(Ty, 16 * 8);
5377 llvm::Type *ArgTy = CGT.ConvertType(Ty);
5378 if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
5379 AI.setCoerceToType(ArgTy);
5380
5381 llvm::Type *BPP = CGF.Int8PtrPtrTy;
5382 CGBuilderTy &Builder = CGF.Builder;
5383 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
5384 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
5385 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
5386 llvm::Value *ArgAddr;
5387 unsigned Stride;
5388
5389 switch (AI.getKind()) {
5390 case ABIArgInfo::Expand:
5391 llvm_unreachable("Unsupported ABI kind for va_arg");
5392
5393 case ABIArgInfo::Extend:
5394 Stride = 8;
5395 ArgAddr = Builder
5396 .CreateConstGEP1_32(Addr, 8 - getDataLayout().getTypeAllocSize(ArgTy),
5397 "extend");
5398 break;
5399
5400 case ABIArgInfo::Direct:
5401 Stride = getDataLayout().getTypeAllocSize(AI.getCoerceToType());
5402 ArgAddr = Addr;
5403 break;
5404
5405 case ABIArgInfo::Indirect:
5406 Stride = 8;
5407 ArgAddr = Builder.CreateBitCast(Addr,
5408 llvm::PointerType::getUnqual(ArgPtrTy),
5409 "indirect");
5410 ArgAddr = Builder.CreateLoad(ArgAddr, "indirect.arg");
5411 break;
5412
5413 case ABIArgInfo::Ignore:
5414 return llvm::UndefValue::get(ArgPtrTy);
5415 }
5416
5417 // Update VAList.
5418 Addr = Builder.CreateConstGEP1_32(Addr, Stride, "ap.next");
5419 Builder.CreateStore(Addr, VAListAddrAsBPP);
5420
5421 return Builder.CreatePointerCast(ArgAddr, ArgPtrTy, "arg.addr");
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00005422}
5423
5424void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const {
5425 FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8);
5426 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
5427 it != ie; ++it)
5428 it->info = classifyType(it->type, 16 * 8);
5429}
5430
5431namespace {
5432class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo {
5433public:
5434 SparcV9TargetCodeGenInfo(CodeGenTypes &CGT)
5435 : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {}
5436};
5437} // end anonymous namespace
5438
5439
Chris Lattnerea044322010-07-29 02:01:43 +00005440const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005441 if (TheTargetCodeGenInfo)
5442 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005443
John McCall64aa4b32013-04-16 22:48:15 +00005444 const llvm::Triple &Triple = getTarget().getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00005445 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00005446 default:
Chris Lattnerea044322010-07-29 02:01:43 +00005447 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00005448
Derek Schuff9ed63f82012-09-06 17:37:28 +00005449 case llvm::Triple::le32:
5450 return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
John McCallaeeb7012010-05-27 06:19:26 +00005451 case llvm::Triple::mips:
5452 case llvm::Triple::mipsel:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00005453 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCallaeeb7012010-05-27 06:19:26 +00005454
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00005455 case llvm::Triple::mips64:
5456 case llvm::Triple::mips64el:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00005457 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00005458
Tim Northoverc264e162013-01-31 12:13:10 +00005459 case llvm::Triple::aarch64:
5460 return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types));
5461
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00005462 case llvm::Triple::arm:
5463 case llvm::Triple::thumb:
Sandeep Patel34c1af82011-04-05 00:23:47 +00005464 {
5465 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
John McCall64aa4b32013-04-16 22:48:15 +00005466 if (strcmp(getTarget().getABI(), "apcs-gnu") == 0)
Sandeep Patel34c1af82011-04-05 00:23:47 +00005467 Kind = ARMABIInfo::APCS;
David Tweedb16abb12012-10-25 13:33:01 +00005468 else if (CodeGenOpts.FloatABI == "hard" ||
John McCall64aa4b32013-04-16 22:48:15 +00005469 (CodeGenOpts.FloatABI != "soft" &&
5470 Triple.getEnvironment() == llvm::Triple::GNUEABIHF))
Sandeep Patel34c1af82011-04-05 00:23:47 +00005471 Kind = ARMABIInfo::AAPCS_VFP;
5472
Derek Schuff263366f2012-10-16 22:30:41 +00005473 switch (Triple.getOS()) {
Eli Bendersky441d9f72012-12-04 18:38:10 +00005474 case llvm::Triple::NaCl:
Derek Schuff263366f2012-10-16 22:30:41 +00005475 return *(TheTargetCodeGenInfo =
5476 new NaClARMTargetCodeGenInfo(Types, Kind));
5477 default:
5478 return *(TheTargetCodeGenInfo =
5479 new ARMTargetCodeGenInfo(Types, Kind));
5480 }
Sandeep Patel34c1af82011-04-05 00:23:47 +00005481 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00005482
John McCallec853ba2010-03-11 00:10:12 +00005483 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00005484 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
Roman Divacky0fbc4b92012-05-09 18:22:46 +00005485 case llvm::Triple::ppc64:
Bill Schmidt2fc107f2012-10-03 19:18:57 +00005486 if (Triple.isOSBinFormatELF())
5487 return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types));
5488 else
5489 return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00005490
Peter Collingbourneedb66f32012-05-20 23:28:41 +00005491 case llvm::Triple::nvptx:
5492 case llvm::Triple::nvptx64:
Justin Holewinski2c585b92012-05-24 17:43:12 +00005493 return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types));
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005494
Wesley Peck276fdf42010-12-19 19:57:51 +00005495 case llvm::Triple::mblaze:
5496 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
5497
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005498 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00005499 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00005500
Ulrich Weigandb8409212013-05-06 16:26:41 +00005501 case llvm::Triple::systemz:
5502 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
5503
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00005504 case llvm::Triple::tce:
5505 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
5506
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00005507 case llvm::Triple::x86: {
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00005508 if (Triple.isOSDarwin())
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005509 return *(TheTargetCodeGenInfo =
Chad Rosier1f1df1f2013-03-25 21:00:27 +00005510 new X86_32TargetCodeGenInfo(Types, true, true, false,
Rafael Espindolab48280b2012-07-31 02:44:24 +00005511 CodeGenOpts.NumRegisterParameters));
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00005512
5513 switch (Triple.getOS()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00005514 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00005515 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00005516 case llvm::Triple::AuroraUX:
5517 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00005518 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00005519 case llvm::Triple::OpenBSD:
Eli Friedman42f74f22012-08-08 23:57:20 +00005520 case llvm::Triple::Bitrig:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005521 return *(TheTargetCodeGenInfo =
Chad Rosier1f1df1f2013-03-25 21:00:27 +00005522 new X86_32TargetCodeGenInfo(Types, false, true, false,
Rafael Espindolab48280b2012-07-31 02:44:24 +00005523 CodeGenOpts.NumRegisterParameters));
Eli Friedman55fc7e22012-01-25 22:46:34 +00005524
5525 case llvm::Triple::Win32:
5526 return *(TheTargetCodeGenInfo =
Reid Kleckner3190ca92013-05-08 13:44:39 +00005527 new WinX86_32TargetCodeGenInfo(Types,
5528 CodeGenOpts.NumRegisterParameters));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00005529
5530 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005531 return *(TheTargetCodeGenInfo =
Chad Rosier1f1df1f2013-03-25 21:00:27 +00005532 new X86_32TargetCodeGenInfo(Types, false, false, false,
Rafael Espindolab48280b2012-07-31 02:44:24 +00005533 CodeGenOpts.NumRegisterParameters));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005534 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00005535 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005536
Eli Friedmanee1ad992011-12-02 00:11:43 +00005537 case llvm::Triple::x86_64: {
John McCall64aa4b32013-04-16 22:48:15 +00005538 bool HasAVX = strcmp(getTarget().getABI(), "avx") == 0;
Eli Friedmanee1ad992011-12-02 00:11:43 +00005539
Chris Lattnerf13721d2010-08-31 16:44:54 +00005540 switch (Triple.getOS()) {
5541 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00005542 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00005543 case llvm::Triple::Cygwin:
5544 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
Eli Bendersky441d9f72012-12-04 18:38:10 +00005545 case llvm::Triple::NaCl:
John McCall64aa4b32013-04-16 22:48:15 +00005546 return *(TheTargetCodeGenInfo = new NaClX86_64TargetCodeGenInfo(Types,
5547 HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00005548 default:
Eli Friedmanee1ad992011-12-02 00:11:43 +00005549 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
5550 HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00005551 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00005552 }
Tony Linthicum96319392011-12-12 21:14:55 +00005553 case llvm::Triple::hexagon:
5554 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00005555 case llvm::Triple::sparcv9:
5556 return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types));
Eli Friedmanee1ad992011-12-02 00:11:43 +00005557 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005558}