blob: 53747e3fcd1a3dc1dce9b26d54984fbcea817e76 [file] [log] [blame]
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikov244360d2009-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 Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000016#include "ABIInfo.h"
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +000017#include "CGCXXABI.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000018#include "CodeGenFunction.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000019#include "clang/AST/RecordLayout.h"
Mark Laceya8e7df32013-10-30 21:53:58 +000020#include "clang/CodeGen/CGFunctionInfo.h"
Sandeep Patel45df3dd2011-04-05 00:23:47 +000021#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbare3532f82009-08-24 08:52:16 +000022#include "llvm/ADT/Triple.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000023#include "llvm/IR/DataLayout.h"
24#include "llvm/IR/Type.h"
Daniel Dunbar7230fa52009-12-03 09:13:49 +000025#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000026using namespace clang;
27using namespace CodeGen;
28
John McCall943fae92010-05-27 06:19:26 +000029static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
30 llvm::Value *Array,
31 llvm::Value *Value,
32 unsigned FirstIndex,
33 unsigned LastIndex) {
34 // Alternatively, we could emit this as a loop in the source.
35 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
36 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
37 Builder.CreateStore(Value, Cell);
38 }
39}
40
John McCalla1dee5302010-08-22 10:59:02 +000041static bool isAggregateTypeForABI(QualType T) {
John McCall47fb9502013-03-07 21:37:08 +000042 return !CodeGenFunction::hasScalarEvaluationKind(T) ||
John McCalla1dee5302010-08-22 10:59:02 +000043 T->isMemberFunctionPointerType();
44}
45
Anton Korobeynikov244360d2009-06-05 22:08:42 +000046ABIInfo::~ABIInfo() {}
47
Mark Lacey3825e832013-10-06 01:33:34 +000048static bool isRecordReturnIndirect(const RecordType *RT,
49 CGCXXABI &CXXABI) {
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +000050 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
51 if (!RD)
52 return false;
Mark Lacey3825e832013-10-06 01:33:34 +000053 return CXXABI.isReturnTypeIndirect(RD);
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +000054}
55
56
Mark Lacey3825e832013-10-06 01:33:34 +000057static bool isRecordReturnIndirect(QualType T, CGCXXABI &CXXABI) {
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +000058 const RecordType *RT = T->getAs<RecordType>();
59 if (!RT)
60 return false;
Mark Lacey3825e832013-10-06 01:33:34 +000061 return isRecordReturnIndirect(RT, CXXABI);
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +000062}
63
64static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT,
Mark Lacey3825e832013-10-06 01:33:34 +000065 CGCXXABI &CXXABI) {
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +000066 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
67 if (!RD)
68 return CGCXXABI::RAA_Default;
Mark Lacey3825e832013-10-06 01:33:34 +000069 return CXXABI.getRecordArgABI(RD);
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +000070}
71
72static CGCXXABI::RecordArgABI getRecordArgABI(QualType T,
Mark Lacey3825e832013-10-06 01:33:34 +000073 CGCXXABI &CXXABI) {
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +000074 const RecordType *RT = T->getAs<RecordType>();
75 if (!RT)
76 return CGCXXABI::RAA_Default;
Mark Lacey3825e832013-10-06 01:33:34 +000077 return getRecordArgABI(RT, CXXABI);
78}
79
80CGCXXABI &ABIInfo::getCXXABI() const {
81 return CGT.getCXXABI();
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +000082}
83
Chris Lattner2b037972010-07-29 02:01:43 +000084ASTContext &ABIInfo::getContext() const {
85 return CGT.getContext();
86}
87
88llvm::LLVMContext &ABIInfo::getVMContext() const {
89 return CGT.getLLVMContext();
90}
91
Micah Villmowdd31ca12012-10-08 16:25:52 +000092const llvm::DataLayout &ABIInfo::getDataLayout() const {
93 return CGT.getDataLayout();
Chris Lattner2b037972010-07-29 02:01:43 +000094}
95
John McCallc8e01702013-04-16 22:48:15 +000096const TargetInfo &ABIInfo::getTarget() const {
97 return CGT.getTarget();
98}
Chris Lattner2b037972010-07-29 02:01:43 +000099
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000100void ABIArgInfo::dump() const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000101 raw_ostream &OS = llvm::errs();
Daniel Dunbar7230fa52009-12-03 09:13:49 +0000102 OS << "(ABIArgInfo Kind=";
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000103 switch (TheKind) {
104 case Direct:
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000105 OS << "Direct Type=";
Chris Lattner2192fe52011-07-18 04:24:23 +0000106 if (llvm::Type *Ty = getCoerceToType())
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000107 Ty->print(OS);
108 else
109 OS << "null";
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000110 break;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000111 case Extend:
Daniel Dunbar7230fa52009-12-03 09:13:49 +0000112 OS << "Extend";
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000113 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000114 case Ignore:
Daniel Dunbar7230fa52009-12-03 09:13:49 +0000115 OS << "Ignore";
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000116 break;
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000117 case InAlloca:
118 OS << "InAlloca Offset=" << getInAllocaFieldIndex();
119 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000120 case Indirect:
Daniel Dunbar557893d2010-04-21 19:10:51 +0000121 OS << "Indirect Align=" << getIndirectAlign()
Joerg Sonnenberger4921fe22011-07-15 18:23:44 +0000122 << " ByVal=" << getIndirectByVal()
Daniel Dunbar7b7c2932010-09-16 20:42:02 +0000123 << " Realign=" << getIndirectRealign();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000124 break;
125 case Expand:
Daniel Dunbar7230fa52009-12-03 09:13:49 +0000126 OS << "Expand";
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000127 break;
128 }
Daniel Dunbar7230fa52009-12-03 09:13:49 +0000129 OS << ")\n";
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000130}
131
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000132TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
133
John McCall3480ef22011-08-30 01:42:09 +0000134// If someone can figure out a general rule for this, that would be great.
135// It's probably just doomed to be platform-dependent, though.
136unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
137 // Verified for:
138 // x86-64 FreeBSD, Linux, Darwin
139 // x86-32 FreeBSD, Linux, Darwin
140 // PowerPC Linux, Darwin
141 // ARM Darwin (*not* EABI)
Tim Northover9bb857a2013-01-31 12:13:10 +0000142 // AArch64 Linux
John McCall3480ef22011-08-30 01:42:09 +0000143 return 32;
144}
145
John McCalla729c622012-02-17 03:33:10 +0000146bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
147 const FunctionNoProtoType *fnType) const {
John McCallcbc038a2011-09-21 08:08:30 +0000148 // The following conventions are known to require this to be false:
149 // x86_stdcall
150 // MIPS
151 // For everything else, we just prefer false unless we opt out.
152 return false;
153}
154
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000155void
156TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib,
157 llvm::SmallString<24> &Opt) const {
158 // This assumes the user is passing a library name like "rt" instead of a
159 // filename like "librt.a/so", and that they don't care whether it's static or
160 // dynamic.
161 Opt = "-l";
162 Opt += Lib;
163}
164
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000165static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000166
Sylvestre Ledru33b5baf2012-09-27 10:16:10 +0000167/// isEmptyField - Return true iff a the field is "empty", that is it
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000168/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000169static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
170 bool AllowArrays) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000171 if (FD->isUnnamedBitfield())
172 return true;
173
174 QualType FT = FD->getType();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000175
Eli Friedman0b3f2012011-11-18 03:47:20 +0000176 // Constant arrays of empty records count as empty, strip them off.
177 // Constant arrays of zero length always count as empty.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000178 if (AllowArrays)
Eli Friedman0b3f2012011-11-18 03:47:20 +0000179 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
180 if (AT->getSize() == 0)
181 return true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000182 FT = AT->getElementType();
Eli Friedman0b3f2012011-11-18 03:47:20 +0000183 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000184
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000185 const RecordType *RT = FT->getAs<RecordType>();
186 if (!RT)
187 return false;
188
189 // C++ record fields are never empty, at least in the Itanium ABI.
190 //
191 // FIXME: We should use a predicate for whether this behavior is true in the
192 // current ABI.
193 if (isa<CXXRecordDecl>(RT->getDecl()))
194 return false;
195
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000196 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000197}
198
Sylvestre Ledru33b5baf2012-09-27 10:16:10 +0000199/// isEmptyRecord - Return true iff a structure contains only empty
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000200/// fields. Note that a structure with a flexible array member is not
201/// considered empty.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000202static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000203 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000204 if (!RT)
205 return 0;
206 const RecordDecl *RD = RT->getDecl();
207 if (RD->hasFlexibleArrayMember())
208 return false;
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000209
Argyrios Kyrtzidisd42411f2011-05-17 02:17:52 +0000210 // If this is a C++ record, check the bases first.
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000211 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Argyrios Kyrtzidisd42411f2011-05-17 02:17:52 +0000212 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
213 e = CXXRD->bases_end(); i != e; ++i)
214 if (!isEmptyRecord(Context, i->getType(), true))
215 return false;
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000216
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000217 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
218 i != e; ++i)
David Blaikie40ed2972012-06-06 20:45:41 +0000219 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000220 return false;
221 return true;
222}
223
224/// isSingleElementStruct - Determine if a structure is a "single
225/// element struct", i.e. it has exactly one non-empty field or
226/// exactly one field which is itself a single element
227/// struct. Structures with flexible array members are never
228/// considered single element structs.
229///
230/// \return The field declaration for the single non-empty field, if
231/// it exists.
232static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
233 const RecordType *RT = T->getAsStructureType();
234 if (!RT)
235 return 0;
236
237 const RecordDecl *RD = RT->getDecl();
238 if (RD->hasFlexibleArrayMember())
239 return 0;
240
241 const Type *Found = 0;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000242
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000243 // If this is a C++ record, check the bases first.
244 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
245 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
246 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000247 // Ignore empty records.
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000248 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000249 continue;
250
251 // If we already found an element then this isn't a single-element struct.
252 if (Found)
253 return 0;
254
255 // If this is non-empty and not a single element struct, the composite
256 // cannot be a single element struct.
257 Found = isSingleElementStruct(i->getType(), Context);
258 if (!Found)
259 return 0;
260 }
261 }
262
263 // Check for single element.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000264 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
265 i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +0000266 const FieldDecl *FD = *i;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000267 QualType FT = FD->getType();
268
269 // Ignore empty fields.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000270 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000271 continue;
272
273 // If we already found an element then this isn't a single-element
274 // struct.
275 if (Found)
276 return 0;
277
278 // Treat single element arrays as the element.
279 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
280 if (AT->getSize().getZExtValue() != 1)
281 break;
282 FT = AT->getElementType();
283 }
284
John McCalla1dee5302010-08-22 10:59:02 +0000285 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000286 Found = FT.getTypePtr();
287 } else {
288 Found = isSingleElementStruct(FT, Context);
289 if (!Found)
290 return 0;
291 }
292 }
293
Eli Friedmanee945342011-11-18 01:25:50 +0000294 // We don't consider a struct a single-element struct if it has
295 // padding beyond the element type.
296 if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
297 return 0;
298
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000299 return Found;
300}
301
302static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Eli Friedmana92db672012-11-29 23:21:04 +0000303 // Treat complex types as the element type.
304 if (const ComplexType *CTy = Ty->getAs<ComplexType>())
305 Ty = CTy->getElementType();
306
307 // Check for a type which we know has a simple scalar argument-passing
308 // convention without any padding. (We're specifically looking for 32
309 // and 64-bit integer and integer-equivalents, float, and double.)
Daniel Dunbar6b45b672010-05-14 03:40:53 +0000310 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Eli Friedmana92db672012-11-29 23:21:04 +0000311 !Ty->isEnumeralType() && !Ty->isBlockPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000312 return false;
313
314 uint64_t Size = Context.getTypeSize(Ty);
315 return Size == 32 || Size == 64;
316}
317
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000318/// canExpandIndirectArgument - Test whether an argument type which is to be
319/// passed indirectly (on the stack) would have the equivalent layout if it was
320/// expanded into separate arguments. If so, we prefer to do the latter to avoid
321/// inhibiting optimizations.
322///
323// FIXME: This predicate is missing many cases, currently it just follows
324// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
325// should probably make this smarter, or better yet make the LLVM backend
326// capable of handling it.
327static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
328 // We can only expand structure types.
329 const RecordType *RT = Ty->getAs<RecordType>();
330 if (!RT)
331 return false;
332
333 // We can only expand (C) structures.
334 //
335 // FIXME: This needs to be generalized to handle classes as well.
336 const RecordDecl *RD = RT->getDecl();
337 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
338 return false;
339
Eli Friedmane5c85622011-11-18 01:32:26 +0000340 uint64_t Size = 0;
341
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000342 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
343 i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +0000344 const FieldDecl *FD = *i;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000345
346 if (!is32Or64BitBasicType(FD->getType(), Context))
347 return false;
348
349 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
350 // how to expand them yet, and the predicate for telling if a bitfield still
351 // counts as "basic" is more complicated than what we were doing previously.
352 if (FD->isBitField())
353 return false;
Eli Friedmane5c85622011-11-18 01:32:26 +0000354
355 Size += Context.getTypeSize(FD->getType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000356 }
357
Eli Friedmane5c85622011-11-18 01:32:26 +0000358 // Make sure there are not any holes in the struct.
359 if (Size != Context.getTypeSize(Ty))
360 return false;
361
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000362 return true;
363}
364
365namespace {
366/// DefaultABIInfo - The default implementation for ABI specific
367/// details. This implementation provides information which results in
368/// self-consistent and sensible LLVM IR generation, but does not
369/// conform to any particular ABI.
370class DefaultABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +0000371public:
372 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000373
Chris Lattner458b2aa2010-07-29 02:16:43 +0000374 ABIArgInfo classifyReturnType(QualType RetTy) const;
375 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000376
Chris Lattner22326a12010-07-29 02:31:05 +0000377 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000378 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000379 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
380 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +0000381 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000382 }
383
384 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
385 CodeGenFunction &CGF) const;
386};
387
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000388class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
389public:
Chris Lattner2b037972010-07-29 02:01:43 +0000390 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
391 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000392};
393
394llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
395 CodeGenFunction &CGF) const {
396 return 0;
397}
398
Chris Lattner458b2aa2010-07-29 02:16:43 +0000399ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
Jan Wen Voung180319f2011-11-03 00:59:44 +0000400 if (isAggregateTypeForABI(Ty)) {
Alp Tokerd4733632013-12-05 04:47:09 +0000401 // Records with non-trivial destructors/constructors should not be passed
Jan Wen Voung180319f2011-11-03 00:59:44 +0000402 // by value.
Mark Lacey3825e832013-10-06 01:33:34 +0000403 if (isRecordReturnIndirect(Ty, getCXXABI()))
Jan Wen Voung180319f2011-11-03 00:59:44 +0000404 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
405
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000406 return ABIArgInfo::getIndirect(0);
Jan Wen Voung180319f2011-11-03 00:59:44 +0000407 }
Daniel Dunbar557893d2010-04-21 19:10:51 +0000408
Chris Lattner9723d6c2010-03-11 18:19:55 +0000409 // Treat an enum type as its underlying type.
410 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
411 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000412
Chris Lattner9723d6c2010-03-11 18:19:55 +0000413 return (Ty->isPromotableIntegerType() ?
414 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000415}
416
Bob Wilsonbd4520b2011-01-10 23:54:17 +0000417ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
418 if (RetTy->isVoidType())
419 return ABIArgInfo::getIgnore();
420
421 if (isAggregateTypeForABI(RetTy))
422 return ABIArgInfo::getIndirect(0);
423
424 // Treat an enum type as its underlying type.
425 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
426 RetTy = EnumTy->getDecl()->getIntegerType();
427
428 return (RetTy->isPromotableIntegerType() ?
429 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
430}
431
Derek Schuff09338a22012-09-06 17:37:28 +0000432//===----------------------------------------------------------------------===//
433// le32/PNaCl bitcode ABI Implementation
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000434//
435// This is a simplified version of the x86_32 ABI. Arguments and return values
436// are always passed on the stack.
Derek Schuff09338a22012-09-06 17:37:28 +0000437//===----------------------------------------------------------------------===//
438
439class PNaClABIInfo : public ABIInfo {
440 public:
441 PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
442
443 ABIArgInfo classifyReturnType(QualType RetTy) const;
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000444 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Derek Schuff09338a22012-09-06 17:37:28 +0000445
446 virtual void computeInfo(CGFunctionInfo &FI) const;
447 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
448 CodeGenFunction &CGF) const;
449};
450
451class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
452 public:
453 PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
454 : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {}
455};
456
457void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
458 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
459
Derek Schuff09338a22012-09-06 17:37:28 +0000460 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
461 it != ie; ++it)
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000462 it->info = classifyArgumentType(it->type);
Derek Schuff09338a22012-09-06 17:37:28 +0000463 }
464
465llvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
466 CodeGenFunction &CGF) const {
467 return 0;
468}
469
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000470/// \brief Classify argument of given type \p Ty.
471ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const {
Derek Schuff09338a22012-09-06 17:37:28 +0000472 if (isAggregateTypeForABI(Ty)) {
Mark Lacey3825e832013-10-06 01:33:34 +0000473 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +0000474 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Derek Schuff09338a22012-09-06 17:37:28 +0000475 return ABIArgInfo::getIndirect(0);
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000476 } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
477 // Treat an enum type as its underlying type.
Derek Schuff09338a22012-09-06 17:37:28 +0000478 Ty = EnumTy->getDecl()->getIntegerType();
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000479 } else if (Ty->isFloatingType()) {
480 // Floating-point types don't go inreg.
481 return ABIArgInfo::getDirect();
Derek Schuff09338a22012-09-06 17:37:28 +0000482 }
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000483
484 return (Ty->isPromotableIntegerType() ?
485 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Derek Schuff09338a22012-09-06 17:37:28 +0000486}
487
488ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
489 if (RetTy->isVoidType())
490 return ABIArgInfo::getIgnore();
491
Eli Benderskye20dad62013-04-04 22:49:35 +0000492 // In the PNaCl ABI we always return records/structures on the stack.
Derek Schuff09338a22012-09-06 17:37:28 +0000493 if (isAggregateTypeForABI(RetTy))
494 return ABIArgInfo::getIndirect(0);
495
496 // Treat an enum type as its underlying type.
497 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
498 RetTy = EnumTy->getDecl()->getIntegerType();
499
500 return (RetTy->isPromotableIntegerType() ?
501 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
502}
503
Chad Rosier651c1832013-03-25 21:00:27 +0000504/// IsX86_MMXType - Return true if this is an MMX type.
505bool IsX86_MMXType(llvm::Type *IRType) {
506 // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>.
Bill Wendling5cd41c42010-10-18 03:41:31 +0000507 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
508 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
509 IRType->getScalarSizeInBits() != 64;
510}
511
Jay Foad7c57be32011-07-11 09:56:20 +0000512static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000513 StringRef Constraint,
Jay Foad7c57be32011-07-11 09:56:20 +0000514 llvm::Type* Ty) {
Tim Northover0ae93912013-06-07 00:04:50 +0000515 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) {
516 if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) {
517 // Invalid MMX constraint
518 return 0;
519 }
520
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000521 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
Tim Northover0ae93912013-06-07 00:04:50 +0000522 }
523
524 // No operation needed
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000525 return Ty;
526}
527
Chris Lattner0cf24192010-06-28 20:05:43 +0000528//===----------------------------------------------------------------------===//
529// X86-32 ABI Implementation
530//===----------------------------------------------------------------------===//
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000531
Reid Kleckner661f35b2014-01-18 01:12:41 +0000532/// \brief Similar to llvm::CCState, but for Clang.
533struct CCState {
534 CCState(unsigned CC) : CC(CC), FreeRegs(0) {}
535
536 unsigned CC;
537 unsigned FreeRegs;
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000538 unsigned StackOffset;
539 bool UseInAlloca;
Reid Kleckner661f35b2014-01-18 01:12:41 +0000540};
541
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000542/// X86_32ABIInfo - The X86-32 ABI information.
543class X86_32ABIInfo : public ABIInfo {
Rafael Espindola06b2b4a2012-07-31 02:44:24 +0000544 enum Class {
545 Integer,
546 Float
547 };
548
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000549 static const unsigned MinABIStackAlignInBytes = 4;
550
David Chisnallde3a0692009-08-17 23:08:21 +0000551 bool IsDarwinVectorABI;
552 bool IsSmallStructInRegABI;
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +0000553 bool IsWin32StructABI;
Rafael Espindola06b2b4a2012-07-31 02:44:24 +0000554 unsigned DefaultNumRegisterParameters;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000555
556 static bool isRegisterSize(unsigned Size) {
557 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
558 }
559
Reid Kleckner4982b822014-01-31 22:54:50 +0000560 bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context,
561 bool IsInstanceMethod) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000562
Daniel Dunbar557893d2010-04-21 19:10:51 +0000563 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
564 /// such that the argument will be passed in memory.
Reid Kleckner661f35b2014-01-18 01:12:41 +0000565 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
566
567 ABIArgInfo getIndirectReturnResult(CCState &State) const;
Daniel Dunbar557893d2010-04-21 19:10:51 +0000568
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000569 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000570 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000571
Rafael Espindola06b2b4a2012-07-31 02:44:24 +0000572 Class classify(QualType Ty) const;
Reid Kleckner4982b822014-01-31 22:54:50 +0000573 ABIArgInfo classifyReturnType(QualType RetTy, CCState &State,
574 bool IsInstanceMethod) const;
Reid Kleckner661f35b2014-01-18 01:12:41 +0000575 ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
576 bool shouldUseInReg(QualType Ty, CCState &State, bool &NeedsPadding) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000577
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000578 /// \brief Rewrite the function info so that all memory arguments use
579 /// inalloca.
580 void rewriteWithInAlloca(CGFunctionInfo &FI) const;
581
582 void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
583 unsigned &StackOffset, ABIArgInfo &Info,
584 QualType Type) const;
585
Rafael Espindola75419dc2012-07-23 23:30:29 +0000586public:
587
Rafael Espindolaa6472962012-07-24 00:01:07 +0000588 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000589 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
590 CodeGenFunction &CGF) const;
591
Chad Rosier651c1832013-03-25 21:00:27 +0000592 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool w,
Rafael Espindola06b2b4a2012-07-31 02:44:24 +0000593 unsigned r)
Eli Friedman33465822011-07-08 23:31:17 +0000594 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +0000595 IsWin32StructABI(w), DefaultNumRegisterParameters(r) {}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000596};
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000597
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000598class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
599public:
Eli Friedmana98d1f82012-01-25 22:46:34 +0000600 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
Chad Rosier651c1832013-03-25 21:00:27 +0000601 bool d, bool p, bool w, unsigned r)
602 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, w, r)) {}
Charles Davis4ea31ab2010-02-13 15:54:06 +0000603
John McCall1fe2a8c2013-06-18 02:46:29 +0000604 static bool isStructReturnInRegABI(
605 const llvm::Triple &Triple, const CodeGenOptions &Opts);
606
Charles Davis4ea31ab2010-02-13 15:54:06 +0000607 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
608 CodeGen::CodeGenModule &CGM) const;
John McCallbeec5a02010-03-06 00:35:14 +0000609
610 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
611 // Darwin uses different dwarf register numbers for EH.
John McCallc8e01702013-04-16 22:48:15 +0000612 if (CGM.getTarget().getTriple().isOSDarwin()) return 5;
John McCallbeec5a02010-03-06 00:35:14 +0000613 return 4;
614 }
615
616 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
617 llvm::Value *Address) const;
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000618
Jay Foad7c57be32011-07-11 09:56:20 +0000619 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000620 StringRef Constraint,
Jay Foad7c57be32011-07-11 09:56:20 +0000621 llvm::Type* Ty) const {
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000622 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
623 }
624
Peter Collingbourneb453cd62013-10-20 21:29:19 +0000625 llvm::Constant *getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const {
626 unsigned Sig = (0xeb << 0) | // jmp rel8
627 (0x06 << 8) | // .+0x08
628 ('F' << 16) |
629 ('T' << 24);
630 return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
631 }
632
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000633};
634
635}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000636
637/// shouldReturnTypeInRegister - Determine if the given type should be
638/// passed in a register (for the Darwin ABI).
Reid Kleckner4982b822014-01-31 22:54:50 +0000639bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, ASTContext &Context,
640 bool IsInstanceMethod) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000641 uint64_t Size = Context.getTypeSize(Ty);
642
643 // Type must be register sized.
644 if (!isRegisterSize(Size))
645 return false;
646
647 if (Ty->isVectorType()) {
648 // 64- and 128- bit vectors inside structures are not returned in
649 // registers.
650 if (Size == 64 || Size == 128)
651 return false;
652
653 return true;
654 }
655
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000656 // If this is a builtin, pointer, enum, complex type, member pointer, or
657 // member function pointer it is ok.
Daniel Dunbar6b45b672010-05-14 03:40:53 +0000658 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000659 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000660 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000661 return true;
662
663 // Arrays are treated like records.
664 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
Aaron Ballman3c424412012-02-22 03:04:13 +0000665 return shouldReturnTypeInRegister(AT->getElementType(), Context,
Reid Kleckner4982b822014-01-31 22:54:50 +0000666 IsInstanceMethod);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000667
668 // Otherwise, it must be a record type.
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000669 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000670 if (!RT) return false;
671
Anders Carlsson40446e82010-01-27 03:25:19 +0000672 // FIXME: Traverse bases here too.
673
Aaron Ballman3c424412012-02-22 03:04:13 +0000674 // For thiscall conventions, structures will never be returned in
675 // a register. This is for compatibility with the MSVC ABI
Reid Kleckner4982b822014-01-31 22:54:50 +0000676 if (IsWin32StructABI && IsInstanceMethod && RT->isStructureType())
Aaron Ballman3c424412012-02-22 03:04:13 +0000677 return false;
Aaron Ballman3c424412012-02-22 03:04:13 +0000678
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000679 // Structure types are passed in register if all fields would be
680 // passed in a register.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000681 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
682 e = RT->getDecl()->field_end(); i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +0000683 const FieldDecl *FD = *i;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000684
685 // Empty fields are ignored.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000686 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000687 continue;
688
689 // Check fields recursively.
Reid Kleckner4982b822014-01-31 22:54:50 +0000690 if (!shouldReturnTypeInRegister(FD->getType(), Context, IsInstanceMethod))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000691 return false;
692 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000693 return true;
694}
695
Reid Kleckner661f35b2014-01-18 01:12:41 +0000696ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(CCState &State) const {
697 // If the return value is indirect, then the hidden argument is consuming one
698 // integer register.
699 if (State.FreeRegs) {
700 --State.FreeRegs;
701 return ABIArgInfo::getIndirectInReg(/*Align=*/0, /*ByVal=*/false);
702 }
703 return ABIArgInfo::getIndirect(/*Align=*/0, /*ByVal=*/false);
704}
705
Reid Kleckner4982b822014-01-31 22:54:50 +0000706ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, CCState &State,
707 bool IsInstanceMethod) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000708 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000709 return ABIArgInfo::getIgnore();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000710
Chris Lattner458b2aa2010-07-29 02:16:43 +0000711 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000712 // On Darwin, some vectors are returned in registers.
David Chisnallde3a0692009-08-17 23:08:21 +0000713 if (IsDarwinVectorABI) {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000714 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000715
716 // 128-bit vectors are a special case; they are returned in
717 // registers and we need to make sure to pick a type the LLVM
718 // backend will like.
719 if (Size == 128)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000720 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattner458b2aa2010-07-29 02:16:43 +0000721 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000722
723 // Always return in register if it fits in a general purpose
724 // register, or if it is 64 bits and has a single element.
725 if ((Size == 8 || Size == 16 || Size == 32) ||
726 (Size == 64 && VT->getNumElements() == 1))
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000727 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +0000728 Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000729
Reid Kleckner661f35b2014-01-18 01:12:41 +0000730 return getIndirectReturnResult(State);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000731 }
732
733 return ABIArgInfo::getDirect();
Chris Lattner458b2aa2010-07-29 02:16:43 +0000734 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000735
John McCalla1dee5302010-08-22 10:59:02 +0000736 if (isAggregateTypeForABI(RetTy)) {
Anders Carlsson40446e82010-01-27 03:25:19 +0000737 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Mark Lacey3825e832013-10-06 01:33:34 +0000738 if (isRecordReturnIndirect(RT, getCXXABI()))
Reid Kleckner661f35b2014-01-18 01:12:41 +0000739 return getIndirectReturnResult(State);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000740
Anders Carlsson5789c492009-10-20 22:07:59 +0000741 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000742 if (RT->getDecl()->hasFlexibleArrayMember())
Reid Kleckner661f35b2014-01-18 01:12:41 +0000743 return getIndirectReturnResult(State);
Anders Carlsson5789c492009-10-20 22:07:59 +0000744 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000745
David Chisnallde3a0692009-08-17 23:08:21 +0000746 // If specified, structs and unions are always indirect.
747 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Reid Kleckner661f35b2014-01-18 01:12:41 +0000748 return getIndirectReturnResult(State);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000749
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000750 // Small structures which are register sized are generally returned
751 // in a register.
Reid Kleckner4982b822014-01-31 22:54:50 +0000752 if (shouldReturnTypeInRegister(RetTy, getContext(), IsInstanceMethod)) {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000753 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanee945342011-11-18 01:25:50 +0000754
755 // As a special-case, if the struct is a "single-element" struct, and
756 // the field is of type "float" or "double", return it in a
Eli Friedmana98d1f82012-01-25 22:46:34 +0000757 // floating-point register. (MSVC does not apply this special case.)
758 // We apply a similar transformation for pointer types to improve the
759 // quality of the generated IR.
Eli Friedmanee945342011-11-18 01:25:50 +0000760 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +0000761 if ((!IsWin32StructABI && SeltTy->isRealFloatingType())
Eli Friedmana98d1f82012-01-25 22:46:34 +0000762 || SeltTy->hasPointerRepresentation())
Eli Friedmanee945342011-11-18 01:25:50 +0000763 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
764
765 // FIXME: We should be able to narrow this integer in cases with dead
766 // padding.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000767 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000768 }
769
Reid Kleckner661f35b2014-01-18 01:12:41 +0000770 return getIndirectReturnResult(State);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000771 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000772
Chris Lattner458b2aa2010-07-29 02:16:43 +0000773 // Treat an enum type as its underlying type.
774 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
775 RetTy = EnumTy->getDecl()->getIntegerType();
776
777 return (RetTy->isPromotableIntegerType() ?
778 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000779}
780
Eli Friedman7919bea2012-06-05 19:40:46 +0000781static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
782 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
783}
784
Daniel Dunbared23de32010-09-16 20:42:00 +0000785static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
786 const RecordType *RT = Ty->getAs<RecordType>();
787 if (!RT)
788 return 0;
789 const RecordDecl *RD = RT->getDecl();
790
791 // If this is a C++ record, check the bases first.
792 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
793 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
794 e = CXXRD->bases_end(); i != e; ++i)
795 if (!isRecordWithSSEVectorType(Context, i->getType()))
796 return false;
797
798 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
799 i != e; ++i) {
800 QualType FT = i->getType();
801
Eli Friedman7919bea2012-06-05 19:40:46 +0000802 if (isSSEVectorType(Context, FT))
Daniel Dunbared23de32010-09-16 20:42:00 +0000803 return true;
804
805 if (isRecordWithSSEVectorType(Context, FT))
806 return true;
807 }
808
809 return false;
810}
811
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000812unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
813 unsigned Align) const {
814 // Otherwise, if the alignment is less than or equal to the minimum ABI
815 // alignment, just use the default; the backend will handle this.
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000816 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000817 return 0; // Use default alignment.
818
819 // On non-Darwin, the stack type alignment is always 4.
820 if (!IsDarwinVectorABI) {
821 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000822 return MinABIStackAlignInBytes;
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000823 }
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000824
Daniel Dunbared23de32010-09-16 20:42:00 +0000825 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedman7919bea2012-06-05 19:40:46 +0000826 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
827 isRecordWithSSEVectorType(getContext(), Ty)))
Daniel Dunbared23de32010-09-16 20:42:00 +0000828 return 16;
829
830 return MinABIStackAlignInBytes;
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000831}
832
Rafael Espindola703c47f2012-10-19 05:04:37 +0000833ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
Reid Kleckner661f35b2014-01-18 01:12:41 +0000834 CCState &State) const {
Rafael Espindola703c47f2012-10-19 05:04:37 +0000835 if (!ByVal) {
Reid Kleckner661f35b2014-01-18 01:12:41 +0000836 if (State.FreeRegs) {
837 --State.FreeRegs; // Non-byval indirects just use one pointer.
Rafael Espindola703c47f2012-10-19 05:04:37 +0000838 return ABIArgInfo::getIndirectInReg(0, false);
839 }
Daniel Dunbar53fac692010-04-21 19:49:55 +0000840 return ABIArgInfo::getIndirect(0, false);
Rafael Espindola703c47f2012-10-19 05:04:37 +0000841 }
Daniel Dunbar53fac692010-04-21 19:49:55 +0000842
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000843 // Compute the byval alignment.
844 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
845 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
846 if (StackAlign == 0)
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000847 return ABIArgInfo::getIndirect(4, /*ByVal=*/true);
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000848
849 // If the stack alignment is less than the type alignment, realign the
850 // argument.
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000851 bool Realign = TypeAlign > StackAlign;
852 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true, Realign);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000853}
854
Rafael Espindola06b2b4a2012-07-31 02:44:24 +0000855X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
856 const Type *T = isSingleElementStruct(Ty, getContext());
857 if (!T)
858 T = Ty.getTypePtr();
859
860 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
861 BuiltinType::Kind K = BT->getKind();
862 if (K == BuiltinType::Float || K == BuiltinType::Double)
863 return Float;
864 }
865 return Integer;
866}
867
Reid Kleckner661f35b2014-01-18 01:12:41 +0000868bool X86_32ABIInfo::shouldUseInReg(QualType Ty, CCState &State,
869 bool &NeedsPadding) const {
Rafael Espindolafad28de2012-10-24 01:59:00 +0000870 NeedsPadding = false;
Rafael Espindola06b2b4a2012-07-31 02:44:24 +0000871 Class C = classify(Ty);
872 if (C == Float)
Rafael Espindola703c47f2012-10-19 05:04:37 +0000873 return false;
Rafael Espindola06b2b4a2012-07-31 02:44:24 +0000874
Rafael Espindola077dd592012-10-24 01:58:58 +0000875 unsigned Size = getContext().getTypeSize(Ty);
876 unsigned SizeInRegs = (Size + 31) / 32;
Rafael Espindolae2a9e902012-10-23 02:04:01 +0000877
878 if (SizeInRegs == 0)
879 return false;
880
Reid Kleckner661f35b2014-01-18 01:12:41 +0000881 if (SizeInRegs > State.FreeRegs) {
882 State.FreeRegs = 0;
Rafael Espindola703c47f2012-10-19 05:04:37 +0000883 return false;
Rafael Espindola06b2b4a2012-07-31 02:44:24 +0000884 }
Rafael Espindola703c47f2012-10-19 05:04:37 +0000885
Reid Kleckner661f35b2014-01-18 01:12:41 +0000886 State.FreeRegs -= SizeInRegs;
Rafael Espindola077dd592012-10-24 01:58:58 +0000887
Reid Kleckner661f35b2014-01-18 01:12:41 +0000888 if (State.CC == llvm::CallingConv::X86_FastCall) {
Rafael Espindola077dd592012-10-24 01:58:58 +0000889 if (Size > 32)
890 return false;
891
892 if (Ty->isIntegralOrEnumerationType())
893 return true;
894
895 if (Ty->isPointerType())
896 return true;
897
898 if (Ty->isReferenceType())
899 return true;
900
Reid Kleckner661f35b2014-01-18 01:12:41 +0000901 if (State.FreeRegs)
Rafael Espindolafad28de2012-10-24 01:59:00 +0000902 NeedsPadding = true;
903
Rafael Espindola077dd592012-10-24 01:58:58 +0000904 return false;
905 }
906
Rafael Espindola703c47f2012-10-19 05:04:37 +0000907 return true;
Rafael Espindola06b2b4a2012-07-31 02:44:24 +0000908}
909
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000910ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
911 CCState &State) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000912 // FIXME: Set alignment on indirect arguments.
John McCalla1dee5302010-08-22 10:59:02 +0000913 if (isAggregateTypeForABI(Ty)) {
Anders Carlsson40446e82010-01-27 03:25:19 +0000914 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000915 // Check with the C++ ABI first.
916 CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
917 if (RAA == CGCXXABI::RAA_Indirect) {
918 return getIndirectResult(Ty, false, State);
919 } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
920 // The field index doesn't matter, we'll fix it up later.
921 return ABIArgInfo::getInAlloca(/*FieldIndex=*/0);
922 }
923
924 // Structs are always byval on win32, regardless of what they contain.
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +0000925 if (IsWin32StructABI)
Reid Kleckner661f35b2014-01-18 01:12:41 +0000926 return getIndirectResult(Ty, true, State);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000927
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +0000928 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000929 if (RT->getDecl()->hasFlexibleArrayMember())
Reid Kleckner661f35b2014-01-18 01:12:41 +0000930 return getIndirectResult(Ty, true, State);
Anders Carlsson40446e82010-01-27 03:25:19 +0000931 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000932
Eli Friedman9f061a32011-11-18 00:28:11 +0000933 // Ignore empty structs/unions.
Eli Friedmanf22fa9e2011-11-18 04:01:36 +0000934 if (isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000935 return ABIArgInfo::getIgnore();
936
Rafael Espindolafad28de2012-10-24 01:59:00 +0000937 llvm::LLVMContext &LLVMContext = getVMContext();
938 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
939 bool NeedsPadding;
Reid Kleckner661f35b2014-01-18 01:12:41 +0000940 if (shouldUseInReg(Ty, State, NeedsPadding)) {
Rafael Espindola703c47f2012-10-19 05:04:37 +0000941 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Craig Topperac9201a2013-07-08 04:47:18 +0000942 SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32);
Rafael Espindola703c47f2012-10-19 05:04:37 +0000943 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
944 return ABIArgInfo::getDirectInReg(Result);
945 }
Rafael Espindolafad28de2012-10-24 01:59:00 +0000946 llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : 0;
Rafael Espindola703c47f2012-10-19 05:04:37 +0000947
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000948 // Expand small (<= 128-bit) record types when we know that the stack layout
949 // of those arguments will match the struct. This is important because the
950 // LLVM backend isn't smart enough to remove byval, which inhibits many
951 // optimizations.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000952 if (getContext().getTypeSize(Ty) <= 4*32 &&
953 canExpandIndirectArgument(Ty, getContext()))
Reid Kleckner661f35b2014-01-18 01:12:41 +0000954 return ABIArgInfo::getExpandWithPadding(
955 State.CC == llvm::CallingConv::X86_FastCall, PaddingType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000956
Reid Kleckner661f35b2014-01-18 01:12:41 +0000957 return getIndirectResult(Ty, true, State);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000958 }
959
Chris Lattnerd774ae92010-08-26 20:05:13 +0000960 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerd7e54802010-08-26 20:08:43 +0000961 // On Darwin, some vectors are passed in memory, we handle this by passing
962 // it as an i8/i16/i32/i64.
Chris Lattnerd774ae92010-08-26 20:05:13 +0000963 if (IsDarwinVectorABI) {
964 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerd774ae92010-08-26 20:05:13 +0000965 if ((Size == 8 || Size == 16 || Size == 32) ||
966 (Size == 64 && VT->getNumElements() == 1))
967 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
968 Size));
Chris Lattnerd774ae92010-08-26 20:05:13 +0000969 }
Bill Wendling5cd41c42010-10-18 03:41:31 +0000970
Chad Rosier651c1832013-03-25 21:00:27 +0000971 if (IsX86_MMXType(CGT.ConvertType(Ty)))
972 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64));
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000973
Chris Lattnerd774ae92010-08-26 20:05:13 +0000974 return ABIArgInfo::getDirect();
975 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000976
977
Chris Lattner458b2aa2010-07-29 02:16:43 +0000978 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
979 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000980
Rafael Espindolafad28de2012-10-24 01:59:00 +0000981 bool NeedsPadding;
Reid Kleckner661f35b2014-01-18 01:12:41 +0000982 bool InReg = shouldUseInReg(Ty, State, NeedsPadding);
Rafael Espindola703c47f2012-10-19 05:04:37 +0000983
984 if (Ty->isPromotableIntegerType()) {
985 if (InReg)
986 return ABIArgInfo::getExtendInReg();
987 return ABIArgInfo::getExtend();
988 }
989 if (InReg)
990 return ABIArgInfo::getDirectInReg();
991 return ABIArgInfo::getDirect();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000992}
993
Rafael Espindolaa6472962012-07-24 00:01:07 +0000994void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Reid Kleckner661f35b2014-01-18 01:12:41 +0000995 CCState State(FI.getCallingConvention());
996 if (State.CC == llvm::CallingConv::X86_FastCall)
997 State.FreeRegs = 2;
Rafael Espindola077dd592012-10-24 01:58:58 +0000998 else if (FI.getHasRegParm())
Reid Kleckner661f35b2014-01-18 01:12:41 +0000999 State.FreeRegs = FI.getRegParm();
Rafael Espindola077dd592012-10-24 01:58:58 +00001000 else
Reid Kleckner661f35b2014-01-18 01:12:41 +00001001 State.FreeRegs = DefaultNumRegisterParameters;
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001002
Reid Kleckner4982b822014-01-31 22:54:50 +00001003 FI.getReturnInfo() =
1004 classifyReturnType(FI.getReturnType(), State, FI.isInstanceMethod());
1005
1006 // On win32, use the x86_cdeclmethodcc convention for cdecl methods that use
1007 // sret. This convention swaps the order of the first two parameters behind
1008 // the scenes to match MSVC.
1009 if (IsWin32StructABI && FI.isInstanceMethod() &&
1010 FI.getCallingConvention() == llvm::CallingConv::C &&
1011 FI.getReturnInfo().isIndirect())
1012 FI.setEffectiveCallingConvention(llvm::CallingConv::X86_CDeclMethod);
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001013
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001014 bool UsedInAlloca = false;
Rafael Espindolaa6472962012-07-24 00:01:07 +00001015 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001016 it != ie; ++it) {
Reid Kleckner661f35b2014-01-18 01:12:41 +00001017 it->info = classifyArgumentType(it->type, State);
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001018 UsedInAlloca |= (it->info.getKind() == ABIArgInfo::InAlloca);
1019 }
1020
1021 // If we needed to use inalloca for any argument, do a second pass and rewrite
1022 // all the memory arguments to use inalloca.
1023 if (UsedInAlloca)
1024 rewriteWithInAlloca(FI);
1025}
1026
1027void
1028X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
1029 unsigned &StackOffset,
1030 ABIArgInfo &Info, QualType Type) const {
1031 // Insert padding bytes to respect alignment. For x86_32, each argument is 4
1032 // byte aligned.
1033 unsigned Align = 4U;
1034 if (Info.getKind() == ABIArgInfo::Indirect && Info.getIndirectByVal())
1035 Align = std::max(Align, Info.getIndirectAlign());
1036 if (StackOffset & (Align - 1)) {
1037 unsigned OldOffset = StackOffset;
1038 StackOffset = llvm::RoundUpToAlignment(StackOffset, Align);
1039 unsigned NumBytes = StackOffset - OldOffset;
1040 assert(NumBytes);
1041 llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext());
1042 Ty = llvm::ArrayType::get(Ty, NumBytes);
1043 FrameFields.push_back(Ty);
1044 }
1045
1046 Info = ABIArgInfo::getInAlloca(FrameFields.size());
1047 FrameFields.push_back(CGT.ConvertTypeForMem(Type));
1048 StackOffset += getContext().getTypeSizeInChars(Type).getQuantity();
1049}
1050
1051void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const {
1052 assert(IsWin32StructABI && "inalloca only supported on win32");
1053
1054 // Build a packed struct type for all of the arguments in memory.
1055 SmallVector<llvm::Type *, 6> FrameFields;
1056
1057 unsigned StackOffset = 0;
1058
1059 // Put the sret parameter into the inalloca struct if it's in memory.
1060 ABIArgInfo &Ret = FI.getReturnInfo();
1061 if (Ret.isIndirect() && !Ret.getInReg()) {
1062 CanQualType PtrTy = getContext().getPointerType(FI.getReturnType());
1063 addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy);
1064 }
1065
1066 // Skip the 'this' parameter in ecx.
1067 CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end();
1068 if (FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall)
1069 ++I;
1070
1071 // Put arguments passed in memory into the struct.
1072 for (; I != E; ++I) {
1073
1074 // Leave ignored and inreg arguments alone.
1075 switch (I->info.getKind()) {
1076 case ABIArgInfo::Indirect:
1077 assert(I->info.getIndirectByVal());
1078 break;
1079 case ABIArgInfo::Ignore:
1080 continue;
1081 case ABIArgInfo::Direct:
1082 case ABIArgInfo::Extend:
1083 if (I->info.getInReg())
1084 continue;
1085 break;
1086 default:
1087 break;
1088 }
1089
1090 addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
1091 }
1092
1093 FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields,
1094 /*isPacked=*/true));
Rafael Espindolaa6472962012-07-24 00:01:07 +00001095}
1096
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001097llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1098 CodeGenFunction &CGF) const {
Chris Lattnerece04092012-02-07 00:39:47 +00001099 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001100
1101 CGBuilderTy &Builder = CGF.Builder;
1102 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1103 "ap");
1104 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Eli Friedman1d7dd3b2011-11-18 02:12:09 +00001105
1106 // Compute if the address needs to be aligned
1107 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
1108 Align = getTypeStackAlignInBytes(Ty, Align);
1109 Align = std::max(Align, 4U);
1110 if (Align > 4) {
1111 // addr = (addr + align - 1) & -align;
1112 llvm::Value *Offset =
1113 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
1114 Addr = CGF.Builder.CreateGEP(Addr, Offset);
1115 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
1116 CGF.Int32Ty);
1117 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
1118 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1119 Addr->getType(),
1120 "ap.cur.aligned");
1121 }
1122
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001123 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00001124 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001125 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1126
1127 uint64_t Offset =
Eli Friedman1d7dd3b2011-11-18 02:12:09 +00001128 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001129 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001130 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001131 "ap.next");
1132 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1133
1134 return AddrTyped;
1135}
1136
Charles Davis4ea31ab2010-02-13 15:54:06 +00001137void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
1138 llvm::GlobalValue *GV,
1139 CodeGen::CodeGenModule &CGM) const {
1140 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1141 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
1142 // Get the LLVM function.
1143 llvm::Function *Fn = cast<llvm::Function>(GV);
1144
1145 // Now add the 'alignstack' attribute with a value of 16.
Bill Wendlinga514ebc2012-10-15 20:36:26 +00001146 llvm::AttrBuilder B;
Bill Wendlingccf94c92012-10-14 03:28:14 +00001147 B.addStackAlignmentAttr(16);
Bill Wendling9a677922013-01-23 00:21:06 +00001148 Fn->addAttributes(llvm::AttributeSet::FunctionIndex,
1149 llvm::AttributeSet::get(CGM.getLLVMContext(),
1150 llvm::AttributeSet::FunctionIndex,
1151 B));
Charles Davis4ea31ab2010-02-13 15:54:06 +00001152 }
1153 }
1154}
1155
John McCallbeec5a02010-03-06 00:35:14 +00001156bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
1157 CodeGen::CodeGenFunction &CGF,
1158 llvm::Value *Address) const {
1159 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallbeec5a02010-03-06 00:35:14 +00001160
Chris Lattnerece04092012-02-07 00:39:47 +00001161 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001162
John McCallbeec5a02010-03-06 00:35:14 +00001163 // 0-7 are the eight integer registers; the order is different
1164 // on Darwin (for EH), but the range is the same.
1165 // 8 is %eip.
John McCall943fae92010-05-27 06:19:26 +00001166 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCallbeec5a02010-03-06 00:35:14 +00001167
John McCallc8e01702013-04-16 22:48:15 +00001168 if (CGF.CGM.getTarget().getTriple().isOSDarwin()) {
John McCallbeec5a02010-03-06 00:35:14 +00001169 // 12-16 are st(0..4). Not sure why we stop at 4.
1170 // These have size 16, which is sizeof(long double) on
1171 // platforms with 8-byte alignment for that type.
Chris Lattnerece04092012-02-07 00:39:47 +00001172 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCall943fae92010-05-27 06:19:26 +00001173 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001174
John McCallbeec5a02010-03-06 00:35:14 +00001175 } else {
1176 // 9 is %eflags, which doesn't get a size on Darwin for some
1177 // reason.
1178 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
1179
1180 // 11-16 are st(0..5). Not sure why we stop at 5.
1181 // These have size 12, which is sizeof(long double) on
1182 // platforms with 4-byte alignment for that type.
Chris Lattnerece04092012-02-07 00:39:47 +00001183 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCall943fae92010-05-27 06:19:26 +00001184 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1185 }
John McCallbeec5a02010-03-06 00:35:14 +00001186
1187 return false;
1188}
1189
Chris Lattner0cf24192010-06-28 20:05:43 +00001190//===----------------------------------------------------------------------===//
1191// X86-64 ABI Implementation
1192//===----------------------------------------------------------------------===//
1193
1194
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001195namespace {
1196/// X86_64ABIInfo - The X86_64 ABI information.
1197class X86_64ABIInfo : public ABIInfo {
1198 enum Class {
1199 Integer = 0,
1200 SSE,
1201 SSEUp,
1202 X87,
1203 X87Up,
1204 ComplexX87,
1205 NoClass,
1206 Memory
1207 };
1208
1209 /// merge - Implement the X86_64 ABI merging algorithm.
1210 ///
1211 /// Merge an accumulating classification \arg Accum with a field
1212 /// classification \arg Field.
1213 ///
1214 /// \param Accum - The accumulating classification. This should
1215 /// always be either NoClass or the result of a previous merge
1216 /// call. In addition, this should never be Memory (the caller
1217 /// should just return Memory for the aggregate).
Chris Lattnerd776fb12010-06-28 21:43:59 +00001218 static Class merge(Class Accum, Class Field);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001219
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001220 /// postMerge - Implement the X86_64 ABI post merging algorithm.
1221 ///
1222 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
1223 /// final MEMORY or SSE classes when necessary.
1224 ///
1225 /// \param AggregateSize - The size of the current aggregate in
1226 /// the classification process.
1227 ///
1228 /// \param Lo - The classification for the parts of the type
1229 /// residing in the low word of the containing object.
1230 ///
1231 /// \param Hi - The classification for the parts of the type
1232 /// residing in the higher words of the containing object.
1233 ///
1234 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
1235
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001236 /// classify - Determine the x86_64 register classes in which the
1237 /// given type T should be passed.
1238 ///
1239 /// \param Lo - The classification for the parts of the type
1240 /// residing in the low word of the containing object.
1241 ///
1242 /// \param Hi - The classification for the parts of the type
1243 /// residing in the high word of the containing object.
1244 ///
1245 /// \param OffsetBase - The bit offset of this type in the
1246 /// containing object. Some parameters are classified different
1247 /// depending on whether they straddle an eightbyte boundary.
1248 ///
Eli Friedman96fd2642013-06-12 00:13:45 +00001249 /// \param isNamedArg - Whether the argument in question is a "named"
1250 /// argument, as used in AMD64-ABI 3.5.7.
1251 ///
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001252 /// If a word is unused its result will be NoClass; if a type should
1253 /// be passed in Memory then at least the classification of \arg Lo
1254 /// will be Memory.
1255 ///
Sylvestre Ledru33b5baf2012-09-27 10:16:10 +00001256 /// The \arg Lo class will be NoClass iff the argument is ignored.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001257 ///
1258 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
1259 /// also be ComplexX87.
Eli Friedman96fd2642013-06-12 00:13:45 +00001260 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi,
1261 bool isNamedArg) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001262
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001263 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattnera5f58b02011-07-09 17:41:47 +00001264 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
1265 unsigned IROffset, QualType SourceTy,
1266 unsigned SourceOffset) const;
1267 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
1268 unsigned IROffset, QualType SourceTy,
1269 unsigned SourceOffset) const;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001270
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001271 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar53fac692010-04-21 19:49:55 +00001272 /// such that the argument will be returned in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +00001273 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar53fac692010-04-21 19:49:55 +00001274
1275 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001276 /// such that the argument will be passed in memory.
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00001277 ///
1278 /// \param freeIntRegs - The number of free integer registers remaining
1279 /// available.
1280 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001281
Chris Lattner458b2aa2010-07-29 02:16:43 +00001282 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001283
Bill Wendling5cd41c42010-10-18 03:41:31 +00001284 ABIArgInfo classifyArgumentType(QualType Ty,
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00001285 unsigned freeIntRegs,
Bill Wendling5cd41c42010-10-18 03:41:31 +00001286 unsigned &neededInt,
Eli Friedman96fd2642013-06-12 00:13:45 +00001287 unsigned &neededSSE,
1288 bool isNamedArg) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001289
Eli Friedmanbfd5add2011-12-02 00:11:43 +00001290 bool IsIllegalVectorType(QualType Ty) const;
1291
John McCalle0fda732011-04-21 01:20:55 +00001292 /// The 0.98 ABI revision clarified a lot of ambiguities,
1293 /// unfortunately in ways that were not always consistent with
1294 /// certain previous compilers. In particular, platforms which
1295 /// required strict binary compatibility with older versions of GCC
1296 /// may need to exempt themselves.
1297 bool honorsRevision0_98() const {
John McCallc8e01702013-04-16 22:48:15 +00001298 return !getTarget().getTriple().isOSDarwin();
John McCalle0fda732011-04-21 01:20:55 +00001299 }
1300
Eli Friedmanbfd5add2011-12-02 00:11:43 +00001301 bool HasAVX;
Derek Schuffc7dd7222012-10-11 15:52:22 +00001302 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
1303 // 64-bit hardware.
1304 bool Has64BitPointers;
Eli Friedmanbfd5add2011-12-02 00:11:43 +00001305
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001306public:
Eli Friedmanbfd5add2011-12-02 00:11:43 +00001307 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
Derek Schuffc7dd7222012-10-11 15:52:22 +00001308 ABIInfo(CGT), HasAVX(hasavx),
Derek Schuff8a872f32012-10-11 18:21:13 +00001309 Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
Derek Schuffc7dd7222012-10-11 15:52:22 +00001310 }
Chris Lattner22a931e2010-06-29 06:01:59 +00001311
John McCalla729c622012-02-17 03:33:10 +00001312 bool isPassedUsingAVXType(QualType type) const {
1313 unsigned neededInt, neededSSE;
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00001314 // The freeIntRegs argument doesn't matter here.
Eli Friedman96fd2642013-06-12 00:13:45 +00001315 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE,
1316 /*isNamedArg*/true);
John McCalla729c622012-02-17 03:33:10 +00001317 if (info.isDirect()) {
1318 llvm::Type *ty = info.getCoerceToType();
1319 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
1320 return (vectorTy->getBitWidth() > 128);
1321 }
1322 return false;
1323 }
1324
Chris Lattner22326a12010-07-29 02:31:05 +00001325 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001326
1327 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1328 CodeGenFunction &CGF) const;
1329};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001330
Chris Lattner04dc9572010-08-31 16:44:54 +00001331/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00001332class WinX86_64ABIInfo : public ABIInfo {
1333
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00001334 ABIArgInfo classify(QualType Ty, bool IsReturnType) const;
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00001335
Chris Lattner04dc9572010-08-31 16:44:54 +00001336public:
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00001337 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
1338
1339 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattner04dc9572010-08-31 16:44:54 +00001340
1341 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1342 CodeGenFunction &CGF) const;
1343};
1344
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001345class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1346public:
Eli Friedmanbfd5add2011-12-02 00:11:43 +00001347 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
Derek Schuffc7dd7222012-10-11 15:52:22 +00001348 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
John McCallbeec5a02010-03-06 00:35:14 +00001349
John McCalla729c622012-02-17 03:33:10 +00001350 const X86_64ABIInfo &getABIInfo() const {
1351 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
1352 }
1353
John McCallbeec5a02010-03-06 00:35:14 +00001354 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1355 return 7;
1356 }
1357
1358 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1359 llvm::Value *Address) const {
Chris Lattnerece04092012-02-07 00:39:47 +00001360 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001361
John McCall943fae92010-05-27 06:19:26 +00001362 // 0-15 are the 16 integer registers.
1363 // 16 is %rip.
Chris Lattnerece04092012-02-07 00:39:47 +00001364 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCallbeec5a02010-03-06 00:35:14 +00001365 return false;
1366 }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +00001367
Jay Foad7c57be32011-07-11 09:56:20 +00001368 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001369 StringRef Constraint,
Jay Foad7c57be32011-07-11 09:56:20 +00001370 llvm::Type* Ty) const {
Peter Collingbourne8f5cf742011-02-19 23:03:58 +00001371 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1372 }
1373
John McCalla729c622012-02-17 03:33:10 +00001374 bool isNoProtoCallVariadic(const CallArgList &args,
1375 const FunctionNoProtoType *fnType) const {
John McCallcbc038a2011-09-21 08:08:30 +00001376 // The default CC on x86-64 sets %al to the number of SSA
1377 // registers used, and GCC sets this when calling an unprototyped
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00001378 // function, so we override the default behavior. However, don't do
Eli Friedmanb8e45b22011-12-06 03:08:26 +00001379 // that when AVX types are involved: the ABI explicitly states it is
1380 // undefined, and it doesn't work in practice because of how the ABI
1381 // defines varargs anyway.
Reid Kleckner78af0702013-08-27 23:08:25 +00001382 if (fnType->getCallConv() == CC_C) {
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00001383 bool HasAVXType = false;
John McCalla729c622012-02-17 03:33:10 +00001384 for (CallArgList::const_iterator
1385 it = args.begin(), ie = args.end(); it != ie; ++it) {
1386 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1387 HasAVXType = true;
1388 break;
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00001389 }
1390 }
John McCalla729c622012-02-17 03:33:10 +00001391
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00001392 if (!HasAVXType)
1393 return true;
1394 }
John McCallcbc038a2011-09-21 08:08:30 +00001395
John McCalla729c622012-02-17 03:33:10 +00001396 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
John McCallcbc038a2011-09-21 08:08:30 +00001397 }
1398
Peter Collingbourneb453cd62013-10-20 21:29:19 +00001399 llvm::Constant *getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const {
1400 unsigned Sig = (0xeb << 0) | // jmp rel8
1401 (0x0a << 8) | // .+0x0c
1402 ('F' << 16) |
1403 ('T' << 24);
1404 return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
1405 }
1406
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001407};
1408
Aaron Ballmanef50ee92013-05-24 15:06:56 +00001409static std::string qualifyWindowsLibrary(llvm::StringRef Lib) {
1410 // If the argument does not end in .lib, automatically add the suffix. This
1411 // matches the behavior of MSVC.
1412 std::string ArgStr = Lib;
Rui Ueyama727025a2013-10-31 19:12:53 +00001413 if (!Lib.endswith_lower(".lib"))
Aaron Ballmanef50ee92013-05-24 15:06:56 +00001414 ArgStr += ".lib";
Aaron Ballmanef50ee92013-05-24 15:06:56 +00001415 return ArgStr;
1416}
1417
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001418class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo {
1419public:
John McCall1fe2a8c2013-06-18 02:46:29 +00001420 WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
1421 bool d, bool p, bool w, unsigned RegParms)
1422 : X86_32TargetCodeGenInfo(CGT, d, p, w, RegParms) {}
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001423
1424 void getDependentLibraryOption(llvm::StringRef Lib,
1425 llvm::SmallString<24> &Opt) const {
1426 Opt = "/DEFAULTLIB:";
Aaron Ballmanef50ee92013-05-24 15:06:56 +00001427 Opt += qualifyWindowsLibrary(Lib);
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001428 }
Aaron Ballman5d041be2013-06-04 02:07:14 +00001429
1430 void getDetectMismatchOption(llvm::StringRef Name,
1431 llvm::StringRef Value,
1432 llvm::SmallString<32> &Opt) const {
Eli Friedmanf60b8ce2013-06-07 22:42:22 +00001433 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
Aaron Ballman5d041be2013-06-04 02:07:14 +00001434 }
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001435};
1436
Chris Lattner04dc9572010-08-31 16:44:54 +00001437class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1438public:
1439 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1440 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1441
1442 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1443 return 7;
1444 }
1445
1446 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1447 llvm::Value *Address) const {
Chris Lattnerece04092012-02-07 00:39:47 +00001448 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001449
Chris Lattner04dc9572010-08-31 16:44:54 +00001450 // 0-15 are the 16 integer registers.
1451 // 16 is %rip.
Chris Lattnerece04092012-02-07 00:39:47 +00001452 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattner04dc9572010-08-31 16:44:54 +00001453 return false;
1454 }
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001455
1456 void getDependentLibraryOption(llvm::StringRef Lib,
1457 llvm::SmallString<24> &Opt) const {
1458 Opt = "/DEFAULTLIB:";
Aaron Ballmanef50ee92013-05-24 15:06:56 +00001459 Opt += qualifyWindowsLibrary(Lib);
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001460 }
Aaron Ballman5d041be2013-06-04 02:07:14 +00001461
1462 void getDetectMismatchOption(llvm::StringRef Name,
1463 llvm::StringRef Value,
1464 llvm::SmallString<32> &Opt) const {
Eli Friedmanf60b8ce2013-06-07 22:42:22 +00001465 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
Aaron Ballman5d041be2013-06-04 02:07:14 +00001466 }
Chris Lattner04dc9572010-08-31 16:44:54 +00001467};
1468
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001469}
1470
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001471void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1472 Class &Hi) const {
1473 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1474 //
1475 // (a) If one of the classes is Memory, the whole argument is passed in
1476 // memory.
1477 //
1478 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1479 // memory.
1480 //
1481 // (c) If the size of the aggregate exceeds two eightbytes and the first
1482 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1483 // argument is passed in memory. NOTE: This is necessary to keep the
1484 // ABI working for processors that don't support the __m256 type.
1485 //
1486 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1487 //
1488 // Some of these are enforced by the merging logic. Others can arise
1489 // only with unions; for example:
1490 // union { _Complex double; unsigned; }
1491 //
1492 // Note that clauses (b) and (c) were added in 0.98.
1493 //
1494 if (Hi == Memory)
1495 Lo = Memory;
1496 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1497 Lo = Memory;
1498 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1499 Lo = Memory;
1500 if (Hi == SSEUp && Lo != SSE)
1501 Hi = SSE;
1502}
1503
Chris Lattnerd776fb12010-06-28 21:43:59 +00001504X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001505 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1506 // classified recursively so that always two fields are
1507 // considered. The resulting class is calculated according to
1508 // the classes of the fields in the eightbyte:
1509 //
1510 // (a) If both classes are equal, this is the resulting class.
1511 //
1512 // (b) If one of the classes is NO_CLASS, the resulting class is
1513 // the other class.
1514 //
1515 // (c) If one of the classes is MEMORY, the result is the MEMORY
1516 // class.
1517 //
1518 // (d) If one of the classes is INTEGER, the result is the
1519 // INTEGER.
1520 //
1521 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1522 // MEMORY is used as class.
1523 //
1524 // (f) Otherwise class SSE is used.
1525
1526 // Accum should never be memory (we should have returned) or
1527 // ComplexX87 (because this cannot be passed in a structure).
1528 assert((Accum != Memory && Accum != ComplexX87) &&
1529 "Invalid accumulated classification during merge.");
1530 if (Accum == Field || Field == NoClass)
1531 return Accum;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001532 if (Field == Memory)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001533 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001534 if (Accum == NoClass)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001535 return Field;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001536 if (Accum == Integer || Field == Integer)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001537 return Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001538 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1539 Accum == X87 || Accum == X87Up)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001540 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001541 return SSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001542}
1543
Chris Lattner5c740f12010-06-30 19:14:05 +00001544void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Eli Friedman96fd2642013-06-12 00:13:45 +00001545 Class &Lo, Class &Hi, bool isNamedArg) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001546 // FIXME: This code can be simplified by introducing a simple value class for
1547 // Class pairs with appropriate constructor methods for the various
1548 // situations.
1549
1550 // FIXME: Some of the split computations are wrong; unaligned vectors
1551 // shouldn't be passed in registers for example, so there is no chance they
1552 // can straddle an eightbyte. Verify & simplify.
1553
1554 Lo = Hi = NoClass;
1555
1556 Class &Current = OffsetBase < 64 ? Lo : Hi;
1557 Current = Memory;
1558
John McCall9dd450b2009-09-21 23:43:11 +00001559 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001560 BuiltinType::Kind k = BT->getKind();
1561
1562 if (k == BuiltinType::Void) {
1563 Current = NoClass;
1564 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1565 Lo = Integer;
1566 Hi = Integer;
1567 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1568 Current = Integer;
Derek Schuff57b7e8f2012-10-11 16:55:58 +00001569 } else if ((k == BuiltinType::Float || k == BuiltinType::Double) ||
1570 (k == BuiltinType::LongDouble &&
Cameron Esfahani556d91e2013-09-14 01:09:11 +00001571 getTarget().getTriple().isOSNaCl())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001572 Current = SSE;
1573 } else if (k == BuiltinType::LongDouble) {
1574 Lo = X87;
1575 Hi = X87Up;
1576 }
1577 // FIXME: _Decimal32 and _Decimal64 are SSE.
1578 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattnerd776fb12010-06-28 21:43:59 +00001579 return;
1580 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001581
Chris Lattnerd776fb12010-06-28 21:43:59 +00001582 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001583 // Classify the underlying integer type.
Eli Friedman96fd2642013-06-12 00:13:45 +00001584 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg);
Chris Lattnerd776fb12010-06-28 21:43:59 +00001585 return;
1586 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001587
Chris Lattnerd776fb12010-06-28 21:43:59 +00001588 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001589 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001590 return;
1591 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001592
Chris Lattnerd776fb12010-06-28 21:43:59 +00001593 if (Ty->isMemberPointerType()) {
Derek Schuffc7dd7222012-10-11 15:52:22 +00001594 if (Ty->isMemberFunctionPointerType() && Has64BitPointers)
Daniel Dunbar36d4d152010-05-15 00:00:37 +00001595 Lo = Hi = Integer;
1596 else
1597 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001598 return;
1599 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001600
Chris Lattnerd776fb12010-06-28 21:43:59 +00001601 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001602 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001603 if (Size == 32) {
1604 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1605 // float> as integer.
1606 Current = Integer;
1607
1608 // If this type crosses an eightbyte boundary, it should be
1609 // split.
1610 uint64_t EB_Real = (OffsetBase) / 64;
1611 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1612 if (EB_Real != EB_Imag)
1613 Hi = Lo;
1614 } else if (Size == 64) {
1615 // gcc passes <1 x double> in memory. :(
1616 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1617 return;
1618
1619 // gcc passes <1 x long long> as INTEGER.
Chris Lattner46830f22010-08-26 18:03:20 +00001620 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner69e683f2010-08-26 18:13:50 +00001621 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1622 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1623 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001624 Current = Integer;
1625 else
1626 Current = SSE;
1627
1628 // If this type crosses an eightbyte boundary, it should be
1629 // split.
1630 if (OffsetBase && OffsetBase != 64)
1631 Hi = Lo;
Eli Friedman96fd2642013-06-12 00:13:45 +00001632 } else if (Size == 128 || (HasAVX && isNamedArg && Size == 256)) {
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001633 // Arguments of 256-bits are split into four eightbyte chunks. The
1634 // least significant one belongs to class SSE and all the others to class
1635 // SSEUP. The original Lo and Hi design considers that types can't be
1636 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1637 // This design isn't correct for 256-bits, but since there're no cases
1638 // where the upper parts would need to be inspected, avoid adding
1639 // complexity and just consider Hi to match the 64-256 part.
Eli Friedman96fd2642013-06-12 00:13:45 +00001640 //
1641 // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in
1642 // registers if they are "named", i.e. not part of the "..." of a
1643 // variadic function.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001644 Lo = SSE;
1645 Hi = SSEUp;
1646 }
Chris Lattnerd776fb12010-06-28 21:43:59 +00001647 return;
1648 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001649
Chris Lattnerd776fb12010-06-28 21:43:59 +00001650 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001651 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001652
Chris Lattner2b037972010-07-29 02:01:43 +00001653 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregorb90df602010-06-16 00:17:44 +00001654 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001655 if (Size <= 64)
1656 Current = Integer;
1657 else if (Size <= 128)
1658 Lo = Hi = Integer;
Chris Lattner2b037972010-07-29 02:01:43 +00001659 } else if (ET == getContext().FloatTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001660 Current = SSE;
Derek Schuff57b7e8f2012-10-11 16:55:58 +00001661 else if (ET == getContext().DoubleTy ||
1662 (ET == getContext().LongDoubleTy &&
Cameron Esfahani556d91e2013-09-14 01:09:11 +00001663 getTarget().getTriple().isOSNaCl()))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001664 Lo = Hi = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +00001665 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001666 Current = ComplexX87;
1667
1668 // If this complex type crosses an eightbyte boundary then it
1669 // should be split.
1670 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattner2b037972010-07-29 02:01:43 +00001671 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001672 if (Hi == NoClass && EB_Real != EB_Imag)
1673 Hi = Lo;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001674
Chris Lattnerd776fb12010-06-28 21:43:59 +00001675 return;
1676 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001677
Chris Lattner2b037972010-07-29 02:01:43 +00001678 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001679 // Arrays are treated like structures.
1680
Chris Lattner2b037972010-07-29 02:01:43 +00001681 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001682
1683 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001684 // than four eightbytes, ..., it has class MEMORY.
1685 if (Size > 256)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001686 return;
1687
1688 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1689 // fields, it has class MEMORY.
1690 //
1691 // Only need to check alignment of array base.
Chris Lattner2b037972010-07-29 02:01:43 +00001692 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001693 return;
1694
1695 // Otherwise implement simplified merge. We could be smarter about
1696 // this, but it isn't worth it and would be harder to verify.
1697 Current = NoClass;
Chris Lattner2b037972010-07-29 02:01:43 +00001698 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001699 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes75541d02011-07-12 01:27:38 +00001700
1701 // The only case a 256-bit wide vector could be used is when the array
1702 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1703 // to work for sizes wider than 128, early check and fallback to memory.
1704 if (Size > 128 && EltSize != 256)
1705 return;
1706
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001707 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1708 Class FieldLo, FieldHi;
Eli Friedman96fd2642013-06-12 00:13:45 +00001709 classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001710 Lo = merge(Lo, FieldLo);
1711 Hi = merge(Hi, FieldHi);
1712 if (Lo == Memory || Hi == Memory)
1713 break;
1714 }
1715
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001716 postMerge(Size, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001717 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattnerd776fb12010-06-28 21:43:59 +00001718 return;
1719 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001720
Chris Lattnerd776fb12010-06-28 21:43:59 +00001721 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001722 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001723
1724 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001725 // than four eightbytes, ..., it has class MEMORY.
1726 if (Size > 256)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001727 return;
1728
Anders Carlsson20759ad2009-09-16 15:53:40 +00001729 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1730 // copy constructor or a non-trivial destructor, it is passed by invisible
1731 // reference.
Mark Lacey3825e832013-10-06 01:33:34 +00001732 if (getRecordArgABI(RT, getCXXABI()))
Anders Carlsson20759ad2009-09-16 15:53:40 +00001733 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001734
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001735 const RecordDecl *RD = RT->getDecl();
1736
1737 // Assume variable sized types are passed in memory.
1738 if (RD->hasFlexibleArrayMember())
1739 return;
1740
Chris Lattner2b037972010-07-29 02:01:43 +00001741 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001742
1743 // Reset Lo class, this will be recomputed.
1744 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001745
1746 // If this is a C++ record, classify the bases first.
1747 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1748 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1749 e = CXXRD->bases_end(); i != e; ++i) {
1750 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1751 "Unexpected base class!");
1752 const CXXRecordDecl *Base =
1753 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1754
1755 // Classify this field.
1756 //
1757 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1758 // single eightbyte, each is classified separately. Each eightbyte gets
1759 // initialized to class NO_CLASS.
1760 Class FieldLo, FieldHi;
Benjamin Kramer2ef30312012-07-04 18:45:14 +00001761 uint64_t Offset =
1762 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
Eli Friedman96fd2642013-06-12 00:13:45 +00001763 classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001764 Lo = merge(Lo, FieldLo);
1765 Hi = merge(Hi, FieldHi);
1766 if (Lo == Memory || Hi == Memory)
1767 break;
1768 }
1769 }
1770
1771 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001772 unsigned idx = 0;
Bruno Cardoso Lopes0aadf832011-07-12 22:30:58 +00001773 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001774 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001775 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1776 bool BitField = i->isBitField();
1777
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00001778 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1779 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001780 //
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00001781 // The only case a 256-bit wide vector could be used is when the struct
1782 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1783 // to work for sizes wider than 128, early check and fallback to memory.
1784 //
1785 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1786 Lo = Memory;
1787 return;
1788 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001789 // Note, skip this test for bit-fields, see below.
Chris Lattner2b037972010-07-29 02:01:43 +00001790 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001791 Lo = Memory;
1792 return;
1793 }
1794
1795 // Classify this field.
1796 //
1797 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1798 // exceeds a single eightbyte, each is classified
1799 // separately. Each eightbyte gets initialized to class
1800 // NO_CLASS.
1801 Class FieldLo, FieldHi;
1802
1803 // Bit-fields require special handling, they do not force the
1804 // structure to be passed in memory even if unaligned, and
1805 // therefore they can straddle an eightbyte.
1806 if (BitField) {
1807 // Ignore padding bit-fields.
1808 if (i->isUnnamedBitfield())
1809 continue;
1810
1811 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smithcaf33902011-10-10 18:28:20 +00001812 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001813
1814 uint64_t EB_Lo = Offset / 64;
1815 uint64_t EB_Hi = (Offset + Size - 1) / 64;
Sylvestre Ledru0c4813e2013-10-06 09:54:18 +00001816
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001817 if (EB_Lo) {
1818 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1819 FieldLo = NoClass;
1820 FieldHi = Integer;
1821 } else {
1822 FieldLo = Integer;
1823 FieldHi = EB_Hi ? Integer : NoClass;
1824 }
1825 } else
Eli Friedman96fd2642013-06-12 00:13:45 +00001826 classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001827 Lo = merge(Lo, FieldLo);
1828 Hi = merge(Hi, FieldHi);
1829 if (Lo == Memory || Hi == Memory)
1830 break;
1831 }
1832
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001833 postMerge(Size, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001834 }
1835}
1836
Chris Lattner22a931e2010-06-29 06:01:59 +00001837ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001838 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1839 // place naturally.
John McCalla1dee5302010-08-22 10:59:02 +00001840 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001841 // Treat an enum type as its underlying type.
1842 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1843 Ty = EnumTy->getDecl()->getIntegerType();
1844
1845 return (Ty->isPromotableIntegerType() ?
1846 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1847 }
1848
1849 return ABIArgInfo::getIndirect(0);
1850}
1851
Eli Friedmanbfd5add2011-12-02 00:11:43 +00001852bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1853 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1854 uint64_t Size = getContext().getTypeSize(VecTy);
1855 unsigned LargestVector = HasAVX ? 256 : 128;
1856 if (Size <= 64 || Size > LargestVector)
1857 return true;
1858 }
1859
1860 return false;
1861}
1862
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00001863ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1864 unsigned freeIntRegs) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001865 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1866 // place naturally.
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00001867 //
1868 // This assumption is optimistic, as there could be free registers available
1869 // when we need to pass this argument in memory, and LLVM could try to pass
1870 // the argument in the free register. This does not seem to happen currently,
1871 // but this code would be much safer if we could mark the argument with
1872 // 'onstack'. See PR12193.
Eli Friedmanbfd5add2011-12-02 00:11:43 +00001873 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00001874 // Treat an enum type as its underlying type.
1875 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1876 Ty = EnumTy->getDecl()->getIntegerType();
1877
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001878 return (Ty->isPromotableIntegerType() ?
1879 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001880 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001881
Mark Lacey3825e832013-10-06 01:33:34 +00001882 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00001883 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Anders Carlsson20759ad2009-09-16 15:53:40 +00001884
Chris Lattner44c2b902011-05-22 23:21:23 +00001885 // Compute the byval alignment. We specify the alignment of the byval in all
1886 // cases so that the mid-level optimizer knows the alignment of the byval.
1887 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00001888
1889 // Attempt to avoid passing indirect results using byval when possible. This
1890 // is important for good codegen.
1891 //
1892 // We do this by coercing the value into a scalar type which the backend can
1893 // handle naturally (i.e., without using byval).
1894 //
1895 // For simplicity, we currently only do this when we have exhausted all of the
1896 // free integer registers. Doing this when there are free integer registers
1897 // would require more care, as we would have to ensure that the coerced value
1898 // did not claim the unused register. That would require either reording the
1899 // arguments to the function (so that any subsequent inreg values came first),
1900 // or only doing this optimization when there were no following arguments that
1901 // might be inreg.
1902 //
1903 // We currently expect it to be rare (particularly in well written code) for
1904 // arguments to be passed on the stack when there are still free integer
1905 // registers available (this would typically imply large structs being passed
1906 // by value), so this seems like a fair tradeoff for now.
1907 //
1908 // We can revisit this if the backend grows support for 'onstack' parameter
1909 // attributes. See PR12193.
1910 if (freeIntRegs == 0) {
1911 uint64_t Size = getContext().getTypeSize(Ty);
1912
1913 // If this type fits in an eightbyte, coerce it into the matching integral
1914 // type, which will end up on the stack (with alignment 8).
1915 if (Align == 8 && Size <= 64)
1916 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1917 Size));
1918 }
1919
Chris Lattner44c2b902011-05-22 23:21:23 +00001920 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001921}
1922
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001923/// GetByteVectorType - The ABI specifies that a value should be passed in an
1924/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner4200fe42010-07-29 04:56:46 +00001925/// vector register.
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001926llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattnera5f58b02011-07-09 17:41:47 +00001927 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001928
Chris Lattner9fa15c32010-07-29 05:02:29 +00001929 // Wrapper structs that just contain vectors are passed just like vectors,
1930 // strip them off if present.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001931 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner9fa15c32010-07-29 05:02:29 +00001932 while (STy && STy->getNumElements() == 1) {
1933 IRType = STy->getElementType(0);
1934 STy = dyn_cast<llvm::StructType>(IRType);
1935 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001936
Bruno Cardoso Lopes129b4cc2011-07-08 22:57:35 +00001937 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001938 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1939 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001940 unsigned BitWidth = VT->getBitWidth();
Tanya Lattner71f1b2d2011-11-28 23:18:11 +00001941 if ((BitWidth >= 128 && BitWidth <= 256) &&
Chris Lattner4200fe42010-07-29 04:56:46 +00001942 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1943 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1944 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1945 EltTy->isIntegerTy(128)))
1946 return VT;
1947 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001948
Chris Lattner4200fe42010-07-29 04:56:46 +00001949 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1950}
1951
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001952/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1953/// is known to either be off the end of the specified type or being in
1954/// alignment padding. The user type specified is known to be at most 128 bits
1955/// in size, and have passed through X86_64ABIInfo::classify with a successful
1956/// classification that put one of the two halves in the INTEGER class.
1957///
1958/// It is conservatively correct to return false.
1959static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1960 unsigned EndBit, ASTContext &Context) {
1961 // If the bytes being queried are off the end of the type, there is no user
1962 // data hiding here. This handles analysis of builtins, vectors and other
1963 // types that don't contain interesting padding.
1964 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1965 if (TySize <= StartBit)
1966 return true;
1967
Chris Lattner98076a22010-07-29 07:43:55 +00001968 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1969 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1970 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1971
1972 // Check each element to see if the element overlaps with the queried range.
1973 for (unsigned i = 0; i != NumElts; ++i) {
1974 // If the element is after the span we care about, then we're done..
1975 unsigned EltOffset = i*EltSize;
1976 if (EltOffset >= EndBit) break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001977
Chris Lattner98076a22010-07-29 07:43:55 +00001978 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1979 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1980 EndBit-EltOffset, Context))
1981 return false;
1982 }
1983 // If it overlaps no elements, then it is safe to process as padding.
1984 return true;
1985 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001986
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001987 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1988 const RecordDecl *RD = RT->getDecl();
1989 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001990
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001991 // If this is a C++ record, check the bases first.
1992 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1993 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1994 e = CXXRD->bases_end(); i != e; ++i) {
1995 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1996 "Unexpected base class!");
1997 const CXXRecordDecl *Base =
1998 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001999
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002000 // If the base is after the span we care about, ignore it.
Benjamin Kramer2ef30312012-07-04 18:45:14 +00002001 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002002 if (BaseOffset >= EndBit) continue;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002003
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002004 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
2005 if (!BitsContainNoUserData(i->getType(), BaseStart,
2006 EndBit-BaseOffset, Context))
2007 return false;
2008 }
2009 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002010
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002011 // Verify that no field has data that overlaps the region of interest. Yes
2012 // this could be sped up a lot by being smarter about queried fields,
2013 // however we're only looking at structs up to 16 bytes, so we don't care
2014 // much.
2015 unsigned idx = 0;
2016 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2017 i != e; ++i, ++idx) {
2018 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002019
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002020 // If we found a field after the region we care about, then we're done.
2021 if (FieldOffset >= EndBit) break;
2022
2023 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
2024 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
2025 Context))
2026 return false;
2027 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002028
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002029 // If nothing in this record overlapped the area of interest, then we're
2030 // clean.
2031 return true;
2032 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002033
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002034 return false;
2035}
2036
Chris Lattnere556a712010-07-29 18:39:32 +00002037/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
2038/// float member at the specified offset. For example, {int,{float}} has a
2039/// float at offset 4. It is conservatively correct for this routine to return
2040/// false.
Chris Lattner2192fe52011-07-18 04:24:23 +00002041static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Micah Villmowdd31ca12012-10-08 16:25:52 +00002042 const llvm::DataLayout &TD) {
Chris Lattnere556a712010-07-29 18:39:32 +00002043 // Base case if we find a float.
2044 if (IROffset == 0 && IRType->isFloatTy())
2045 return true;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002046
Chris Lattnere556a712010-07-29 18:39:32 +00002047 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2192fe52011-07-18 04:24:23 +00002048 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnere556a712010-07-29 18:39:32 +00002049 const llvm::StructLayout *SL = TD.getStructLayout(STy);
2050 unsigned Elt = SL->getElementContainingOffset(IROffset);
2051 IROffset -= SL->getElementOffset(Elt);
2052 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
2053 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002054
Chris Lattnere556a712010-07-29 18:39:32 +00002055 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2192fe52011-07-18 04:24:23 +00002056 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
2057 llvm::Type *EltTy = ATy->getElementType();
Chris Lattnere556a712010-07-29 18:39:32 +00002058 unsigned EltSize = TD.getTypeAllocSize(EltTy);
2059 IROffset -= IROffset/EltSize*EltSize;
2060 return ContainsFloatAtOffset(EltTy, IROffset, TD);
2061 }
2062
2063 return false;
2064}
2065
Chris Lattner7f4b81a2010-07-29 18:13:09 +00002066
2067/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
2068/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattnera5f58b02011-07-09 17:41:47 +00002069llvm::Type *X86_64ABIInfo::
2070GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner7f4b81a2010-07-29 18:13:09 +00002071 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattner50a357e2010-07-29 18:19:50 +00002072 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattner7f4b81a2010-07-29 18:13:09 +00002073 // pass as float if the last 4 bytes is just padding. This happens for
2074 // structs that contain 3 floats.
2075 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
2076 SourceOffset*8+64, getContext()))
2077 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002078
Chris Lattnere556a712010-07-29 18:39:32 +00002079 // We want to pass as <2 x float> if the LLVM IR type contains a float at
2080 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
2081 // case.
Micah Villmowdd31ca12012-10-08 16:25:52 +00002082 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
2083 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
Chris Lattner9f8b4512010-08-25 23:39:14 +00002084 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002085
Chris Lattner7f4b81a2010-07-29 18:13:09 +00002086 return llvm::Type::getDoubleTy(getVMContext());
2087}
2088
2089
Chris Lattner1c56d9a2010-07-29 17:40:35 +00002090/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
2091/// an 8-byte GPR. This means that we either have a scalar or we are talking
2092/// about the high or low part of an up-to-16-byte struct. This routine picks
2093/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002094/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
2095/// etc).
2096///
2097/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
2098/// the source type. IROffset is an offset in bytes into the LLVM IR type that
2099/// the 8-byte value references. PrefType may be null.
2100///
2101/// SourceTy is the source level type for the entire argument. SourceOffset is
2102/// an offset into this that we're processing (which is always either 0 or 8).
2103///
Chris Lattnera5f58b02011-07-09 17:41:47 +00002104llvm::Type *X86_64ABIInfo::
2105GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner1c56d9a2010-07-29 17:40:35 +00002106 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002107 // If we're dealing with an un-offset LLVM IR type, then it means that we're
2108 // returning an 8-byte unit starting with it. See if we can safely use it.
2109 if (IROffset == 0) {
2110 // Pointers and int64's always fill the 8-byte unit.
Derek Schuffc7dd7222012-10-11 15:52:22 +00002111 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
2112 IRType->isIntegerTy(64))
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002113 return IRType;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002114
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002115 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
2116 // goodness in the source type is just tail padding. This is allowed to
2117 // kick in for struct {double,int} on the int, but not on
2118 // struct{double,int,int} because we wouldn't return the second int. We
2119 // have to do this analysis on the source type because we can't depend on
2120 // unions being lowered a specific way etc.
2121 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
Derek Schuffc7dd7222012-10-11 15:52:22 +00002122 IRType->isIntegerTy(32) ||
2123 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
2124 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
2125 cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002126
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002127 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
2128 SourceOffset*8+64, getContext()))
2129 return IRType;
2130 }
2131 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002132
Chris Lattner2192fe52011-07-18 04:24:23 +00002133 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002134 // If this is a struct, recurse into the field at the specified offset.
Micah Villmowdd31ca12012-10-08 16:25:52 +00002135 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002136 if (IROffset < SL->getSizeInBytes()) {
2137 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
2138 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002139
Chris Lattner1c56d9a2010-07-29 17:40:35 +00002140 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
2141 SourceTy, SourceOffset);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002142 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002143 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002144
Chris Lattner2192fe52011-07-18 04:24:23 +00002145 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattnera5f58b02011-07-09 17:41:47 +00002146 llvm::Type *EltTy = ATy->getElementType();
Micah Villmowdd31ca12012-10-08 16:25:52 +00002147 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
Chris Lattner98076a22010-07-29 07:43:55 +00002148 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner1c56d9a2010-07-29 17:40:35 +00002149 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
2150 SourceOffset);
Chris Lattner98076a22010-07-29 07:43:55 +00002151 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002152
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002153 // Okay, we don't have any better idea of what to pass, so we pass this in an
2154 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner3f763422010-07-29 17:34:39 +00002155 unsigned TySizeInBytes =
2156 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002157
Chris Lattner3f763422010-07-29 17:34:39 +00002158 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002159
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002160 // It is always safe to classify this as an integer type up to i64 that
2161 // isn't larger than the structure.
Chris Lattner3f763422010-07-29 17:34:39 +00002162 return llvm::IntegerType::get(getVMContext(),
2163 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner22a931e2010-06-29 06:01:59 +00002164}
2165
Chris Lattnerd426c8e2010-09-01 00:50:20 +00002166
2167/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
2168/// be used as elements of a two register pair to pass or return, return a
2169/// first class aggregate to represent them. For example, if the low part of
2170/// a by-value argument should be passed as i32* and the high part as float,
2171/// return {i32*, float}.
Chris Lattnera5f58b02011-07-09 17:41:47 +00002172static llvm::Type *
Jay Foad7c57be32011-07-11 09:56:20 +00002173GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Micah Villmowdd31ca12012-10-08 16:25:52 +00002174 const llvm::DataLayout &TD) {
Chris Lattnerd426c8e2010-09-01 00:50:20 +00002175 // In order to correctly satisfy the ABI, we need to the high part to start
2176 // at offset 8. If the high and low parts we inferred are both 4-byte types
2177 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
2178 // the second element at offset 8. Check for this:
2179 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
2180 unsigned HiAlign = TD.getABITypeAlignment(Hi);
Micah Villmowdd31ca12012-10-08 16:25:52 +00002181 unsigned HiStart = llvm::DataLayout::RoundUpAlignment(LoSize, HiAlign);
Chris Lattnerd426c8e2010-09-01 00:50:20 +00002182 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002183
Chris Lattnerd426c8e2010-09-01 00:50:20 +00002184 // To handle this, we have to increase the size of the low part so that the
2185 // second element will start at an 8 byte offset. We can't increase the size
2186 // of the second element because it might make us access off the end of the
2187 // struct.
2188 if (HiStart != 8) {
2189 // There are only two sorts of types the ABI generation code can produce for
2190 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
2191 // Promote these to a larger type.
2192 if (Lo->isFloatTy())
2193 Lo = llvm::Type::getDoubleTy(Lo->getContext());
2194 else {
2195 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
2196 Lo = llvm::Type::getInt64Ty(Lo->getContext());
2197 }
2198 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002199
Chris Lattnera5f58b02011-07-09 17:41:47 +00002200 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002201
2202
Chris Lattnerd426c8e2010-09-01 00:50:20 +00002203 // Verify that the second element is at an 8-byte offset.
2204 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
2205 "Invalid x86-64 argument pair!");
2206 return Result;
2207}
2208
Chris Lattner31faff52010-07-28 23:06:14 +00002209ABIArgInfo X86_64ABIInfo::
Chris Lattner458b2aa2010-07-29 02:16:43 +00002210classifyReturnType(QualType RetTy) const {
Chris Lattner31faff52010-07-28 23:06:14 +00002211 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
2212 // classification algorithm.
2213 X86_64ABIInfo::Class Lo, Hi;
Eli Friedman96fd2642013-06-12 00:13:45 +00002214 classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true);
Chris Lattner31faff52010-07-28 23:06:14 +00002215
2216 // Check some invariants.
2217 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner31faff52010-07-28 23:06:14 +00002218 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2219
Chris Lattnera5f58b02011-07-09 17:41:47 +00002220 llvm::Type *ResType = 0;
Chris Lattner31faff52010-07-28 23:06:14 +00002221 switch (Lo) {
2222 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00002223 if (Hi == NoClass)
2224 return ABIArgInfo::getIgnore();
2225 // If the low part is just padding, it takes no register, leave ResType
2226 // null.
2227 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2228 "Unknown missing lo part");
2229 break;
Chris Lattner31faff52010-07-28 23:06:14 +00002230
2231 case SSEUp:
2232 case X87Up:
David Blaikie83d382b2011-09-23 05:06:16 +00002233 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner31faff52010-07-28 23:06:14 +00002234
2235 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
2236 // hidden argument.
2237 case Memory:
2238 return getIndirectReturnResult(RetTy);
2239
2240 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
2241 // available register of the sequence %rax, %rdx is used.
2242 case Integer:
Chris Lattnera5f58b02011-07-09 17:41:47 +00002243 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002244
Chris Lattner1f3a0632010-07-29 21:42:50 +00002245 // If we have a sign or zero extended integer, make sure to return Extend
2246 // so that the parameter gets the right LLVM IR attributes.
2247 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2248 // Treat an enum type as its underlying type.
2249 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2250 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002251
Chris Lattner1f3a0632010-07-29 21:42:50 +00002252 if (RetTy->isIntegralOrEnumerationType() &&
2253 RetTy->isPromotableIntegerType())
2254 return ABIArgInfo::getExtend();
2255 }
Chris Lattner31faff52010-07-28 23:06:14 +00002256 break;
2257
2258 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
2259 // available SSE register of the sequence %xmm0, %xmm1 is used.
2260 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00002261 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattnerfa560fe2010-07-28 23:12:33 +00002262 break;
Chris Lattner31faff52010-07-28 23:06:14 +00002263
2264 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
2265 // returned on the X87 stack in %st0 as 80-bit x87 number.
2266 case X87:
Chris Lattner2b037972010-07-29 02:01:43 +00002267 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00002268 break;
Chris Lattner31faff52010-07-28 23:06:14 +00002269
2270 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
2271 // part of the value is returned in %st0 and the imaginary part in
2272 // %st1.
2273 case ComplexX87:
2274 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner845511f2011-06-18 22:49:11 +00002275 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner2b037972010-07-29 02:01:43 +00002276 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner31faff52010-07-28 23:06:14 +00002277 NULL);
2278 break;
2279 }
2280
Chris Lattnera5f58b02011-07-09 17:41:47 +00002281 llvm::Type *HighPart = 0;
Chris Lattner31faff52010-07-28 23:06:14 +00002282 switch (Hi) {
2283 // Memory was handled previously and X87 should
2284 // never occur as a hi class.
2285 case Memory:
2286 case X87:
David Blaikie83d382b2011-09-23 05:06:16 +00002287 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner31faff52010-07-28 23:06:14 +00002288
2289 case ComplexX87: // Previously handled.
Chris Lattnerfa560fe2010-07-28 23:12:33 +00002290 case NoClass:
2291 break;
Chris Lattner31faff52010-07-28 23:06:14 +00002292
Chris Lattner52b3c132010-09-01 00:20:33 +00002293 case Integer:
Chris Lattnera5f58b02011-07-09 17:41:47 +00002294 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00002295 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2296 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00002297 break;
Chris Lattner52b3c132010-09-01 00:20:33 +00002298 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00002299 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00002300 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2301 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00002302 break;
2303
2304 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002305 // is passed in the next available eightbyte chunk if the last used
2306 // vector register.
Chris Lattner31faff52010-07-28 23:06:14 +00002307 //
Chris Lattner57540c52011-04-15 05:22:18 +00002308 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner31faff52010-07-28 23:06:14 +00002309 case SSEUp:
2310 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002311 ResType = GetByteVectorType(RetTy);
Chris Lattner31faff52010-07-28 23:06:14 +00002312 break;
2313
2314 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
2315 // returned together with the previous X87 value in %st0.
2316 case X87Up:
Chris Lattner57540c52011-04-15 05:22:18 +00002317 // If X87Up is preceded by X87, we don't need to do
Chris Lattner31faff52010-07-28 23:06:14 +00002318 // anything. However, in some cases with unions it may not be
Chris Lattner57540c52011-04-15 05:22:18 +00002319 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner31faff52010-07-28 23:06:14 +00002320 // extra bits in an SSE reg.
Chris Lattnerc95a3982010-07-29 17:49:08 +00002321 if (Lo != X87) {
Chris Lattnera5f58b02011-07-09 17:41:47 +00002322 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00002323 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2324 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattnerc95a3982010-07-29 17:49:08 +00002325 }
Chris Lattner31faff52010-07-28 23:06:14 +00002326 break;
2327 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002328
Chris Lattner52b3c132010-09-01 00:20:33 +00002329 // If a high part was specified, merge it together with the low part. It is
Chris Lattnerbe5eb172010-09-01 00:24:35 +00002330 // known to pass in the high eightbyte of the result. We do this by forming a
2331 // first class struct aggregate with the high and low part: {low, high}
Chris Lattnerd426c8e2010-09-01 00:50:20 +00002332 if (HighPart)
Micah Villmowdd31ca12012-10-08 16:25:52 +00002333 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Chris Lattner31faff52010-07-28 23:06:14 +00002334
Chris Lattner1f3a0632010-07-29 21:42:50 +00002335 return ABIArgInfo::getDirect(ResType);
Chris Lattner31faff52010-07-28 23:06:14 +00002336}
2337
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002338ABIArgInfo X86_64ABIInfo::classifyArgumentType(
Eli Friedman96fd2642013-06-12 00:13:45 +00002339 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE,
2340 bool isNamedArg)
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002341 const
2342{
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002343 X86_64ABIInfo::Class Lo, Hi;
Eli Friedman96fd2642013-06-12 00:13:45 +00002344 classify(Ty, 0, Lo, Hi, isNamedArg);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002345
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002346 // Check some invariants.
2347 // FIXME: Enforce these by construction.
2348 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002349 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2350
2351 neededInt = 0;
2352 neededSSE = 0;
Chris Lattnera5f58b02011-07-09 17:41:47 +00002353 llvm::Type *ResType = 0;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002354 switch (Lo) {
2355 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00002356 if (Hi == NoClass)
2357 return ABIArgInfo::getIgnore();
2358 // If the low part is just padding, it takes no register, leave ResType
2359 // null.
2360 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2361 "Unknown missing lo part");
2362 break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002363
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002364 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
2365 // on the stack.
2366 case Memory:
2367
2368 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
2369 // COMPLEX_X87, it is passed in memory.
2370 case X87:
2371 case ComplexX87:
Mark Lacey3825e832013-10-06 01:33:34 +00002372 if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect)
Eli Friedman4774b7e2011-06-29 07:04:55 +00002373 ++neededInt;
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002374 return getIndirectResult(Ty, freeIntRegs);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002375
2376 case SSEUp:
2377 case X87Up:
David Blaikie83d382b2011-09-23 05:06:16 +00002378 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002379
2380 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
2381 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
2382 // and %r9 is used.
2383 case Integer:
Chris Lattner22a931e2010-06-29 06:01:59 +00002384 ++neededInt;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002385
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002386 // Pick an 8-byte type based on the preferred type.
Chris Lattnera5f58b02011-07-09 17:41:47 +00002387 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattner1f3a0632010-07-29 21:42:50 +00002388
2389 // If we have a sign or zero extended integer, make sure to return Extend
2390 // so that the parameter gets the right LLVM IR attributes.
2391 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2392 // Treat an enum type as its underlying type.
2393 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2394 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002395
Chris Lattner1f3a0632010-07-29 21:42:50 +00002396 if (Ty->isIntegralOrEnumerationType() &&
2397 Ty->isPromotableIntegerType())
2398 return ABIArgInfo::getExtend();
2399 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002400
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002401 break;
2402
2403 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
2404 // available SSE register is used, the registers are taken in the
2405 // order from %xmm0 to %xmm7.
Bill Wendling5cd41c42010-10-18 03:41:31 +00002406 case SSE: {
Chris Lattnera5f58b02011-07-09 17:41:47 +00002407 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman1310c682011-07-02 00:57:27 +00002408 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling9987c0e2010-10-18 23:51:38 +00002409 ++neededSSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002410 break;
2411 }
Bill Wendling5cd41c42010-10-18 03:41:31 +00002412 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002413
Chris Lattnera5f58b02011-07-09 17:41:47 +00002414 llvm::Type *HighPart = 0;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002415 switch (Hi) {
2416 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattner57540c52011-04-15 05:22:18 +00002417 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002418 // which is passed in memory.
2419 case Memory:
2420 case X87:
2421 case ComplexX87:
David Blaikie83d382b2011-09-23 05:06:16 +00002422 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002423
2424 case NoClass: break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002425
Chris Lattnerbe5eb172010-09-01 00:24:35 +00002426 case Integer:
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002427 ++neededInt;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002428 // Pick an 8-byte type based on the preferred type.
Chris Lattnera5f58b02011-07-09 17:41:47 +00002429 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002430
Chris Lattnerbe5eb172010-09-01 00:24:35 +00002431 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2432 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002433 break;
2434
2435 // X87Up generally doesn't occur here (long double is passed in
2436 // memory), except in situations involving unions.
2437 case X87Up:
Chris Lattnerbe5eb172010-09-01 00:24:35 +00002438 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00002439 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002440
Chris Lattnerbe5eb172010-09-01 00:24:35 +00002441 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2442 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner8a2f3c72010-07-30 04:02:24 +00002443
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002444 ++neededSSE;
2445 break;
2446
2447 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
2448 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002449 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002450 case SSEUp:
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00002451 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002452 ResType = GetByteVectorType(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002453 break;
2454 }
2455
Chris Lattnerbe5eb172010-09-01 00:24:35 +00002456 // If a high part was specified, merge it together with the low part. It is
2457 // known to pass in the high eightbyte of the result. We do this by forming a
2458 // first class struct aggregate with the high and low part: {low, high}
2459 if (HighPart)
Micah Villmowdd31ca12012-10-08 16:25:52 +00002460 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002461
Chris Lattner1f3a0632010-07-29 21:42:50 +00002462 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002463}
2464
Chris Lattner22326a12010-07-29 02:31:05 +00002465void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002466
Chris Lattner458b2aa2010-07-29 02:16:43 +00002467 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002468
2469 // Keep track of the number of assigned registers.
Bill Wendling9987c0e2010-10-18 23:51:38 +00002470 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002471
2472 // If the return value is indirect, then the hidden argument is consuming one
2473 // integer register.
2474 if (FI.getReturnInfo().isIndirect())
2475 --freeIntRegs;
2476
Eli Friedman96fd2642013-06-12 00:13:45 +00002477 bool isVariadic = FI.isVariadic();
2478 unsigned numRequiredArgs = 0;
2479 if (isVariadic)
2480 numRequiredArgs = FI.getRequiredArgs().getNumRequiredArgs();
2481
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002482 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2483 // get assigned (in left-to-right order) for passing as follows...
2484 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2485 it != ie; ++it) {
Eli Friedman96fd2642013-06-12 00:13:45 +00002486 bool isNamedArg = true;
2487 if (isVariadic)
Aaron Ballman6a302642013-06-12 15:03:45 +00002488 isNamedArg = (it - FI.arg_begin()) <
2489 static_cast<signed>(numRequiredArgs);
Eli Friedman96fd2642013-06-12 00:13:45 +00002490
Bill Wendling9987c0e2010-10-18 23:51:38 +00002491 unsigned neededInt, neededSSE;
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002492 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
Eli Friedman96fd2642013-06-12 00:13:45 +00002493 neededSSE, isNamedArg);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002494
2495 // AMD64-ABI 3.2.3p3: If there are no registers available for any
2496 // eightbyte of an argument, the whole argument is passed on the
2497 // stack. If registers have already been assigned for some
2498 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling9987c0e2010-10-18 23:51:38 +00002499 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002500 freeIntRegs -= neededInt;
2501 freeSSERegs -= neededSSE;
2502 } else {
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002503 it->info = getIndirectResult(it->type, freeIntRegs);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002504 }
2505 }
2506}
2507
2508static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2509 QualType Ty,
2510 CodeGenFunction &CGF) {
2511 llvm::Value *overflow_arg_area_p =
2512 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2513 llvm::Value *overflow_arg_area =
2514 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2515
2516 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2517 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedmana1748562011-11-18 02:44:19 +00002518 // It isn't stated explicitly in the standard, but in practice we use
2519 // alignment greater than 16 where necessary.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002520 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2521 if (Align > 8) {
Eli Friedmana1748562011-11-18 02:44:19 +00002522 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
Owen Anderson41a75022009-08-13 21:57:51 +00002523 llvm::Value *Offset =
Eli Friedmana1748562011-11-18 02:44:19 +00002524 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002525 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2526 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner5e016ae2010-06-27 07:15:29 +00002527 CGF.Int64Ty);
Eli Friedmana1748562011-11-18 02:44:19 +00002528 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002529 overflow_arg_area =
2530 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2531 overflow_arg_area->getType(),
2532 "overflow_arg_area.align");
2533 }
2534
2535 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2192fe52011-07-18 04:24:23 +00002536 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002537 llvm::Value *Res =
2538 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00002539 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002540
2541 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2542 // l->overflow_arg_area + sizeof(type).
2543 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2544 // an 8 byte boundary.
2545
2546 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00002547 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00002548 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002549 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2550 "overflow_arg_area.next");
2551 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2552
2553 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2554 return Res;
2555}
2556
2557llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2558 CodeGenFunction &CGF) const {
2559 // Assume that va_list type is correct; should be pointer to LLVM type:
2560 // struct {
2561 // i32 gp_offset;
2562 // i32 fp_offset;
2563 // i8* overflow_arg_area;
2564 // i8* reg_save_area;
2565 // };
Bill Wendling9987c0e2010-10-18 23:51:38 +00002566 unsigned neededInt, neededSSE;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002567
Chris Lattner9723d6c2010-03-11 18:19:55 +00002568 Ty = CGF.getContext().getCanonicalType(Ty);
Eli Friedman96fd2642013-06-12 00:13:45 +00002569 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE,
2570 /*isNamedArg*/false);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002571
2572 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2573 // in the registers. If not go to step 7.
2574 if (!neededInt && !neededSSE)
2575 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2576
2577 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2578 // general purpose registers needed to pass type and num_fp to hold
2579 // the number of floating point registers needed.
2580
2581 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2582 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2583 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2584 //
2585 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2586 // register save space).
2587
2588 llvm::Value *InRegs = 0;
2589 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2590 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2591 if (neededInt) {
2592 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2593 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattnerd776fb12010-06-28 21:43:59 +00002594 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2595 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002596 }
2597
2598 if (neededSSE) {
2599 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2600 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2601 llvm::Value *FitsInFP =
Chris Lattnerd776fb12010-06-28 21:43:59 +00002602 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2603 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002604 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2605 }
2606
2607 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2608 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2609 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2610 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2611
2612 // Emit code to load the value if it was passed in registers.
2613
2614 CGF.EmitBlock(InRegBlock);
2615
2616 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2617 // an offset of l->gp_offset and/or l->fp_offset. This may require
2618 // copying to a temporary location in case the parameter is passed
2619 // in different register classes or requires an alignment greater
2620 // than 8 for general purpose registers and 16 for XMM registers.
2621 //
2622 // FIXME: This really results in shameful code when we end up needing to
2623 // collect arguments from different places; often what should result in a
2624 // simple assembling of a structure from scattered addresses has many more
2625 // loads than necessary. Can we clean this up?
Chris Lattner2192fe52011-07-18 04:24:23 +00002626 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002627 llvm::Value *RegAddr =
2628 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2629 "reg_save_area");
2630 if (neededInt && neededSSE) {
2631 // FIXME: Cleanup.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002632 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00002633 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Eli Friedmanc11c1692013-06-07 23:20:55 +00002634 llvm::Value *Tmp = CGF.CreateMemTemp(Ty);
2635 Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002636 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00002637 llvm::Type *TyLo = ST->getElementType(0);
2638 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattner51e1cc22010-08-26 06:28:35 +00002639 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002640 "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00002641 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2642 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002643 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2644 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sands998f9d92010-02-15 16:14:01 +00002645 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2646 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002647 llvm::Value *V =
2648 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2649 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2650 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2651 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2652
Owen Anderson170229f2009-07-14 23:10:40 +00002653 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00002654 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002655 } else if (neededInt) {
2656 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2657 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00002658 llvm::PointerType::getUnqual(LTy));
Eli Friedmanc11c1692013-06-07 23:20:55 +00002659
2660 // Copy to a temporary if necessary to ensure the appropriate alignment.
2661 std::pair<CharUnits, CharUnits> SizeAlign =
2662 CGF.getContext().getTypeInfoInChars(Ty);
2663 uint64_t TySize = SizeAlign.first.getQuantity();
2664 unsigned TyAlign = SizeAlign.second.getQuantity();
2665 if (TyAlign > 8) {
Eli Friedmanc11c1692013-06-07 23:20:55 +00002666 llvm::Value *Tmp = CGF.CreateMemTemp(Ty);
2667 CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, 8, false);
2668 RegAddr = Tmp;
2669 }
Chris Lattner0cf24192010-06-28 20:05:43 +00002670 } else if (neededSSE == 1) {
2671 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2672 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2673 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002674 } else {
Chris Lattner0cf24192010-06-28 20:05:43 +00002675 assert(neededSSE == 2 && "Invalid number of needed registers!");
2676 // SSE registers are spaced 16 bytes apart in the register save
2677 // area, we need to collect the two eightbytes together.
2678 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattnerd776fb12010-06-28 21:43:59 +00002679 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattnerece04092012-02-07 00:39:47 +00002680 llvm::Type *DoubleTy = CGF.DoubleTy;
Chris Lattner2192fe52011-07-18 04:24:23 +00002681 llvm::Type *DblPtrTy =
Chris Lattner0cf24192010-06-28 20:05:43 +00002682 llvm::PointerType::getUnqual(DoubleTy);
Eli Friedmanc11c1692013-06-07 23:20:55 +00002683 llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, NULL);
2684 llvm::Value *V, *Tmp = CGF.CreateMemTemp(Ty);
2685 Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo());
Chris Lattner0cf24192010-06-28 20:05:43 +00002686 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2687 DblPtrTy));
2688 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2689 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2690 DblPtrTy));
2691 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2692 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2693 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002694 }
2695
2696 // AMD64-ABI 3.5.7p5: Step 5. Set:
2697 // l->gp_offset = l->gp_offset + num_gp * 8
2698 // l->fp_offset = l->fp_offset + num_fp * 16.
2699 if (neededInt) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00002700 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002701 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2702 gp_offset_p);
2703 }
2704 if (neededSSE) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00002705 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002706 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2707 fp_offset_p);
2708 }
2709 CGF.EmitBranch(ContBlock);
2710
2711 // Emit code to load the value if it was passed in memory.
2712
2713 CGF.EmitBlock(InMemBlock);
2714 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2715
2716 // Return the appropriate result.
2717
2718 CGF.EmitBlock(ContBlock);
Jay Foad20c0f022011-03-30 11:28:58 +00002719 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002720 "vaarg.addr");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002721 ResAddr->addIncoming(RegAddr, InRegBlock);
2722 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002723 return ResAddr;
2724}
2725
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00002726ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, bool IsReturnType) const {
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002727
2728 if (Ty->isVoidType())
2729 return ABIArgInfo::getIgnore();
2730
2731 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2732 Ty = EnumTy->getDecl()->getIntegerType();
2733
2734 uint64_t Size = getContext().getTypeSize(Ty);
2735
2736 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00002737 if (IsReturnType) {
Mark Lacey3825e832013-10-06 01:33:34 +00002738 if (isRecordReturnIndirect(RT, getCXXABI()))
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00002739 return ABIArgInfo::getIndirect(0, false);
2740 } else {
Mark Lacey3825e832013-10-06 01:33:34 +00002741 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()))
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00002742 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
2743 }
2744
2745 if (RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002746 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2747
NAKAMURA Takumif8a6e802011-02-22 03:56:57 +00002748 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
John McCallc8e01702013-04-16 22:48:15 +00002749 if (Size == 128 && getTarget().getTriple().getOS() == llvm::Triple::MinGW32)
NAKAMURA Takumif8a6e802011-02-22 03:56:57 +00002750 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2751 Size));
2752
2753 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2754 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2755 if (Size <= 64 &&
NAKAMURA Takumie03c6032011-01-19 00:11:33 +00002756 (Size & (Size - 1)) == 0)
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002757 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2758 Size));
2759
2760 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2761 }
2762
2763 if (Ty->isPromotableIntegerType())
2764 return ABIArgInfo::getExtend();
2765
2766 return ABIArgInfo::getDirect();
2767}
2768
2769void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2770
2771 QualType RetTy = FI.getReturnType();
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00002772 FI.getReturnInfo() = classify(RetTy, true);
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002773
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002774 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2775 it != ie; ++it)
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00002776 it->info = classify(it->type, false);
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002777}
2778
Chris Lattner04dc9572010-08-31 16:44:54 +00002779llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2780 CodeGenFunction &CGF) const {
Chris Lattnerece04092012-02-07 00:39:47 +00002781 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Chris Lattner0cf24192010-06-28 20:05:43 +00002782
Chris Lattner04dc9572010-08-31 16:44:54 +00002783 CGBuilderTy &Builder = CGF.Builder;
2784 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2785 "ap");
2786 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2787 llvm::Type *PTy =
2788 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2789 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2790
2791 uint64_t Offset =
2792 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2793 llvm::Value *NextAddr =
2794 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2795 "ap.next");
2796 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2797
2798 return AddrTyped;
2799}
Chris Lattner0cf24192010-06-28 20:05:43 +00002800
Benjamin Kramer1cdb23d2012-10-20 13:02:06 +00002801namespace {
2802
Derek Schuffa2020962012-10-16 22:30:41 +00002803class NaClX86_64ABIInfo : public ABIInfo {
2804 public:
2805 NaClX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2806 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, HasAVX) {}
2807 virtual void computeInfo(CGFunctionInfo &FI) const;
2808 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2809 CodeGenFunction &CGF) const;
2810 private:
2811 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
2812 X86_64ABIInfo NInfo; // Used for everything else.
2813};
2814
2815class NaClX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2816 public:
2817 NaClX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2818 : TargetCodeGenInfo(new NaClX86_64ABIInfo(CGT, HasAVX)) {}
2819};
2820
Benjamin Kramer1cdb23d2012-10-20 13:02:06 +00002821}
2822
Derek Schuffa2020962012-10-16 22:30:41 +00002823void NaClX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2824 if (FI.getASTCallingConvention() == CC_PnaclCall)
2825 PInfo.computeInfo(FI);
2826 else
2827 NInfo.computeInfo(FI);
2828}
2829
2830llvm::Value *NaClX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2831 CodeGenFunction &CGF) const {
2832 // Always use the native convention; calling pnacl-style varargs functions
2833 // is unuspported.
2834 return NInfo.EmitVAArg(VAListAddr, Ty, CGF);
2835}
2836
2837
John McCallea8d8bb2010-03-11 00:10:12 +00002838// PowerPC-32
2839
2840namespace {
2841class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2842public:
Chris Lattner2b037972010-07-29 02:01:43 +00002843 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002844
John McCallea8d8bb2010-03-11 00:10:12 +00002845 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2846 // This is recovered from gcc output.
2847 return 1; // r1 is the dedicated stack pointer
2848 }
2849
2850 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002851 llvm::Value *Address) const;
John McCallea8d8bb2010-03-11 00:10:12 +00002852};
2853
2854}
2855
2856bool
2857PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2858 llvm::Value *Address) const {
2859 // This is calculated from the LLVM and GCC tables and verified
2860 // against gcc output. AFAIK all ABIs use the same encoding.
2861
2862 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallea8d8bb2010-03-11 00:10:12 +00002863
Chris Lattnerece04092012-02-07 00:39:47 +00002864 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallea8d8bb2010-03-11 00:10:12 +00002865 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2866 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2867 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2868
2869 // 0-31: r0-31, the 4-byte general-purpose registers
John McCall943fae92010-05-27 06:19:26 +00002870 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallea8d8bb2010-03-11 00:10:12 +00002871
2872 // 32-63: fp0-31, the 8-byte floating-point registers
John McCall943fae92010-05-27 06:19:26 +00002873 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallea8d8bb2010-03-11 00:10:12 +00002874
2875 // 64-76 are various 4-byte special-purpose registers:
2876 // 64: mq
2877 // 65: lr
2878 // 66: ctr
2879 // 67: ap
2880 // 68-75 cr0-7
2881 // 76: xer
John McCall943fae92010-05-27 06:19:26 +00002882 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallea8d8bb2010-03-11 00:10:12 +00002883
2884 // 77-108: v0-31, the 16-byte vector registers
John McCall943fae92010-05-27 06:19:26 +00002885 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallea8d8bb2010-03-11 00:10:12 +00002886
2887 // 109: vrsave
2888 // 110: vscr
2889 // 111: spe_acc
2890 // 112: spefscr
2891 // 113: sfp
John McCall943fae92010-05-27 06:19:26 +00002892 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallea8d8bb2010-03-11 00:10:12 +00002893
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002894 return false;
John McCallea8d8bb2010-03-11 00:10:12 +00002895}
2896
Roman Divackyd966e722012-05-09 18:22:46 +00002897// PowerPC-64
2898
2899namespace {
Bill Schmidt25cb3492012-10-03 19:18:57 +00002900/// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
2901class PPC64_SVR4_ABIInfo : public DefaultABIInfo {
2902
2903public:
2904 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
2905
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00002906 bool isPromotableTypeForABI(QualType Ty) const;
2907
2908 ABIArgInfo classifyReturnType(QualType RetTy) const;
2909 ABIArgInfo classifyArgumentType(QualType Ty) const;
2910
Bill Schmidt84d37792012-10-12 19:26:17 +00002911 // TODO: We can add more logic to computeInfo to improve performance.
2912 // Example: For aggregate arguments that fit in a register, we could
2913 // use getDirectInReg (as is done below for structs containing a single
2914 // floating-point value) to avoid pushing them to memory on function
2915 // entry. This would require changing the logic in PPCISelLowering
2916 // when lowering the parameters in the caller and args in the callee.
2917 virtual void computeInfo(CGFunctionInfo &FI) const {
2918 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2919 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2920 it != ie; ++it) {
2921 // We rely on the default argument classification for the most part.
2922 // One exception: An aggregate containing a single floating-point
Bill Schmidt179afae2013-07-23 22:15:57 +00002923 // or vector item must be passed in a register if one is available.
Bill Schmidt84d37792012-10-12 19:26:17 +00002924 const Type *T = isSingleElementStruct(it->type, getContext());
2925 if (T) {
2926 const BuiltinType *BT = T->getAs<BuiltinType>();
Bill Schmidt179afae2013-07-23 22:15:57 +00002927 if (T->isVectorType() || (BT && BT->isFloatingPoint())) {
Bill Schmidt84d37792012-10-12 19:26:17 +00002928 QualType QT(T, 0);
2929 it->info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
2930 continue;
2931 }
2932 }
2933 it->info = classifyArgumentType(it->type);
2934 }
2935 }
Bill Schmidt25cb3492012-10-03 19:18:57 +00002936
2937 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr,
2938 QualType Ty,
2939 CodeGenFunction &CGF) const;
2940};
2941
2942class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
2943public:
2944 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT)
2945 : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT)) {}
2946
2947 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2948 // This is recovered from gcc output.
2949 return 1; // r1 is the dedicated stack pointer
2950 }
2951
2952 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2953 llvm::Value *Address) const;
2954};
2955
Roman Divackyd966e722012-05-09 18:22:46 +00002956class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2957public:
2958 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
2959
2960 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2961 // This is recovered from gcc output.
2962 return 1; // r1 is the dedicated stack pointer
2963 }
2964
2965 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2966 llvm::Value *Address) const;
2967};
2968
2969}
2970
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00002971// Return true if the ABI requires Ty to be passed sign- or zero-
2972// extended to 64 bits.
2973bool
2974PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
2975 // Treat an enum type as its underlying type.
2976 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2977 Ty = EnumTy->getDecl()->getIntegerType();
2978
2979 // Promotable integer types are required to be promoted by the ABI.
2980 if (Ty->isPromotableIntegerType())
2981 return true;
2982
2983 // In addition to the usual promotable integer types, we also need to
2984 // extend all 32-bit types, since the ABI requires promotion to 64 bits.
2985 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2986 switch (BT->getKind()) {
2987 case BuiltinType::Int:
2988 case BuiltinType::UInt:
2989 return true;
2990 default:
2991 break;
2992 }
2993
2994 return false;
2995}
2996
2997ABIArgInfo
2998PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
Bill Schmidt90b22c92012-11-27 02:46:43 +00002999 if (Ty->isAnyComplexType())
3000 return ABIArgInfo::getDirect();
3001
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00003002 if (isAggregateTypeForABI(Ty)) {
Mark Lacey3825e832013-10-06 01:33:34 +00003003 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00003004 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00003005
3006 return ABIArgInfo::getIndirect(0);
3007 }
3008
3009 return (isPromotableTypeForABI(Ty) ?
3010 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3011}
3012
3013ABIArgInfo
3014PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
3015 if (RetTy->isVoidType())
3016 return ABIArgInfo::getIgnore();
3017
Bill Schmidta3d121c2012-12-17 04:20:17 +00003018 if (RetTy->isAnyComplexType())
3019 return ABIArgInfo::getDirect();
3020
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00003021 if (isAggregateTypeForABI(RetTy))
3022 return ABIArgInfo::getIndirect(0);
3023
3024 return (isPromotableTypeForABI(RetTy) ?
3025 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3026}
3027
Bill Schmidt25cb3492012-10-03 19:18:57 +00003028// Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
3029llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr,
3030 QualType Ty,
3031 CodeGenFunction &CGF) const {
3032 llvm::Type *BP = CGF.Int8PtrTy;
3033 llvm::Type *BPP = CGF.Int8PtrPtrTy;
3034
3035 CGBuilderTy &Builder = CGF.Builder;
3036 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3037 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
3038
Bill Schmidt924c4782013-01-14 17:45:36 +00003039 // Update the va_list pointer. The pointer should be bumped by the
3040 // size of the object. We can trust getTypeSize() except for a complex
3041 // type whose base type is smaller than a doubleword. For these, the
3042 // size of the object is 16 bytes; see below for further explanation.
Bill Schmidt25cb3492012-10-03 19:18:57 +00003043 unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8;
Bill Schmidt924c4782013-01-14 17:45:36 +00003044 QualType BaseTy;
3045 unsigned CplxBaseSize = 0;
3046
3047 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
3048 BaseTy = CTy->getElementType();
3049 CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8;
3050 if (CplxBaseSize < 8)
3051 SizeInBytes = 16;
3052 }
3053
Bill Schmidt25cb3492012-10-03 19:18:57 +00003054 unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8);
3055 llvm::Value *NextAddr =
3056 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset),
3057 "ap.next");
3058 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3059
Bill Schmidt924c4782013-01-14 17:45:36 +00003060 // If we have a complex type and the base type is smaller than 8 bytes,
3061 // the ABI calls for the real and imaginary parts to be right-adjusted
3062 // in separate doublewords. However, Clang expects us to produce a
3063 // pointer to a structure with the two parts packed tightly. So generate
3064 // loads of the real and imaginary parts relative to the va_list pointer,
3065 // and store them to a temporary structure.
3066 if (CplxBaseSize && CplxBaseSize < 8) {
3067 llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
3068 llvm::Value *ImagAddr = RealAddr;
3069 RealAddr = Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize));
3070 ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize));
3071 llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy));
3072 RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy);
3073 ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy);
3074 llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal");
3075 llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag");
3076 llvm::Value *Ptr = CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty),
3077 "vacplx");
3078 llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, ".real");
3079 llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, ".imag");
3080 Builder.CreateStore(Real, RealPtr, false);
3081 Builder.CreateStore(Imag, ImagPtr, false);
3082 return Ptr;
3083 }
3084
Bill Schmidt25cb3492012-10-03 19:18:57 +00003085 // If the argument is smaller than 8 bytes, it is right-adjusted in
3086 // its doubleword slot. Adjust the pointer to pick it up from the
3087 // correct offset.
3088 if (SizeInBytes < 8) {
3089 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
3090 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes));
3091 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
3092 }
3093
3094 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3095 return Builder.CreateBitCast(Addr, PTy);
3096}
3097
3098static bool
3099PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3100 llvm::Value *Address) {
Roman Divackyd966e722012-05-09 18:22:46 +00003101 // This is calculated from the LLVM and GCC tables and verified
3102 // against gcc output. AFAIK all ABIs use the same encoding.
3103
3104 CodeGen::CGBuilderTy &Builder = CGF.Builder;
3105
3106 llvm::IntegerType *i8 = CGF.Int8Ty;
3107 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
3108 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
3109 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
3110
3111 // 0-31: r0-31, the 8-byte general-purpose registers
3112 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
3113
3114 // 32-63: fp0-31, the 8-byte floating-point registers
3115 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
3116
3117 // 64-76 are various 4-byte special-purpose registers:
3118 // 64: mq
3119 // 65: lr
3120 // 66: ctr
3121 // 67: ap
3122 // 68-75 cr0-7
3123 // 76: xer
3124 AssignToArrayRange(Builder, Address, Four8, 64, 76);
3125
3126 // 77-108: v0-31, the 16-byte vector registers
3127 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
3128
3129 // 109: vrsave
3130 // 110: vscr
3131 // 111: spe_acc
3132 // 112: spefscr
3133 // 113: sfp
3134 AssignToArrayRange(Builder, Address, Four8, 109, 113);
3135
3136 return false;
3137}
John McCallea8d8bb2010-03-11 00:10:12 +00003138
Bill Schmidt25cb3492012-10-03 19:18:57 +00003139bool
3140PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
3141 CodeGen::CodeGenFunction &CGF,
3142 llvm::Value *Address) const {
3143
3144 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
3145}
3146
3147bool
3148PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3149 llvm::Value *Address) const {
3150
3151 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
3152}
3153
Chris Lattner0cf24192010-06-28 20:05:43 +00003154//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00003155// ARM ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00003156//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00003157
3158namespace {
3159
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003160class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00003161public:
3162 enum ABIKind {
3163 APCS = 0,
3164 AAPCS = 1,
3165 AAPCS_VFP
3166 };
3167
3168private:
3169 ABIKind Kind;
3170
3171public:
John McCall882987f2013-02-28 19:01:20 +00003172 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {
3173 setRuntimeCC();
3174 }
Daniel Dunbar020daa92009-09-12 01:00:39 +00003175
John McCall3480ef22011-08-30 01:42:09 +00003176 bool isEABI() const {
Joerg Sonnenberger782e6aa2013-12-12 21:29:27 +00003177 switch (getTarget().getTriple().getEnvironment()) {
3178 case llvm::Triple::Android:
3179 case llvm::Triple::EABI:
Joerg Sonnenbergerd75a1f82013-12-16 19:16:04 +00003180 case llvm::Triple::EABIHF:
Joerg Sonnenberger782e6aa2013-12-12 21:29:27 +00003181 case llvm::Triple::GNUEABI:
Joerg Sonnenberger0c1652d2013-12-16 18:30:28 +00003182 case llvm::Triple::GNUEABIHF:
Joerg Sonnenberger782e6aa2013-12-12 21:29:27 +00003183 return true;
3184 default:
3185 return false;
3186 }
John McCall3480ef22011-08-30 01:42:09 +00003187 }
3188
Joerg Sonnenbergerd75a1f82013-12-16 19:16:04 +00003189 bool isEABIHF() const {
3190 switch (getTarget().getTriple().getEnvironment()) {
3191 case llvm::Triple::EABIHF:
3192 case llvm::Triple::GNUEABIHF:
3193 return true;
3194 default:
3195 return false;
3196 }
3197 }
3198
Daniel Dunbar020daa92009-09-12 01:00:39 +00003199 ABIKind getABIKind() const { return Kind; }
3200
Tim Northovera484bc02013-10-01 14:34:25 +00003201private:
Amara Emerson9dc78782014-01-28 10:56:36 +00003202 ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const;
Manman Renb505d332012-10-31 19:02:26 +00003203 ABIArgInfo classifyArgumentType(QualType RetTy, int *VFPRegs,
3204 unsigned &AllocatedVFP,
Amara Emerson9dc78782014-01-28 10:56:36 +00003205 bool &IsHA, bool isVariadic) const;
Manman Renfef9e312012-10-16 19:18:39 +00003206 bool isIllegalVectorType(QualType Ty) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003207
Chris Lattner22326a12010-07-29 02:31:05 +00003208 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003209
3210 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3211 CodeGenFunction &CGF) const;
John McCall882987f2013-02-28 19:01:20 +00003212
3213 llvm::CallingConv::ID getLLVMDefaultCC() const;
3214 llvm::CallingConv::ID getABIDefaultCC() const;
3215 void setRuntimeCC();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003216};
3217
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003218class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
3219public:
Chris Lattner2b037972010-07-29 02:01:43 +00003220 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
3221 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00003222
John McCall3480ef22011-08-30 01:42:09 +00003223 const ARMABIInfo &getABIInfo() const {
3224 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
3225 }
3226
John McCallbeec5a02010-03-06 00:35:14 +00003227 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3228 return 13;
3229 }
Roman Divackyc1617352011-05-18 19:36:54 +00003230
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003231 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCall31168b02011-06-15 23:02:42 +00003232 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
3233 }
3234
Roman Divackyc1617352011-05-18 19:36:54 +00003235 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3236 llvm::Value *Address) const {
Chris Lattnerece04092012-02-07 00:39:47 +00003237 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divackyc1617352011-05-18 19:36:54 +00003238
3239 // 0-15 are the 16 integer registers.
Chris Lattnerece04092012-02-07 00:39:47 +00003240 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divackyc1617352011-05-18 19:36:54 +00003241 return false;
3242 }
John McCall3480ef22011-08-30 01:42:09 +00003243
3244 unsigned getSizeOfUnwindException() const {
3245 if (getABIInfo().isEABI()) return 88;
3246 return TargetCodeGenInfo::getSizeOfUnwindException();
3247 }
Tim Northovera484bc02013-10-01 14:34:25 +00003248
3249 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3250 CodeGen::CodeGenModule &CGM) const {
3251 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3252 if (!FD)
3253 return;
3254
3255 const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>();
3256 if (!Attr)
3257 return;
3258
3259 const char *Kind;
3260 switch (Attr->getInterrupt()) {
3261 case ARMInterruptAttr::Generic: Kind = ""; break;
3262 case ARMInterruptAttr::IRQ: Kind = "IRQ"; break;
3263 case ARMInterruptAttr::FIQ: Kind = "FIQ"; break;
3264 case ARMInterruptAttr::SWI: Kind = "SWI"; break;
3265 case ARMInterruptAttr::ABORT: Kind = "ABORT"; break;
3266 case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break;
3267 }
3268
3269 llvm::Function *Fn = cast<llvm::Function>(GV);
3270
3271 Fn->addFnAttr("interrupt", Kind);
3272
3273 if (cast<ARMABIInfo>(getABIInfo()).getABIKind() == ARMABIInfo::APCS)
3274 return;
3275
3276 // AAPCS guarantees that sp will be 8-byte aligned on any public interface,
3277 // however this is not necessarily true on taking any interrupt. Instruct
3278 // the backend to perform a realignment as part of the function prologue.
3279 llvm::AttrBuilder B;
3280 B.addStackAlignmentAttr(8);
3281 Fn->addAttributes(llvm::AttributeSet::FunctionIndex,
3282 llvm::AttributeSet::get(CGM.getLLVMContext(),
3283 llvm::AttributeSet::FunctionIndex,
3284 B));
3285 }
3286
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003287};
3288
Daniel Dunbard59655c2009-09-12 00:59:49 +00003289}
3290
Chris Lattner22326a12010-07-29 02:31:05 +00003291void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Manman Ren2a523d82012-10-30 23:21:41 +00003292 // To correctly handle Homogeneous Aggregate, we need to keep track of the
Manman Renb505d332012-10-31 19:02:26 +00003293 // VFP registers allocated so far.
Manman Ren2a523d82012-10-30 23:21:41 +00003294 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
3295 // VFP registers of the appropriate type unallocated then the argument is
3296 // allocated to the lowest-numbered sequence of such registers.
3297 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
3298 // unallocated are marked as unavailable.
3299 unsigned AllocatedVFP = 0;
Manman Renb505d332012-10-31 19:02:26 +00003300 int VFPRegs[16] = { 0 };
Amara Emerson9dc78782014-01-28 10:56:36 +00003301 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), FI.isVariadic());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003302 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Manman Ren2a523d82012-10-30 23:21:41 +00003303 it != ie; ++it) {
3304 unsigned PreAllocation = AllocatedVFP;
3305 bool IsHA = false;
3306 // 6.1.2.3 There is one VFP co-processor register class using registers
3307 // s0-s15 (d0-d7) for passing arguments.
3308 const unsigned NumVFPs = 16;
Amara Emerson9dc78782014-01-28 10:56:36 +00003309 it->info = classifyArgumentType(it->type, VFPRegs, AllocatedVFP, IsHA, FI.isVariadic());
Manman Ren2a523d82012-10-30 23:21:41 +00003310 // If we do not have enough VFP registers for the HA, any VFP registers
3311 // that are unallocated are marked as unavailable. To achieve this, we add
3312 // padding of (NumVFPs - PreAllocation) floats.
Amara Emerson9dc78782014-01-28 10:56:36 +00003313 // Note that IsHA will only be set when using the AAPCS-VFP calling convention,
3314 // and the callee is not variadic.
Manman Ren2a523d82012-10-30 23:21:41 +00003315 if (IsHA && AllocatedVFP > NumVFPs && PreAllocation < NumVFPs) {
3316 llvm::Type *PaddingTy = llvm::ArrayType::get(
3317 llvm::Type::getFloatTy(getVMContext()), NumVFPs - PreAllocation);
3318 it->info = ABIArgInfo::getExpandWithPadding(false, PaddingTy);
3319 }
3320 }
Daniel Dunbar020daa92009-09-12 01:00:39 +00003321
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003322 // Always honor user-specified calling convention.
3323 if (FI.getCallingConvention() != llvm::CallingConv::C)
3324 return;
3325
John McCall882987f2013-02-28 19:01:20 +00003326 llvm::CallingConv::ID cc = getRuntimeCC();
3327 if (cc != llvm::CallingConv::C)
3328 FI.setEffectiveCallingConvention(cc);
3329}
Rafael Espindolaa92c4422010-06-16 16:13:39 +00003330
John McCall882987f2013-02-28 19:01:20 +00003331/// Return the default calling convention that LLVM will use.
3332llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
3333 // The default calling convention that LLVM will infer.
Joerg Sonnenbergerd75a1f82013-12-16 19:16:04 +00003334 if (isEABIHF())
John McCall882987f2013-02-28 19:01:20 +00003335 return llvm::CallingConv::ARM_AAPCS_VFP;
3336 else if (isEABI())
3337 return llvm::CallingConv::ARM_AAPCS;
3338 else
3339 return llvm::CallingConv::ARM_APCS;
3340}
3341
3342/// Return the calling convention that our ABI would like us to use
3343/// as the C calling convention.
3344llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
Daniel Dunbar020daa92009-09-12 01:00:39 +00003345 switch (getABIKind()) {
John McCall882987f2013-02-28 19:01:20 +00003346 case APCS: return llvm::CallingConv::ARM_APCS;
3347 case AAPCS: return llvm::CallingConv::ARM_AAPCS;
3348 case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
Daniel Dunbar020daa92009-09-12 01:00:39 +00003349 }
John McCall882987f2013-02-28 19:01:20 +00003350 llvm_unreachable("bad ABI kind");
3351}
3352
3353void ARMABIInfo::setRuntimeCC() {
3354 assert(getRuntimeCC() == llvm::CallingConv::C);
3355
3356 // Don't muddy up the IR with a ton of explicit annotations if
3357 // they'd just match what LLVM will infer from the triple.
3358 llvm::CallingConv::ID abiCC = getABIDefaultCC();
3359 if (abiCC != getLLVMDefaultCC())
3360 RuntimeCC = abiCC;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003361}
3362
Bob Wilsone826a2a2011-08-03 05:58:22 +00003363/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
3364/// aggregate. If HAMembers is non-null, the number of base elements
3365/// contained in the type is returned through it; this is used for the
3366/// recursive calls that check aggregate component types.
3367static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
3368 ASTContext &Context,
3369 uint64_t *HAMembers = 0) {
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00003370 uint64_t Members = 0;
Bob Wilsone826a2a2011-08-03 05:58:22 +00003371 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
3372 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
3373 return false;
3374 Members *= AT->getSize().getZExtValue();
3375 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
3376 const RecordDecl *RD = RT->getDecl();
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00003377 if (RD->hasFlexibleArrayMember())
Bob Wilsone826a2a2011-08-03 05:58:22 +00003378 return false;
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00003379
Bob Wilsone826a2a2011-08-03 05:58:22 +00003380 Members = 0;
3381 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3382 i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +00003383 const FieldDecl *FD = *i;
Bob Wilsone826a2a2011-08-03 05:58:22 +00003384 uint64_t FldMembers;
3385 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
3386 return false;
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00003387
3388 Members = (RD->isUnion() ?
3389 std::max(Members, FldMembers) : Members + FldMembers);
Bob Wilsone826a2a2011-08-03 05:58:22 +00003390 }
3391 } else {
3392 Members = 1;
3393 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
3394 Members = 2;
3395 Ty = CT->getElementType();
3396 }
3397
3398 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
3399 // double, or 64-bit or 128-bit vectors.
3400 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3401 if (BT->getKind() != BuiltinType::Float &&
Tim Northovereb752d42012-07-20 22:29:29 +00003402 BT->getKind() != BuiltinType::Double &&
3403 BT->getKind() != BuiltinType::LongDouble)
Bob Wilsone826a2a2011-08-03 05:58:22 +00003404 return false;
3405 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
3406 unsigned VecSize = Context.getTypeSize(VT);
3407 if (VecSize != 64 && VecSize != 128)
3408 return false;
3409 } else {
3410 return false;
3411 }
3412
3413 // The base type must be the same for all members. Vector types of the
3414 // same total size are treated as being equivalent here.
3415 const Type *TyPtr = Ty.getTypePtr();
3416 if (!Base)
3417 Base = TyPtr;
3418 if (Base != TyPtr &&
3419 (!Base->isVectorType() || !TyPtr->isVectorType() ||
3420 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
3421 return false;
3422 }
3423
3424 // Homogeneous Aggregates can have at most 4 members of the base type.
3425 if (HAMembers)
3426 *HAMembers = Members;
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00003427
3428 return (Members > 0 && Members <= 4);
Bob Wilsone826a2a2011-08-03 05:58:22 +00003429}
3430
Manman Renb505d332012-10-31 19:02:26 +00003431/// markAllocatedVFPs - update VFPRegs according to the alignment and
3432/// number of VFP registers (unit is S register) requested.
3433static void markAllocatedVFPs(int *VFPRegs, unsigned &AllocatedVFP,
3434 unsigned Alignment,
3435 unsigned NumRequired) {
3436 // Early Exit.
3437 if (AllocatedVFP >= 16)
3438 return;
3439 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
3440 // VFP registers of the appropriate type unallocated then the argument is
3441 // allocated to the lowest-numbered sequence of such registers.
3442 for (unsigned I = 0; I < 16; I += Alignment) {
3443 bool FoundSlot = true;
3444 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3445 if (J >= 16 || VFPRegs[J]) {
3446 FoundSlot = false;
3447 break;
3448 }
3449 if (FoundSlot) {
3450 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3451 VFPRegs[J] = 1;
3452 AllocatedVFP += NumRequired;
3453 return;
3454 }
3455 }
3456 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
3457 // unallocated are marked as unavailable.
3458 for (unsigned I = 0; I < 16; I++)
3459 VFPRegs[I] = 1;
3460 AllocatedVFP = 17; // We do not have enough VFP registers.
3461}
3462
3463ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, int *VFPRegs,
3464 unsigned &AllocatedVFP,
Amara Emerson9dc78782014-01-28 10:56:36 +00003465 bool &IsHA, bool isVariadic) const {
Manman Ren2a523d82012-10-30 23:21:41 +00003466 // We update number of allocated VFPs according to
3467 // 6.1.2.1 The following argument types are VFP CPRCs:
3468 // A single-precision floating-point type (including promoted
3469 // half-precision types); A double-precision floating-point type;
3470 // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
3471 // with a Base Type of a single- or double-precision floating-point type,
3472 // 64-bit containerized vectors or 128-bit containerized vectors with one
3473 // to four Elements.
3474
Manman Renfef9e312012-10-16 19:18:39 +00003475 // Handle illegal vector types here.
3476 if (isIllegalVectorType(Ty)) {
3477 uint64_t Size = getContext().getTypeSize(Ty);
3478 if (Size <= 32) {
3479 llvm::Type *ResType =
3480 llvm::Type::getInt32Ty(getVMContext());
3481 return ABIArgInfo::getDirect(ResType);
3482 }
3483 if (Size == 64) {
3484 llvm::Type *ResType = llvm::VectorType::get(
3485 llvm::Type::getInt32Ty(getVMContext()), 2);
Manman Renb505d332012-10-31 19:02:26 +00003486 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
Manman Renfef9e312012-10-16 19:18:39 +00003487 return ABIArgInfo::getDirect(ResType);
3488 }
3489 if (Size == 128) {
3490 llvm::Type *ResType = llvm::VectorType::get(
3491 llvm::Type::getInt32Ty(getVMContext()), 4);
Manman Renb505d332012-10-31 19:02:26 +00003492 markAllocatedVFPs(VFPRegs, AllocatedVFP, 4, 4);
Manman Renfef9e312012-10-16 19:18:39 +00003493 return ABIArgInfo::getDirect(ResType);
3494 }
3495 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3496 }
Manman Renb505d332012-10-31 19:02:26 +00003497 // Update VFPRegs for legal vector types.
Manman Ren2a523d82012-10-30 23:21:41 +00003498 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3499 uint64_t Size = getContext().getTypeSize(VT);
3500 // Size of a legal vector should be power of 2 and above 64.
Manman Renb505d332012-10-31 19:02:26 +00003501 markAllocatedVFPs(VFPRegs, AllocatedVFP, Size >= 128 ? 4 : 2, Size / 32);
Manman Ren2a523d82012-10-30 23:21:41 +00003502 }
Manman Renb505d332012-10-31 19:02:26 +00003503 // Update VFPRegs for floating point types.
Manman Ren2a523d82012-10-30 23:21:41 +00003504 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3505 if (BT->getKind() == BuiltinType::Half ||
3506 BT->getKind() == BuiltinType::Float)
Manman Renb505d332012-10-31 19:02:26 +00003507 markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, 1);
Manman Ren2a523d82012-10-30 23:21:41 +00003508 if (BT->getKind() == BuiltinType::Double ||
Manman Renb505d332012-10-31 19:02:26 +00003509 BT->getKind() == BuiltinType::LongDouble)
3510 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
Manman Ren2a523d82012-10-30 23:21:41 +00003511 }
Manman Renfef9e312012-10-16 19:18:39 +00003512
John McCalla1dee5302010-08-22 10:59:02 +00003513 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00003514 // Treat an enum type as its underlying type.
3515 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3516 Ty = EnumTy->getDecl()->getIntegerType();
3517
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00003518 return (Ty->isPromotableIntegerType() ?
3519 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00003520 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00003521
Mark Lacey3825e832013-10-06 01:33:34 +00003522 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Tim Northover1060eae2013-06-21 22:49:34 +00003523 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
3524
Daniel Dunbar09d33622009-09-14 21:54:03 +00003525 // Ignore empty records.
Chris Lattner458b2aa2010-07-29 02:16:43 +00003526 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar09d33622009-09-14 21:54:03 +00003527 return ABIArgInfo::getIgnore();
3528
Amara Emerson9dc78782014-01-28 10:56:36 +00003529 if (getABIKind() == ARMABIInfo::AAPCS_VFP && !isVariadic) {
Manman Ren2a523d82012-10-30 23:21:41 +00003530 // Homogeneous Aggregates need to be expanded when we can fit the aggregate
3531 // into VFP registers.
Bob Wilsone826a2a2011-08-03 05:58:22 +00003532 const Type *Base = 0;
Manman Ren2a523d82012-10-30 23:21:41 +00003533 uint64_t Members = 0;
3534 if (isHomogeneousAggregate(Ty, Base, getContext(), &Members)) {
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00003535 assert(Base && "Base class should be set for homogeneous aggregate");
Manman Ren2a523d82012-10-30 23:21:41 +00003536 // Base can be a floating-point or a vector.
3537 if (Base->isVectorType()) {
3538 // ElementSize is in number of floats.
3539 unsigned ElementSize = getContext().getTypeSize(Base) == 64 ? 2 : 4;
Manman Ren77b02382012-11-06 19:05:29 +00003540 markAllocatedVFPs(VFPRegs, AllocatedVFP, ElementSize,
3541 Members * ElementSize);
Manman Ren2a523d82012-10-30 23:21:41 +00003542 } else if (Base->isSpecificBuiltinType(BuiltinType::Float))
Manman Renb505d332012-10-31 19:02:26 +00003543 markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, Members);
Manman Ren2a523d82012-10-30 23:21:41 +00003544 else {
3545 assert(Base->isSpecificBuiltinType(BuiltinType::Double) ||
3546 Base->isSpecificBuiltinType(BuiltinType::LongDouble));
Manman Renb505d332012-10-31 19:02:26 +00003547 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, Members * 2);
Manman Ren2a523d82012-10-30 23:21:41 +00003548 }
3549 IsHA = true;
Bob Wilsone826a2a2011-08-03 05:58:22 +00003550 return ABIArgInfo::getExpand();
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00003551 }
Bob Wilsone826a2a2011-08-03 05:58:22 +00003552 }
3553
Manman Ren6c30e132012-08-13 21:23:55 +00003554 // Support byval for ARM.
Manman Ren77b02382012-11-06 19:05:29 +00003555 // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
3556 // most 8-byte. We realign the indirect argument if type alignment is bigger
3557 // than ABI alignment.
Manman Ren505d68f2012-11-05 22:42:46 +00003558 uint64_t ABIAlign = 4;
3559 uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8;
3560 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3561 getABIKind() == ARMABIInfo::AAPCS)
3562 ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
Manman Ren8cd99812012-11-06 04:58:01 +00003563 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
3564 return ABIArgInfo::getIndirect(0, /*ByVal=*/true,
Manman Ren77b02382012-11-06 19:05:29 +00003565 /*Realign=*/TyAlign > ABIAlign);
Eli Friedmane66abda2012-08-09 00:31:40 +00003566 }
3567
Daniel Dunbarb34b0802010-09-23 01:54:28 +00003568 // Otherwise, pass by coercing to a structure of the appropriate size.
Chris Lattner2192fe52011-07-18 04:24:23 +00003569 llvm::Type* ElemTy;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003570 unsigned SizeRegs;
Eli Friedmane66abda2012-08-09 00:31:40 +00003571 // FIXME: Try to match the types of the arguments more accurately where
3572 // we can.
3573 if (getContext().getTypeAlign(Ty) <= 32) {
Bob Wilson8e2b75d2011-08-01 23:39:04 +00003574 ElemTy = llvm::Type::getInt32Ty(getVMContext());
3575 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Manman Ren6fdb1582012-06-25 22:04:00 +00003576 } else {
Manman Ren6fdb1582012-06-25 22:04:00 +00003577 ElemTy = llvm::Type::getInt64Ty(getVMContext());
3578 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastingsf2752a32011-04-27 17:24:02 +00003579 }
Stuart Hastings4b214952011-04-28 18:16:06 +00003580
Chris Lattnera5f58b02011-07-09 17:41:47 +00003581 llvm::Type *STy =
Chris Lattner845511f2011-06-18 22:49:11 +00003582 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastings4b214952011-04-28 18:16:06 +00003583 return ABIArgInfo::getDirect(STy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003584}
3585
Chris Lattner458b2aa2010-07-29 02:16:43 +00003586static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar626f1d82009-09-13 08:03:58 +00003587 llvm::LLVMContext &VMContext) {
3588 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
3589 // is called integer-like if its size is less than or equal to one word, and
3590 // the offset of each of its addressable sub-fields is zero.
3591
3592 uint64_t Size = Context.getTypeSize(Ty);
3593
3594 // Check that the type fits in a word.
3595 if (Size > 32)
3596 return false;
3597
3598 // FIXME: Handle vector types!
3599 if (Ty->isVectorType())
3600 return false;
3601
Daniel Dunbard53bac72009-09-14 02:20:34 +00003602 // Float types are never treated as "integer like".
3603 if (Ty->isRealFloatingType())
3604 return false;
3605
Daniel Dunbar626f1d82009-09-13 08:03:58 +00003606 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00003607 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00003608 return true;
3609
Daniel Dunbar96ebba52010-02-01 23:31:26 +00003610 // Small complex integer types are "integer like".
3611 if (const ComplexType *CT = Ty->getAs<ComplexType>())
3612 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00003613
3614 // Single element and zero sized arrays should be allowed, by the definition
3615 // above, but they are not.
3616
3617 // Otherwise, it must be a record type.
3618 const RecordType *RT = Ty->getAs<RecordType>();
3619 if (!RT) return false;
3620
3621 // Ignore records with flexible arrays.
3622 const RecordDecl *RD = RT->getDecl();
3623 if (RD->hasFlexibleArrayMember())
3624 return false;
3625
3626 // Check that all sub-fields are at offset 0, and are themselves "integer
3627 // like".
3628 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3629
3630 bool HadField = false;
3631 unsigned idx = 0;
3632 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3633 i != e; ++i, ++idx) {
David Blaikie40ed2972012-06-06 20:45:41 +00003634 const FieldDecl *FD = *i;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00003635
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00003636 // Bit-fields are not addressable, we only need to verify they are "integer
3637 // like". We still have to disallow a subsequent non-bitfield, for example:
3638 // struct { int : 0; int x }
3639 // is non-integer like according to gcc.
3640 if (FD->isBitField()) {
3641 if (!RD->isUnion())
3642 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00003643
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00003644 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3645 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00003646
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00003647 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00003648 }
3649
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00003650 // Check if this field is at offset 0.
3651 if (Layout.getFieldOffset(idx) != 0)
3652 return false;
3653
Daniel Dunbar626f1d82009-09-13 08:03:58 +00003654 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3655 return false;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003656
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00003657 // Only allow at most one field in a structure. This doesn't match the
3658 // wording above, but follows gcc in situations with a field following an
3659 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00003660 if (!RD->isUnion()) {
3661 if (HadField)
3662 return false;
3663
3664 HadField = true;
3665 }
3666 }
3667
3668 return true;
3669}
3670
Amara Emerson9dc78782014-01-28 10:56:36 +00003671ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, bool isVariadic) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00003672 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003673 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00003674
Daniel Dunbar19964db2010-09-23 01:54:32 +00003675 // Large vector types should be returned via memory.
3676 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
3677 return ABIArgInfo::getIndirect(0);
3678
John McCalla1dee5302010-08-22 10:59:02 +00003679 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00003680 // Treat an enum type as its underlying type.
3681 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3682 RetTy = EnumTy->getDecl()->getIntegerType();
3683
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00003684 return (RetTy->isPromotableIntegerType() ?
3685 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00003686 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00003687
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00003688 // Structures with either a non-trivial destructor or a non-trivial
3689 // copy constructor are always indirect.
Mark Lacey3825e832013-10-06 01:33:34 +00003690 if (isRecordReturnIndirect(RetTy, getCXXABI()))
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00003691 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3692
Daniel Dunbar626f1d82009-09-13 08:03:58 +00003693 // Are we following APCS?
3694 if (getABIKind() == APCS) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00003695 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar626f1d82009-09-13 08:03:58 +00003696 return ABIArgInfo::getIgnore();
3697
Daniel Dunbareedf1512010-02-01 23:31:19 +00003698 // Complex types are all returned as packed integers.
3699 //
3700 // FIXME: Consider using 2 x vector types if the back end handles them
3701 // correctly.
3702 if (RetTy->isAnyComplexType())
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00003703 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +00003704 getContext().getTypeSize(RetTy)));
Daniel Dunbareedf1512010-02-01 23:31:19 +00003705
Daniel Dunbar626f1d82009-09-13 08:03:58 +00003706 // Integer like structures are returned in r0.
Chris Lattner458b2aa2010-07-29 02:16:43 +00003707 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00003708 // Return in the smallest viable integer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +00003709 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00003710 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00003711 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00003712 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00003713 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3714 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00003715 }
3716
3717 // Otherwise return in memory.
3718 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003719 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00003720
3721 // Otherwise this is an AAPCS variant.
3722
Chris Lattner458b2aa2010-07-29 02:16:43 +00003723 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar1ce72512009-09-14 00:56:55 +00003724 return ABIArgInfo::getIgnore();
3725
Bob Wilson1d9269a2011-11-02 04:51:36 +00003726 // Check for homogeneous aggregates with AAPCS-VFP.
Amara Emerson9dc78782014-01-28 10:56:36 +00003727 if (getABIKind() == AAPCS_VFP && !isVariadic) {
Bob Wilson1d9269a2011-11-02 04:51:36 +00003728 const Type *Base = 0;
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00003729 if (isHomogeneousAggregate(RetTy, Base, getContext())) {
3730 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson1d9269a2011-11-02 04:51:36 +00003731 // Homogeneous Aggregates are returned directly.
3732 return ABIArgInfo::getDirect();
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00003733 }
Bob Wilson1d9269a2011-11-02 04:51:36 +00003734 }
3735
Daniel Dunbar626f1d82009-09-13 08:03:58 +00003736 // Aggregates <= 4 bytes are returned in r0; other aggregates
3737 // are returned indirectly.
Chris Lattner458b2aa2010-07-29 02:16:43 +00003738 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00003739 if (Size <= 32) {
3740 // Return in the smallest viable integer type.
3741 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00003742 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00003743 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00003744 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3745 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00003746 }
3747
Daniel Dunbar626f1d82009-09-13 08:03:58 +00003748 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003749}
3750
Manman Renfef9e312012-10-16 19:18:39 +00003751/// isIllegalVector - check whether Ty is an illegal vector type.
3752bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
3753 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3754 // Check whether VT is legal.
3755 unsigned NumElements = VT->getNumElements();
3756 uint64_t Size = getContext().getTypeSize(VT);
3757 // NumElements should be power of 2.
3758 if ((NumElements & (NumElements - 1)) != 0)
3759 return true;
3760 // Size should be greater than 32 bits.
3761 return Size <= 32;
3762 }
3763 return false;
3764}
3765
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003766llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00003767 CodeGenFunction &CGF) const {
Chris Lattnerece04092012-02-07 00:39:47 +00003768 llvm::Type *BP = CGF.Int8PtrTy;
3769 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003770
3771 CGBuilderTy &Builder = CGF.Builder;
Chris Lattnerece04092012-02-07 00:39:47 +00003772 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003773 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Manman Rencca54d02012-10-16 19:01:37 +00003774
Tim Northover1711cc92013-06-21 23:05:33 +00003775 if (isEmptyRecord(getContext(), Ty, true)) {
3776 // These are ignored for parameter passing purposes.
3777 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3778 return Builder.CreateBitCast(Addr, PTy);
3779 }
3780
Manman Rencca54d02012-10-16 19:01:37 +00003781 uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8;
Rafael Espindola11d994b2011-08-02 22:33:37 +00003782 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
Manman Renfef9e312012-10-16 19:18:39 +00003783 bool IsIndirect = false;
Manman Rencca54d02012-10-16 19:01:37 +00003784
3785 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
3786 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
Manman Ren67effb92012-10-16 19:51:48 +00003787 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3788 getABIKind() == ARMABIInfo::AAPCS)
3789 TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
3790 else
3791 TyAlign = 4;
Manman Renfef9e312012-10-16 19:18:39 +00003792 // Use indirect if size of the illegal vector is bigger than 16 bytes.
3793 if (isIllegalVectorType(Ty) && Size > 16) {
3794 IsIndirect = true;
3795 Size = 4;
3796 TyAlign = 4;
3797 }
Manman Rencca54d02012-10-16 19:01:37 +00003798
3799 // Handle address alignment for ABI alignment > 4 bytes.
Rafael Espindola11d994b2011-08-02 22:33:37 +00003800 if (TyAlign > 4) {
3801 assert((TyAlign & (TyAlign - 1)) == 0 &&
3802 "Alignment is not power of 2!");
3803 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3804 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
3805 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
Manman Rencca54d02012-10-16 19:01:37 +00003806 Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align");
Rafael Espindola11d994b2011-08-02 22:33:37 +00003807 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003808
3809 uint64_t Offset =
Manman Rencca54d02012-10-16 19:01:37 +00003810 llvm::RoundUpToAlignment(Size, 4);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003811 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +00003812 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003813 "ap.next");
3814 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3815
Manman Renfef9e312012-10-16 19:18:39 +00003816 if (IsIndirect)
3817 Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP));
Manman Ren67effb92012-10-16 19:51:48 +00003818 else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) {
Manman Rencca54d02012-10-16 19:01:37 +00003819 // We can't directly cast ap.cur to pointer to a vector type, since ap.cur
3820 // may not be correctly aligned for the vector type. We create an aligned
3821 // temporary space and copy the content over from ap.cur to the temporary
3822 // space. This is necessary if the natural alignment of the type is greater
3823 // than the ABI alignment.
3824 llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
3825 CharUnits CharSize = getContext().getTypeSizeInChars(Ty);
3826 llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty),
3827 "var.align");
3828 llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
3829 llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy);
3830 Builder.CreateMemCpy(Dst, Src,
3831 llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()),
3832 TyAlign, false);
3833 Addr = AlignedTemp; //The content is in aligned location.
3834 }
3835 llvm::Type *PTy =
3836 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3837 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3838
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003839 return AddrTyped;
3840}
3841
Benjamin Kramer1cdb23d2012-10-20 13:02:06 +00003842namespace {
3843
Derek Schuffa2020962012-10-16 22:30:41 +00003844class NaClARMABIInfo : public ABIInfo {
3845 public:
3846 NaClARMABIInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3847 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, Kind) {}
3848 virtual void computeInfo(CGFunctionInfo &FI) const;
3849 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3850 CodeGenFunction &CGF) const;
3851 private:
3852 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
3853 ARMABIInfo NInfo; // Used for everything else.
3854};
3855
3856class NaClARMTargetCodeGenInfo : public TargetCodeGenInfo {
3857 public:
3858 NaClARMTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3859 : TargetCodeGenInfo(new NaClARMABIInfo(CGT, Kind)) {}
3860};
3861
Benjamin Kramer1cdb23d2012-10-20 13:02:06 +00003862}
3863
Derek Schuffa2020962012-10-16 22:30:41 +00003864void NaClARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
3865 if (FI.getASTCallingConvention() == CC_PnaclCall)
3866 PInfo.computeInfo(FI);
3867 else
3868 static_cast<const ABIInfo&>(NInfo).computeInfo(FI);
3869}
3870
3871llvm::Value *NaClARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3872 CodeGenFunction &CGF) const {
3873 // Always use the native convention; calling pnacl-style varargs functions
3874 // is unsupported.
3875 return static_cast<const ABIInfo&>(NInfo).EmitVAArg(VAListAddr, Ty, CGF);
3876}
3877
Chris Lattner0cf24192010-06-28 20:05:43 +00003878//===----------------------------------------------------------------------===//
Tim Northover9bb857a2013-01-31 12:13:10 +00003879// AArch64 ABI Implementation
3880//===----------------------------------------------------------------------===//
3881
3882namespace {
3883
3884class AArch64ABIInfo : public ABIInfo {
3885public:
3886 AArch64ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3887
3888private:
3889 // The AArch64 PCS is explicit about return types and argument types being
3890 // handled identically, so we don't need to draw a distinction between
3891 // Argument and Return classification.
3892 ABIArgInfo classifyGenericType(QualType Ty, int &FreeIntRegs,
3893 int &FreeVFPRegs) const;
3894
3895 ABIArgInfo tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded, bool IsInt,
3896 llvm::Type *DirectTy = 0) const;
3897
3898 virtual void computeInfo(CGFunctionInfo &FI) const;
3899
3900 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3901 CodeGenFunction &CGF) const;
3902};
3903
3904class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
3905public:
3906 AArch64TargetCodeGenInfo(CodeGenTypes &CGT)
3907 :TargetCodeGenInfo(new AArch64ABIInfo(CGT)) {}
3908
3909 const AArch64ABIInfo &getABIInfo() const {
3910 return static_cast<const AArch64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
3911 }
3912
3913 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3914 return 31;
3915 }
3916
3917 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3918 llvm::Value *Address) const {
3919 // 0-31 are x0-x30 and sp: 8 bytes each
3920 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
3921 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 31);
3922
3923 // 64-95 are v0-v31: 16 bytes each
3924 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
3925 AssignToArrayRange(CGF.Builder, Address, Sixteen8, 64, 95);
3926
3927 return false;
3928 }
3929
3930};
3931
3932}
3933
3934void AArch64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
3935 int FreeIntRegs = 8, FreeVFPRegs = 8;
3936
3937 FI.getReturnInfo() = classifyGenericType(FI.getReturnType(),
3938 FreeIntRegs, FreeVFPRegs);
3939
3940 FreeIntRegs = FreeVFPRegs = 8;
3941 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3942 it != ie; ++it) {
3943 it->info = classifyGenericType(it->type, FreeIntRegs, FreeVFPRegs);
3944
3945 }
3946}
3947
3948ABIArgInfo
3949AArch64ABIInfo::tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded,
3950 bool IsInt, llvm::Type *DirectTy) const {
3951 if (FreeRegs >= RegsNeeded) {
3952 FreeRegs -= RegsNeeded;
3953 return ABIArgInfo::getDirect(DirectTy);
3954 }
3955
3956 llvm::Type *Padding = 0;
3957
3958 // We need padding so that later arguments don't get filled in anyway. That
3959 // wouldn't happen if only ByVal arguments followed in the same category, but
3960 // a large structure will simply seem to be a pointer as far as LLVM is
3961 // concerned.
3962 if (FreeRegs > 0) {
3963 if (IsInt)
3964 Padding = llvm::Type::getInt64Ty(getVMContext());
3965 else
3966 Padding = llvm::Type::getFloatTy(getVMContext());
3967
3968 // Either [N x i64] or [N x float].
3969 Padding = llvm::ArrayType::get(Padding, FreeRegs);
3970 FreeRegs = 0;
3971 }
3972
3973 return ABIArgInfo::getIndirect(getContext().getTypeAlign(Ty) / 8,
3974 /*IsByVal=*/ true, /*Realign=*/ false,
3975 Padding);
3976}
3977
3978
3979ABIArgInfo AArch64ABIInfo::classifyGenericType(QualType Ty,
3980 int &FreeIntRegs,
3981 int &FreeVFPRegs) const {
3982 // Can only occurs for return, but harmless otherwise.
3983 if (Ty->isVoidType())
3984 return ABIArgInfo::getIgnore();
3985
3986 // Large vector types should be returned via memory. There's no such concept
3987 // in the ABI, but they'd be over 16 bytes anyway so no matter how they're
3988 // classified they'd go into memory (see B.3).
3989 if (Ty->isVectorType() && getContext().getTypeSize(Ty) > 128) {
3990 if (FreeIntRegs > 0)
3991 --FreeIntRegs;
3992 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3993 }
3994
3995 // All non-aggregate LLVM types have a concrete ABI representation so they can
3996 // be passed directly. After this block we're guaranteed to be in a
3997 // complicated case.
3998 if (!isAggregateTypeForABI(Ty)) {
3999 // Treat an enum type as its underlying type.
4000 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4001 Ty = EnumTy->getDecl()->getIntegerType();
4002
4003 if (Ty->isFloatingType() || Ty->isVectorType())
4004 return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ false);
4005
4006 assert(getContext().getTypeSize(Ty) <= 128 &&
4007 "unexpectedly large scalar type");
4008
4009 int RegsNeeded = getContext().getTypeSize(Ty) > 64 ? 2 : 1;
4010
4011 // If the type may need padding registers to ensure "alignment", we must be
4012 // careful when this is accounted for. Increasing the effective size covers
4013 // all cases.
4014 if (getContext().getTypeAlign(Ty) == 128)
4015 RegsNeeded += FreeIntRegs % 2 != 0;
4016
4017 return tryUseRegs(Ty, FreeIntRegs, RegsNeeded, /*IsInt=*/ true);
4018 }
4019
Mark Lacey3825e832013-10-06 01:33:34 +00004020 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00004021 if (FreeIntRegs > 0 && RAA == CGCXXABI::RAA_Indirect)
Tim Northover9bb857a2013-01-31 12:13:10 +00004022 --FreeIntRegs;
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00004023 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Tim Northover9bb857a2013-01-31 12:13:10 +00004024 }
4025
4026 if (isEmptyRecord(getContext(), Ty, true)) {
4027 if (!getContext().getLangOpts().CPlusPlus) {
4028 // Empty structs outside C++ mode are a GNU extension, so no ABI can
4029 // possibly tell us what to do. It turns out (I believe) that GCC ignores
4030 // the object for parameter-passsing purposes.
4031 return ABIArgInfo::getIgnore();
4032 }
4033
4034 // The combination of C++98 9p5 (sizeof(struct) != 0) and the pseudocode
4035 // description of va_arg in the PCS require that an empty struct does
4036 // actually occupy space for parameter-passing. I'm hoping for a
4037 // clarification giving an explicit paragraph to point to in future.
4038 return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ true,
4039 llvm::Type::getInt8Ty(getVMContext()));
4040 }
4041
4042 // Homogeneous vector aggregates get passed in registers or on the stack.
4043 const Type *Base = 0;
4044 uint64_t NumMembers = 0;
4045 if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)) {
4046 assert(Base && "Base class should be set for homogeneous aggregate");
4047 // Homogeneous aggregates are passed and returned directly.
4048 return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ NumMembers,
4049 /*IsInt=*/ false);
4050 }
4051
4052 uint64_t Size = getContext().getTypeSize(Ty);
4053 if (Size <= 128) {
4054 // Small structs can use the same direct type whether they're in registers
4055 // or on the stack.
4056 llvm::Type *BaseTy;
4057 unsigned NumBases;
4058 int SizeInRegs = (Size + 63) / 64;
4059
4060 if (getContext().getTypeAlign(Ty) == 128) {
4061 BaseTy = llvm::Type::getIntNTy(getVMContext(), 128);
4062 NumBases = 1;
4063
4064 // If the type may need padding registers to ensure "alignment", we must
4065 // be careful when this is accounted for. Increasing the effective size
4066 // covers all cases.
4067 SizeInRegs += FreeIntRegs % 2 != 0;
4068 } else {
4069 BaseTy = llvm::Type::getInt64Ty(getVMContext());
4070 NumBases = SizeInRegs;
4071 }
4072 llvm::Type *DirectTy = llvm::ArrayType::get(BaseTy, NumBases);
4073
4074 return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ SizeInRegs,
4075 /*IsInt=*/ true, DirectTy);
4076 }
4077
4078 // If the aggregate is > 16 bytes, it's passed and returned indirectly. In
4079 // LLVM terms the return uses an "sret" pointer, but that's handled elsewhere.
4080 --FreeIntRegs;
4081 return ABIArgInfo::getIndirect(0, /* byVal = */ false);
4082}
4083
4084llvm::Value *AArch64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4085 CodeGenFunction &CGF) const {
4086 // The AArch64 va_list type and handling is specified in the Procedure Call
4087 // Standard, section B.4:
4088 //
4089 // struct {
4090 // void *__stack;
4091 // void *__gr_top;
4092 // void *__vr_top;
4093 // int __gr_offs;
4094 // int __vr_offs;
4095 // };
4096
4097 assert(!CGF.CGM.getDataLayout().isBigEndian()
4098 && "va_arg not implemented for big-endian AArch64");
4099
4100 int FreeIntRegs = 8, FreeVFPRegs = 8;
4101 Ty = CGF.getContext().getCanonicalType(Ty);
4102 ABIArgInfo AI = classifyGenericType(Ty, FreeIntRegs, FreeVFPRegs);
4103
4104 llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
4105 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
4106 llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
4107 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
4108
4109 llvm::Value *reg_offs_p = 0, *reg_offs = 0;
4110 int reg_top_index;
4111 int RegSize;
4112 if (FreeIntRegs < 8) {
4113 assert(FreeVFPRegs == 8 && "Arguments never split between int & VFP regs");
4114 // 3 is the field number of __gr_offs
4115 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p");
4116 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
4117 reg_top_index = 1; // field number for __gr_top
4118 RegSize = 8 * (8 - FreeIntRegs);
4119 } else {
4120 assert(FreeVFPRegs < 8 && "Argument must go in VFP or int regs");
4121 // 4 is the field number of __vr_offs.
4122 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p");
4123 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
4124 reg_top_index = 2; // field number for __vr_top
4125 RegSize = 16 * (8 - FreeVFPRegs);
4126 }
4127
4128 //=======================================
4129 // Find out where argument was passed
4130 //=======================================
4131
4132 // If reg_offs >= 0 we're already using the stack for this type of
4133 // argument. We don't want to keep updating reg_offs (in case it overflows,
4134 // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
4135 // whatever they get).
4136 llvm::Value *UsingStack = 0;
4137 UsingStack = CGF.Builder.CreateICmpSGE(reg_offs,
4138 llvm::ConstantInt::get(CGF.Int32Ty, 0));
4139
4140 CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
4141
4142 // Otherwise, at least some kind of argument could go in these registers, the
4143 // quesiton is whether this particular type is too big.
4144 CGF.EmitBlock(MaybeRegBlock);
4145
4146 // Integer arguments may need to correct register alignment (for example a
4147 // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
4148 // align __gr_offs to calculate the potential address.
4149 if (FreeIntRegs < 8 && AI.isDirect() && getContext().getTypeAlign(Ty) > 64) {
4150 int Align = getContext().getTypeAlign(Ty) / 8;
4151
4152 reg_offs = CGF.Builder.CreateAdd(reg_offs,
4153 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
4154 "align_regoffs");
4155 reg_offs = CGF.Builder.CreateAnd(reg_offs,
4156 llvm::ConstantInt::get(CGF.Int32Ty, -Align),
4157 "aligned_regoffs");
4158 }
4159
4160 // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
4161 llvm::Value *NewOffset = 0;
4162 NewOffset = CGF.Builder.CreateAdd(reg_offs,
4163 llvm::ConstantInt::get(CGF.Int32Ty, RegSize),
4164 "new_reg_offs");
4165 CGF.Builder.CreateStore(NewOffset, reg_offs_p);
4166
4167 // Now we're in a position to decide whether this argument really was in
4168 // registers or not.
4169 llvm::Value *InRegs = 0;
4170 InRegs = CGF.Builder.CreateICmpSLE(NewOffset,
4171 llvm::ConstantInt::get(CGF.Int32Ty, 0),
4172 "inreg");
4173
4174 CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
4175
4176 //=======================================
4177 // Argument was in registers
4178 //=======================================
4179
4180 // Now we emit the code for if the argument was originally passed in
4181 // registers. First start the appropriate block:
4182 CGF.EmitBlock(InRegBlock);
4183
4184 llvm::Value *reg_top_p = 0, *reg_top = 0;
4185 reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p");
4186 reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
4187 llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs);
4188 llvm::Value *RegAddr = 0;
4189 llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
4190
4191 if (!AI.isDirect()) {
4192 // If it's been passed indirectly (actually a struct), whatever we find from
4193 // stored registers or on the stack will actually be a struct **.
4194 MemTy = llvm::PointerType::getUnqual(MemTy);
4195 }
4196
4197 const Type *Base = 0;
4198 uint64_t NumMembers;
4199 if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)
4200 && NumMembers > 1) {
4201 // Homogeneous aggregates passed in registers will have their elements split
4202 // and stored 16-bytes apart regardless of size (they're notionally in qN,
4203 // qN+1, ...). We reload and store into a temporary local variable
4204 // contiguously.
4205 assert(AI.isDirect() && "Homogeneous aggregates should be passed directly");
4206 llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
4207 llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
4208 llvm::Value *Tmp = CGF.CreateTempAlloca(HFATy);
4209
4210 for (unsigned i = 0; i < NumMembers; ++i) {
4211 llvm::Value *BaseOffset = llvm::ConstantInt::get(CGF.Int32Ty, 16 * i);
4212 llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset);
4213 LoadAddr = CGF.Builder.CreateBitCast(LoadAddr,
4214 llvm::PointerType::getUnqual(BaseTy));
4215 llvm::Value *StoreAddr = CGF.Builder.CreateStructGEP(Tmp, i);
4216
4217 llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
4218 CGF.Builder.CreateStore(Elem, StoreAddr);
4219 }
4220
4221 RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy);
4222 } else {
4223 // Otherwise the object is contiguous in memory
4224 RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy);
4225 }
4226
4227 CGF.EmitBranch(ContBlock);
4228
4229 //=======================================
4230 // Argument was on the stack
4231 //=======================================
4232 CGF.EmitBlock(OnStackBlock);
4233
4234 llvm::Value *stack_p = 0, *OnStackAddr = 0;
4235 stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p");
4236 OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack");
4237
4238 // Again, stack arguments may need realigmnent. In this case both integer and
4239 // floating-point ones might be affected.
4240 if (AI.isDirect() && getContext().getTypeAlign(Ty) > 64) {
4241 int Align = getContext().getTypeAlign(Ty) / 8;
4242
4243 OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty);
4244
4245 OnStackAddr = CGF.Builder.CreateAdd(OnStackAddr,
4246 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
4247 "align_stack");
4248 OnStackAddr = CGF.Builder.CreateAnd(OnStackAddr,
4249 llvm::ConstantInt::get(CGF.Int64Ty, -Align),
4250 "align_stack");
4251
4252 OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy);
4253 }
4254
4255 uint64_t StackSize;
4256 if (AI.isDirect())
4257 StackSize = getContext().getTypeSize(Ty) / 8;
4258 else
4259 StackSize = 8;
4260
4261 // All stack slots are 8 bytes
4262 StackSize = llvm::RoundUpToAlignment(StackSize, 8);
4263
4264 llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize);
4265 llvm::Value *NewStack = CGF.Builder.CreateGEP(OnStackAddr, StackSizeC,
4266 "new_stack");
4267
4268 // Write the new value of __stack for the next call to va_arg
4269 CGF.Builder.CreateStore(NewStack, stack_p);
4270
4271 OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy);
4272
4273 CGF.EmitBranch(ContBlock);
4274
4275 //=======================================
4276 // Tidy up
4277 //=======================================
4278 CGF.EmitBlock(ContBlock);
4279
4280 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr");
4281 ResAddr->addIncoming(RegAddr, InRegBlock);
4282 ResAddr->addIncoming(OnStackAddr, OnStackBlock);
4283
4284 if (AI.isDirect())
4285 return ResAddr;
4286
4287 return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr");
4288}
4289
4290//===----------------------------------------------------------------------===//
Justin Holewinski83e96682012-05-24 17:43:12 +00004291// NVPTX ABI Implementation
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00004292//===----------------------------------------------------------------------===//
4293
4294namespace {
4295
Justin Holewinski83e96682012-05-24 17:43:12 +00004296class NVPTXABIInfo : public ABIInfo {
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00004297public:
Justin Holewinski36837432013-03-30 14:38:24 +00004298 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00004299
4300 ABIArgInfo classifyReturnType(QualType RetTy) const;
4301 ABIArgInfo classifyArgumentType(QualType Ty) const;
4302
4303 virtual void computeInfo(CGFunctionInfo &FI) const;
4304 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4305 CodeGenFunction &CFG) const;
4306};
4307
Justin Holewinski83e96682012-05-24 17:43:12 +00004308class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00004309public:
Justin Holewinski83e96682012-05-24 17:43:12 +00004310 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
4311 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
Justin Holewinski38031972011-10-05 17:58:44 +00004312
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00004313 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4314 CodeGen::CodeGenModule &M) const;
Justin Holewinski36837432013-03-30 14:38:24 +00004315private:
4316 static void addKernelMetadata(llvm::Function *F);
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00004317};
4318
Justin Holewinski83e96682012-05-24 17:43:12 +00004319ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00004320 if (RetTy->isVoidType())
4321 return ABIArgInfo::getIgnore();
Justin Holewinskif9329ff2013-11-20 20:35:34 +00004322
4323 // note: this is different from default ABI
4324 if (!RetTy->isScalarType())
4325 return ABIArgInfo::getDirect();
4326
4327 // Treat an enum type as its underlying type.
4328 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4329 RetTy = EnumTy->getDecl()->getIntegerType();
4330
4331 return (RetTy->isPromotableIntegerType() ?
4332 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00004333}
4334
Justin Holewinski83e96682012-05-24 17:43:12 +00004335ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
Justin Holewinskif9329ff2013-11-20 20:35:34 +00004336 // Treat an enum type as its underlying type.
4337 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4338 Ty = EnumTy->getDecl()->getIntegerType();
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00004339
Justin Holewinskif9329ff2013-11-20 20:35:34 +00004340 return (Ty->isPromotableIntegerType() ?
4341 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00004342}
4343
Justin Holewinski83e96682012-05-24 17:43:12 +00004344void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00004345 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4346 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4347 it != ie; ++it)
4348 it->info = classifyArgumentType(it->type);
4349
4350 // Always honor user-specified calling convention.
4351 if (FI.getCallingConvention() != llvm::CallingConv::C)
4352 return;
4353
John McCall882987f2013-02-28 19:01:20 +00004354 FI.setEffectiveCallingConvention(getRuntimeCC());
4355}
4356
Justin Holewinski83e96682012-05-24 17:43:12 +00004357llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4358 CodeGenFunction &CFG) const {
4359 llvm_unreachable("NVPTX does not support varargs");
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00004360}
4361
Justin Holewinski83e96682012-05-24 17:43:12 +00004362void NVPTXTargetCodeGenInfo::
4363SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4364 CodeGen::CodeGenModule &M) const{
Justin Holewinski38031972011-10-05 17:58:44 +00004365 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4366 if (!FD) return;
4367
4368 llvm::Function *F = cast<llvm::Function>(GV);
4369
4370 // Perform special handling in OpenCL mode
David Blaikiebbafb8a2012-03-11 07:00:24 +00004371 if (M.getLangOpts().OpenCL) {
Justin Holewinski36837432013-03-30 14:38:24 +00004372 // Use OpenCL function attributes to check for kernel functions
Justin Holewinski38031972011-10-05 17:58:44 +00004373 // By default, all functions are device functions
Justin Holewinski38031972011-10-05 17:58:44 +00004374 if (FD->hasAttr<OpenCLKernelAttr>()) {
Justin Holewinski36837432013-03-30 14:38:24 +00004375 // OpenCL __kernel functions get kernel metadata
4376 addKernelMetadata(F);
Justin Holewinski38031972011-10-05 17:58:44 +00004377 // And kernel functions are not subject to inlining
Bill Wendling207f0532012-12-20 19:27:06 +00004378 F->addFnAttr(llvm::Attribute::NoInline);
Justin Holewinski38031972011-10-05 17:58:44 +00004379 }
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00004380 }
Justin Holewinski38031972011-10-05 17:58:44 +00004381
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00004382 // Perform special handling in CUDA mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004383 if (M.getLangOpts().CUDA) {
Justin Holewinski36837432013-03-30 14:38:24 +00004384 // CUDA __global__ functions get a kernel metadata entry. Since
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00004385 // __global__ functions cannot be called from the device, we do not
4386 // need to set the noinline attribute.
Aaron Ballman9ead1242013-12-19 02:39:40 +00004387 if (FD->hasAttr<CUDAGlobalAttr>())
Justin Holewinski36837432013-03-30 14:38:24 +00004388 addKernelMetadata(F);
Justin Holewinski38031972011-10-05 17:58:44 +00004389 }
4390}
4391
Justin Holewinski36837432013-03-30 14:38:24 +00004392void NVPTXTargetCodeGenInfo::addKernelMetadata(llvm::Function *F) {
4393 llvm::Module *M = F->getParent();
4394 llvm::LLVMContext &Ctx = M->getContext();
4395
4396 // Get "nvvm.annotations" metadata node
4397 llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
4398
4399 // Create !{<func-ref>, metadata !"kernel", i32 1} node
4400 llvm::SmallVector<llvm::Value *, 3> MDVals;
4401 MDVals.push_back(F);
4402 MDVals.push_back(llvm::MDString::get(Ctx, "kernel"));
4403 MDVals.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), 1));
4404
4405 // Append metadata to nvvm.annotations
4406 MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
4407}
4408
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00004409}
4410
4411//===----------------------------------------------------------------------===//
Ulrich Weigand47445072013-05-06 16:26:41 +00004412// SystemZ ABI Implementation
4413//===----------------------------------------------------------------------===//
4414
4415namespace {
4416
4417class SystemZABIInfo : public ABIInfo {
4418public:
4419 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
4420
4421 bool isPromotableIntegerType(QualType Ty) const;
4422 bool isCompoundType(QualType Ty) const;
4423 bool isFPArgumentType(QualType Ty) const;
4424
4425 ABIArgInfo classifyReturnType(QualType RetTy) const;
4426 ABIArgInfo classifyArgumentType(QualType ArgTy) const;
4427
4428 virtual void computeInfo(CGFunctionInfo &FI) const {
4429 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4430 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4431 it != ie; ++it)
4432 it->info = classifyArgumentType(it->type);
4433 }
4434
4435 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4436 CodeGenFunction &CGF) const;
4437};
4438
4439class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
4440public:
4441 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
4442 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
4443};
4444
4445}
4446
4447bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
4448 // Treat an enum type as its underlying type.
4449 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4450 Ty = EnumTy->getDecl()->getIntegerType();
4451
4452 // Promotable integer types are required to be promoted by the ABI.
4453 if (Ty->isPromotableIntegerType())
4454 return true;
4455
4456 // 32-bit values must also be promoted.
4457 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4458 switch (BT->getKind()) {
4459 case BuiltinType::Int:
4460 case BuiltinType::UInt:
4461 return true;
4462 default:
4463 return false;
4464 }
4465 return false;
4466}
4467
4468bool SystemZABIInfo::isCompoundType(QualType Ty) const {
4469 return Ty->isAnyComplexType() || isAggregateTypeForABI(Ty);
4470}
4471
4472bool SystemZABIInfo::isFPArgumentType(QualType Ty) const {
4473 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4474 switch (BT->getKind()) {
4475 case BuiltinType::Float:
4476 case BuiltinType::Double:
4477 return true;
4478 default:
4479 return false;
4480 }
4481
4482 if (const RecordType *RT = Ty->getAsStructureType()) {
4483 const RecordDecl *RD = RT->getDecl();
4484 bool Found = false;
4485
4486 // If this is a C++ record, check the bases first.
4487 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
4488 for (CXXRecordDecl::base_class_const_iterator I = CXXRD->bases_begin(),
4489 E = CXXRD->bases_end(); I != E; ++I) {
4490 QualType Base = I->getType();
4491
4492 // Empty bases don't affect things either way.
4493 if (isEmptyRecord(getContext(), Base, true))
4494 continue;
4495
4496 if (Found)
4497 return false;
4498 Found = isFPArgumentType(Base);
4499 if (!Found)
4500 return false;
4501 }
4502
4503 // Check the fields.
4504 for (RecordDecl::field_iterator I = RD->field_begin(),
4505 E = RD->field_end(); I != E; ++I) {
4506 const FieldDecl *FD = *I;
4507
4508 // Empty bitfields don't affect things either way.
4509 // Unlike isSingleElementStruct(), empty structure and array fields
4510 // do count. So do anonymous bitfields that aren't zero-sized.
4511 if (FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
4512 return true;
4513
4514 // Unlike isSingleElementStruct(), arrays do not count.
4515 // Nested isFPArgumentType structures still do though.
4516 if (Found)
4517 return false;
4518 Found = isFPArgumentType(FD->getType());
4519 if (!Found)
4520 return false;
4521 }
4522
4523 // Unlike isSingleElementStruct(), trailing padding is allowed.
4524 // An 8-byte aligned struct s { float f; } is passed as a double.
4525 return Found;
4526 }
4527
4528 return false;
4529}
4530
4531llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4532 CodeGenFunction &CGF) const {
4533 // Assume that va_list type is correct; should be pointer to LLVM type:
4534 // struct {
4535 // i64 __gpr;
4536 // i64 __fpr;
4537 // i8 *__overflow_arg_area;
4538 // i8 *__reg_save_area;
4539 // };
4540
4541 // Every argument occupies 8 bytes and is passed by preference in either
4542 // GPRs or FPRs.
4543 Ty = CGF.getContext().getCanonicalType(Ty);
4544 ABIArgInfo AI = classifyArgumentType(Ty);
4545 bool InFPRs = isFPArgumentType(Ty);
4546
4547 llvm::Type *APTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
4548 bool IsIndirect = AI.isIndirect();
4549 unsigned UnpaddedBitSize;
4550 if (IsIndirect) {
4551 APTy = llvm::PointerType::getUnqual(APTy);
4552 UnpaddedBitSize = 64;
4553 } else
4554 UnpaddedBitSize = getContext().getTypeSize(Ty);
4555 unsigned PaddedBitSize = 64;
4556 assert((UnpaddedBitSize <= PaddedBitSize) && "Invalid argument size.");
4557
4558 unsigned PaddedSize = PaddedBitSize / 8;
4559 unsigned Padding = (PaddedBitSize - UnpaddedBitSize) / 8;
4560
4561 unsigned MaxRegs, RegCountField, RegSaveIndex, RegPadding;
4562 if (InFPRs) {
4563 MaxRegs = 4; // Maximum of 4 FPR arguments
4564 RegCountField = 1; // __fpr
4565 RegSaveIndex = 16; // save offset for f0
4566 RegPadding = 0; // floats are passed in the high bits of an FPR
4567 } else {
4568 MaxRegs = 5; // Maximum of 5 GPR arguments
4569 RegCountField = 0; // __gpr
4570 RegSaveIndex = 2; // save offset for r2
4571 RegPadding = Padding; // values are passed in the low bits of a GPR
4572 }
4573
4574 llvm::Value *RegCountPtr =
4575 CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr");
4576 llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count");
4577 llvm::Type *IndexTy = RegCount->getType();
4578 llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs);
4579 llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV,
4580 "fits_in_regs");
4581
4582 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
4583 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
4584 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
4585 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
4586
4587 // Emit code to load the value if it was passed in registers.
4588 CGF.EmitBlock(InRegBlock);
4589
4590 // Work out the address of an argument register.
4591 llvm::Value *PaddedSizeV = llvm::ConstantInt::get(IndexTy, PaddedSize);
4592 llvm::Value *ScaledRegCount =
4593 CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count");
4594 llvm::Value *RegBase =
4595 llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize + RegPadding);
4596 llvm::Value *RegOffset =
4597 CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset");
4598 llvm::Value *RegSaveAreaPtr =
4599 CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr");
4600 llvm::Value *RegSaveArea =
4601 CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area");
4602 llvm::Value *RawRegAddr =
4603 CGF.Builder.CreateGEP(RegSaveArea, RegOffset, "raw_reg_addr");
4604 llvm::Value *RegAddr =
4605 CGF.Builder.CreateBitCast(RawRegAddr, APTy, "reg_addr");
4606
4607 // Update the register count
4608 llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1);
4609 llvm::Value *NewRegCount =
4610 CGF.Builder.CreateAdd(RegCount, One, "reg_count");
4611 CGF.Builder.CreateStore(NewRegCount, RegCountPtr);
4612 CGF.EmitBranch(ContBlock);
4613
4614 // Emit code to load the value if it was passed in memory.
4615 CGF.EmitBlock(InMemBlock);
4616
4617 // Work out the address of a stack argument.
4618 llvm::Value *OverflowArgAreaPtr =
4619 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr");
4620 llvm::Value *OverflowArgArea =
4621 CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area");
4622 llvm::Value *PaddingV = llvm::ConstantInt::get(IndexTy, Padding);
4623 llvm::Value *RawMemAddr =
4624 CGF.Builder.CreateGEP(OverflowArgArea, PaddingV, "raw_mem_addr");
4625 llvm::Value *MemAddr =
4626 CGF.Builder.CreateBitCast(RawMemAddr, APTy, "mem_addr");
4627
4628 // Update overflow_arg_area_ptr pointer
4629 llvm::Value *NewOverflowArgArea =
4630 CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area");
4631 CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
4632 CGF.EmitBranch(ContBlock);
4633
4634 // Return the appropriate result.
4635 CGF.EmitBlock(ContBlock);
4636 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(APTy, 2, "va_arg.addr");
4637 ResAddr->addIncoming(RegAddr, InRegBlock);
4638 ResAddr->addIncoming(MemAddr, InMemBlock);
4639
4640 if (IsIndirect)
4641 return CGF.Builder.CreateLoad(ResAddr, "indirect_arg");
4642
4643 return ResAddr;
4644}
4645
John McCall1fe2a8c2013-06-18 02:46:29 +00004646bool X86_32TargetCodeGenInfo::isStructReturnInRegABI(
4647 const llvm::Triple &Triple, const CodeGenOptions &Opts) {
4648 assert(Triple.getArch() == llvm::Triple::x86);
4649
4650 switch (Opts.getStructReturnConvention()) {
4651 case CodeGenOptions::SRCK_Default:
4652 break;
4653 case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return
4654 return false;
4655 case CodeGenOptions::SRCK_InRegs: // -freg-struct-return
4656 return true;
4657 }
4658
4659 if (Triple.isOSDarwin())
4660 return true;
4661
4662 switch (Triple.getOS()) {
4663 case llvm::Triple::Cygwin:
4664 case llvm::Triple::MinGW32:
4665 case llvm::Triple::AuroraUX:
4666 case llvm::Triple::DragonFly:
4667 case llvm::Triple::FreeBSD:
4668 case llvm::Triple::OpenBSD:
4669 case llvm::Triple::Bitrig:
4670 case llvm::Triple::Win32:
4671 return true;
4672 default:
4673 return false;
4674 }
4675}
Ulrich Weigand47445072013-05-06 16:26:41 +00004676
4677ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
4678 if (RetTy->isVoidType())
4679 return ABIArgInfo::getIgnore();
4680 if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64)
4681 return ABIArgInfo::getIndirect(0);
4682 return (isPromotableIntegerType(RetTy) ?
4683 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4684}
4685
4686ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
4687 // Handle the generic C++ ABI.
Mark Lacey3825e832013-10-06 01:33:34 +00004688 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Ulrich Weigand47445072013-05-06 16:26:41 +00004689 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
4690
4691 // Integers and enums are extended to full register width.
4692 if (isPromotableIntegerType(Ty))
4693 return ABIArgInfo::getExtend();
4694
4695 // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly.
4696 uint64_t Size = getContext().getTypeSize(Ty);
4697 if (Size != 8 && Size != 16 && Size != 32 && Size != 64)
Richard Sandifordcdd86882013-12-04 09:59:57 +00004698 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Ulrich Weigand47445072013-05-06 16:26:41 +00004699
4700 // Handle small structures.
4701 if (const RecordType *RT = Ty->getAs<RecordType>()) {
4702 // Structures with flexible arrays have variable length, so really
4703 // fail the size test above.
4704 const RecordDecl *RD = RT->getDecl();
4705 if (RD->hasFlexibleArrayMember())
Richard Sandifordcdd86882013-12-04 09:59:57 +00004706 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Ulrich Weigand47445072013-05-06 16:26:41 +00004707
4708 // The structure is passed as an unextended integer, a float, or a double.
4709 llvm::Type *PassTy;
4710 if (isFPArgumentType(Ty)) {
4711 assert(Size == 32 || Size == 64);
4712 if (Size == 32)
4713 PassTy = llvm::Type::getFloatTy(getVMContext());
4714 else
4715 PassTy = llvm::Type::getDoubleTy(getVMContext());
4716 } else
4717 PassTy = llvm::IntegerType::get(getVMContext(), Size);
4718 return ABIArgInfo::getDirect(PassTy);
4719 }
4720
4721 // Non-structure compounds are passed indirectly.
4722 if (isCompoundType(Ty))
Richard Sandifordcdd86882013-12-04 09:59:57 +00004723 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Ulrich Weigand47445072013-05-06 16:26:41 +00004724
4725 return ABIArgInfo::getDirect(0);
4726}
4727
4728//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004729// MSP430 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00004730//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004731
4732namespace {
4733
4734class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
4735public:
Chris Lattner2b037972010-07-29 02:01:43 +00004736 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
4737 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004738 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4739 CodeGen::CodeGenModule &M) const;
4740};
4741
4742}
4743
4744void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4745 llvm::GlobalValue *GV,
4746 CodeGen::CodeGenModule &M) const {
4747 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4748 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
4749 // Handle 'interrupt' attribute:
4750 llvm::Function *F = cast<llvm::Function>(GV);
4751
4752 // Step 1: Set ISR calling convention.
4753 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
4754
4755 // Step 2: Add attributes goodness.
Bill Wendling207f0532012-12-20 19:27:06 +00004756 F->addFnAttr(llvm::Attribute::NoInline);
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004757
4758 // Step 3: Emit ISR vector alias.
Anton Korobeynikovc5a7f922012-11-26 18:59:10 +00004759 unsigned Num = attr->getNumber() / 2;
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004760 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Anton Korobeynikovc5a7f922012-11-26 18:59:10 +00004761 "__isr_" + Twine(Num),
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004762 GV, &M.getModule());
4763 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00004764 }
4765}
4766
Chris Lattner0cf24192010-06-28 20:05:43 +00004767//===----------------------------------------------------------------------===//
John McCall943fae92010-05-27 06:19:26 +00004768// MIPS ABI Implementation. This works for both little-endian and
4769// big-endian variants.
Chris Lattner0cf24192010-06-28 20:05:43 +00004770//===----------------------------------------------------------------------===//
4771
John McCall943fae92010-05-27 06:19:26 +00004772namespace {
Akira Hatanakab579fe52011-06-02 00:09:17 +00004773class MipsABIInfo : public ABIInfo {
Akira Hatanaka14378522011-11-02 23:14:57 +00004774 bool IsO32;
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00004775 unsigned MinABIStackAlignInBytes, StackAlignInBytes;
4776 void CoerceToIntArgs(uint64_t TySize,
Craig Topper5603df42013-07-05 19:34:19 +00004777 SmallVectorImpl<llvm::Type *> &ArgList) const;
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00004778 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00004779 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanaka1632af62012-01-09 19:31:25 +00004780 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanakab579fe52011-06-02 00:09:17 +00004781public:
Akira Hatanakac4baedd2013-11-11 22:10:46 +00004782 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00004783 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
Akira Hatanakac4baedd2013-11-11 22:10:46 +00004784 StackAlignInBytes(IsO32 ? 8 : 16) {}
Akira Hatanakab579fe52011-06-02 00:09:17 +00004785
4786 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00004787 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Akira Hatanakab579fe52011-06-02 00:09:17 +00004788 virtual void computeInfo(CGFunctionInfo &FI) const;
4789 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4790 CodeGenFunction &CGF) const;
4791};
4792
John McCall943fae92010-05-27 06:19:26 +00004793class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanaka0486db02011-09-20 18:23:28 +00004794 unsigned SizeOfUnwindException;
John McCall943fae92010-05-27 06:19:26 +00004795public:
Akira Hatanakac4baedd2013-11-11 22:10:46 +00004796 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
4797 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
Akira Hatanaka14378522011-11-02 23:14:57 +00004798 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCall943fae92010-05-27 06:19:26 +00004799
4800 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
4801 return 29;
4802 }
4803
Reed Kotler373feca2013-01-16 17:10:28 +00004804 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4805 CodeGen::CodeGenModule &CGM) const {
Reed Kotler3d5966f2013-03-13 20:40:30 +00004806 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4807 if (!FD) return;
Rafael Espindolaa0851a22013-03-19 14:32:23 +00004808 llvm::Function *Fn = cast<llvm::Function>(GV);
Reed Kotler3d5966f2013-03-13 20:40:30 +00004809 if (FD->hasAttr<Mips16Attr>()) {
4810 Fn->addFnAttr("mips16");
4811 }
4812 else if (FD->hasAttr<NoMips16Attr>()) {
4813 Fn->addFnAttr("nomips16");
4814 }
Reed Kotler373feca2013-01-16 17:10:28 +00004815 }
Reed Kotler3d5966f2013-03-13 20:40:30 +00004816
John McCall943fae92010-05-27 06:19:26 +00004817 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00004818 llvm::Value *Address) const;
John McCall3480ef22011-08-30 01:42:09 +00004819
4820 unsigned getSizeOfUnwindException() const {
Akira Hatanaka0486db02011-09-20 18:23:28 +00004821 return SizeOfUnwindException;
John McCall3480ef22011-08-30 01:42:09 +00004822 }
John McCall943fae92010-05-27 06:19:26 +00004823};
4824}
4825
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00004826void MipsABIInfo::CoerceToIntArgs(uint64_t TySize,
Craig Topper5603df42013-07-05 19:34:19 +00004827 SmallVectorImpl<llvm::Type *> &ArgList) const {
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00004828 llvm::IntegerType *IntTy =
4829 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00004830
4831 // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
4832 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
4833 ArgList.push_back(IntTy);
4834
4835 // If necessary, add one more integer type to ArgList.
4836 unsigned R = TySize % (MinABIStackAlignInBytes * 8);
4837
4838 if (R)
4839 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00004840}
4841
Akira Hatanaka101f70d2011-11-02 23:54:49 +00004842// In N32/64, an aligned double precision floating point field is passed in
4843// a register.
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00004844llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00004845 SmallVector<llvm::Type*, 8> ArgList, IntArgList;
4846
4847 if (IsO32) {
4848 CoerceToIntArgs(TySize, ArgList);
4849 return llvm::StructType::get(getVMContext(), ArgList);
4850 }
Akira Hatanaka101f70d2011-11-02 23:54:49 +00004851
Akira Hatanaka02e13e52012-01-12 00:52:17 +00004852 if (Ty->isComplexType())
4853 return CGT.ConvertType(Ty);
Akira Hatanaka79f04612012-01-10 23:12:19 +00004854
Akira Hatanaka4984f5d2012-02-09 19:54:16 +00004855 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanaka101f70d2011-11-02 23:54:49 +00004856
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00004857 // Unions/vectors are passed in integer registers.
4858 if (!RT || !RT->isStructureOrClassType()) {
4859 CoerceToIntArgs(TySize, ArgList);
4860 return llvm::StructType::get(getVMContext(), ArgList);
4861 }
Akira Hatanaka101f70d2011-11-02 23:54:49 +00004862
4863 const RecordDecl *RD = RT->getDecl();
4864 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00004865 assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
Akira Hatanaka101f70d2011-11-02 23:54:49 +00004866
Akira Hatanaka101f70d2011-11-02 23:54:49 +00004867 uint64_t LastOffset = 0;
4868 unsigned idx = 0;
4869 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
4870
Akira Hatanaka4984f5d2012-02-09 19:54:16 +00004871 // Iterate over fields in the struct/class and check if there are any aligned
4872 // double fields.
Akira Hatanaka101f70d2011-11-02 23:54:49 +00004873 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
4874 i != e; ++i, ++idx) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00004875 const QualType Ty = i->getType();
Akira Hatanaka101f70d2011-11-02 23:54:49 +00004876 const BuiltinType *BT = Ty->getAs<BuiltinType>();
4877
4878 if (!BT || BT->getKind() != BuiltinType::Double)
4879 continue;
4880
4881 uint64_t Offset = Layout.getFieldOffset(idx);
4882 if (Offset % 64) // Ignore doubles that are not aligned.
4883 continue;
4884
4885 // Add ((Offset - LastOffset) / 64) args of type i64.
4886 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
4887 ArgList.push_back(I64);
4888
4889 // Add double type.
4890 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
4891 LastOffset = Offset + 64;
4892 }
4893
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00004894 CoerceToIntArgs(TySize - LastOffset, IntArgList);
4895 ArgList.append(IntArgList.begin(), IntArgList.end());
Akira Hatanaka101f70d2011-11-02 23:54:49 +00004896
4897 return llvm::StructType::get(getVMContext(), ArgList);
4898}
4899
Akira Hatanakaddd66342013-10-29 18:41:15 +00004900llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset,
4901 uint64_t Offset) const {
4902 if (OrigOffset + MinABIStackAlignInBytes > Offset)
4903 return 0;
Akira Hatanaka1632af62012-01-09 19:31:25 +00004904
Akira Hatanakaddd66342013-10-29 18:41:15 +00004905 return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8);
Akira Hatanaka1632af62012-01-09 19:31:25 +00004906}
Akira Hatanaka21ee88c2012-01-10 22:44:52 +00004907
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00004908ABIArgInfo
4909MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Akira Hatanaka1632af62012-01-09 19:31:25 +00004910 uint64_t OrigOffset = Offset;
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00004911 uint64_t TySize = getContext().getTypeSize(Ty);
Akira Hatanaka1632af62012-01-09 19:31:25 +00004912 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00004913
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00004914 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
4915 (uint64_t)StackAlignInBytes);
Akira Hatanakaddd66342013-10-29 18:41:15 +00004916 unsigned CurrOffset = llvm::RoundUpToAlignment(Offset, Align);
4917 Offset = CurrOffset + llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
Akira Hatanaka1632af62012-01-09 19:31:25 +00004918
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00004919 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
Akira Hatanakab579fe52011-06-02 00:09:17 +00004920 // Ignore empty aggregates.
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00004921 if (TySize == 0)
Akira Hatanakab579fe52011-06-02 00:09:17 +00004922 return ABIArgInfo::getIgnore();
4923
Mark Lacey3825e832013-10-06 01:33:34 +00004924 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00004925 Offset = OrigOffset + MinABIStackAlignInBytes;
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00004926 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00004927 }
Akira Hatanakadf425db2011-08-01 18:09:58 +00004928
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00004929 // If we have reached here, aggregates are passed directly by coercing to
4930 // another structure type. Padding is inserted if the offset of the
4931 // aggregate is unaligned.
4932 return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
Akira Hatanakaddd66342013-10-29 18:41:15 +00004933 getPaddingType(OrigOffset, CurrOffset));
Akira Hatanakab579fe52011-06-02 00:09:17 +00004934 }
4935
4936 // Treat an enum type as its underlying type.
4937 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4938 Ty = EnumTy->getDecl()->getIntegerType();
4939
Akira Hatanaka1632af62012-01-09 19:31:25 +00004940 if (Ty->isPromotableIntegerType())
4941 return ABIArgInfo::getExtend();
4942
Akira Hatanakaddd66342013-10-29 18:41:15 +00004943 return ABIArgInfo::getDirect(
4944 0, 0, IsO32 ? 0 : getPaddingType(OrigOffset, CurrOffset));
Akira Hatanakab579fe52011-06-02 00:09:17 +00004945}
4946
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00004947llvm::Type*
4948MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakab6f74432012-02-09 18:49:26 +00004949 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00004950 SmallVector<llvm::Type*, 8> RTList;
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00004951
Akira Hatanakab6f74432012-02-09 18:49:26 +00004952 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00004953 const RecordDecl *RD = RT->getDecl();
Akira Hatanakab6f74432012-02-09 18:49:26 +00004954 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
4955 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00004956
Akira Hatanakab6f74432012-02-09 18:49:26 +00004957 // N32/64 returns struct/classes in floating point registers if the
4958 // following conditions are met:
4959 // 1. The size of the struct/class is no larger than 128-bit.
4960 // 2. The struct/class has one or two fields all of which are floating
4961 // point types.
4962 // 3. The offset of the first field is zero (this follows what gcc does).
4963 //
4964 // Any other composite results are returned in integer registers.
4965 //
4966 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
4967 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
4968 for (; b != e; ++b) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00004969 const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00004970
Akira Hatanakab6f74432012-02-09 18:49:26 +00004971 if (!BT || !BT->isFloatingPoint())
4972 break;
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00004973
David Blaikie2d7c57e2012-04-30 02:36:29 +00004974 RTList.push_back(CGT.ConvertType(b->getType()));
Akira Hatanakab6f74432012-02-09 18:49:26 +00004975 }
4976
4977 if (b == e)
4978 return llvm::StructType::get(getVMContext(), RTList,
4979 RD->hasAttr<PackedAttr>());
4980
4981 RTList.clear();
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00004982 }
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00004983 }
4984
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00004985 CoerceToIntArgs(Size, RTList);
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00004986 return llvm::StructType::get(getVMContext(), RTList);
4987}
4988
Akira Hatanakab579fe52011-06-02 00:09:17 +00004989ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanaka60f5fe62012-01-23 23:18:57 +00004990 uint64_t Size = getContext().getTypeSize(RetTy);
4991
4992 if (RetTy->isVoidType() || Size == 0)
Akira Hatanakab579fe52011-06-02 00:09:17 +00004993 return ABIArgInfo::getIgnore();
4994
Akira Hatanakac37eddf2012-05-11 21:01:17 +00004995 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
Mark Lacey3825e832013-10-06 01:33:34 +00004996 if (isRecordReturnIndirect(RetTy, getCXXABI()))
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00004997 return ABIArgInfo::getIndirect(0);
4998
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00004999 if (Size <= 128) {
5000 if (RetTy->isAnyComplexType())
5001 return ABIArgInfo::getDirect();
5002
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00005003 // O32 returns integer vectors in registers.
5004 if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())
5005 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
5006
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00005007 if (!IsO32)
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00005008 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
5009 }
Akira Hatanakab579fe52011-06-02 00:09:17 +00005010
5011 return ABIArgInfo::getIndirect(0);
5012 }
5013
5014 // Treat an enum type as its underlying type.
5015 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5016 RetTy = EnumTy->getDecl()->getIntegerType();
5017
5018 return (RetTy->isPromotableIntegerType() ?
5019 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
5020}
5021
5022void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanaka32604a92012-01-12 01:10:09 +00005023 ABIArgInfo &RetInfo = FI.getReturnInfo();
5024 RetInfo = classifyReturnType(FI.getReturnType());
5025
5026 // Check if a pointer to an aggregate is passed as a hidden argument.
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00005027 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
Akira Hatanaka32604a92012-01-12 01:10:09 +00005028
Akira Hatanakab579fe52011-06-02 00:09:17 +00005029 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
5030 it != ie; ++it)
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00005031 it->info = classifyArgumentType(it->type, Offset);
Akira Hatanakab579fe52011-06-02 00:09:17 +00005032}
5033
5034llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5035 CodeGenFunction &CGF) const {
Chris Lattnerece04092012-02-07 00:39:47 +00005036 llvm::Type *BP = CGF.Int8PtrTy;
5037 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Akira Hatanakafb1d9f32011-08-01 20:48:01 +00005038
5039 CGBuilderTy &Builder = CGF.Builder;
5040 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
5041 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Akira Hatanaka37715282012-01-23 23:59:52 +00005042 int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
Akira Hatanakafb1d9f32011-08-01 20:48:01 +00005043 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
5044 llvm::Value *AddrTyped;
John McCallc8e01702013-04-16 22:48:15 +00005045 unsigned PtrWidth = getTarget().getPointerWidth(0);
Akira Hatanaka37715282012-01-23 23:59:52 +00005046 llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
Akira Hatanakafb1d9f32011-08-01 20:48:01 +00005047
5048 if (TypeAlign > MinABIStackAlignInBytes) {
Akira Hatanaka37715282012-01-23 23:59:52 +00005049 llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
5050 llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
5051 llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
5052 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
Akira Hatanakafb1d9f32011-08-01 20:48:01 +00005053 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
5054 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
5055 }
5056 else
5057 AddrTyped = Builder.CreateBitCast(Addr, PTy);
5058
5059 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka37715282012-01-23 23:59:52 +00005060 TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakafb1d9f32011-08-01 20:48:01 +00005061 uint64_t Offset =
5062 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
5063 llvm::Value *NextAddr =
Akira Hatanaka37715282012-01-23 23:59:52 +00005064 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
Akira Hatanakafb1d9f32011-08-01 20:48:01 +00005065 "ap.next");
5066 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
5067
5068 return AddrTyped;
Akira Hatanakab579fe52011-06-02 00:09:17 +00005069}
5070
John McCall943fae92010-05-27 06:19:26 +00005071bool
5072MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
5073 llvm::Value *Address) const {
5074 // This information comes from gcc's implementation, which seems to
5075 // as canonical as it gets.
5076
John McCall943fae92010-05-27 06:19:26 +00005077 // Everything on MIPS is 4 bytes. Double-precision FP registers
5078 // are aliased to pairs of single-precision FP registers.
Chris Lattnerece04092012-02-07 00:39:47 +00005079 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCall943fae92010-05-27 06:19:26 +00005080
5081 // 0-31 are the general purpose registers, $0 - $31.
5082 // 32-63 are the floating-point registers, $f0 - $f31.
5083 // 64 and 65 are the multiply/divide registers, $hi and $lo.
5084 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattnerece04092012-02-07 00:39:47 +00005085 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCall943fae92010-05-27 06:19:26 +00005086
5087 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
5088 // They are one bit wide and ignored here.
5089
5090 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
5091 // (coprocessor 1 is the FP unit)
5092 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
5093 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
5094 // 176-181 are the DSP accumulator registers.
Chris Lattnerece04092012-02-07 00:39:47 +00005095 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCall943fae92010-05-27 06:19:26 +00005096 return false;
5097}
5098
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00005099//===----------------------------------------------------------------------===//
5100// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
5101// Currently subclassed only to implement custom OpenCL C function attribute
5102// handling.
5103//===----------------------------------------------------------------------===//
5104
5105namespace {
5106
5107class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
5108public:
5109 TCETargetCodeGenInfo(CodeGenTypes &CGT)
5110 : DefaultTargetCodeGenInfo(CGT) {}
5111
5112 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
5113 CodeGen::CodeGenModule &M) const;
5114};
5115
5116void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
5117 llvm::GlobalValue *GV,
5118 CodeGen::CodeGenModule &M) const {
5119 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
5120 if (!FD) return;
5121
5122 llvm::Function *F = cast<llvm::Function>(GV);
5123
David Blaikiebbafb8a2012-03-11 07:00:24 +00005124 if (M.getLangOpts().OpenCL) {
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00005125 if (FD->hasAttr<OpenCLKernelAttr>()) {
5126 // OpenCL C Kernel functions are not subject to inlining
Bill Wendling207f0532012-12-20 19:27:06 +00005127 F->addFnAttr(llvm::Attribute::NoInline);
Aaron Ballman36a18ff2013-12-19 13:16:35 +00005128 const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>();
5129 if (Attr) {
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00005130 // Convert the reqd_work_group_size() attributes to metadata.
5131 llvm::LLVMContext &Context = F->getContext();
5132 llvm::NamedMDNode *OpenCLMetadata =
5133 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
5134
5135 SmallVector<llvm::Value*, 5> Operands;
5136 Operands.push_back(F);
5137
Chris Lattnerece04092012-02-07 00:39:47 +00005138 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
Aaron Ballman36a18ff2013-12-19 13:16:35 +00005139 llvm::APInt(32, Attr->getXDim())));
Chris Lattnerece04092012-02-07 00:39:47 +00005140 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
Aaron Ballman36a18ff2013-12-19 13:16:35 +00005141 llvm::APInt(32, Attr->getYDim())));
Chris Lattnerece04092012-02-07 00:39:47 +00005142 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
Aaron Ballman36a18ff2013-12-19 13:16:35 +00005143 llvm::APInt(32, Attr->getZDim())));
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00005144
5145 // Add a boolean constant operand for "required" (true) or "hint" (false)
5146 // for implementing the work_group_size_hint attr later. Currently
5147 // always true as the hint is not yet implemented.
Chris Lattnerece04092012-02-07 00:39:47 +00005148 Operands.push_back(llvm::ConstantInt::getTrue(Context));
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00005149 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
5150 }
5151 }
5152 }
5153}
5154
5155}
John McCall943fae92010-05-27 06:19:26 +00005156
Tony Linthicum76329bf2011-12-12 21:14:55 +00005157//===----------------------------------------------------------------------===//
5158// Hexagon ABI Implementation
5159//===----------------------------------------------------------------------===//
5160
5161namespace {
5162
5163class HexagonABIInfo : public ABIInfo {
5164
5165
5166public:
5167 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
5168
5169private:
5170
5171 ABIArgInfo classifyReturnType(QualType RetTy) const;
5172 ABIArgInfo classifyArgumentType(QualType RetTy) const;
5173
5174 virtual void computeInfo(CGFunctionInfo &FI) const;
5175
5176 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5177 CodeGenFunction &CGF) const;
5178};
5179
5180class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
5181public:
5182 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
5183 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
5184
5185 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
5186 return 29;
5187 }
5188};
5189
5190}
5191
5192void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
5193 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
5194 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
5195 it != ie; ++it)
5196 it->info = classifyArgumentType(it->type);
5197}
5198
5199ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
5200 if (!isAggregateTypeForABI(Ty)) {
5201 // Treat an enum type as its underlying type.
5202 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5203 Ty = EnumTy->getDecl()->getIntegerType();
5204
5205 return (Ty->isPromotableIntegerType() ?
5206 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
5207 }
5208
5209 // Ignore empty records.
5210 if (isEmptyRecord(getContext(), Ty, true))
5211 return ABIArgInfo::getIgnore();
5212
Mark Lacey3825e832013-10-06 01:33:34 +00005213 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00005214 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Tony Linthicum76329bf2011-12-12 21:14:55 +00005215
5216 uint64_t Size = getContext().getTypeSize(Ty);
5217 if (Size > 64)
5218 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
5219 // Pass in the smallest viable integer type.
5220 else if (Size > 32)
5221 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
5222 else if (Size > 16)
5223 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
5224 else if (Size > 8)
5225 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5226 else
5227 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
5228}
5229
5230ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
5231 if (RetTy->isVoidType())
5232 return ABIArgInfo::getIgnore();
5233
5234 // Large vector types should be returned via memory.
5235 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
5236 return ABIArgInfo::getIndirect(0);
5237
5238 if (!isAggregateTypeForABI(RetTy)) {
5239 // Treat an enum type as its underlying type.
5240 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5241 RetTy = EnumTy->getDecl()->getIntegerType();
5242
5243 return (RetTy->isPromotableIntegerType() ?
5244 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
5245 }
5246
5247 // Structures with either a non-trivial destructor or a non-trivial
5248 // copy constructor are always indirect.
Mark Lacey3825e832013-10-06 01:33:34 +00005249 if (isRecordReturnIndirect(RetTy, getCXXABI()))
Tony Linthicum76329bf2011-12-12 21:14:55 +00005250 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
5251
5252 if (isEmptyRecord(getContext(), RetTy, true))
5253 return ABIArgInfo::getIgnore();
5254
5255 // Aggregates <= 8 bytes are returned in r0; other aggregates
5256 // are returned indirectly.
5257 uint64_t Size = getContext().getTypeSize(RetTy);
5258 if (Size <= 64) {
5259 // Return in the smallest viable integer type.
5260 if (Size <= 8)
5261 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
5262 if (Size <= 16)
5263 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5264 if (Size <= 32)
5265 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
5266 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
5267 }
5268
5269 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
5270}
5271
5272llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattnerece04092012-02-07 00:39:47 +00005273 CodeGenFunction &CGF) const {
Tony Linthicum76329bf2011-12-12 21:14:55 +00005274 // FIXME: Need to handle alignment
Chris Lattnerece04092012-02-07 00:39:47 +00005275 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Tony Linthicum76329bf2011-12-12 21:14:55 +00005276
5277 CGBuilderTy &Builder = CGF.Builder;
5278 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
5279 "ap");
5280 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
5281 llvm::Type *PTy =
5282 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
5283 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
5284
5285 uint64_t Offset =
5286 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
5287 llvm::Value *NextAddr =
5288 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
5289 "ap.next");
5290 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
5291
5292 return AddrTyped;
5293}
5294
5295
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00005296//===----------------------------------------------------------------------===//
5297// SPARC v9 ABI Implementation.
5298// Based on the SPARC Compliance Definition version 2.4.1.
5299//
5300// Function arguments a mapped to a nominal "parameter array" and promoted to
5301// registers depending on their type. Each argument occupies 8 or 16 bytes in
5302// the array, structs larger than 16 bytes are passed indirectly.
5303//
5304// One case requires special care:
5305//
5306// struct mixed {
5307// int i;
5308// float f;
5309// };
5310//
5311// When a struct mixed is passed by value, it only occupies 8 bytes in the
5312// parameter array, but the int is passed in an integer register, and the float
5313// is passed in a floating point register. This is represented as two arguments
5314// with the LLVM IR inreg attribute:
5315//
5316// declare void f(i32 inreg %i, float inreg %f)
5317//
5318// The code generator will only allocate 4 bytes from the parameter array for
5319// the inreg arguments. All other arguments are allocated a multiple of 8
5320// bytes.
5321//
5322namespace {
5323class SparcV9ABIInfo : public ABIInfo {
5324public:
5325 SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
5326
5327private:
5328 ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const;
5329 virtual void computeInfo(CGFunctionInfo &FI) const;
5330 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5331 CodeGenFunction &CGF) const;
Jakob Stoklund Olesen02dc6a12013-05-28 04:57:37 +00005332
5333 // Coercion type builder for structs passed in registers. The coercion type
5334 // serves two purposes:
5335 //
5336 // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned'
5337 // in registers.
5338 // 2. Expose aligned floating point elements as first-level elements, so the
5339 // code generator knows to pass them in floating point registers.
5340 //
5341 // We also compute the InReg flag which indicates that the struct contains
5342 // aligned 32-bit floats.
5343 //
5344 struct CoerceBuilder {
5345 llvm::LLVMContext &Context;
5346 const llvm::DataLayout &DL;
5347 SmallVector<llvm::Type*, 8> Elems;
5348 uint64_t Size;
5349 bool InReg;
5350
5351 CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl)
5352 : Context(c), DL(dl), Size(0), InReg(false) {}
5353
5354 // Pad Elems with integers until Size is ToSize.
5355 void pad(uint64_t ToSize) {
5356 assert(ToSize >= Size && "Cannot remove elements");
5357 if (ToSize == Size)
5358 return;
5359
5360 // Finish the current 64-bit word.
5361 uint64_t Aligned = llvm::RoundUpToAlignment(Size, 64);
5362 if (Aligned > Size && Aligned <= ToSize) {
5363 Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size));
5364 Size = Aligned;
5365 }
5366
5367 // Add whole 64-bit words.
5368 while (Size + 64 <= ToSize) {
5369 Elems.push_back(llvm::Type::getInt64Ty(Context));
5370 Size += 64;
5371 }
5372
5373 // Final in-word padding.
5374 if (Size < ToSize) {
5375 Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size));
5376 Size = ToSize;
5377 }
5378 }
5379
5380 // Add a floating point element at Offset.
5381 void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) {
5382 // Unaligned floats are treated as integers.
5383 if (Offset % Bits)
5384 return;
5385 // The InReg flag is only required if there are any floats < 64 bits.
5386 if (Bits < 64)
5387 InReg = true;
5388 pad(Offset);
5389 Elems.push_back(Ty);
5390 Size = Offset + Bits;
5391 }
5392
5393 // Add a struct type to the coercion type, starting at Offset (in bits).
5394 void addStruct(uint64_t Offset, llvm::StructType *StrTy) {
5395 const llvm::StructLayout *Layout = DL.getStructLayout(StrTy);
5396 for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) {
5397 llvm::Type *ElemTy = StrTy->getElementType(i);
5398 uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i);
5399 switch (ElemTy->getTypeID()) {
5400 case llvm::Type::StructTyID:
5401 addStruct(ElemOffset, cast<llvm::StructType>(ElemTy));
5402 break;
5403 case llvm::Type::FloatTyID:
5404 addFloat(ElemOffset, ElemTy, 32);
5405 break;
5406 case llvm::Type::DoubleTyID:
5407 addFloat(ElemOffset, ElemTy, 64);
5408 break;
5409 case llvm::Type::FP128TyID:
5410 addFloat(ElemOffset, ElemTy, 128);
5411 break;
5412 case llvm::Type::PointerTyID:
5413 if (ElemOffset % 64 == 0) {
5414 pad(ElemOffset);
5415 Elems.push_back(ElemTy);
5416 Size += 64;
5417 }
5418 break;
5419 default:
5420 break;
5421 }
5422 }
5423 }
5424
5425 // Check if Ty is a usable substitute for the coercion type.
5426 bool isUsableType(llvm::StructType *Ty) const {
5427 if (Ty->getNumElements() != Elems.size())
5428 return false;
5429 for (unsigned i = 0, e = Elems.size(); i != e; ++i)
5430 if (Elems[i] != Ty->getElementType(i))
5431 return false;
5432 return true;
5433 }
5434
5435 // Get the coercion type as a literal struct type.
5436 llvm::Type *getType() const {
5437 if (Elems.size() == 1)
5438 return Elems.front();
5439 else
5440 return llvm::StructType::get(Context, Elems);
5441 }
5442 };
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00005443};
5444} // end anonymous namespace
5445
5446ABIArgInfo
5447SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const {
5448 if (Ty->isVoidType())
5449 return ABIArgInfo::getIgnore();
5450
5451 uint64_t Size = getContext().getTypeSize(Ty);
5452
5453 // Anything too big to fit in registers is passed with an explicit indirect
5454 // pointer / sret pointer.
5455 if (Size > SizeLimit)
5456 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
5457
5458 // Treat an enum type as its underlying type.
5459 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5460 Ty = EnumTy->getDecl()->getIntegerType();
5461
5462 // Integer types smaller than a register are extended.
5463 if (Size < 64 && Ty->isIntegerType())
5464 return ABIArgInfo::getExtend();
5465
5466 // Other non-aggregates go in registers.
5467 if (!isAggregateTypeForABI(Ty))
5468 return ABIArgInfo::getDirect();
5469
Jakob Stoklund Olesenb81eb3e2014-01-12 06:54:56 +00005470 // If a C++ object has either a non-trivial copy constructor or a non-trivial
5471 // destructor, it is passed with an explicit indirect pointer / sret pointer.
5472 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
5473 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
5474
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00005475 // This is a small aggregate type that should be passed in registers.
Jakob Stoklund Olesen02dc6a12013-05-28 04:57:37 +00005476 // Build a coercion type from the LLVM struct type.
5477 llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty));
5478 if (!StrTy)
5479 return ABIArgInfo::getDirect();
5480
5481 CoerceBuilder CB(getVMContext(), getDataLayout());
5482 CB.addStruct(0, StrTy);
5483 CB.pad(llvm::RoundUpToAlignment(CB.DL.getTypeSizeInBits(StrTy), 64));
5484
5485 // Try to use the original type for coercion.
5486 llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();
5487
5488 if (CB.InReg)
5489 return ABIArgInfo::getDirectInReg(CoerceTy);
5490 else
5491 return ABIArgInfo::getDirect(CoerceTy);
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00005492}
5493
5494llvm::Value *SparcV9ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5495 CodeGenFunction &CGF) const {
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00005496 ABIArgInfo AI = classifyType(Ty, 16 * 8);
5497 llvm::Type *ArgTy = CGT.ConvertType(Ty);
5498 if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
5499 AI.setCoerceToType(ArgTy);
5500
5501 llvm::Type *BPP = CGF.Int8PtrPtrTy;
5502 CGBuilderTy &Builder = CGF.Builder;
5503 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
5504 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
5505 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
5506 llvm::Value *ArgAddr;
5507 unsigned Stride;
5508
5509 switch (AI.getKind()) {
5510 case ABIArgInfo::Expand:
Reid Kleckner314ef7b2014-02-01 00:04:45 +00005511 case ABIArgInfo::InAlloca:
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00005512 llvm_unreachable("Unsupported ABI kind for va_arg");
5513
5514 case ABIArgInfo::Extend:
5515 Stride = 8;
5516 ArgAddr = Builder
5517 .CreateConstGEP1_32(Addr, 8 - getDataLayout().getTypeAllocSize(ArgTy),
5518 "extend");
5519 break;
5520
5521 case ABIArgInfo::Direct:
5522 Stride = getDataLayout().getTypeAllocSize(AI.getCoerceToType());
5523 ArgAddr = Addr;
5524 break;
5525
5526 case ABIArgInfo::Indirect:
5527 Stride = 8;
5528 ArgAddr = Builder.CreateBitCast(Addr,
5529 llvm::PointerType::getUnqual(ArgPtrTy),
5530 "indirect");
5531 ArgAddr = Builder.CreateLoad(ArgAddr, "indirect.arg");
5532 break;
5533
5534 case ABIArgInfo::Ignore:
5535 return llvm::UndefValue::get(ArgPtrTy);
5536 }
5537
5538 // Update VAList.
5539 Addr = Builder.CreateConstGEP1_32(Addr, Stride, "ap.next");
5540 Builder.CreateStore(Addr, VAListAddrAsBPP);
5541
5542 return Builder.CreatePointerCast(ArgAddr, ArgPtrTy, "arg.addr");
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00005543}
5544
5545void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const {
5546 FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8);
5547 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
5548 it != ie; ++it)
5549 it->info = classifyType(it->type, 16 * 8);
5550}
5551
5552namespace {
5553class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo {
5554public:
5555 SparcV9TargetCodeGenInfo(CodeGenTypes &CGT)
5556 : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {}
5557};
5558} // end anonymous namespace
5559
5560
Robert Lytton0e076492013-08-13 09:43:10 +00005561//===----------------------------------------------------------------------===//
5562// Xcore ABI Implementation
5563//===----------------------------------------------------------------------===//
5564namespace {
Robert Lytton7d1db152013-08-19 09:46:39 +00005565class XCoreABIInfo : public DefaultABIInfo {
5566public:
5567 XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
5568 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5569 CodeGenFunction &CGF) const;
5570};
5571
Robert Lytton0e076492013-08-13 09:43:10 +00005572class XcoreTargetCodeGenInfo : public TargetCodeGenInfo {
5573public:
5574 XcoreTargetCodeGenInfo(CodeGenTypes &CGT)
Robert Lytton7d1db152013-08-19 09:46:39 +00005575 :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {}
Robert Lytton0e076492013-08-13 09:43:10 +00005576};
Robert Lytton2d196952013-10-11 10:29:34 +00005577} // End anonymous namespace.
Robert Lytton0e076492013-08-13 09:43:10 +00005578
Robert Lytton7d1db152013-08-19 09:46:39 +00005579llvm::Value *XCoreABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5580 CodeGenFunction &CGF) const {
Robert Lytton7d1db152013-08-19 09:46:39 +00005581 CGBuilderTy &Builder = CGF.Builder;
Robert Lytton7d1db152013-08-19 09:46:39 +00005582
Robert Lytton2d196952013-10-11 10:29:34 +00005583 // Get the VAList.
Robert Lytton7d1db152013-08-19 09:46:39 +00005584 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr,
5585 CGF.Int8PtrPtrTy);
5586 llvm::Value *AP = Builder.CreateLoad(VAListAddrAsBPP);
Robert Lytton7d1db152013-08-19 09:46:39 +00005587
Robert Lytton2d196952013-10-11 10:29:34 +00005588 // Handle the argument.
5589 ABIArgInfo AI = classifyArgumentType(Ty);
5590 llvm::Type *ArgTy = CGT.ConvertType(Ty);
5591 if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
5592 AI.setCoerceToType(ArgTy);
Robert Lytton7d1db152013-08-19 09:46:39 +00005593 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
Robert Lytton2d196952013-10-11 10:29:34 +00005594 llvm::Value *Val;
Andy Gibbsd9ba4722013-10-14 07:02:04 +00005595 uint64_t ArgSize = 0;
Robert Lytton7d1db152013-08-19 09:46:39 +00005596 switch (AI.getKind()) {
Robert Lytton7d1db152013-08-19 09:46:39 +00005597 case ABIArgInfo::Expand:
Reid Kleckner314ef7b2014-02-01 00:04:45 +00005598 case ABIArgInfo::InAlloca:
Robert Lytton7d1db152013-08-19 09:46:39 +00005599 llvm_unreachable("Unsupported ABI kind for va_arg");
5600 case ABIArgInfo::Ignore:
Robert Lytton2d196952013-10-11 10:29:34 +00005601 Val = llvm::UndefValue::get(ArgPtrTy);
5602 ArgSize = 0;
5603 break;
Robert Lytton7d1db152013-08-19 09:46:39 +00005604 case ABIArgInfo::Extend:
5605 case ABIArgInfo::Direct:
Robert Lytton2d196952013-10-11 10:29:34 +00005606 Val = Builder.CreatePointerCast(AP, ArgPtrTy);
5607 ArgSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType());
5608 if (ArgSize < 4)
5609 ArgSize = 4;
5610 break;
Robert Lytton7d1db152013-08-19 09:46:39 +00005611 case ABIArgInfo::Indirect:
5612 llvm::Value *ArgAddr;
5613 ArgAddr = Builder.CreateBitCast(AP, llvm::PointerType::getUnqual(ArgPtrTy));
5614 ArgAddr = Builder.CreateLoad(ArgAddr);
Robert Lytton2d196952013-10-11 10:29:34 +00005615 Val = Builder.CreatePointerCast(ArgAddr, ArgPtrTy);
5616 ArgSize = 4;
5617 break;
Robert Lytton7d1db152013-08-19 09:46:39 +00005618 }
Robert Lytton2d196952013-10-11 10:29:34 +00005619
5620 // Increment the VAList.
5621 if (ArgSize) {
5622 llvm::Value *APN = Builder.CreateConstGEP1_32(AP, ArgSize);
5623 Builder.CreateStore(APN, VAListAddrAsBPP);
5624 }
5625 return Val;
Robert Lytton7d1db152013-08-19 09:46:39 +00005626}
Robert Lytton0e076492013-08-13 09:43:10 +00005627
5628//===----------------------------------------------------------------------===//
5629// Driver code
5630//===----------------------------------------------------------------------===//
5631
Chris Lattner2b037972010-07-29 02:01:43 +00005632const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00005633 if (TheTargetCodeGenInfo)
5634 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005635
John McCallc8e01702013-04-16 22:48:15 +00005636 const llvm::Triple &Triple = getTarget().getTriple();
Daniel Dunbar40165182009-08-24 09:10:05 +00005637 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00005638 default:
Chris Lattner2b037972010-07-29 02:01:43 +00005639 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00005640
Derek Schuff09338a22012-09-06 17:37:28 +00005641 case llvm::Triple::le32:
5642 return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
John McCall943fae92010-05-27 06:19:26 +00005643 case llvm::Triple::mips:
5644 case llvm::Triple::mipsel:
Akira Hatanakac4baedd2013-11-11 22:10:46 +00005645 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
5646
Akira Hatanakaec11b4f2011-09-20 18:30:57 +00005647 case llvm::Triple::mips64:
5648 case llvm::Triple::mips64el:
Akira Hatanakac4baedd2013-11-11 22:10:46 +00005649 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
5650
Tim Northover9bb857a2013-01-31 12:13:10 +00005651 case llvm::Triple::aarch64:
5652 return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types));
5653
Daniel Dunbard59655c2009-09-12 00:59:49 +00005654 case llvm::Triple::arm:
5655 case llvm::Triple::thumb:
Sandeep Patel45df3dd2011-04-05 00:23:47 +00005656 {
5657 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
John McCallc8e01702013-04-16 22:48:15 +00005658 if (strcmp(getTarget().getABI(), "apcs-gnu") == 0)
Sandeep Patel45df3dd2011-04-05 00:23:47 +00005659 Kind = ARMABIInfo::APCS;
David Tweed8f676532012-10-25 13:33:01 +00005660 else if (CodeGenOpts.FloatABI == "hard" ||
John McCallc8e01702013-04-16 22:48:15 +00005661 (CodeGenOpts.FloatABI != "soft" &&
5662 Triple.getEnvironment() == llvm::Triple::GNUEABIHF))
Sandeep Patel45df3dd2011-04-05 00:23:47 +00005663 Kind = ARMABIInfo::AAPCS_VFP;
5664
Derek Schuffa2020962012-10-16 22:30:41 +00005665 switch (Triple.getOS()) {
Eli Benderskyd7c92032012-12-04 18:38:10 +00005666 case llvm::Triple::NaCl:
Derek Schuffa2020962012-10-16 22:30:41 +00005667 return *(TheTargetCodeGenInfo =
5668 new NaClARMTargetCodeGenInfo(Types, Kind));
5669 default:
5670 return *(TheTargetCodeGenInfo =
5671 new ARMTargetCodeGenInfo(Types, Kind));
5672 }
Sandeep Patel45df3dd2011-04-05 00:23:47 +00005673 }
Daniel Dunbard59655c2009-09-12 00:59:49 +00005674
John McCallea8d8bb2010-03-11 00:10:12 +00005675 case llvm::Triple::ppc:
Chris Lattner2b037972010-07-29 02:01:43 +00005676 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
Roman Divackyd966e722012-05-09 18:22:46 +00005677 case llvm::Triple::ppc64:
Bill Schmidt25cb3492012-10-03 19:18:57 +00005678 if (Triple.isOSBinFormatELF())
5679 return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types));
5680 else
5681 return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
Bill Schmidt778d3872013-07-26 01:36:11 +00005682 case llvm::Triple::ppc64le:
5683 assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!");
5684 return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types));
John McCallea8d8bb2010-03-11 00:10:12 +00005685
Peter Collingbournec947aae2012-05-20 23:28:41 +00005686 case llvm::Triple::nvptx:
5687 case llvm::Triple::nvptx64:
Justin Holewinski83e96682012-05-24 17:43:12 +00005688 return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types));
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00005689
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00005690 case llvm::Triple::msp430:
Chris Lattner2b037972010-07-29 02:01:43 +00005691 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00005692
Ulrich Weigand47445072013-05-06 16:26:41 +00005693 case llvm::Triple::systemz:
5694 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
5695
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00005696 case llvm::Triple::tce:
5697 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
5698
Eli Friedman33465822011-07-08 23:31:17 +00005699 case llvm::Triple::x86: {
John McCall1fe2a8c2013-06-18 02:46:29 +00005700 bool IsDarwinVectorABI = Triple.isOSDarwin();
5701 bool IsSmallStructInRegABI =
5702 X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
5703 bool IsWin32FloatStructABI = (Triple.getOS() == llvm::Triple::Win32);
Daniel Dunbar14ad22f2011-04-19 21:43:27 +00005704
John McCall1fe2a8c2013-06-18 02:46:29 +00005705 if (Triple.getOS() == llvm::Triple::Win32) {
Eli Friedmana98d1f82012-01-25 22:46:34 +00005706 return *(TheTargetCodeGenInfo =
Reid Klecknere43f0fe2013-05-08 13:44:39 +00005707 new WinX86_32TargetCodeGenInfo(Types,
John McCall1fe2a8c2013-06-18 02:46:29 +00005708 IsDarwinVectorABI, IsSmallStructInRegABI,
5709 IsWin32FloatStructABI,
Reid Klecknere43f0fe2013-05-08 13:44:39 +00005710 CodeGenOpts.NumRegisterParameters));
John McCall1fe2a8c2013-06-18 02:46:29 +00005711 } else {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00005712 return *(TheTargetCodeGenInfo =
John McCall1fe2a8c2013-06-18 02:46:29 +00005713 new X86_32TargetCodeGenInfo(Types,
5714 IsDarwinVectorABI, IsSmallStructInRegABI,
5715 IsWin32FloatStructABI,
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00005716 CodeGenOpts.NumRegisterParameters));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005717 }
Eli Friedman33465822011-07-08 23:31:17 +00005718 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005719
Eli Friedmanbfd5add2011-12-02 00:11:43 +00005720 case llvm::Triple::x86_64: {
John McCallc8e01702013-04-16 22:48:15 +00005721 bool HasAVX = strcmp(getTarget().getABI(), "avx") == 0;
Eli Friedmanbfd5add2011-12-02 00:11:43 +00005722
Chris Lattner04dc9572010-08-31 16:44:54 +00005723 switch (Triple.getOS()) {
5724 case llvm::Triple::Win32:
NAKAMURA Takumi31ea2f12011-02-17 08:51:38 +00005725 case llvm::Triple::MinGW32:
Chris Lattner04dc9572010-08-31 16:44:54 +00005726 case llvm::Triple::Cygwin:
5727 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
Eli Benderskyd7c92032012-12-04 18:38:10 +00005728 case llvm::Triple::NaCl:
John McCallc8e01702013-04-16 22:48:15 +00005729 return *(TheTargetCodeGenInfo = new NaClX86_64TargetCodeGenInfo(Types,
5730 HasAVX));
Chris Lattner04dc9572010-08-31 16:44:54 +00005731 default:
Eli Friedmanbfd5add2011-12-02 00:11:43 +00005732 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
5733 HasAVX));
Chris Lattner04dc9572010-08-31 16:44:54 +00005734 }
Daniel Dunbare3532f82009-08-24 08:52:16 +00005735 }
Tony Linthicum76329bf2011-12-12 21:14:55 +00005736 case llvm::Triple::hexagon:
5737 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00005738 case llvm::Triple::sparcv9:
5739 return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types));
Robert Lytton0e076492013-08-13 09:43:10 +00005740 case llvm::Triple::xcore:
5741 return *(TheTargetCodeGenInfo = new XcoreTargetCodeGenInfo(Types));
5742
Eli Friedmanbfd5add2011-12-02 00:11:43 +00005743 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005744}