blob: 9f5812bbe9b08e2d6ea82ac1f40dbbbd066ccdd4 [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"
Reid Kleckner9b3e3df2014-09-04 20:04:38 +000018#include "CGValue.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000019#include "CodeGenFunction.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000020#include "clang/AST/RecordLayout.h"
Mark Laceya8e7df32013-10-30 21:53:58 +000021#include "clang/CodeGen/CGFunctionInfo.h"
Sandeep Patel45df3dd2011-04-05 00:23:47 +000022#include "clang/Frontend/CodeGenOptions.h"
Matt Arsenault43fae6c2014-12-04 20:38:18 +000023#include "llvm/ADT/StringExtras.h"
Daniel Dunbare3532f82009-08-24 08:52:16 +000024#include "llvm/ADT/Triple.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000025#include "llvm/IR/DataLayout.h"
26#include "llvm/IR/Type.h"
Daniel Dunbar7230fa52009-12-03 09:13:49 +000027#include "llvm/Support/raw_ostream.h"
Robert Lytton844aeeb2014-05-02 09:33:20 +000028
29#include <algorithm> // std::sort
30
Anton Korobeynikov244360d2009-06-05 22:08:42 +000031using namespace clang;
32using namespace CodeGen;
33
John McCall943fae92010-05-27 06:19:26 +000034static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
35 llvm::Value *Array,
36 llvm::Value *Value,
37 unsigned FirstIndex,
38 unsigned LastIndex) {
39 // Alternatively, we could emit this as a loop in the source.
40 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
41 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
42 Builder.CreateStore(Value, Cell);
43 }
44}
45
John McCalla1dee5302010-08-22 10:59:02 +000046static bool isAggregateTypeForABI(QualType T) {
John McCall47fb9502013-03-07 21:37:08 +000047 return !CodeGenFunction::hasScalarEvaluationKind(T) ||
John McCalla1dee5302010-08-22 10:59:02 +000048 T->isMemberFunctionPointerType();
49}
50
Anton Korobeynikov244360d2009-06-05 22:08:42 +000051ABIInfo::~ABIInfo() {}
52
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +000053static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT,
Mark Lacey3825e832013-10-06 01:33:34 +000054 CGCXXABI &CXXABI) {
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +000055 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
56 if (!RD)
57 return CGCXXABI::RAA_Default;
Mark Lacey3825e832013-10-06 01:33:34 +000058 return CXXABI.getRecordArgABI(RD);
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +000059}
60
61static CGCXXABI::RecordArgABI getRecordArgABI(QualType T,
Mark Lacey3825e832013-10-06 01:33:34 +000062 CGCXXABI &CXXABI) {
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +000063 const RecordType *RT = T->getAs<RecordType>();
64 if (!RT)
65 return CGCXXABI::RAA_Default;
Mark Lacey3825e832013-10-06 01:33:34 +000066 return getRecordArgABI(RT, CXXABI);
67}
68
Reid Klecknerb1be6832014-11-15 01:41:41 +000069/// Pass transparent unions as if they were the type of the first element. Sema
70/// should ensure that all elements of the union have the same "machine type".
71static QualType useFirstFieldIfTransparentUnion(QualType Ty) {
72 if (const RecordType *UT = Ty->getAsUnionType()) {
73 const RecordDecl *UD = UT->getDecl();
74 if (UD->hasAttr<TransparentUnionAttr>()) {
75 assert(!UD->field_empty() && "sema created an empty transparent union");
76 return UD->field_begin()->getType();
77 }
78 }
79 return Ty;
80}
81
Mark Lacey3825e832013-10-06 01:33:34 +000082CGCXXABI &ABIInfo::getCXXABI() const {
83 return CGT.getCXXABI();
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +000084}
85
Chris Lattner2b037972010-07-29 02:01:43 +000086ASTContext &ABIInfo::getContext() const {
87 return CGT.getContext();
88}
89
90llvm::LLVMContext &ABIInfo::getVMContext() const {
91 return CGT.getLLVMContext();
92}
93
Micah Villmowdd31ca12012-10-08 16:25:52 +000094const llvm::DataLayout &ABIInfo::getDataLayout() const {
95 return CGT.getDataLayout();
Chris Lattner2b037972010-07-29 02:01:43 +000096}
97
John McCallc8e01702013-04-16 22:48:15 +000098const TargetInfo &ABIInfo::getTarget() const {
99 return CGT.getTarget();
100}
Chris Lattner2b037972010-07-29 02:01:43 +0000101
Reid Klecknere9f6a712014-10-31 17:10:41 +0000102bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
103 return false;
104}
105
106bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
107 uint64_t Members) const {
108 return false;
109}
110
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000111void ABIArgInfo::dump() const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000112 raw_ostream &OS = llvm::errs();
Daniel Dunbar7230fa52009-12-03 09:13:49 +0000113 OS << "(ABIArgInfo Kind=";
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000114 switch (TheKind) {
115 case Direct:
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000116 OS << "Direct Type=";
Chris Lattner2192fe52011-07-18 04:24:23 +0000117 if (llvm::Type *Ty = getCoerceToType())
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000118 Ty->print(OS);
119 else
120 OS << "null";
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000121 break;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000122 case Extend:
Daniel Dunbar7230fa52009-12-03 09:13:49 +0000123 OS << "Extend";
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000124 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000125 case Ignore:
Daniel Dunbar7230fa52009-12-03 09:13:49 +0000126 OS << "Ignore";
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000127 break;
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000128 case InAlloca:
129 OS << "InAlloca Offset=" << getInAllocaFieldIndex();
130 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000131 case Indirect:
Daniel Dunbar557893d2010-04-21 19:10:51 +0000132 OS << "Indirect Align=" << getIndirectAlign()
Joerg Sonnenberger4921fe22011-07-15 18:23:44 +0000133 << " ByVal=" << getIndirectByVal()
Daniel Dunbar7b7c2932010-09-16 20:42:02 +0000134 << " Realign=" << getIndirectRealign();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000135 break;
136 case Expand:
Daniel Dunbar7230fa52009-12-03 09:13:49 +0000137 OS << "Expand";
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000138 break;
139 }
Daniel Dunbar7230fa52009-12-03 09:13:49 +0000140 OS << ")\n";
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000141}
142
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000143TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
144
John McCall3480ef22011-08-30 01:42:09 +0000145// If someone can figure out a general rule for this, that would be great.
146// It's probably just doomed to be platform-dependent, though.
147unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
148 // Verified for:
149 // x86-64 FreeBSD, Linux, Darwin
150 // x86-32 FreeBSD, Linux, Darwin
151 // PowerPC Linux, Darwin
152 // ARM Darwin (*not* EABI)
Tim Northover9bb857a2013-01-31 12:13:10 +0000153 // AArch64 Linux
John McCall3480ef22011-08-30 01:42:09 +0000154 return 32;
155}
156
John McCalla729c622012-02-17 03:33:10 +0000157bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
158 const FunctionNoProtoType *fnType) const {
John McCallcbc038a2011-09-21 08:08:30 +0000159 // The following conventions are known to require this to be false:
160 // x86_stdcall
161 // MIPS
162 // For everything else, we just prefer false unless we opt out.
163 return false;
164}
165
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000166void
167TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib,
168 llvm::SmallString<24> &Opt) const {
169 // This assumes the user is passing a library name like "rt" instead of a
170 // filename like "librt.a/so", and that they don't care whether it's static or
171 // dynamic.
172 Opt = "-l";
173 Opt += Lib;
174}
175
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000176static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000177
Sylvestre Ledru33b5baf2012-09-27 10:16:10 +0000178/// isEmptyField - Return true iff a the field is "empty", that is it
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000179/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000180static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
181 bool AllowArrays) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000182 if (FD->isUnnamedBitfield())
183 return true;
184
185 QualType FT = FD->getType();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000186
Eli Friedman0b3f2012011-11-18 03:47:20 +0000187 // Constant arrays of empty records count as empty, strip them off.
188 // Constant arrays of zero length always count as empty.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000189 if (AllowArrays)
Eli Friedman0b3f2012011-11-18 03:47:20 +0000190 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
191 if (AT->getSize() == 0)
192 return true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000193 FT = AT->getElementType();
Eli Friedman0b3f2012011-11-18 03:47:20 +0000194 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000195
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000196 const RecordType *RT = FT->getAs<RecordType>();
197 if (!RT)
198 return false;
199
200 // C++ record fields are never empty, at least in the Itanium ABI.
201 //
202 // FIXME: We should use a predicate for whether this behavior is true in the
203 // current ABI.
204 if (isa<CXXRecordDecl>(RT->getDecl()))
205 return false;
206
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000207 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000208}
209
Sylvestre Ledru33b5baf2012-09-27 10:16:10 +0000210/// isEmptyRecord - Return true iff a structure contains only empty
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000211/// fields. Note that a structure with a flexible array member is not
212/// considered empty.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000213static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000214 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000215 if (!RT)
216 return 0;
217 const RecordDecl *RD = RT->getDecl();
218 if (RD->hasFlexibleArrayMember())
219 return false;
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000220
Argyrios Kyrtzidisd42411f2011-05-17 02:17:52 +0000221 // If this is a C++ record, check the bases first.
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000222 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Aaron Ballman574705e2014-03-13 15:41:46 +0000223 for (const auto &I : CXXRD->bases())
224 if (!isEmptyRecord(Context, I.getType(), true))
Argyrios Kyrtzidisd42411f2011-05-17 02:17:52 +0000225 return false;
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000226
Aaron Ballmane8a8bae2014-03-08 20:12:42 +0000227 for (const auto *I : RD->fields())
228 if (!isEmptyField(Context, I, AllowArrays))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000229 return false;
230 return true;
231}
232
233/// isSingleElementStruct - Determine if a structure is a "single
234/// element struct", i.e. it has exactly one non-empty field or
235/// exactly one field which is itself a single element
236/// struct. Structures with flexible array members are never
237/// considered single element structs.
238///
239/// \return The field declaration for the single non-empty field, if
240/// it exists.
241static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
242 const RecordType *RT = T->getAsStructureType();
243 if (!RT)
Craig Topper8a13c412014-05-21 05:09:00 +0000244 return nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000245
246 const RecordDecl *RD = RT->getDecl();
247 if (RD->hasFlexibleArrayMember())
Craig Topper8a13c412014-05-21 05:09:00 +0000248 return nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000249
Craig Topper8a13c412014-05-21 05:09:00 +0000250 const Type *Found = nullptr;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000251
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000252 // If this is a C++ record, check the bases first.
253 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
Aaron Ballman574705e2014-03-13 15:41:46 +0000254 for (const auto &I : CXXRD->bases()) {
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000255 // Ignore empty records.
Aaron Ballman574705e2014-03-13 15:41:46 +0000256 if (isEmptyRecord(Context, I.getType(), true))
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000257 continue;
258
259 // If we already found an element then this isn't a single-element struct.
260 if (Found)
Craig Topper8a13c412014-05-21 05:09:00 +0000261 return nullptr;
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000262
263 // If this is non-empty and not a single element struct, the composite
264 // cannot be a single element struct.
Aaron Ballman574705e2014-03-13 15:41:46 +0000265 Found = isSingleElementStruct(I.getType(), Context);
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000266 if (!Found)
Craig Topper8a13c412014-05-21 05:09:00 +0000267 return nullptr;
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000268 }
269 }
270
271 // Check for single element.
Aaron Ballmane8a8bae2014-03-08 20:12:42 +0000272 for (const auto *FD : RD->fields()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000273 QualType FT = FD->getType();
274
275 // Ignore empty fields.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000276 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000277 continue;
278
279 // If we already found an element then this isn't a single-element
280 // struct.
281 if (Found)
Craig Topper8a13c412014-05-21 05:09:00 +0000282 return nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000283
284 // Treat single element arrays as the element.
285 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
286 if (AT->getSize().getZExtValue() != 1)
287 break;
288 FT = AT->getElementType();
289 }
290
John McCalla1dee5302010-08-22 10:59:02 +0000291 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000292 Found = FT.getTypePtr();
293 } else {
294 Found = isSingleElementStruct(FT, Context);
295 if (!Found)
Craig Topper8a13c412014-05-21 05:09:00 +0000296 return nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000297 }
298 }
299
Eli Friedmanee945342011-11-18 01:25:50 +0000300 // We don't consider a struct a single-element struct if it has
301 // padding beyond the element type.
302 if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
Craig Topper8a13c412014-05-21 05:09:00 +0000303 return nullptr;
Eli Friedmanee945342011-11-18 01:25:50 +0000304
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000305 return Found;
306}
307
308static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Eli Friedmana92db672012-11-29 23:21:04 +0000309 // Treat complex types as the element type.
310 if (const ComplexType *CTy = Ty->getAs<ComplexType>())
311 Ty = CTy->getElementType();
312
313 // Check for a type which we know has a simple scalar argument-passing
314 // convention without any padding. (We're specifically looking for 32
315 // and 64-bit integer and integer-equivalents, float, and double.)
Daniel Dunbar6b45b672010-05-14 03:40:53 +0000316 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Eli Friedmana92db672012-11-29 23:21:04 +0000317 !Ty->isEnumeralType() && !Ty->isBlockPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000318 return false;
319
320 uint64_t Size = Context.getTypeSize(Ty);
321 return Size == 32 || Size == 64;
322}
323
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000324/// canExpandIndirectArgument - Test whether an argument type which is to be
325/// passed indirectly (on the stack) would have the equivalent layout if it was
326/// expanded into separate arguments. If so, we prefer to do the latter to avoid
327/// inhibiting optimizations.
328///
329// FIXME: This predicate is missing many cases, currently it just follows
330// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
331// should probably make this smarter, or better yet make the LLVM backend
332// capable of handling it.
333static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
334 // We can only expand structure types.
335 const RecordType *RT = Ty->getAs<RecordType>();
336 if (!RT)
337 return false;
338
339 // We can only expand (C) structures.
340 //
341 // FIXME: This needs to be generalized to handle classes as well.
342 const RecordDecl *RD = RT->getDecl();
343 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
344 return false;
345
Eli Friedmane5c85622011-11-18 01:32:26 +0000346 uint64_t Size = 0;
347
Aaron Ballmane8a8bae2014-03-08 20:12:42 +0000348 for (const auto *FD : RD->fields()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000349 if (!is32Or64BitBasicType(FD->getType(), Context))
350 return false;
351
352 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
353 // how to expand them yet, and the predicate for telling if a bitfield still
354 // counts as "basic" is more complicated than what we were doing previously.
355 if (FD->isBitField())
356 return false;
Eli Friedmane5c85622011-11-18 01:32:26 +0000357
358 Size += Context.getTypeSize(FD->getType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000359 }
360
Eli Friedmane5c85622011-11-18 01:32:26 +0000361 // Make sure there are not any holes in the struct.
362 if (Size != Context.getTypeSize(Ty))
363 return false;
364
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000365 return true;
366}
367
368namespace {
369/// DefaultABIInfo - The default implementation for ABI specific
370/// details. This implementation provides information which results in
371/// self-consistent and sensible LLVM IR generation, but does not
372/// conform to any particular ABI.
373class DefaultABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +0000374public:
375 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000376
Chris Lattner458b2aa2010-07-29 02:16:43 +0000377 ABIArgInfo classifyReturnType(QualType RetTy) const;
378 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000379
Craig Topper4f12f102014-03-12 06:41:41 +0000380 void computeInfo(CGFunctionInfo &FI) const override {
Reid Kleckner40ca9132014-05-13 22:05:45 +0000381 if (!getCXXABI().classifyReturnType(FI))
382 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Aaron Ballmanec47bc22014-03-17 18:10:01 +0000383 for (auto &I : FI.arguments())
384 I.info = classifyArgumentType(I.type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000385 }
386
Craig Topper4f12f102014-03-12 06:41:41 +0000387 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
388 CodeGenFunction &CGF) const override;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000389};
390
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000391class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
392public:
Chris Lattner2b037972010-07-29 02:01:43 +0000393 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
394 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000395};
396
397llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
398 CodeGenFunction &CGF) const {
Craig Topper8a13c412014-05-21 05:09:00 +0000399 return nullptr;
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000400}
401
Chris Lattner458b2aa2010-07-29 02:16:43 +0000402ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
Reid Kleckner40ca9132014-05-13 22:05:45 +0000403 if (isAggregateTypeForABI(Ty))
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000404 return ABIArgInfo::getIndirect(0);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000405
Chris Lattner9723d6c2010-03-11 18:19:55 +0000406 // Treat an enum type as its underlying type.
407 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
408 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000409
Chris Lattner9723d6c2010-03-11 18:19:55 +0000410 return (Ty->isPromotableIntegerType() ?
411 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000412}
413
Bob Wilsonbd4520b2011-01-10 23:54:17 +0000414ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
415 if (RetTy->isVoidType())
416 return ABIArgInfo::getIgnore();
417
418 if (isAggregateTypeForABI(RetTy))
419 return ABIArgInfo::getIndirect(0);
420
421 // Treat an enum type as its underlying type.
422 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
423 RetTy = EnumTy->getDecl()->getIntegerType();
424
425 return (RetTy->isPromotableIntegerType() ?
426 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
427}
428
Derek Schuff09338a22012-09-06 17:37:28 +0000429//===----------------------------------------------------------------------===//
430// le32/PNaCl bitcode ABI Implementation
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000431//
432// This is a simplified version of the x86_32 ABI. Arguments and return values
433// are always passed on the stack.
Derek Schuff09338a22012-09-06 17:37:28 +0000434//===----------------------------------------------------------------------===//
435
436class PNaClABIInfo : public ABIInfo {
437 public:
438 PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
439
440 ABIArgInfo classifyReturnType(QualType RetTy) const;
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000441 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Derek Schuff09338a22012-09-06 17:37:28 +0000442
Craig Topper4f12f102014-03-12 06:41:41 +0000443 void computeInfo(CGFunctionInfo &FI) const override;
444 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
445 CodeGenFunction &CGF) const override;
Derek Schuff09338a22012-09-06 17:37:28 +0000446};
447
448class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
449 public:
450 PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
451 : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {}
452};
453
454void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
Reid Kleckner40ca9132014-05-13 22:05:45 +0000455 if (!getCXXABI().classifyReturnType(FI))
Derek Schuff09338a22012-09-06 17:37:28 +0000456 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
457
Reid Kleckner40ca9132014-05-13 22:05:45 +0000458 for (auto &I : FI.arguments())
459 I.info = classifyArgumentType(I.type);
460}
Derek Schuff09338a22012-09-06 17:37:28 +0000461
462llvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
463 CodeGenFunction &CGF) const {
Craig Topper8a13c412014-05-21 05:09:00 +0000464 return nullptr;
Derek Schuff09338a22012-09-06 17:37:28 +0000465}
466
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000467/// \brief Classify argument of given type \p Ty.
468ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const {
Derek Schuff09338a22012-09-06 17:37:28 +0000469 if (isAggregateTypeForABI(Ty)) {
Mark Lacey3825e832013-10-06 01:33:34 +0000470 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +0000471 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Derek Schuff09338a22012-09-06 17:37:28 +0000472 return ABIArgInfo::getIndirect(0);
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000473 } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
474 // Treat an enum type as its underlying type.
Derek Schuff09338a22012-09-06 17:37:28 +0000475 Ty = EnumTy->getDecl()->getIntegerType();
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000476 } else if (Ty->isFloatingType()) {
477 // Floating-point types don't go inreg.
478 return ABIArgInfo::getDirect();
Derek Schuff09338a22012-09-06 17:37:28 +0000479 }
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000480
481 return (Ty->isPromotableIntegerType() ?
482 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Derek Schuff09338a22012-09-06 17:37:28 +0000483}
484
485ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
486 if (RetTy->isVoidType())
487 return ABIArgInfo::getIgnore();
488
Eli Benderskye20dad62013-04-04 22:49:35 +0000489 // In the PNaCl ABI we always return records/structures on the stack.
Derek Schuff09338a22012-09-06 17:37:28 +0000490 if (isAggregateTypeForABI(RetTy))
491 return ABIArgInfo::getIndirect(0);
492
493 // Treat an enum type as its underlying type.
494 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
495 RetTy = EnumTy->getDecl()->getIntegerType();
496
497 return (RetTy->isPromotableIntegerType() ?
498 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
499}
500
Chad Rosier651c1832013-03-25 21:00:27 +0000501/// IsX86_MMXType - Return true if this is an MMX type.
502bool IsX86_MMXType(llvm::Type *IRType) {
503 // 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 +0000504 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
505 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
506 IRType->getScalarSizeInBits() != 64;
507}
508
Jay Foad7c57be32011-07-11 09:56:20 +0000509static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000510 StringRef Constraint,
Jay Foad7c57be32011-07-11 09:56:20 +0000511 llvm::Type* Ty) {
Tim Northover0ae93912013-06-07 00:04:50 +0000512 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) {
513 if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) {
514 // Invalid MMX constraint
Craig Topper8a13c412014-05-21 05:09:00 +0000515 return nullptr;
Tim Northover0ae93912013-06-07 00:04:50 +0000516 }
517
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000518 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
Tim Northover0ae93912013-06-07 00:04:50 +0000519 }
520
521 // No operation needed
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000522 return Ty;
523}
524
Reid Kleckner80944df2014-10-31 22:00:51 +0000525/// Returns true if this type can be passed in SSE registers with the
526/// X86_VectorCall calling convention. Shared between x86_32 and x86_64.
527static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) {
528 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
529 if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half)
530 return true;
531 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
532 // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX
533 // registers specially.
534 unsigned VecSize = Context.getTypeSize(VT);
535 if (VecSize == 128 || VecSize == 256 || VecSize == 512)
536 return true;
537 }
538 return false;
539}
540
541/// Returns true if this aggregate is small enough to be passed in SSE registers
542/// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64.
543static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) {
544 return NumMembers <= 4;
545}
546
Chris Lattner0cf24192010-06-28 20:05:43 +0000547//===----------------------------------------------------------------------===//
548// X86-32 ABI Implementation
549//===----------------------------------------------------------------------===//
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000550
Reid Kleckner661f35b2014-01-18 01:12:41 +0000551/// \brief Similar to llvm::CCState, but for Clang.
552struct CCState {
Reid Kleckner80944df2014-10-31 22:00:51 +0000553 CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {}
Reid Kleckner661f35b2014-01-18 01:12:41 +0000554
555 unsigned CC;
556 unsigned FreeRegs;
Reid Kleckner80944df2014-10-31 22:00:51 +0000557 unsigned FreeSSERegs;
Reid Kleckner661f35b2014-01-18 01:12:41 +0000558};
559
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000560/// X86_32ABIInfo - The X86-32 ABI information.
561class X86_32ABIInfo : public ABIInfo {
Rafael Espindola06b2b4a2012-07-31 02:44:24 +0000562 enum Class {
563 Integer,
564 Float
565 };
566
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000567 static const unsigned MinABIStackAlignInBytes = 4;
568
David Chisnallde3a0692009-08-17 23:08:21 +0000569 bool IsDarwinVectorABI;
570 bool IsSmallStructInRegABI;
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +0000571 bool IsWin32StructABI;
Rafael Espindola06b2b4a2012-07-31 02:44:24 +0000572 unsigned DefaultNumRegisterParameters;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000573
574 static bool isRegisterSize(unsigned Size) {
575 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
576 }
577
Reid Kleckner80944df2014-10-31 22:00:51 +0000578 bool isHomogeneousAggregateBaseType(QualType Ty) const override {
579 // FIXME: Assumes vectorcall is in use.
580 return isX86VectorTypeForVectorCall(getContext(), Ty);
581 }
582
583 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
584 uint64_t NumMembers) const override {
585 // FIXME: Assumes vectorcall is in use.
586 return isX86VectorCallAggregateSmallEnough(NumMembers);
587 }
588
Reid Kleckner40ca9132014-05-13 22:05:45 +0000589 bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000590
Daniel Dunbar557893d2010-04-21 19:10:51 +0000591 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
592 /// such that the argument will be passed in memory.
Reid Kleckner661f35b2014-01-18 01:12:41 +0000593 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
594
595 ABIArgInfo getIndirectReturnResult(CCState &State) const;
Daniel Dunbar557893d2010-04-21 19:10:51 +0000596
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000597 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000598 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000599
Rafael Espindola06b2b4a2012-07-31 02:44:24 +0000600 Class classify(QualType Ty) const;
Reid Kleckner40ca9132014-05-13 22:05:45 +0000601 ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const;
Reid Kleckner661f35b2014-01-18 01:12:41 +0000602 ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
603 bool shouldUseInReg(QualType Ty, CCState &State, bool &NeedsPadding) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000604
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000605 /// \brief Rewrite the function info so that all memory arguments use
606 /// inalloca.
607 void rewriteWithInAlloca(CGFunctionInfo &FI) const;
608
609 void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
610 unsigned &StackOffset, ABIArgInfo &Info,
611 QualType Type) const;
612
Rafael Espindola75419dc2012-07-23 23:30:29 +0000613public:
614
Craig Topper4f12f102014-03-12 06:41:41 +0000615 void computeInfo(CGFunctionInfo &FI) const override;
616 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
617 CodeGenFunction &CGF) const override;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000618
Chad Rosier651c1832013-03-25 21:00:27 +0000619 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool w,
Rafael Espindola06b2b4a2012-07-31 02:44:24 +0000620 unsigned r)
Eli Friedman33465822011-07-08 23:31:17 +0000621 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +0000622 IsWin32StructABI(w), DefaultNumRegisterParameters(r) {}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000623};
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000624
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000625class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
626public:
Eli Friedmana98d1f82012-01-25 22:46:34 +0000627 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
Chad Rosier651c1832013-03-25 21:00:27 +0000628 bool d, bool p, bool w, unsigned r)
629 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, w, r)) {}
Charles Davis4ea31ab2010-02-13 15:54:06 +0000630
John McCall1fe2a8c2013-06-18 02:46:29 +0000631 static bool isStructReturnInRegABI(
632 const llvm::Triple &Triple, const CodeGenOptions &Opts);
633
Charles Davis4ea31ab2010-02-13 15:54:06 +0000634 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Craig Topper4f12f102014-03-12 06:41:41 +0000635 CodeGen::CodeGenModule &CGM) const override;
John McCallbeec5a02010-03-06 00:35:14 +0000636
Craig Topper4f12f102014-03-12 06:41:41 +0000637 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
John McCallbeec5a02010-03-06 00:35:14 +0000638 // Darwin uses different dwarf register numbers for EH.
John McCallc8e01702013-04-16 22:48:15 +0000639 if (CGM.getTarget().getTriple().isOSDarwin()) return 5;
John McCallbeec5a02010-03-06 00:35:14 +0000640 return 4;
641 }
642
643 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +0000644 llvm::Value *Address) const override;
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000645
Jay Foad7c57be32011-07-11 09:56:20 +0000646 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000647 StringRef Constraint,
Craig Topper4f12f102014-03-12 06:41:41 +0000648 llvm::Type* Ty) const override {
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000649 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
650 }
651
Reid Kleckner9b3e3df2014-09-04 20:04:38 +0000652 void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue,
653 std::string &Constraints,
654 std::vector<llvm::Type *> &ResultRegTypes,
655 std::vector<llvm::Type *> &ResultTruncRegTypes,
656 std::vector<LValue> &ResultRegDests,
657 std::string &AsmString,
658 unsigned NumOutputs) const override;
659
Craig Topper4f12f102014-03-12 06:41:41 +0000660 llvm::Constant *
661 getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
Peter Collingbourneb453cd62013-10-20 21:29:19 +0000662 unsigned Sig = (0xeb << 0) | // jmp rel8
663 (0x06 << 8) | // .+0x08
664 ('F' << 16) |
665 ('T' << 24);
666 return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
667 }
668
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000669};
670
671}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000672
Reid Kleckner9b3e3df2014-09-04 20:04:38 +0000673/// Rewrite input constraint references after adding some output constraints.
674/// In the case where there is one output and one input and we add one output,
675/// we need to replace all operand references greater than or equal to 1:
676/// mov $0, $1
677/// mov eax, $1
678/// The result will be:
679/// mov $0, $2
680/// mov eax, $2
681static void rewriteInputConstraintReferences(unsigned FirstIn,
682 unsigned NumNewOuts,
683 std::string &AsmString) {
684 std::string Buf;
685 llvm::raw_string_ostream OS(Buf);
686 size_t Pos = 0;
687 while (Pos < AsmString.size()) {
688 size_t DollarStart = AsmString.find('$', Pos);
689 if (DollarStart == std::string::npos)
690 DollarStart = AsmString.size();
691 size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart);
692 if (DollarEnd == std::string::npos)
693 DollarEnd = AsmString.size();
694 OS << StringRef(&AsmString[Pos], DollarEnd - Pos);
695 Pos = DollarEnd;
696 size_t NumDollars = DollarEnd - DollarStart;
697 if (NumDollars % 2 != 0 && Pos < AsmString.size()) {
698 // We have an operand reference.
699 size_t DigitStart = Pos;
700 size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart);
701 if (DigitEnd == std::string::npos)
702 DigitEnd = AsmString.size();
703 StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart);
704 unsigned OperandIndex;
705 if (!OperandStr.getAsInteger(10, OperandIndex)) {
706 if (OperandIndex >= FirstIn)
707 OperandIndex += NumNewOuts;
708 OS << OperandIndex;
709 } else {
710 OS << OperandStr;
711 }
712 Pos = DigitEnd;
713 }
714 }
715 AsmString = std::move(OS.str());
716}
717
718/// Add output constraints for EAX:EDX because they are return registers.
719void X86_32TargetCodeGenInfo::addReturnRegisterOutputs(
720 CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints,
721 std::vector<llvm::Type *> &ResultRegTypes,
722 std::vector<llvm::Type *> &ResultTruncRegTypes,
723 std::vector<LValue> &ResultRegDests, std::string &AsmString,
724 unsigned NumOutputs) const {
725 uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType());
726
727 // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is
728 // larger.
729 if (!Constraints.empty())
730 Constraints += ',';
731 if (RetWidth <= 32) {
732 Constraints += "={eax}";
733 ResultRegTypes.push_back(CGF.Int32Ty);
734 } else {
735 // Use the 'A' constraint for EAX:EDX.
736 Constraints += "=A";
737 ResultRegTypes.push_back(CGF.Int64Ty);
738 }
739
740 // Truncate EAX or EAX:EDX to an integer of the appropriate size.
741 llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth);
742 ResultTruncRegTypes.push_back(CoerceTy);
743
744 // Coerce the integer by bitcasting the return slot pointer.
745 ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(),
746 CoerceTy->getPointerTo()));
747 ResultRegDests.push_back(ReturnSlot);
748
749 rewriteInputConstraintReferences(NumOutputs, 1, AsmString);
750}
751
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000752/// shouldReturnTypeInRegister - Determine if the given type should be
753/// passed in a register (for the Darwin ABI).
Reid Kleckner40ca9132014-05-13 22:05:45 +0000754bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
755 ASTContext &Context) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000756 uint64_t Size = Context.getTypeSize(Ty);
757
758 // Type must be register sized.
759 if (!isRegisterSize(Size))
760 return false;
761
762 if (Ty->isVectorType()) {
763 // 64- and 128- bit vectors inside structures are not returned in
764 // registers.
765 if (Size == 64 || Size == 128)
766 return false;
767
768 return true;
769 }
770
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000771 // If this is a builtin, pointer, enum, complex type, member pointer, or
772 // member function pointer it is ok.
Daniel Dunbar6b45b672010-05-14 03:40:53 +0000773 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000774 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000775 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000776 return true;
777
778 // Arrays are treated like records.
779 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
Reid Kleckner40ca9132014-05-13 22:05:45 +0000780 return shouldReturnTypeInRegister(AT->getElementType(), Context);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000781
782 // Otherwise, it must be a record type.
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000783 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000784 if (!RT) return false;
785
Anders Carlsson40446e82010-01-27 03:25:19 +0000786 // FIXME: Traverse bases here too.
787
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000788 // Structure types are passed in register if all fields would be
789 // passed in a register.
Aaron Ballmane8a8bae2014-03-08 20:12:42 +0000790 for (const auto *FD : RT->getDecl()->fields()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000791 // Empty fields are ignored.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000792 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000793 continue;
794
795 // Check fields recursively.
Reid Kleckner40ca9132014-05-13 22:05:45 +0000796 if (!shouldReturnTypeInRegister(FD->getType(), Context))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000797 return false;
798 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000799 return true;
800}
801
Reid Kleckner661f35b2014-01-18 01:12:41 +0000802ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(CCState &State) const {
803 // If the return value is indirect, then the hidden argument is consuming one
804 // integer register.
805 if (State.FreeRegs) {
806 --State.FreeRegs;
807 return ABIArgInfo::getIndirectInReg(/*Align=*/0, /*ByVal=*/false);
808 }
809 return ABIArgInfo::getIndirect(/*Align=*/0, /*ByVal=*/false);
810}
811
Reid Kleckner40ca9132014-05-13 22:05:45 +0000812ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, CCState &State) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000813 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000814 return ABIArgInfo::getIgnore();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000815
Reid Kleckner80944df2014-10-31 22:00:51 +0000816 const Type *Base = nullptr;
817 uint64_t NumElts = 0;
818 if (State.CC == llvm::CallingConv::X86_VectorCall &&
819 isHomogeneousAggregate(RetTy, Base, NumElts)) {
820 // The LLVM struct type for such an aggregate should lower properly.
821 return ABIArgInfo::getDirect();
822 }
823
Chris Lattner458b2aa2010-07-29 02:16:43 +0000824 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000825 // On Darwin, some vectors are returned in registers.
David Chisnallde3a0692009-08-17 23:08:21 +0000826 if (IsDarwinVectorABI) {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000827 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000828
829 // 128-bit vectors are a special case; they are returned in
830 // registers and we need to make sure to pick a type the LLVM
831 // backend will like.
832 if (Size == 128)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000833 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattner458b2aa2010-07-29 02:16:43 +0000834 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000835
836 // Always return in register if it fits in a general purpose
837 // register, or if it is 64 bits and has a single element.
838 if ((Size == 8 || Size == 16 || Size == 32) ||
839 (Size == 64 && VT->getNumElements() == 1))
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000840 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +0000841 Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000842
Reid Kleckner661f35b2014-01-18 01:12:41 +0000843 return getIndirectReturnResult(State);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000844 }
845
846 return ABIArgInfo::getDirect();
Chris Lattner458b2aa2010-07-29 02:16:43 +0000847 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000848
John McCalla1dee5302010-08-22 10:59:02 +0000849 if (isAggregateTypeForABI(RetTy)) {
Anders Carlsson40446e82010-01-27 03:25:19 +0000850 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson5789c492009-10-20 22:07:59 +0000851 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000852 if (RT->getDecl()->hasFlexibleArrayMember())
Reid Kleckner661f35b2014-01-18 01:12:41 +0000853 return getIndirectReturnResult(State);
Anders Carlsson5789c492009-10-20 22:07:59 +0000854 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000855
David Chisnallde3a0692009-08-17 23:08:21 +0000856 // If specified, structs and unions are always indirect.
857 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Reid Kleckner661f35b2014-01-18 01:12:41 +0000858 return getIndirectReturnResult(State);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000859
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000860 // Small structures which are register sized are generally returned
861 // in a register.
Reid Kleckner40ca9132014-05-13 22:05:45 +0000862 if (shouldReturnTypeInRegister(RetTy, getContext())) {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000863 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanee945342011-11-18 01:25:50 +0000864
865 // As a special-case, if the struct is a "single-element" struct, and
866 // the field is of type "float" or "double", return it in a
Eli Friedmana98d1f82012-01-25 22:46:34 +0000867 // floating-point register. (MSVC does not apply this special case.)
868 // We apply a similar transformation for pointer types to improve the
869 // quality of the generated IR.
Eli Friedmanee945342011-11-18 01:25:50 +0000870 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +0000871 if ((!IsWin32StructABI && SeltTy->isRealFloatingType())
Eli Friedmana98d1f82012-01-25 22:46:34 +0000872 || SeltTy->hasPointerRepresentation())
Eli Friedmanee945342011-11-18 01:25:50 +0000873 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
874
875 // FIXME: We should be able to narrow this integer in cases with dead
876 // padding.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000877 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000878 }
879
Reid Kleckner661f35b2014-01-18 01:12:41 +0000880 return getIndirectReturnResult(State);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000881 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000882
Chris Lattner458b2aa2010-07-29 02:16:43 +0000883 // Treat an enum type as its underlying type.
884 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
885 RetTy = EnumTy->getDecl()->getIntegerType();
886
887 return (RetTy->isPromotableIntegerType() ?
888 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000889}
890
Eli Friedman7919bea2012-06-05 19:40:46 +0000891static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
892 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
893}
894
Daniel Dunbared23de32010-09-16 20:42:00 +0000895static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
896 const RecordType *RT = Ty->getAs<RecordType>();
897 if (!RT)
898 return 0;
899 const RecordDecl *RD = RT->getDecl();
900
901 // If this is a C++ record, check the bases first.
902 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Aaron Ballman574705e2014-03-13 15:41:46 +0000903 for (const auto &I : CXXRD->bases())
904 if (!isRecordWithSSEVectorType(Context, I.getType()))
Daniel Dunbared23de32010-09-16 20:42:00 +0000905 return false;
906
Aaron Ballmane8a8bae2014-03-08 20:12:42 +0000907 for (const auto *i : RD->fields()) {
Daniel Dunbared23de32010-09-16 20:42:00 +0000908 QualType FT = i->getType();
909
Eli Friedman7919bea2012-06-05 19:40:46 +0000910 if (isSSEVectorType(Context, FT))
Daniel Dunbared23de32010-09-16 20:42:00 +0000911 return true;
912
913 if (isRecordWithSSEVectorType(Context, FT))
914 return true;
915 }
916
917 return false;
918}
919
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000920unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
921 unsigned Align) const {
922 // Otherwise, if the alignment is less than or equal to the minimum ABI
923 // alignment, just use the default; the backend will handle this.
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000924 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000925 return 0; // Use default alignment.
926
927 // On non-Darwin, the stack type alignment is always 4.
928 if (!IsDarwinVectorABI) {
929 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000930 return MinABIStackAlignInBytes;
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000931 }
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000932
Daniel Dunbared23de32010-09-16 20:42:00 +0000933 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedman7919bea2012-06-05 19:40:46 +0000934 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
935 isRecordWithSSEVectorType(getContext(), Ty)))
Daniel Dunbared23de32010-09-16 20:42:00 +0000936 return 16;
937
938 return MinABIStackAlignInBytes;
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000939}
940
Rafael Espindola703c47f2012-10-19 05:04:37 +0000941ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
Reid Kleckner661f35b2014-01-18 01:12:41 +0000942 CCState &State) const {
Rafael Espindola703c47f2012-10-19 05:04:37 +0000943 if (!ByVal) {
Reid Kleckner661f35b2014-01-18 01:12:41 +0000944 if (State.FreeRegs) {
945 --State.FreeRegs; // Non-byval indirects just use one pointer.
Rafael Espindola703c47f2012-10-19 05:04:37 +0000946 return ABIArgInfo::getIndirectInReg(0, false);
947 }
Daniel Dunbar53fac692010-04-21 19:49:55 +0000948 return ABIArgInfo::getIndirect(0, false);
Rafael Espindola703c47f2012-10-19 05:04:37 +0000949 }
Daniel Dunbar53fac692010-04-21 19:49:55 +0000950
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000951 // Compute the byval alignment.
952 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
953 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
954 if (StackAlign == 0)
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000955 return ABIArgInfo::getIndirect(4, /*ByVal=*/true);
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000956
957 // If the stack alignment is less than the type alignment, realign the
958 // argument.
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000959 bool Realign = TypeAlign > StackAlign;
960 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true, Realign);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000961}
962
Rafael Espindola06b2b4a2012-07-31 02:44:24 +0000963X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
964 const Type *T = isSingleElementStruct(Ty, getContext());
965 if (!T)
966 T = Ty.getTypePtr();
967
968 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
969 BuiltinType::Kind K = BT->getKind();
970 if (K == BuiltinType::Float || K == BuiltinType::Double)
971 return Float;
972 }
973 return Integer;
974}
975
Reid Kleckner661f35b2014-01-18 01:12:41 +0000976bool X86_32ABIInfo::shouldUseInReg(QualType Ty, CCState &State,
977 bool &NeedsPadding) const {
Rafael Espindolafad28de2012-10-24 01:59:00 +0000978 NeedsPadding = false;
Rafael Espindola06b2b4a2012-07-31 02:44:24 +0000979 Class C = classify(Ty);
980 if (C == Float)
Rafael Espindola703c47f2012-10-19 05:04:37 +0000981 return false;
Rafael Espindola06b2b4a2012-07-31 02:44:24 +0000982
Rafael Espindola077dd592012-10-24 01:58:58 +0000983 unsigned Size = getContext().getTypeSize(Ty);
984 unsigned SizeInRegs = (Size + 31) / 32;
Rafael Espindolae2a9e902012-10-23 02:04:01 +0000985
986 if (SizeInRegs == 0)
987 return false;
988
Reid Kleckner661f35b2014-01-18 01:12:41 +0000989 if (SizeInRegs > State.FreeRegs) {
990 State.FreeRegs = 0;
Rafael Espindola703c47f2012-10-19 05:04:37 +0000991 return false;
Rafael Espindola06b2b4a2012-07-31 02:44:24 +0000992 }
Rafael Espindola703c47f2012-10-19 05:04:37 +0000993
Reid Kleckner661f35b2014-01-18 01:12:41 +0000994 State.FreeRegs -= SizeInRegs;
Rafael Espindola077dd592012-10-24 01:58:58 +0000995
Reid Kleckner80944df2014-10-31 22:00:51 +0000996 if (State.CC == llvm::CallingConv::X86_FastCall ||
997 State.CC == llvm::CallingConv::X86_VectorCall) {
Rafael Espindola077dd592012-10-24 01:58:58 +0000998 if (Size > 32)
999 return false;
1000
1001 if (Ty->isIntegralOrEnumerationType())
1002 return true;
1003
1004 if (Ty->isPointerType())
1005 return true;
1006
1007 if (Ty->isReferenceType())
1008 return true;
1009
Reid Kleckner661f35b2014-01-18 01:12:41 +00001010 if (State.FreeRegs)
Rafael Espindolafad28de2012-10-24 01:59:00 +00001011 NeedsPadding = true;
1012
Rafael Espindola077dd592012-10-24 01:58:58 +00001013 return false;
1014 }
1015
Rafael Espindola703c47f2012-10-19 05:04:37 +00001016 return true;
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001017}
1018
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001019ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
1020 CCState &State) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001021 // FIXME: Set alignment on indirect arguments.
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001022
Reid Klecknerb1be6832014-11-15 01:41:41 +00001023 Ty = useFirstFieldIfTransparentUnion(Ty);
1024
Reid Kleckner80944df2014-10-31 22:00:51 +00001025 // Check with the C++ ABI first.
1026 const RecordType *RT = Ty->getAs<RecordType>();
1027 if (RT) {
1028 CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
1029 if (RAA == CGCXXABI::RAA_Indirect) {
1030 return getIndirectResult(Ty, false, State);
1031 } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
1032 // The field index doesn't matter, we'll fix it up later.
1033 return ABIArgInfo::getInAlloca(/*FieldIndex=*/0);
1034 }
1035 }
1036
1037 // vectorcall adds the concept of a homogenous vector aggregate, similar
1038 // to other targets.
1039 const Type *Base = nullptr;
1040 uint64_t NumElts = 0;
1041 if (State.CC == llvm::CallingConv::X86_VectorCall &&
1042 isHomogeneousAggregate(Ty, Base, NumElts)) {
1043 if (State.FreeSSERegs >= NumElts) {
1044 State.FreeSSERegs -= NumElts;
1045 if (Ty->isBuiltinType() || Ty->isVectorType())
1046 return ABIArgInfo::getDirect();
1047 return ABIArgInfo::getExpand();
1048 }
1049 return getIndirectResult(Ty, /*ByVal=*/false, State);
1050 }
1051
1052 if (isAggregateTypeForABI(Ty)) {
1053 if (RT) {
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001054 // Structs are always byval on win32, regardless of what they contain.
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00001055 if (IsWin32StructABI)
Reid Kleckner661f35b2014-01-18 01:12:41 +00001056 return getIndirectResult(Ty, true, State);
Daniel Dunbar557893d2010-04-21 19:10:51 +00001057
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00001058 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001059 if (RT->getDecl()->hasFlexibleArrayMember())
Reid Kleckner661f35b2014-01-18 01:12:41 +00001060 return getIndirectResult(Ty, true, State);
Anders Carlsson40446e82010-01-27 03:25:19 +00001061 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001062
Eli Friedman9f061a32011-11-18 00:28:11 +00001063 // Ignore empty structs/unions.
Eli Friedmanf22fa9e2011-11-18 04:01:36 +00001064 if (isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001065 return ABIArgInfo::getIgnore();
1066
Rafael Espindolafad28de2012-10-24 01:59:00 +00001067 llvm::LLVMContext &LLVMContext = getVMContext();
1068 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
1069 bool NeedsPadding;
Reid Kleckner661f35b2014-01-18 01:12:41 +00001070 if (shouldUseInReg(Ty, State, NeedsPadding)) {
Rafael Espindola703c47f2012-10-19 05:04:37 +00001071 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Craig Topperac9201a2013-07-08 04:47:18 +00001072 SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32);
Rafael Espindola703c47f2012-10-19 05:04:37 +00001073 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
1074 return ABIArgInfo::getDirectInReg(Result);
1075 }
Craig Topper8a13c412014-05-21 05:09:00 +00001076 llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr;
Rafael Espindola703c47f2012-10-19 05:04:37 +00001077
Daniel Dunbar11c08c82009-11-09 01:33:53 +00001078 // Expand small (<= 128-bit) record types when we know that the stack layout
1079 // of those arguments will match the struct. This is important because the
1080 // LLVM backend isn't smart enough to remove byval, which inhibits many
1081 // optimizations.
Chris Lattner458b2aa2010-07-29 02:16:43 +00001082 if (getContext().getTypeSize(Ty) <= 4*32 &&
1083 canExpandIndirectArgument(Ty, getContext()))
Reid Kleckner661f35b2014-01-18 01:12:41 +00001084 return ABIArgInfo::getExpandWithPadding(
Reid Kleckner80944df2014-10-31 22:00:51 +00001085 State.CC == llvm::CallingConv::X86_FastCall ||
1086 State.CC == llvm::CallingConv::X86_VectorCall,
1087 PaddingType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001088
Reid Kleckner661f35b2014-01-18 01:12:41 +00001089 return getIndirectResult(Ty, true, State);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001090 }
1091
Chris Lattnerd774ae92010-08-26 20:05:13 +00001092 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerd7e54802010-08-26 20:08:43 +00001093 // On Darwin, some vectors are passed in memory, we handle this by passing
1094 // it as an i8/i16/i32/i64.
Chris Lattnerd774ae92010-08-26 20:05:13 +00001095 if (IsDarwinVectorABI) {
1096 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerd774ae92010-08-26 20:05:13 +00001097 if ((Size == 8 || Size == 16 || Size == 32) ||
1098 (Size == 64 && VT->getNumElements() == 1))
1099 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1100 Size));
Chris Lattnerd774ae92010-08-26 20:05:13 +00001101 }
Bill Wendling5cd41c42010-10-18 03:41:31 +00001102
Chad Rosier651c1832013-03-25 21:00:27 +00001103 if (IsX86_MMXType(CGT.ConvertType(Ty)))
1104 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64));
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001105
Chris Lattnerd774ae92010-08-26 20:05:13 +00001106 return ABIArgInfo::getDirect();
1107 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001108
1109
Chris Lattner458b2aa2010-07-29 02:16:43 +00001110 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1111 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +00001112
Rafael Espindolafad28de2012-10-24 01:59:00 +00001113 bool NeedsPadding;
Reid Kleckner661f35b2014-01-18 01:12:41 +00001114 bool InReg = shouldUseInReg(Ty, State, NeedsPadding);
Rafael Espindola703c47f2012-10-19 05:04:37 +00001115
1116 if (Ty->isPromotableIntegerType()) {
1117 if (InReg)
1118 return ABIArgInfo::getExtendInReg();
1119 return ABIArgInfo::getExtend();
1120 }
1121 if (InReg)
1122 return ABIArgInfo::getDirectInReg();
1123 return ABIArgInfo::getDirect();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001124}
1125
Rafael Espindolaa6472962012-07-24 00:01:07 +00001126void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Reid Kleckner661f35b2014-01-18 01:12:41 +00001127 CCState State(FI.getCallingConvention());
1128 if (State.CC == llvm::CallingConv::X86_FastCall)
1129 State.FreeRegs = 2;
Reid Kleckner80944df2014-10-31 22:00:51 +00001130 else if (State.CC == llvm::CallingConv::X86_VectorCall) {
1131 State.FreeRegs = 2;
1132 State.FreeSSERegs = 6;
1133 } else if (FI.getHasRegParm())
Reid Kleckner661f35b2014-01-18 01:12:41 +00001134 State.FreeRegs = FI.getRegParm();
Rafael Espindola077dd592012-10-24 01:58:58 +00001135 else
Reid Kleckner661f35b2014-01-18 01:12:41 +00001136 State.FreeRegs = DefaultNumRegisterParameters;
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001137
Reid Kleckner677539d2014-07-10 01:58:55 +00001138 if (!getCXXABI().classifyReturnType(FI)) {
Reid Kleckner40ca9132014-05-13 22:05:45 +00001139 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State);
Reid Kleckner677539d2014-07-10 01:58:55 +00001140 } else if (FI.getReturnInfo().isIndirect()) {
1141 // The C++ ABI is not aware of register usage, so we have to check if the
1142 // return value was sret and put it in a register ourselves if appropriate.
1143 if (State.FreeRegs) {
1144 --State.FreeRegs; // The sret parameter consumes a register.
1145 FI.getReturnInfo().setInReg(true);
1146 }
1147 }
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001148
Peter Collingbournef7706832014-12-12 23:41:25 +00001149 // The chain argument effectively gives us another free register.
1150 if (FI.isChainCall())
1151 ++State.FreeRegs;
1152
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001153 bool UsedInAlloca = false;
Aaron Ballmanec47bc22014-03-17 18:10:01 +00001154 for (auto &I : FI.arguments()) {
1155 I.info = classifyArgumentType(I.type, State);
1156 UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca);
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001157 }
1158
1159 // If we needed to use inalloca for any argument, do a second pass and rewrite
1160 // all the memory arguments to use inalloca.
1161 if (UsedInAlloca)
1162 rewriteWithInAlloca(FI);
1163}
1164
1165void
1166X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
1167 unsigned &StackOffset,
1168 ABIArgInfo &Info, QualType Type) const {
Reid Klecknerd378a712014-04-10 19:09:43 +00001169 assert(StackOffset % 4U == 0 && "unaligned inalloca struct");
1170 Info = ABIArgInfo::getInAlloca(FrameFields.size());
1171 FrameFields.push_back(CGT.ConvertTypeForMem(Type));
1172 StackOffset += getContext().getTypeSizeInChars(Type).getQuantity();
1173
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001174 // Insert padding bytes to respect alignment. For x86_32, each argument is 4
1175 // byte aligned.
Reid Klecknerd378a712014-04-10 19:09:43 +00001176 if (StackOffset % 4U) {
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001177 unsigned OldOffset = StackOffset;
Reid Klecknerd378a712014-04-10 19:09:43 +00001178 StackOffset = llvm::RoundUpToAlignment(StackOffset, 4U);
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001179 unsigned NumBytes = StackOffset - OldOffset;
1180 assert(NumBytes);
1181 llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext());
1182 Ty = llvm::ArrayType::get(Ty, NumBytes);
1183 FrameFields.push_back(Ty);
1184 }
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001185}
1186
Reid Kleckner852361d2014-07-26 00:12:26 +00001187static bool isArgInAlloca(const ABIArgInfo &Info) {
1188 // Leave ignored and inreg arguments alone.
1189 switch (Info.getKind()) {
1190 case ABIArgInfo::InAlloca:
1191 return true;
1192 case ABIArgInfo::Indirect:
1193 assert(Info.getIndirectByVal());
1194 return true;
1195 case ABIArgInfo::Ignore:
1196 return false;
1197 case ABIArgInfo::Direct:
1198 case ABIArgInfo::Extend:
1199 case ABIArgInfo::Expand:
1200 if (Info.getInReg())
1201 return false;
1202 return true;
1203 }
1204 llvm_unreachable("invalid enum");
1205}
1206
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001207void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const {
1208 assert(IsWin32StructABI && "inalloca only supported on win32");
1209
1210 // Build a packed struct type for all of the arguments in memory.
1211 SmallVector<llvm::Type *, 6> FrameFields;
1212
1213 unsigned StackOffset = 0;
Reid Kleckner852361d2014-07-26 00:12:26 +00001214 CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end();
1215
1216 // Put 'this' into the struct before 'sret', if necessary.
1217 bool IsThisCall =
1218 FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall;
1219 ABIArgInfo &Ret = FI.getReturnInfo();
1220 if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall &&
1221 isArgInAlloca(I->info)) {
1222 addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
1223 ++I;
1224 }
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001225
1226 // Put the sret parameter into the inalloca struct if it's in memory.
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001227 if (Ret.isIndirect() && !Ret.getInReg()) {
1228 CanQualType PtrTy = getContext().getPointerType(FI.getReturnType());
1229 addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy);
Reid Klecknerfab1e892014-02-25 00:59:14 +00001230 // On Windows, the hidden sret parameter is always returned in eax.
1231 Ret.setInAllocaSRet(IsWin32StructABI);
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001232 }
1233
1234 // Skip the 'this' parameter in ecx.
Reid Kleckner852361d2014-07-26 00:12:26 +00001235 if (IsThisCall)
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001236 ++I;
1237
1238 // Put arguments passed in memory into the struct.
1239 for (; I != E; ++I) {
Reid Kleckner852361d2014-07-26 00:12:26 +00001240 if (isArgInAlloca(I->info))
1241 addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001242 }
1243
1244 FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields,
1245 /*isPacked=*/true));
Rafael Espindolaa6472962012-07-24 00:01:07 +00001246}
1247
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001248llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1249 CodeGenFunction &CGF) const {
Chris Lattnerece04092012-02-07 00:39:47 +00001250 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001251
1252 CGBuilderTy &Builder = CGF.Builder;
1253 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1254 "ap");
1255 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Eli Friedman1d7dd3b2011-11-18 02:12:09 +00001256
1257 // Compute if the address needs to be aligned
1258 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
1259 Align = getTypeStackAlignInBytes(Ty, Align);
1260 Align = std::max(Align, 4U);
1261 if (Align > 4) {
1262 // addr = (addr + align - 1) & -align;
1263 llvm::Value *Offset =
1264 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
1265 Addr = CGF.Builder.CreateGEP(Addr, Offset);
1266 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
1267 CGF.Int32Ty);
1268 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
1269 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1270 Addr->getType(),
1271 "ap.cur.aligned");
1272 }
1273
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001274 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00001275 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001276 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1277
1278 uint64_t Offset =
Eli Friedman1d7dd3b2011-11-18 02:12:09 +00001279 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001280 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001281 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001282 "ap.next");
1283 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1284
1285 return AddrTyped;
1286}
1287
Richard Sandiforddcb8d9c2014-07-08 11:10:34 +00001288bool X86_32TargetCodeGenInfo::isStructReturnInRegABI(
1289 const llvm::Triple &Triple, const CodeGenOptions &Opts) {
1290 assert(Triple.getArch() == llvm::Triple::x86);
1291
1292 switch (Opts.getStructReturnConvention()) {
1293 case CodeGenOptions::SRCK_Default:
1294 break;
1295 case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return
1296 return false;
1297 case CodeGenOptions::SRCK_InRegs: // -freg-struct-return
1298 return true;
1299 }
1300
1301 if (Triple.isOSDarwin())
1302 return true;
1303
1304 switch (Triple.getOS()) {
Richard Sandiforddcb8d9c2014-07-08 11:10:34 +00001305 case llvm::Triple::DragonFly:
1306 case llvm::Triple::FreeBSD:
1307 case llvm::Triple::OpenBSD:
1308 case llvm::Triple::Bitrig:
Richard Sandiforddcb8d9c2014-07-08 11:10:34 +00001309 case llvm::Triple::Win32:
Reid Kleckner2918fef2014-11-24 22:05:42 +00001310 return true;
Richard Sandiforddcb8d9c2014-07-08 11:10:34 +00001311 default:
1312 return false;
1313 }
1314}
1315
Charles Davis4ea31ab2010-02-13 15:54:06 +00001316void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
1317 llvm::GlobalValue *GV,
1318 CodeGen::CodeGenModule &CGM) const {
1319 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1320 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
1321 // Get the LLVM function.
1322 llvm::Function *Fn = cast<llvm::Function>(GV);
1323
1324 // Now add the 'alignstack' attribute with a value of 16.
Bill Wendlinga514ebc2012-10-15 20:36:26 +00001325 llvm::AttrBuilder B;
Bill Wendlingccf94c92012-10-14 03:28:14 +00001326 B.addStackAlignmentAttr(16);
Bill Wendling9a677922013-01-23 00:21:06 +00001327 Fn->addAttributes(llvm::AttributeSet::FunctionIndex,
1328 llvm::AttributeSet::get(CGM.getLLVMContext(),
1329 llvm::AttributeSet::FunctionIndex,
1330 B));
Charles Davis4ea31ab2010-02-13 15:54:06 +00001331 }
1332 }
1333}
1334
John McCallbeec5a02010-03-06 00:35:14 +00001335bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
1336 CodeGen::CodeGenFunction &CGF,
1337 llvm::Value *Address) const {
1338 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallbeec5a02010-03-06 00:35:14 +00001339
Chris Lattnerece04092012-02-07 00:39:47 +00001340 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001341
John McCallbeec5a02010-03-06 00:35:14 +00001342 // 0-7 are the eight integer registers; the order is different
1343 // on Darwin (for EH), but the range is the same.
1344 // 8 is %eip.
John McCall943fae92010-05-27 06:19:26 +00001345 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCallbeec5a02010-03-06 00:35:14 +00001346
John McCallc8e01702013-04-16 22:48:15 +00001347 if (CGF.CGM.getTarget().getTriple().isOSDarwin()) {
John McCallbeec5a02010-03-06 00:35:14 +00001348 // 12-16 are st(0..4). Not sure why we stop at 4.
1349 // These have size 16, which is sizeof(long double) on
1350 // platforms with 8-byte alignment for that type.
Chris Lattnerece04092012-02-07 00:39:47 +00001351 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCall943fae92010-05-27 06:19:26 +00001352 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001353
John McCallbeec5a02010-03-06 00:35:14 +00001354 } else {
1355 // 9 is %eflags, which doesn't get a size on Darwin for some
1356 // reason.
1357 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
1358
1359 // 11-16 are st(0..5). Not sure why we stop at 5.
1360 // These have size 12, which is sizeof(long double) on
1361 // platforms with 4-byte alignment for that type.
Chris Lattnerece04092012-02-07 00:39:47 +00001362 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCall943fae92010-05-27 06:19:26 +00001363 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1364 }
John McCallbeec5a02010-03-06 00:35:14 +00001365
1366 return false;
1367}
1368
Chris Lattner0cf24192010-06-28 20:05:43 +00001369//===----------------------------------------------------------------------===//
1370// X86-64 ABI Implementation
1371//===----------------------------------------------------------------------===//
1372
1373
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001374namespace {
1375/// X86_64ABIInfo - The X86_64 ABI information.
1376class X86_64ABIInfo : public ABIInfo {
1377 enum Class {
1378 Integer = 0,
1379 SSE,
1380 SSEUp,
1381 X87,
1382 X87Up,
1383 ComplexX87,
1384 NoClass,
1385 Memory
1386 };
1387
1388 /// merge - Implement the X86_64 ABI merging algorithm.
1389 ///
1390 /// Merge an accumulating classification \arg Accum with a field
1391 /// classification \arg Field.
1392 ///
1393 /// \param Accum - The accumulating classification. This should
1394 /// always be either NoClass or the result of a previous merge
1395 /// call. In addition, this should never be Memory (the caller
1396 /// should just return Memory for the aggregate).
Chris Lattnerd776fb12010-06-28 21:43:59 +00001397 static Class merge(Class Accum, Class Field);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001398
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001399 /// postMerge - Implement the X86_64 ABI post merging algorithm.
1400 ///
1401 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
1402 /// final MEMORY or SSE classes when necessary.
1403 ///
1404 /// \param AggregateSize - The size of the current aggregate in
1405 /// the classification process.
1406 ///
1407 /// \param Lo - The classification for the parts of the type
1408 /// residing in the low word of the containing object.
1409 ///
1410 /// \param Hi - The classification for the parts of the type
1411 /// residing in the higher words of the containing object.
1412 ///
1413 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
1414
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001415 /// classify - Determine the x86_64 register classes in which the
1416 /// given type T should be passed.
1417 ///
1418 /// \param Lo - The classification for the parts of the type
1419 /// residing in the low word of the containing object.
1420 ///
1421 /// \param Hi - The classification for the parts of the type
1422 /// residing in the high word of the containing object.
1423 ///
1424 /// \param OffsetBase - The bit offset of this type in the
1425 /// containing object. Some parameters are classified different
1426 /// depending on whether they straddle an eightbyte boundary.
1427 ///
Eli Friedman96fd2642013-06-12 00:13:45 +00001428 /// \param isNamedArg - Whether the argument in question is a "named"
1429 /// argument, as used in AMD64-ABI 3.5.7.
1430 ///
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001431 /// If a word is unused its result will be NoClass; if a type should
1432 /// be passed in Memory then at least the classification of \arg Lo
1433 /// will be Memory.
1434 ///
Sylvestre Ledru33b5baf2012-09-27 10:16:10 +00001435 /// The \arg Lo class will be NoClass iff the argument is ignored.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001436 ///
1437 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
1438 /// also be ComplexX87.
Eli Friedman96fd2642013-06-12 00:13:45 +00001439 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi,
1440 bool isNamedArg) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001441
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001442 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattnera5f58b02011-07-09 17:41:47 +00001443 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
1444 unsigned IROffset, QualType SourceTy,
1445 unsigned SourceOffset) const;
1446 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
1447 unsigned IROffset, QualType SourceTy,
1448 unsigned SourceOffset) const;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001449
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001450 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar53fac692010-04-21 19:49:55 +00001451 /// such that the argument will be returned in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +00001452 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar53fac692010-04-21 19:49:55 +00001453
1454 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001455 /// such that the argument will be passed in memory.
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00001456 ///
1457 /// \param freeIntRegs - The number of free integer registers remaining
1458 /// available.
1459 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001460
Chris Lattner458b2aa2010-07-29 02:16:43 +00001461 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001462
Bill Wendling5cd41c42010-10-18 03:41:31 +00001463 ABIArgInfo classifyArgumentType(QualType Ty,
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00001464 unsigned freeIntRegs,
Bill Wendling5cd41c42010-10-18 03:41:31 +00001465 unsigned &neededInt,
Eli Friedman96fd2642013-06-12 00:13:45 +00001466 unsigned &neededSSE,
1467 bool isNamedArg) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001468
Eli Friedmanbfd5add2011-12-02 00:11:43 +00001469 bool IsIllegalVectorType(QualType Ty) const;
1470
John McCalle0fda732011-04-21 01:20:55 +00001471 /// The 0.98 ABI revision clarified a lot of ambiguities,
1472 /// unfortunately in ways that were not always consistent with
1473 /// certain previous compilers. In particular, platforms which
1474 /// required strict binary compatibility with older versions of GCC
1475 /// may need to exempt themselves.
1476 bool honorsRevision0_98() const {
John McCallc8e01702013-04-16 22:48:15 +00001477 return !getTarget().getTriple().isOSDarwin();
John McCalle0fda732011-04-21 01:20:55 +00001478 }
1479
Eli Friedmanbfd5add2011-12-02 00:11:43 +00001480 bool HasAVX;
Derek Schuffc7dd7222012-10-11 15:52:22 +00001481 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
1482 // 64-bit hardware.
1483 bool Has64BitPointers;
Eli Friedmanbfd5add2011-12-02 00:11:43 +00001484
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001485public:
Eli Friedmanbfd5add2011-12-02 00:11:43 +00001486 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
Derek Schuffc7dd7222012-10-11 15:52:22 +00001487 ABIInfo(CGT), HasAVX(hasavx),
Derek Schuff8a872f32012-10-11 18:21:13 +00001488 Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
Derek Schuffc7dd7222012-10-11 15:52:22 +00001489 }
Chris Lattner22a931e2010-06-29 06:01:59 +00001490
John McCalla729c622012-02-17 03:33:10 +00001491 bool isPassedUsingAVXType(QualType type) const {
1492 unsigned neededInt, neededSSE;
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00001493 // The freeIntRegs argument doesn't matter here.
Eli Friedman96fd2642013-06-12 00:13:45 +00001494 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE,
1495 /*isNamedArg*/true);
John McCalla729c622012-02-17 03:33:10 +00001496 if (info.isDirect()) {
1497 llvm::Type *ty = info.getCoerceToType();
1498 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
1499 return (vectorTy->getBitWidth() > 128);
1500 }
1501 return false;
1502 }
1503
Craig Topper4f12f102014-03-12 06:41:41 +00001504 void computeInfo(CGFunctionInfo &FI) const override;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001505
Craig Topper4f12f102014-03-12 06:41:41 +00001506 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1507 CodeGenFunction &CGF) const override;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001508};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001509
Chris Lattner04dc9572010-08-31 16:44:54 +00001510/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00001511class WinX86_64ABIInfo : public ABIInfo {
1512
Reid Kleckner80944df2014-10-31 22:00:51 +00001513 ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs,
1514 bool IsReturnType) const;
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00001515
Chris Lattner04dc9572010-08-31 16:44:54 +00001516public:
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00001517 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
1518
Craig Topper4f12f102014-03-12 06:41:41 +00001519 void computeInfo(CGFunctionInfo &FI) const override;
Chris Lattner04dc9572010-08-31 16:44:54 +00001520
Craig Topper4f12f102014-03-12 06:41:41 +00001521 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1522 CodeGenFunction &CGF) const override;
Reid Kleckner80944df2014-10-31 22:00:51 +00001523
1524 bool isHomogeneousAggregateBaseType(QualType Ty) const override {
1525 // FIXME: Assumes vectorcall is in use.
1526 return isX86VectorTypeForVectorCall(getContext(), Ty);
1527 }
1528
1529 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
1530 uint64_t NumMembers) const override {
1531 // FIXME: Assumes vectorcall is in use.
1532 return isX86VectorCallAggregateSmallEnough(NumMembers);
1533 }
Chris Lattner04dc9572010-08-31 16:44:54 +00001534};
1535
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001536class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
Alexander Musman09184fe2014-09-30 05:29:28 +00001537 bool HasAVX;
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001538public:
Eli Friedmanbfd5add2011-12-02 00:11:43 +00001539 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
Alexander Musman09184fe2014-09-30 05:29:28 +00001540 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)), HasAVX(HasAVX) {}
John McCallbeec5a02010-03-06 00:35:14 +00001541
John McCalla729c622012-02-17 03:33:10 +00001542 const X86_64ABIInfo &getABIInfo() const {
1543 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
1544 }
1545
Craig Topper4f12f102014-03-12 06:41:41 +00001546 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
John McCallbeec5a02010-03-06 00:35:14 +00001547 return 7;
1548 }
1549
1550 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00001551 llvm::Value *Address) const override {
Chris Lattnerece04092012-02-07 00:39:47 +00001552 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001553
John McCall943fae92010-05-27 06:19:26 +00001554 // 0-15 are the 16 integer registers.
1555 // 16 is %rip.
Chris Lattnerece04092012-02-07 00:39:47 +00001556 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCallbeec5a02010-03-06 00:35:14 +00001557 return false;
1558 }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +00001559
Jay Foad7c57be32011-07-11 09:56:20 +00001560 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001561 StringRef Constraint,
Craig Topper4f12f102014-03-12 06:41:41 +00001562 llvm::Type* Ty) const override {
Peter Collingbourne8f5cf742011-02-19 23:03:58 +00001563 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1564 }
1565
John McCalla729c622012-02-17 03:33:10 +00001566 bool isNoProtoCallVariadic(const CallArgList &args,
Craig Topper4f12f102014-03-12 06:41:41 +00001567 const FunctionNoProtoType *fnType) const override {
John McCallcbc038a2011-09-21 08:08:30 +00001568 // The default CC on x86-64 sets %al to the number of SSA
1569 // registers used, and GCC sets this when calling an unprototyped
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00001570 // function, so we override the default behavior. However, don't do
Eli Friedmanb8e45b22011-12-06 03:08:26 +00001571 // that when AVX types are involved: the ABI explicitly states it is
1572 // undefined, and it doesn't work in practice because of how the ABI
1573 // defines varargs anyway.
Reid Kleckner78af0702013-08-27 23:08:25 +00001574 if (fnType->getCallConv() == CC_C) {
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00001575 bool HasAVXType = false;
John McCalla729c622012-02-17 03:33:10 +00001576 for (CallArgList::const_iterator
1577 it = args.begin(), ie = args.end(); it != ie; ++it) {
1578 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1579 HasAVXType = true;
1580 break;
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00001581 }
1582 }
John McCalla729c622012-02-17 03:33:10 +00001583
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00001584 if (!HasAVXType)
1585 return true;
1586 }
John McCallcbc038a2011-09-21 08:08:30 +00001587
John McCalla729c622012-02-17 03:33:10 +00001588 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
John McCallcbc038a2011-09-21 08:08:30 +00001589 }
1590
Craig Topper4f12f102014-03-12 06:41:41 +00001591 llvm::Constant *
1592 getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
Peter Collingbourneb453cd62013-10-20 21:29:19 +00001593 unsigned Sig = (0xeb << 0) | // jmp rel8
1594 (0x0a << 8) | // .+0x0c
1595 ('F' << 16) |
1596 ('T' << 24);
1597 return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
1598 }
1599
Alexander Musman09184fe2014-09-30 05:29:28 +00001600 unsigned getOpenMPSimdDefaultAlignment(QualType) const override {
1601 return HasAVX ? 32 : 16;
1602 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001603};
1604
Aaron Ballmanef50ee92013-05-24 15:06:56 +00001605static std::string qualifyWindowsLibrary(llvm::StringRef Lib) {
1606 // If the argument does not end in .lib, automatically add the suffix. This
1607 // matches the behavior of MSVC.
1608 std::string ArgStr = Lib;
Rui Ueyama727025a2013-10-31 19:12:53 +00001609 if (!Lib.endswith_lower(".lib"))
Aaron Ballmanef50ee92013-05-24 15:06:56 +00001610 ArgStr += ".lib";
Aaron Ballmanef50ee92013-05-24 15:06:56 +00001611 return ArgStr;
1612}
1613
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001614class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo {
1615public:
John McCall1fe2a8c2013-06-18 02:46:29 +00001616 WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
1617 bool d, bool p, bool w, unsigned RegParms)
1618 : X86_32TargetCodeGenInfo(CGT, d, p, w, RegParms) {}
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001619
1620 void getDependentLibraryOption(llvm::StringRef Lib,
Craig Topper4f12f102014-03-12 06:41:41 +00001621 llvm::SmallString<24> &Opt) const override {
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001622 Opt = "/DEFAULTLIB:";
Aaron Ballmanef50ee92013-05-24 15:06:56 +00001623 Opt += qualifyWindowsLibrary(Lib);
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001624 }
Aaron Ballman5d041be2013-06-04 02:07:14 +00001625
1626 void getDetectMismatchOption(llvm::StringRef Name,
1627 llvm::StringRef Value,
Craig Topper4f12f102014-03-12 06:41:41 +00001628 llvm::SmallString<32> &Opt) const override {
Eli Friedmanf60b8ce2013-06-07 22:42:22 +00001629 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
Aaron Ballman5d041be2013-06-04 02:07:14 +00001630 }
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001631};
1632
Chris Lattner04dc9572010-08-31 16:44:54 +00001633class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
Alexander Musman09184fe2014-09-30 05:29:28 +00001634 bool HasAVX;
Chris Lattner04dc9572010-08-31 16:44:54 +00001635public:
Alexander Musman09184fe2014-09-30 05:29:28 +00001636 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
1637 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)), HasAVX(HasAVX) {}
Chris Lattner04dc9572010-08-31 16:44:54 +00001638
Craig Topper4f12f102014-03-12 06:41:41 +00001639 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
Chris Lattner04dc9572010-08-31 16:44:54 +00001640 return 7;
1641 }
1642
1643 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00001644 llvm::Value *Address) const override {
Chris Lattnerece04092012-02-07 00:39:47 +00001645 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001646
Chris Lattner04dc9572010-08-31 16:44:54 +00001647 // 0-15 are the 16 integer registers.
1648 // 16 is %rip.
Chris Lattnerece04092012-02-07 00:39:47 +00001649 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattner04dc9572010-08-31 16:44:54 +00001650 return false;
1651 }
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001652
1653 void getDependentLibraryOption(llvm::StringRef Lib,
Craig Topper4f12f102014-03-12 06:41:41 +00001654 llvm::SmallString<24> &Opt) const override {
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001655 Opt = "/DEFAULTLIB:";
Aaron Ballmanef50ee92013-05-24 15:06:56 +00001656 Opt += qualifyWindowsLibrary(Lib);
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001657 }
Aaron Ballman5d041be2013-06-04 02:07:14 +00001658
1659 void getDetectMismatchOption(llvm::StringRef Name,
1660 llvm::StringRef Value,
Craig Topper4f12f102014-03-12 06:41:41 +00001661 llvm::SmallString<32> &Opt) const override {
Eli Friedmanf60b8ce2013-06-07 22:42:22 +00001662 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
Aaron Ballman5d041be2013-06-04 02:07:14 +00001663 }
Alexander Musman09184fe2014-09-30 05:29:28 +00001664
1665 unsigned getOpenMPSimdDefaultAlignment(QualType) const override {
1666 return HasAVX ? 32 : 16;
1667 }
Chris Lattner04dc9572010-08-31 16:44:54 +00001668};
1669
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001670}
1671
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001672void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1673 Class &Hi) const {
1674 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1675 //
1676 // (a) If one of the classes is Memory, the whole argument is passed in
1677 // memory.
1678 //
1679 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1680 // memory.
1681 //
1682 // (c) If the size of the aggregate exceeds two eightbytes and the first
1683 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1684 // argument is passed in memory. NOTE: This is necessary to keep the
1685 // ABI working for processors that don't support the __m256 type.
1686 //
1687 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1688 //
1689 // Some of these are enforced by the merging logic. Others can arise
1690 // only with unions; for example:
1691 // union { _Complex double; unsigned; }
1692 //
1693 // Note that clauses (b) and (c) were added in 0.98.
1694 //
1695 if (Hi == Memory)
1696 Lo = Memory;
1697 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1698 Lo = Memory;
1699 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1700 Lo = Memory;
1701 if (Hi == SSEUp && Lo != SSE)
1702 Hi = SSE;
1703}
1704
Chris Lattnerd776fb12010-06-28 21:43:59 +00001705X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001706 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1707 // classified recursively so that always two fields are
1708 // considered. The resulting class is calculated according to
1709 // the classes of the fields in the eightbyte:
1710 //
1711 // (a) If both classes are equal, this is the resulting class.
1712 //
1713 // (b) If one of the classes is NO_CLASS, the resulting class is
1714 // the other class.
1715 //
1716 // (c) If one of the classes is MEMORY, the result is the MEMORY
1717 // class.
1718 //
1719 // (d) If one of the classes is INTEGER, the result is the
1720 // INTEGER.
1721 //
1722 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1723 // MEMORY is used as class.
1724 //
1725 // (f) Otherwise class SSE is used.
1726
1727 // Accum should never be memory (we should have returned) or
1728 // ComplexX87 (because this cannot be passed in a structure).
1729 assert((Accum != Memory && Accum != ComplexX87) &&
1730 "Invalid accumulated classification during merge.");
1731 if (Accum == Field || Field == NoClass)
1732 return Accum;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001733 if (Field == Memory)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001734 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001735 if (Accum == NoClass)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001736 return Field;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001737 if (Accum == Integer || Field == Integer)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001738 return Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001739 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1740 Accum == X87 || Accum == X87Up)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001741 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001742 return SSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001743}
1744
Chris Lattner5c740f12010-06-30 19:14:05 +00001745void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Eli Friedman96fd2642013-06-12 00:13:45 +00001746 Class &Lo, Class &Hi, bool isNamedArg) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001747 // FIXME: This code can be simplified by introducing a simple value class for
1748 // Class pairs with appropriate constructor methods for the various
1749 // situations.
1750
1751 // FIXME: Some of the split computations are wrong; unaligned vectors
1752 // shouldn't be passed in registers for example, so there is no chance they
1753 // can straddle an eightbyte. Verify & simplify.
1754
1755 Lo = Hi = NoClass;
1756
1757 Class &Current = OffsetBase < 64 ? Lo : Hi;
1758 Current = Memory;
1759
John McCall9dd450b2009-09-21 23:43:11 +00001760 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001761 BuiltinType::Kind k = BT->getKind();
1762
1763 if (k == BuiltinType::Void) {
1764 Current = NoClass;
1765 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1766 Lo = Integer;
1767 Hi = Integer;
1768 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1769 Current = Integer;
Derek Schuff57b7e8f2012-10-11 16:55:58 +00001770 } else if ((k == BuiltinType::Float || k == BuiltinType::Double) ||
1771 (k == BuiltinType::LongDouble &&
Cameron Esfahani556d91e2013-09-14 01:09:11 +00001772 getTarget().getTriple().isOSNaCl())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001773 Current = SSE;
1774 } else if (k == BuiltinType::LongDouble) {
1775 Lo = X87;
1776 Hi = X87Up;
1777 }
1778 // FIXME: _Decimal32 and _Decimal64 are SSE.
1779 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattnerd776fb12010-06-28 21:43:59 +00001780 return;
1781 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001782
Chris Lattnerd776fb12010-06-28 21:43:59 +00001783 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001784 // Classify the underlying integer type.
Eli Friedman96fd2642013-06-12 00:13:45 +00001785 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg);
Chris Lattnerd776fb12010-06-28 21:43:59 +00001786 return;
1787 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001788
Chris Lattnerd776fb12010-06-28 21:43:59 +00001789 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001790 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001791 return;
1792 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001793
Chris Lattnerd776fb12010-06-28 21:43:59 +00001794 if (Ty->isMemberPointerType()) {
Jan Wen Voung01c21e82014-10-02 16:56:57 +00001795 if (Ty->isMemberFunctionPointerType()) {
1796 if (Has64BitPointers) {
1797 // If Has64BitPointers, this is an {i64, i64}, so classify both
1798 // Lo and Hi now.
1799 Lo = Hi = Integer;
1800 } else {
1801 // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that
1802 // straddles an eightbyte boundary, Hi should be classified as well.
1803 uint64_t EB_FuncPtr = (OffsetBase) / 64;
1804 uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64;
1805 if (EB_FuncPtr != EB_ThisAdj) {
1806 Lo = Hi = Integer;
1807 } else {
1808 Current = Integer;
1809 }
1810 }
1811 } else {
Daniel Dunbar36d4d152010-05-15 00:00:37 +00001812 Current = Integer;
Jan Wen Voung01c21e82014-10-02 16:56:57 +00001813 }
Chris Lattnerd776fb12010-06-28 21:43:59 +00001814 return;
1815 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001816
Chris Lattnerd776fb12010-06-28 21:43:59 +00001817 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001818 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001819 if (Size == 32) {
1820 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1821 // float> as integer.
1822 Current = Integer;
1823
1824 // If this type crosses an eightbyte boundary, it should be
1825 // split.
1826 uint64_t EB_Real = (OffsetBase) / 64;
1827 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1828 if (EB_Real != EB_Imag)
1829 Hi = Lo;
1830 } else if (Size == 64) {
1831 // gcc passes <1 x double> in memory. :(
1832 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1833 return;
1834
1835 // gcc passes <1 x long long> as INTEGER.
Chris Lattner46830f22010-08-26 18:03:20 +00001836 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner69e683f2010-08-26 18:13:50 +00001837 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1838 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1839 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001840 Current = Integer;
1841 else
1842 Current = SSE;
1843
1844 // If this type crosses an eightbyte boundary, it should be
1845 // split.
1846 if (OffsetBase && OffsetBase != 64)
1847 Hi = Lo;
Eli Friedman96fd2642013-06-12 00:13:45 +00001848 } else if (Size == 128 || (HasAVX && isNamedArg && Size == 256)) {
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001849 // Arguments of 256-bits are split into four eightbyte chunks. The
1850 // least significant one belongs to class SSE and all the others to class
1851 // SSEUP. The original Lo and Hi design considers that types can't be
1852 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1853 // This design isn't correct for 256-bits, but since there're no cases
1854 // where the upper parts would need to be inspected, avoid adding
1855 // complexity and just consider Hi to match the 64-256 part.
Eli Friedman96fd2642013-06-12 00:13:45 +00001856 //
1857 // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in
1858 // registers if they are "named", i.e. not part of the "..." of a
1859 // variadic function.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001860 Lo = SSE;
1861 Hi = SSEUp;
1862 }
Chris Lattnerd776fb12010-06-28 21:43:59 +00001863 return;
1864 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001865
Chris Lattnerd776fb12010-06-28 21:43:59 +00001866 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001867 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001868
Chris Lattner2b037972010-07-29 02:01:43 +00001869 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregorb90df602010-06-16 00:17:44 +00001870 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001871 if (Size <= 64)
1872 Current = Integer;
1873 else if (Size <= 128)
1874 Lo = Hi = Integer;
Chris Lattner2b037972010-07-29 02:01:43 +00001875 } else if (ET == getContext().FloatTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001876 Current = SSE;
Derek Schuff57b7e8f2012-10-11 16:55:58 +00001877 else if (ET == getContext().DoubleTy ||
1878 (ET == getContext().LongDoubleTy &&
Cameron Esfahani556d91e2013-09-14 01:09:11 +00001879 getTarget().getTriple().isOSNaCl()))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001880 Lo = Hi = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +00001881 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001882 Current = ComplexX87;
1883
1884 // If this complex type crosses an eightbyte boundary then it
1885 // should be split.
1886 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattner2b037972010-07-29 02:01:43 +00001887 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001888 if (Hi == NoClass && EB_Real != EB_Imag)
1889 Hi = Lo;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001890
Chris Lattnerd776fb12010-06-28 21:43:59 +00001891 return;
1892 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001893
Chris Lattner2b037972010-07-29 02:01:43 +00001894 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001895 // Arrays are treated like structures.
1896
Chris Lattner2b037972010-07-29 02:01:43 +00001897 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001898
1899 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001900 // than four eightbytes, ..., it has class MEMORY.
1901 if (Size > 256)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001902 return;
1903
1904 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1905 // fields, it has class MEMORY.
1906 //
1907 // Only need to check alignment of array base.
Chris Lattner2b037972010-07-29 02:01:43 +00001908 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001909 return;
1910
1911 // Otherwise implement simplified merge. We could be smarter about
1912 // this, but it isn't worth it and would be harder to verify.
1913 Current = NoClass;
Chris Lattner2b037972010-07-29 02:01:43 +00001914 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001915 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes75541d02011-07-12 01:27:38 +00001916
1917 // The only case a 256-bit wide vector could be used is when the array
1918 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1919 // to work for sizes wider than 128, early check and fallback to memory.
1920 if (Size > 128 && EltSize != 256)
1921 return;
1922
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001923 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1924 Class FieldLo, FieldHi;
Eli Friedman96fd2642013-06-12 00:13:45 +00001925 classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001926 Lo = merge(Lo, FieldLo);
1927 Hi = merge(Hi, FieldHi);
1928 if (Lo == Memory || Hi == Memory)
1929 break;
1930 }
1931
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001932 postMerge(Size, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001933 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattnerd776fb12010-06-28 21:43:59 +00001934 return;
1935 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001936
Chris Lattnerd776fb12010-06-28 21:43:59 +00001937 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001938 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001939
1940 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001941 // than four eightbytes, ..., it has class MEMORY.
1942 if (Size > 256)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001943 return;
1944
Anders Carlsson20759ad2009-09-16 15:53:40 +00001945 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1946 // copy constructor or a non-trivial destructor, it is passed by invisible
1947 // reference.
Mark Lacey3825e832013-10-06 01:33:34 +00001948 if (getRecordArgABI(RT, getCXXABI()))
Anders Carlsson20759ad2009-09-16 15:53:40 +00001949 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001950
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001951 const RecordDecl *RD = RT->getDecl();
1952
1953 // Assume variable sized types are passed in memory.
1954 if (RD->hasFlexibleArrayMember())
1955 return;
1956
Chris Lattner2b037972010-07-29 02:01:43 +00001957 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001958
1959 // Reset Lo class, this will be recomputed.
1960 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001961
1962 // If this is a C++ record, classify the bases first.
1963 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
Aaron Ballman574705e2014-03-13 15:41:46 +00001964 for (const auto &I : CXXRD->bases()) {
1965 assert(!I.isVirtual() && !I.getType()->isDependentType() &&
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001966 "Unexpected base class!");
1967 const CXXRecordDecl *Base =
Aaron Ballman574705e2014-03-13 15:41:46 +00001968 cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001969
1970 // Classify this field.
1971 //
1972 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1973 // single eightbyte, each is classified separately. Each eightbyte gets
1974 // initialized to class NO_CLASS.
1975 Class FieldLo, FieldHi;
Benjamin Kramer2ef30312012-07-04 18:45:14 +00001976 uint64_t Offset =
1977 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
Aaron Ballman574705e2014-03-13 15:41:46 +00001978 classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg);
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001979 Lo = merge(Lo, FieldLo);
1980 Hi = merge(Hi, FieldHi);
1981 if (Lo == Memory || Hi == Memory)
1982 break;
1983 }
1984 }
1985
1986 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001987 unsigned idx = 0;
Bruno Cardoso Lopes0aadf832011-07-12 22:30:58 +00001988 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001989 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001990 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1991 bool BitField = i->isBitField();
1992
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00001993 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1994 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001995 //
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00001996 // The only case a 256-bit wide vector could be used is when the struct
1997 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1998 // to work for sizes wider than 128, early check and fallback to memory.
1999 //
2000 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
2001 Lo = Memory;
2002 return;
2003 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002004 // Note, skip this test for bit-fields, see below.
Chris Lattner2b037972010-07-29 02:01:43 +00002005 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002006 Lo = Memory;
2007 return;
2008 }
2009
2010 // Classify this field.
2011 //
2012 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
2013 // exceeds a single eightbyte, each is classified
2014 // separately. Each eightbyte gets initialized to class
2015 // NO_CLASS.
2016 Class FieldLo, FieldHi;
2017
2018 // Bit-fields require special handling, they do not force the
2019 // structure to be passed in memory even if unaligned, and
2020 // therefore they can straddle an eightbyte.
2021 if (BitField) {
2022 // Ignore padding bit-fields.
2023 if (i->isUnnamedBitfield())
2024 continue;
2025
2026 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smithcaf33902011-10-10 18:28:20 +00002027 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002028
2029 uint64_t EB_Lo = Offset / 64;
2030 uint64_t EB_Hi = (Offset + Size - 1) / 64;
Sylvestre Ledru0c4813e2013-10-06 09:54:18 +00002031
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002032 if (EB_Lo) {
2033 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
2034 FieldLo = NoClass;
2035 FieldHi = Integer;
2036 } else {
2037 FieldLo = Integer;
2038 FieldHi = EB_Hi ? Integer : NoClass;
2039 }
2040 } else
Eli Friedman96fd2642013-06-12 00:13:45 +00002041 classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002042 Lo = merge(Lo, FieldLo);
2043 Hi = merge(Hi, FieldHi);
2044 if (Lo == Memory || Hi == Memory)
2045 break;
2046 }
2047
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002048 postMerge(Size, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002049 }
2050}
2051
Chris Lattner22a931e2010-06-29 06:01:59 +00002052ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +00002053 // If this is a scalar LLVM value then assume LLVM will pass it in the right
2054 // place naturally.
John McCalla1dee5302010-08-22 10:59:02 +00002055 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar53fac692010-04-21 19:49:55 +00002056 // Treat an enum type as its underlying type.
2057 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2058 Ty = EnumTy->getDecl()->getIntegerType();
2059
2060 return (Ty->isPromotableIntegerType() ?
2061 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2062 }
2063
2064 return ABIArgInfo::getIndirect(0);
2065}
2066
Eli Friedmanbfd5add2011-12-02 00:11:43 +00002067bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
2068 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
2069 uint64_t Size = getContext().getTypeSize(VecTy);
2070 unsigned LargestVector = HasAVX ? 256 : 128;
2071 if (Size <= 64 || Size > LargestVector)
2072 return true;
2073 }
2074
2075 return false;
2076}
2077
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002078ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
2079 unsigned freeIntRegs) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002080 // If this is a scalar LLVM value then assume LLVM will pass it in the right
2081 // place naturally.
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002082 //
2083 // This assumption is optimistic, as there could be free registers available
2084 // when we need to pass this argument in memory, and LLVM could try to pass
2085 // the argument in the free register. This does not seem to happen currently,
2086 // but this code would be much safer if we could mark the argument with
2087 // 'onstack'. See PR12193.
Eli Friedmanbfd5add2011-12-02 00:11:43 +00002088 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002089 // Treat an enum type as its underlying type.
2090 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2091 Ty = EnumTy->getDecl()->getIntegerType();
2092
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002093 return (Ty->isPromotableIntegerType() ?
2094 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002095 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002096
Mark Lacey3825e832013-10-06 01:33:34 +00002097 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00002098 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Anders Carlsson20759ad2009-09-16 15:53:40 +00002099
Chris Lattner44c2b902011-05-22 23:21:23 +00002100 // Compute the byval alignment. We specify the alignment of the byval in all
2101 // cases so that the mid-level optimizer knows the alignment of the byval.
2102 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002103
2104 // Attempt to avoid passing indirect results using byval when possible. This
2105 // is important for good codegen.
2106 //
2107 // We do this by coercing the value into a scalar type which the backend can
2108 // handle naturally (i.e., without using byval).
2109 //
2110 // For simplicity, we currently only do this when we have exhausted all of the
2111 // free integer registers. Doing this when there are free integer registers
2112 // would require more care, as we would have to ensure that the coerced value
2113 // did not claim the unused register. That would require either reording the
2114 // arguments to the function (so that any subsequent inreg values came first),
2115 // or only doing this optimization when there were no following arguments that
2116 // might be inreg.
2117 //
2118 // We currently expect it to be rare (particularly in well written code) for
2119 // arguments to be passed on the stack when there are still free integer
2120 // registers available (this would typically imply large structs being passed
2121 // by value), so this seems like a fair tradeoff for now.
2122 //
2123 // We can revisit this if the backend grows support for 'onstack' parameter
2124 // attributes. See PR12193.
2125 if (freeIntRegs == 0) {
2126 uint64_t Size = getContext().getTypeSize(Ty);
2127
2128 // If this type fits in an eightbyte, coerce it into the matching integral
2129 // type, which will end up on the stack (with alignment 8).
2130 if (Align == 8 && Size <= 64)
2131 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2132 Size));
2133 }
2134
Chris Lattner44c2b902011-05-22 23:21:23 +00002135 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002136}
2137
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002138/// GetByteVectorType - The ABI specifies that a value should be passed in an
2139/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner4200fe42010-07-29 04:56:46 +00002140/// vector register.
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002141llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattnera5f58b02011-07-09 17:41:47 +00002142 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002143
Chris Lattner9fa15c32010-07-29 05:02:29 +00002144 // Wrapper structs that just contain vectors are passed just like vectors,
2145 // strip them off if present.
Chris Lattnera5f58b02011-07-09 17:41:47 +00002146 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner9fa15c32010-07-29 05:02:29 +00002147 while (STy && STy->getNumElements() == 1) {
2148 IRType = STy->getElementType(0);
2149 STy = dyn_cast<llvm::StructType>(IRType);
2150 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002151
Bruno Cardoso Lopes129b4cc2011-07-08 22:57:35 +00002152 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattnera5f58b02011-07-09 17:41:47 +00002153 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
2154 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002155 unsigned BitWidth = VT->getBitWidth();
Tanya Lattner71f1b2d2011-11-28 23:18:11 +00002156 if ((BitWidth >= 128 && BitWidth <= 256) &&
Chris Lattner4200fe42010-07-29 04:56:46 +00002157 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
2158 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
2159 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
2160 EltTy->isIntegerTy(128)))
2161 return VT;
2162 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002163
Chris Lattner4200fe42010-07-29 04:56:46 +00002164 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
2165}
2166
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002167/// BitsContainNoUserData - Return true if the specified [start,end) bit range
2168/// is known to either be off the end of the specified type or being in
2169/// alignment padding. The user type specified is known to be at most 128 bits
2170/// in size, and have passed through X86_64ABIInfo::classify with a successful
2171/// classification that put one of the two halves in the INTEGER class.
2172///
2173/// It is conservatively correct to return false.
2174static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
2175 unsigned EndBit, ASTContext &Context) {
2176 // If the bytes being queried are off the end of the type, there is no user
2177 // data hiding here. This handles analysis of builtins, vectors and other
2178 // types that don't contain interesting padding.
2179 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
2180 if (TySize <= StartBit)
2181 return true;
2182
Chris Lattner98076a22010-07-29 07:43:55 +00002183 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2184 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
2185 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
2186
2187 // Check each element to see if the element overlaps with the queried range.
2188 for (unsigned i = 0; i != NumElts; ++i) {
2189 // If the element is after the span we care about, then we're done..
2190 unsigned EltOffset = i*EltSize;
2191 if (EltOffset >= EndBit) break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002192
Chris Lattner98076a22010-07-29 07:43:55 +00002193 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
2194 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
2195 EndBit-EltOffset, Context))
2196 return false;
2197 }
2198 // If it overlaps no elements, then it is safe to process as padding.
2199 return true;
2200 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002201
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002202 if (const RecordType *RT = Ty->getAs<RecordType>()) {
2203 const RecordDecl *RD = RT->getDecl();
2204 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002205
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002206 // If this is a C++ record, check the bases first.
2207 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
Aaron Ballman574705e2014-03-13 15:41:46 +00002208 for (const auto &I : CXXRD->bases()) {
2209 assert(!I.isVirtual() && !I.getType()->isDependentType() &&
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002210 "Unexpected base class!");
2211 const CXXRecordDecl *Base =
Aaron Ballman574705e2014-03-13 15:41:46 +00002212 cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002213
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002214 // If the base is after the span we care about, ignore it.
Benjamin Kramer2ef30312012-07-04 18:45:14 +00002215 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002216 if (BaseOffset >= EndBit) continue;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002217
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002218 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
Aaron Ballman574705e2014-03-13 15:41:46 +00002219 if (!BitsContainNoUserData(I.getType(), BaseStart,
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002220 EndBit-BaseOffset, Context))
2221 return false;
2222 }
2223 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002224
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002225 // Verify that no field has data that overlaps the region of interest. Yes
2226 // this could be sped up a lot by being smarter about queried fields,
2227 // however we're only looking at structs up to 16 bytes, so we don't care
2228 // much.
2229 unsigned idx = 0;
2230 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2231 i != e; ++i, ++idx) {
2232 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002233
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002234 // If we found a field after the region we care about, then we're done.
2235 if (FieldOffset >= EndBit) break;
2236
2237 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
2238 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
2239 Context))
2240 return false;
2241 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002242
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002243 // If nothing in this record overlapped the area of interest, then we're
2244 // clean.
2245 return true;
2246 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002247
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002248 return false;
2249}
2250
Chris Lattnere556a712010-07-29 18:39:32 +00002251/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
2252/// float member at the specified offset. For example, {int,{float}} has a
2253/// float at offset 4. It is conservatively correct for this routine to return
2254/// false.
Chris Lattner2192fe52011-07-18 04:24:23 +00002255static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Micah Villmowdd31ca12012-10-08 16:25:52 +00002256 const llvm::DataLayout &TD) {
Chris Lattnere556a712010-07-29 18:39:32 +00002257 // Base case if we find a float.
2258 if (IROffset == 0 && IRType->isFloatTy())
2259 return true;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002260
Chris Lattnere556a712010-07-29 18:39:32 +00002261 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2192fe52011-07-18 04:24:23 +00002262 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnere556a712010-07-29 18:39:32 +00002263 const llvm::StructLayout *SL = TD.getStructLayout(STy);
2264 unsigned Elt = SL->getElementContainingOffset(IROffset);
2265 IROffset -= SL->getElementOffset(Elt);
2266 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
2267 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002268
Chris Lattnere556a712010-07-29 18:39:32 +00002269 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2192fe52011-07-18 04:24:23 +00002270 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
2271 llvm::Type *EltTy = ATy->getElementType();
Chris Lattnere556a712010-07-29 18:39:32 +00002272 unsigned EltSize = TD.getTypeAllocSize(EltTy);
2273 IROffset -= IROffset/EltSize*EltSize;
2274 return ContainsFloatAtOffset(EltTy, IROffset, TD);
2275 }
2276
2277 return false;
2278}
2279
Chris Lattner7f4b81a2010-07-29 18:13:09 +00002280
2281/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
2282/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattnera5f58b02011-07-09 17:41:47 +00002283llvm::Type *X86_64ABIInfo::
2284GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner7f4b81a2010-07-29 18:13:09 +00002285 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattner50a357e2010-07-29 18:19:50 +00002286 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattner7f4b81a2010-07-29 18:13:09 +00002287 // pass as float if the last 4 bytes is just padding. This happens for
2288 // structs that contain 3 floats.
2289 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
2290 SourceOffset*8+64, getContext()))
2291 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002292
Chris Lattnere556a712010-07-29 18:39:32 +00002293 // We want to pass as <2 x float> if the LLVM IR type contains a float at
2294 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
2295 // case.
Micah Villmowdd31ca12012-10-08 16:25:52 +00002296 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
2297 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
Chris Lattner9f8b4512010-08-25 23:39:14 +00002298 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002299
Chris Lattner7f4b81a2010-07-29 18:13:09 +00002300 return llvm::Type::getDoubleTy(getVMContext());
2301}
2302
2303
Chris Lattner1c56d9a2010-07-29 17:40:35 +00002304/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
2305/// an 8-byte GPR. This means that we either have a scalar or we are talking
2306/// about the high or low part of an up-to-16-byte struct. This routine picks
2307/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002308/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
2309/// etc).
2310///
2311/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
2312/// the source type. IROffset is an offset in bytes into the LLVM IR type that
2313/// the 8-byte value references. PrefType may be null.
2314///
Alp Toker9907f082014-07-09 14:06:35 +00002315/// SourceTy is the source-level type for the entire argument. SourceOffset is
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002316/// an offset into this that we're processing (which is always either 0 or 8).
2317///
Chris Lattnera5f58b02011-07-09 17:41:47 +00002318llvm::Type *X86_64ABIInfo::
2319GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner1c56d9a2010-07-29 17:40:35 +00002320 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002321 // If we're dealing with an un-offset LLVM IR type, then it means that we're
2322 // returning an 8-byte unit starting with it. See if we can safely use it.
2323 if (IROffset == 0) {
2324 // Pointers and int64's always fill the 8-byte unit.
Derek Schuffc7dd7222012-10-11 15:52:22 +00002325 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
2326 IRType->isIntegerTy(64))
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002327 return IRType;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002328
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002329 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
2330 // goodness in the source type is just tail padding. This is allowed to
2331 // kick in for struct {double,int} on the int, but not on
2332 // struct{double,int,int} because we wouldn't return the second int. We
2333 // have to do this analysis on the source type because we can't depend on
2334 // unions being lowered a specific way etc.
2335 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
Derek Schuffc7dd7222012-10-11 15:52:22 +00002336 IRType->isIntegerTy(32) ||
2337 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
2338 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
2339 cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002340
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002341 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
2342 SourceOffset*8+64, getContext()))
2343 return IRType;
2344 }
2345 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002346
Chris Lattner2192fe52011-07-18 04:24:23 +00002347 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002348 // If this is a struct, recurse into the field at the specified offset.
Micah Villmowdd31ca12012-10-08 16:25:52 +00002349 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002350 if (IROffset < SL->getSizeInBytes()) {
2351 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
2352 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002353
Chris Lattner1c56d9a2010-07-29 17:40:35 +00002354 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
2355 SourceTy, SourceOffset);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002356 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002357 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002358
Chris Lattner2192fe52011-07-18 04:24:23 +00002359 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattnera5f58b02011-07-09 17:41:47 +00002360 llvm::Type *EltTy = ATy->getElementType();
Micah Villmowdd31ca12012-10-08 16:25:52 +00002361 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
Chris Lattner98076a22010-07-29 07:43:55 +00002362 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner1c56d9a2010-07-29 17:40:35 +00002363 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
2364 SourceOffset);
Chris Lattner98076a22010-07-29 07:43:55 +00002365 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002366
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002367 // Okay, we don't have any better idea of what to pass, so we pass this in an
2368 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner3f763422010-07-29 17:34:39 +00002369 unsigned TySizeInBytes =
2370 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002371
Chris Lattner3f763422010-07-29 17:34:39 +00002372 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002373
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002374 // It is always safe to classify this as an integer type up to i64 that
2375 // isn't larger than the structure.
Chris Lattner3f763422010-07-29 17:34:39 +00002376 return llvm::IntegerType::get(getVMContext(),
2377 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner22a931e2010-06-29 06:01:59 +00002378}
2379
Chris Lattnerd426c8e2010-09-01 00:50:20 +00002380
2381/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
2382/// be used as elements of a two register pair to pass or return, return a
2383/// first class aggregate to represent them. For example, if the low part of
2384/// a by-value argument should be passed as i32* and the high part as float,
2385/// return {i32*, float}.
Chris Lattnera5f58b02011-07-09 17:41:47 +00002386static llvm::Type *
Jay Foad7c57be32011-07-11 09:56:20 +00002387GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Micah Villmowdd31ca12012-10-08 16:25:52 +00002388 const llvm::DataLayout &TD) {
Chris Lattnerd426c8e2010-09-01 00:50:20 +00002389 // In order to correctly satisfy the ABI, we need to the high part to start
2390 // at offset 8. If the high and low parts we inferred are both 4-byte types
2391 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
2392 // the second element at offset 8. Check for this:
2393 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
2394 unsigned HiAlign = TD.getABITypeAlignment(Hi);
David Majnemered684072014-10-20 06:13:36 +00002395 unsigned HiStart = llvm::RoundUpToAlignment(LoSize, HiAlign);
Chris Lattnerd426c8e2010-09-01 00:50:20 +00002396 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002397
Chris Lattnerd426c8e2010-09-01 00:50:20 +00002398 // To handle this, we have to increase the size of the low part so that the
2399 // second element will start at an 8 byte offset. We can't increase the size
2400 // of the second element because it might make us access off the end of the
2401 // struct.
2402 if (HiStart != 8) {
2403 // There are only two sorts of types the ABI generation code can produce for
2404 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
2405 // Promote these to a larger type.
2406 if (Lo->isFloatTy())
2407 Lo = llvm::Type::getDoubleTy(Lo->getContext());
2408 else {
2409 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
2410 Lo = llvm::Type::getInt64Ty(Lo->getContext());
2411 }
2412 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002413
Reid Kleckneree7cf842014-12-01 22:02:27 +00002414 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, nullptr);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002415
2416
Chris Lattnerd426c8e2010-09-01 00:50:20 +00002417 // Verify that the second element is at an 8-byte offset.
2418 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
2419 "Invalid x86-64 argument pair!");
2420 return Result;
2421}
2422
Chris Lattner31faff52010-07-28 23:06:14 +00002423ABIArgInfo X86_64ABIInfo::
Chris Lattner458b2aa2010-07-29 02:16:43 +00002424classifyReturnType(QualType RetTy) const {
Chris Lattner31faff52010-07-28 23:06:14 +00002425 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
2426 // classification algorithm.
2427 X86_64ABIInfo::Class Lo, Hi;
Eli Friedman96fd2642013-06-12 00:13:45 +00002428 classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true);
Chris Lattner31faff52010-07-28 23:06:14 +00002429
2430 // Check some invariants.
2431 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner31faff52010-07-28 23:06:14 +00002432 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2433
Craig Topper8a13c412014-05-21 05:09:00 +00002434 llvm::Type *ResType = nullptr;
Chris Lattner31faff52010-07-28 23:06:14 +00002435 switch (Lo) {
2436 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00002437 if (Hi == NoClass)
2438 return ABIArgInfo::getIgnore();
2439 // If the low part is just padding, it takes no register, leave ResType
2440 // null.
2441 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2442 "Unknown missing lo part");
2443 break;
Chris Lattner31faff52010-07-28 23:06:14 +00002444
2445 case SSEUp:
2446 case X87Up:
David Blaikie83d382b2011-09-23 05:06:16 +00002447 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner31faff52010-07-28 23:06:14 +00002448
2449 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
2450 // hidden argument.
2451 case Memory:
2452 return getIndirectReturnResult(RetTy);
2453
2454 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
2455 // available register of the sequence %rax, %rdx is used.
2456 case Integer:
Chris Lattnera5f58b02011-07-09 17:41:47 +00002457 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002458
Chris Lattner1f3a0632010-07-29 21:42:50 +00002459 // If we have a sign or zero extended integer, make sure to return Extend
2460 // so that the parameter gets the right LLVM IR attributes.
2461 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2462 // Treat an enum type as its underlying type.
2463 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2464 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002465
Chris Lattner1f3a0632010-07-29 21:42:50 +00002466 if (RetTy->isIntegralOrEnumerationType() &&
2467 RetTy->isPromotableIntegerType())
2468 return ABIArgInfo::getExtend();
2469 }
Chris Lattner31faff52010-07-28 23:06:14 +00002470 break;
2471
2472 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
2473 // available SSE register of the sequence %xmm0, %xmm1 is used.
2474 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00002475 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattnerfa560fe2010-07-28 23:12:33 +00002476 break;
Chris Lattner31faff52010-07-28 23:06:14 +00002477
2478 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
2479 // returned on the X87 stack in %st0 as 80-bit x87 number.
2480 case X87:
Chris Lattner2b037972010-07-29 02:01:43 +00002481 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00002482 break;
Chris Lattner31faff52010-07-28 23:06:14 +00002483
2484 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
2485 // part of the value is returned in %st0 and the imaginary part in
2486 // %st1.
2487 case ComplexX87:
2488 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner845511f2011-06-18 22:49:11 +00002489 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner2b037972010-07-29 02:01:43 +00002490 llvm::Type::getX86_FP80Ty(getVMContext()),
Reid Kleckneree7cf842014-12-01 22:02:27 +00002491 nullptr);
Chris Lattner31faff52010-07-28 23:06:14 +00002492 break;
2493 }
2494
Craig Topper8a13c412014-05-21 05:09:00 +00002495 llvm::Type *HighPart = nullptr;
Chris Lattner31faff52010-07-28 23:06:14 +00002496 switch (Hi) {
2497 // Memory was handled previously and X87 should
2498 // never occur as a hi class.
2499 case Memory:
2500 case X87:
David Blaikie83d382b2011-09-23 05:06:16 +00002501 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner31faff52010-07-28 23:06:14 +00002502
2503 case ComplexX87: // Previously handled.
Chris Lattnerfa560fe2010-07-28 23:12:33 +00002504 case NoClass:
2505 break;
Chris Lattner31faff52010-07-28 23:06:14 +00002506
Chris Lattner52b3c132010-09-01 00:20:33 +00002507 case Integer:
Chris Lattnera5f58b02011-07-09 17:41:47 +00002508 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00002509 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2510 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00002511 break;
Chris Lattner52b3c132010-09-01 00:20:33 +00002512 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00002513 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00002514 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2515 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00002516 break;
2517
2518 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002519 // is passed in the next available eightbyte chunk if the last used
2520 // vector register.
Chris Lattner31faff52010-07-28 23:06:14 +00002521 //
Chris Lattner57540c52011-04-15 05:22:18 +00002522 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner31faff52010-07-28 23:06:14 +00002523 case SSEUp:
2524 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002525 ResType = GetByteVectorType(RetTy);
Chris Lattner31faff52010-07-28 23:06:14 +00002526 break;
2527
2528 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
2529 // returned together with the previous X87 value in %st0.
2530 case X87Up:
Chris Lattner57540c52011-04-15 05:22:18 +00002531 // If X87Up is preceded by X87, we don't need to do
Chris Lattner31faff52010-07-28 23:06:14 +00002532 // anything. However, in some cases with unions it may not be
Chris Lattner57540c52011-04-15 05:22:18 +00002533 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner31faff52010-07-28 23:06:14 +00002534 // extra bits in an SSE reg.
Chris Lattnerc95a3982010-07-29 17:49:08 +00002535 if (Lo != X87) {
Chris Lattnera5f58b02011-07-09 17:41:47 +00002536 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00002537 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2538 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattnerc95a3982010-07-29 17:49:08 +00002539 }
Chris Lattner31faff52010-07-28 23:06:14 +00002540 break;
2541 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002542
Chris Lattner52b3c132010-09-01 00:20:33 +00002543 // If a high part was specified, merge it together with the low part. It is
Chris Lattnerbe5eb172010-09-01 00:24:35 +00002544 // known to pass in the high eightbyte of the result. We do this by forming a
2545 // first class struct aggregate with the high and low part: {low, high}
Chris Lattnerd426c8e2010-09-01 00:50:20 +00002546 if (HighPart)
Micah Villmowdd31ca12012-10-08 16:25:52 +00002547 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Chris Lattner31faff52010-07-28 23:06:14 +00002548
Chris Lattner1f3a0632010-07-29 21:42:50 +00002549 return ABIArgInfo::getDirect(ResType);
Chris Lattner31faff52010-07-28 23:06:14 +00002550}
2551
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002552ABIArgInfo X86_64ABIInfo::classifyArgumentType(
Eli Friedman96fd2642013-06-12 00:13:45 +00002553 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE,
2554 bool isNamedArg)
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002555 const
2556{
Reid Klecknerb1be6832014-11-15 01:41:41 +00002557 Ty = useFirstFieldIfTransparentUnion(Ty);
2558
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002559 X86_64ABIInfo::Class Lo, Hi;
Eli Friedman96fd2642013-06-12 00:13:45 +00002560 classify(Ty, 0, Lo, Hi, isNamedArg);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002561
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002562 // Check some invariants.
2563 // FIXME: Enforce these by construction.
2564 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002565 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2566
2567 neededInt = 0;
2568 neededSSE = 0;
Craig Topper8a13c412014-05-21 05:09:00 +00002569 llvm::Type *ResType = nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002570 switch (Lo) {
2571 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00002572 if (Hi == NoClass)
2573 return ABIArgInfo::getIgnore();
2574 // If the low part is just padding, it takes no register, leave ResType
2575 // null.
2576 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2577 "Unknown missing lo part");
2578 break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002579
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002580 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
2581 // on the stack.
2582 case Memory:
2583
2584 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
2585 // COMPLEX_X87, it is passed in memory.
2586 case X87:
2587 case ComplexX87:
Mark Lacey3825e832013-10-06 01:33:34 +00002588 if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect)
Eli Friedman4774b7e2011-06-29 07:04:55 +00002589 ++neededInt;
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002590 return getIndirectResult(Ty, freeIntRegs);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002591
2592 case SSEUp:
2593 case X87Up:
David Blaikie83d382b2011-09-23 05:06:16 +00002594 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002595
2596 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
2597 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
2598 // and %r9 is used.
2599 case Integer:
Chris Lattner22a931e2010-06-29 06:01:59 +00002600 ++neededInt;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002601
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002602 // Pick an 8-byte type based on the preferred type.
Chris Lattnera5f58b02011-07-09 17:41:47 +00002603 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattner1f3a0632010-07-29 21:42:50 +00002604
2605 // If we have a sign or zero extended integer, make sure to return Extend
2606 // so that the parameter gets the right LLVM IR attributes.
2607 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2608 // Treat an enum type as its underlying type.
2609 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2610 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002611
Chris Lattner1f3a0632010-07-29 21:42:50 +00002612 if (Ty->isIntegralOrEnumerationType() &&
2613 Ty->isPromotableIntegerType())
2614 return ABIArgInfo::getExtend();
2615 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002616
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002617 break;
2618
2619 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
2620 // available SSE register is used, the registers are taken in the
2621 // order from %xmm0 to %xmm7.
Bill Wendling5cd41c42010-10-18 03:41:31 +00002622 case SSE: {
Chris Lattnera5f58b02011-07-09 17:41:47 +00002623 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman1310c682011-07-02 00:57:27 +00002624 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling9987c0e2010-10-18 23:51:38 +00002625 ++neededSSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002626 break;
2627 }
Bill Wendling5cd41c42010-10-18 03:41:31 +00002628 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002629
Craig Topper8a13c412014-05-21 05:09:00 +00002630 llvm::Type *HighPart = nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002631 switch (Hi) {
2632 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattner57540c52011-04-15 05:22:18 +00002633 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002634 // which is passed in memory.
2635 case Memory:
2636 case X87:
2637 case ComplexX87:
David Blaikie83d382b2011-09-23 05:06:16 +00002638 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002639
2640 case NoClass: break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002641
Chris Lattnerbe5eb172010-09-01 00:24:35 +00002642 case Integer:
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002643 ++neededInt;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002644 // Pick an 8-byte type based on the preferred type.
Chris Lattnera5f58b02011-07-09 17:41:47 +00002645 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002646
Chris Lattnerbe5eb172010-09-01 00:24:35 +00002647 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2648 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002649 break;
2650
2651 // X87Up generally doesn't occur here (long double is passed in
2652 // memory), except in situations involving unions.
2653 case X87Up:
Chris Lattnerbe5eb172010-09-01 00:24:35 +00002654 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00002655 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002656
Chris Lattnerbe5eb172010-09-01 00:24:35 +00002657 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2658 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner8a2f3c72010-07-30 04:02:24 +00002659
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002660 ++neededSSE;
2661 break;
2662
2663 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
2664 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002665 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002666 case SSEUp:
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00002667 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002668 ResType = GetByteVectorType(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002669 break;
2670 }
2671
Chris Lattnerbe5eb172010-09-01 00:24:35 +00002672 // If a high part was specified, merge it together with the low part. It is
2673 // known to pass in the high eightbyte of the result. We do this by forming a
2674 // first class struct aggregate with the high and low part: {low, high}
2675 if (HighPart)
Micah Villmowdd31ca12012-10-08 16:25:52 +00002676 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002677
Chris Lattner1f3a0632010-07-29 21:42:50 +00002678 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002679}
2680
Chris Lattner22326a12010-07-29 02:31:05 +00002681void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002682
Reid Kleckner40ca9132014-05-13 22:05:45 +00002683 if (!getCXXABI().classifyReturnType(FI))
2684 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002685
2686 // Keep track of the number of assigned registers.
Bill Wendling9987c0e2010-10-18 23:51:38 +00002687 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002688
2689 // If the return value is indirect, then the hidden argument is consuming one
2690 // integer register.
2691 if (FI.getReturnInfo().isIndirect())
2692 --freeIntRegs;
2693
Peter Collingbournef7706832014-12-12 23:41:25 +00002694 // The chain argument effectively gives us another free register.
2695 if (FI.isChainCall())
2696 ++freeIntRegs;
2697
Alexey Samsonov34625dd2014-09-29 21:21:48 +00002698 unsigned NumRequiredArgs = FI.getNumRequiredArgs();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002699 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2700 // get assigned (in left-to-right order) for passing as follows...
Alexey Samsonov34625dd2014-09-29 21:21:48 +00002701 unsigned ArgNo = 0;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002702 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Alexey Samsonov34625dd2014-09-29 21:21:48 +00002703 it != ie; ++it, ++ArgNo) {
2704 bool IsNamedArg = ArgNo < NumRequiredArgs;
Eli Friedman96fd2642013-06-12 00:13:45 +00002705
Bill Wendling9987c0e2010-10-18 23:51:38 +00002706 unsigned neededInt, neededSSE;
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002707 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
Alexey Samsonov34625dd2014-09-29 21:21:48 +00002708 neededSSE, IsNamedArg);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002709
2710 // AMD64-ABI 3.2.3p3: If there are no registers available for any
2711 // eightbyte of an argument, the whole argument is passed on the
2712 // stack. If registers have already been assigned for some
2713 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling9987c0e2010-10-18 23:51:38 +00002714 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002715 freeIntRegs -= neededInt;
2716 freeSSERegs -= neededSSE;
2717 } else {
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002718 it->info = getIndirectResult(it->type, freeIntRegs);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002719 }
2720 }
2721}
2722
2723static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2724 QualType Ty,
2725 CodeGenFunction &CGF) {
2726 llvm::Value *overflow_arg_area_p =
2727 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2728 llvm::Value *overflow_arg_area =
2729 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2730
2731 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2732 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedmana1748562011-11-18 02:44:19 +00002733 // It isn't stated explicitly in the standard, but in practice we use
2734 // alignment greater than 16 where necessary.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002735 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2736 if (Align > 8) {
Eli Friedmana1748562011-11-18 02:44:19 +00002737 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
Owen Anderson41a75022009-08-13 21:57:51 +00002738 llvm::Value *Offset =
Eli Friedmana1748562011-11-18 02:44:19 +00002739 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002740 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2741 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner5e016ae2010-06-27 07:15:29 +00002742 CGF.Int64Ty);
Eli Friedmana1748562011-11-18 02:44:19 +00002743 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002744 overflow_arg_area =
2745 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2746 overflow_arg_area->getType(),
2747 "overflow_arg_area.align");
2748 }
2749
2750 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2192fe52011-07-18 04:24:23 +00002751 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002752 llvm::Value *Res =
2753 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00002754 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002755
2756 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2757 // l->overflow_arg_area + sizeof(type).
2758 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2759 // an 8 byte boundary.
2760
2761 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00002762 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00002763 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002764 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2765 "overflow_arg_area.next");
2766 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2767
2768 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2769 return Res;
2770}
2771
2772llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2773 CodeGenFunction &CGF) const {
2774 // Assume that va_list type is correct; should be pointer to LLVM type:
2775 // struct {
2776 // i32 gp_offset;
2777 // i32 fp_offset;
2778 // i8* overflow_arg_area;
2779 // i8* reg_save_area;
2780 // };
Bill Wendling9987c0e2010-10-18 23:51:38 +00002781 unsigned neededInt, neededSSE;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002782
Chris Lattner9723d6c2010-03-11 18:19:55 +00002783 Ty = CGF.getContext().getCanonicalType(Ty);
Eli Friedman96fd2642013-06-12 00:13:45 +00002784 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE,
2785 /*isNamedArg*/false);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002786
2787 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2788 // in the registers. If not go to step 7.
2789 if (!neededInt && !neededSSE)
2790 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2791
2792 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2793 // general purpose registers needed to pass type and num_fp to hold
2794 // the number of floating point registers needed.
2795
2796 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2797 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2798 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2799 //
2800 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2801 // register save space).
2802
Craig Topper8a13c412014-05-21 05:09:00 +00002803 llvm::Value *InRegs = nullptr;
2804 llvm::Value *gp_offset_p = nullptr, *gp_offset = nullptr;
2805 llvm::Value *fp_offset_p = nullptr, *fp_offset = nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002806 if (neededInt) {
2807 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2808 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattnerd776fb12010-06-28 21:43:59 +00002809 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2810 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002811 }
2812
2813 if (neededSSE) {
2814 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2815 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2816 llvm::Value *FitsInFP =
Chris Lattnerd776fb12010-06-28 21:43:59 +00002817 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2818 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002819 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2820 }
2821
2822 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2823 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2824 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2825 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2826
2827 // Emit code to load the value if it was passed in registers.
2828
2829 CGF.EmitBlock(InRegBlock);
2830
2831 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2832 // an offset of l->gp_offset and/or l->fp_offset. This may require
2833 // copying to a temporary location in case the parameter is passed
2834 // in different register classes or requires an alignment greater
2835 // than 8 for general purpose registers and 16 for XMM registers.
2836 //
2837 // FIXME: This really results in shameful code when we end up needing to
2838 // collect arguments from different places; often what should result in a
2839 // simple assembling of a structure from scattered addresses has many more
2840 // loads than necessary. Can we clean this up?
Chris Lattner2192fe52011-07-18 04:24:23 +00002841 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002842 llvm::Value *RegAddr =
2843 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2844 "reg_save_area");
2845 if (neededInt && neededSSE) {
2846 // FIXME: Cleanup.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002847 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00002848 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Eli Friedmanc11c1692013-06-07 23:20:55 +00002849 llvm::Value *Tmp = CGF.CreateMemTemp(Ty);
2850 Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002851 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00002852 llvm::Type *TyLo = ST->getElementType(0);
2853 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattner51e1cc22010-08-26 06:28:35 +00002854 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002855 "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00002856 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2857 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002858 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2859 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Rafael Espindola0a500af2014-06-24 20:01:50 +00002860 llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr;
2861 llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002862 llvm::Value *V =
2863 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2864 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2865 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2866 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2867
Owen Anderson170229f2009-07-14 23:10:40 +00002868 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00002869 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002870 } else if (neededInt) {
2871 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2872 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00002873 llvm::PointerType::getUnqual(LTy));
Eli Friedmanc11c1692013-06-07 23:20:55 +00002874
2875 // Copy to a temporary if necessary to ensure the appropriate alignment.
2876 std::pair<CharUnits, CharUnits> SizeAlign =
2877 CGF.getContext().getTypeInfoInChars(Ty);
2878 uint64_t TySize = SizeAlign.first.getQuantity();
2879 unsigned TyAlign = SizeAlign.second.getQuantity();
2880 if (TyAlign > 8) {
Eli Friedmanc11c1692013-06-07 23:20:55 +00002881 llvm::Value *Tmp = CGF.CreateMemTemp(Ty);
2882 CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, 8, false);
2883 RegAddr = Tmp;
2884 }
Chris Lattner0cf24192010-06-28 20:05:43 +00002885 } else if (neededSSE == 1) {
2886 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2887 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2888 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002889 } else {
Chris Lattner0cf24192010-06-28 20:05:43 +00002890 assert(neededSSE == 2 && "Invalid number of needed registers!");
2891 // SSE registers are spaced 16 bytes apart in the register save
2892 // area, we need to collect the two eightbytes together.
2893 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattnerd776fb12010-06-28 21:43:59 +00002894 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattnerece04092012-02-07 00:39:47 +00002895 llvm::Type *DoubleTy = CGF.DoubleTy;
Chris Lattner2192fe52011-07-18 04:24:23 +00002896 llvm::Type *DblPtrTy =
Chris Lattner0cf24192010-06-28 20:05:43 +00002897 llvm::PointerType::getUnqual(DoubleTy);
Reid Kleckneree7cf842014-12-01 22:02:27 +00002898 llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, nullptr);
Eli Friedmanc11c1692013-06-07 23:20:55 +00002899 llvm::Value *V, *Tmp = CGF.CreateMemTemp(Ty);
2900 Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo());
Chris Lattner0cf24192010-06-28 20:05:43 +00002901 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2902 DblPtrTy));
2903 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2904 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2905 DblPtrTy));
2906 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2907 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2908 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002909 }
2910
2911 // AMD64-ABI 3.5.7p5: Step 5. Set:
2912 // l->gp_offset = l->gp_offset + num_gp * 8
2913 // l->fp_offset = l->fp_offset + num_fp * 16.
2914 if (neededInt) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00002915 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002916 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2917 gp_offset_p);
2918 }
2919 if (neededSSE) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00002920 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002921 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2922 fp_offset_p);
2923 }
2924 CGF.EmitBranch(ContBlock);
2925
2926 // Emit code to load the value if it was passed in memory.
2927
2928 CGF.EmitBlock(InMemBlock);
2929 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2930
2931 // Return the appropriate result.
2932
2933 CGF.EmitBlock(ContBlock);
Jay Foad20c0f022011-03-30 11:28:58 +00002934 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002935 "vaarg.addr");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002936 ResAddr->addIncoming(RegAddr, InRegBlock);
2937 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002938 return ResAddr;
2939}
2940
Reid Kleckner80944df2014-10-31 22:00:51 +00002941ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs,
2942 bool IsReturnType) const {
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002943
2944 if (Ty->isVoidType())
2945 return ABIArgInfo::getIgnore();
2946
2947 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2948 Ty = EnumTy->getDecl()->getIntegerType();
2949
Reid Kleckner80944df2014-10-31 22:00:51 +00002950 TypeInfo Info = getContext().getTypeInfo(Ty);
2951 uint64_t Width = Info.Width;
2952 unsigned Align = getContext().toCharUnitsFromBits(Info.Align).getQuantity();
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002953
Reid Kleckner9005f412014-05-02 00:51:20 +00002954 const RecordType *RT = Ty->getAs<RecordType>();
2955 if (RT) {
Reid Kleckner40ca9132014-05-13 22:05:45 +00002956 if (!IsReturnType) {
Mark Lacey3825e832013-10-06 01:33:34 +00002957 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()))
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00002958 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
2959 }
2960
2961 if (RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002962 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2963
NAKAMURA Takumif8a6e802011-02-22 03:56:57 +00002964 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
Reid Kleckner80944df2014-10-31 22:00:51 +00002965 if (Width == 128 && getTarget().getTriple().isWindowsGNUEnvironment())
NAKAMURA Takumif8a6e802011-02-22 03:56:57 +00002966 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Reid Kleckner80944df2014-10-31 22:00:51 +00002967 Width));
Reid Kleckner9005f412014-05-02 00:51:20 +00002968 }
NAKAMURA Takumif8a6e802011-02-22 03:56:57 +00002969
Reid Kleckner80944df2014-10-31 22:00:51 +00002970 // vectorcall adds the concept of a homogenous vector aggregate, similar to
2971 // other targets.
2972 const Type *Base = nullptr;
2973 uint64_t NumElts = 0;
2974 if (FreeSSERegs && isHomogeneousAggregate(Ty, Base, NumElts)) {
2975 if (FreeSSERegs >= NumElts) {
2976 FreeSSERegs -= NumElts;
2977 if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())
2978 return ABIArgInfo::getDirect();
2979 return ABIArgInfo::getExpand();
2980 }
2981 return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
2982 }
2983
2984
Reid Klecknerec87fec2014-05-02 01:17:12 +00002985 if (Ty->isMemberPointerType()) {
Reid Kleckner7f5f0f32014-05-02 01:14:59 +00002986 // If the member pointer is represented by an LLVM int or ptr, pass it
2987 // directly.
2988 llvm::Type *LLTy = CGT.ConvertType(Ty);
2989 if (LLTy->isPointerTy() || LLTy->isIntegerTy())
2990 return ABIArgInfo::getDirect();
Reid Kleckner9005f412014-05-02 00:51:20 +00002991 }
2992
2993 if (RT || Ty->isMemberPointerType()) {
NAKAMURA Takumif8a6e802011-02-22 03:56:57 +00002994 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2995 // not 1, 2, 4, or 8 bytes, must be passed by reference."
Reid Kleckner80944df2014-10-31 22:00:51 +00002996 if (Width > 64 || !llvm::isPowerOf2_64(Width))
Reid Kleckner9005f412014-05-02 00:51:20 +00002997 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002998
Reid Kleckner9005f412014-05-02 00:51:20 +00002999 // Otherwise, coerce it to a small integer.
Reid Kleckner80944df2014-10-31 22:00:51 +00003000 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width));
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003001 }
3002
Julien Lerouge10dcff82014-08-27 00:36:55 +00003003 // Bool type is always extended to the ABI, other builtin types are not
3004 // extended.
3005 const BuiltinType *BT = Ty->getAs<BuiltinType>();
3006 if (BT && BT->getKind() == BuiltinType::Bool)
Julien Lerougee8d34fa2014-08-26 22:11:53 +00003007 return ABIArgInfo::getExtend();
3008
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003009 return ABIArgInfo::getDirect();
3010}
3011
3012void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Reid Kleckner80944df2014-10-31 22:00:51 +00003013 bool IsVectorCall =
3014 FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall;
Reid Kleckner37abaca2014-05-09 22:46:15 +00003015
Reid Kleckner80944df2014-10-31 22:00:51 +00003016 // We can use up to 4 SSE return registers with vectorcall.
3017 unsigned FreeSSERegs = IsVectorCall ? 4 : 0;
3018 if (!getCXXABI().classifyReturnType(FI))
3019 FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true);
3020
3021 // We can use up to 6 SSE register parameters with vectorcall.
3022 FreeSSERegs = IsVectorCall ? 6 : 0;
Aaron Ballmanec47bc22014-03-17 18:10:01 +00003023 for (auto &I : FI.arguments())
Reid Kleckner80944df2014-10-31 22:00:51 +00003024 I.info = classify(I.type, FreeSSERegs, false);
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003025}
3026
Chris Lattner04dc9572010-08-31 16:44:54 +00003027llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3028 CodeGenFunction &CGF) const {
Chris Lattnerece04092012-02-07 00:39:47 +00003029 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Chris Lattner0cf24192010-06-28 20:05:43 +00003030
Chris Lattner04dc9572010-08-31 16:44:54 +00003031 CGBuilderTy &Builder = CGF.Builder;
3032 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
3033 "ap");
3034 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
3035 llvm::Type *PTy =
3036 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3037 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3038
3039 uint64_t Offset =
3040 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
3041 llvm::Value *NextAddr =
3042 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
3043 "ap.next");
3044 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3045
3046 return AddrTyped;
3047}
Chris Lattner0cf24192010-06-28 20:05:43 +00003048
Benjamin Kramer1cdb23d2012-10-20 13:02:06 +00003049namespace {
3050
Derek Schuffa2020962012-10-16 22:30:41 +00003051class NaClX86_64ABIInfo : public ABIInfo {
3052 public:
3053 NaClX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
3054 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, HasAVX) {}
Craig Topper4f12f102014-03-12 06:41:41 +00003055 void computeInfo(CGFunctionInfo &FI) const override;
3056 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3057 CodeGenFunction &CGF) const override;
Derek Schuffa2020962012-10-16 22:30:41 +00003058 private:
3059 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
3060 X86_64ABIInfo NInfo; // Used for everything else.
3061};
3062
3063class NaClX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
Alexander Musman09184fe2014-09-30 05:29:28 +00003064 bool HasAVX;
Derek Schuffa2020962012-10-16 22:30:41 +00003065 public:
Alexander Musman09184fe2014-09-30 05:29:28 +00003066 NaClX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
3067 : TargetCodeGenInfo(new NaClX86_64ABIInfo(CGT, HasAVX)), HasAVX(HasAVX) {
3068 }
3069 unsigned getOpenMPSimdDefaultAlignment(QualType) const override {
3070 return HasAVX ? 32 : 16;
3071 }
Derek Schuffa2020962012-10-16 22:30:41 +00003072};
3073
Benjamin Kramer1cdb23d2012-10-20 13:02:06 +00003074}
3075
Derek Schuffa2020962012-10-16 22:30:41 +00003076void NaClX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
3077 if (FI.getASTCallingConvention() == CC_PnaclCall)
3078 PInfo.computeInfo(FI);
3079 else
3080 NInfo.computeInfo(FI);
3081}
3082
3083llvm::Value *NaClX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3084 CodeGenFunction &CGF) const {
3085 // Always use the native convention; calling pnacl-style varargs functions
3086 // is unuspported.
3087 return NInfo.EmitVAArg(VAListAddr, Ty, CGF);
3088}
3089
3090
John McCallea8d8bb2010-03-11 00:10:12 +00003091// PowerPC-32
John McCallea8d8bb2010-03-11 00:10:12 +00003092namespace {
Roman Divacky8a12d842014-11-03 18:32:54 +00003093/// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information.
3094class PPC32_SVR4_ABIInfo : public DefaultABIInfo {
John McCallea8d8bb2010-03-11 00:10:12 +00003095public:
Roman Divacky8a12d842014-11-03 18:32:54 +00003096 PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
3097
3098 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3099 CodeGenFunction &CGF) const override;
3100};
3101
3102class PPC32TargetCodeGenInfo : public TargetCodeGenInfo {
3103public:
3104 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT)) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003105
Craig Topper4f12f102014-03-12 06:41:41 +00003106 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
John McCallea8d8bb2010-03-11 00:10:12 +00003107 // This is recovered from gcc output.
3108 return 1; // r1 is the dedicated stack pointer
3109 }
3110
3111 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00003112 llvm::Value *Address) const override;
Hal Finkel92e31a52014-10-03 17:45:20 +00003113
3114 unsigned getOpenMPSimdDefaultAlignment(QualType) const override {
3115 return 16; // Natural alignment for Altivec vectors.
3116 }
John McCallea8d8bb2010-03-11 00:10:12 +00003117};
3118
3119}
3120
Roman Divacky8a12d842014-11-03 18:32:54 +00003121llvm::Value *PPC32_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr,
3122 QualType Ty,
3123 CodeGenFunction &CGF) const {
3124 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
3125 // TODO: Implement this. For now ignore.
3126 (void)CTy;
3127 return nullptr;
3128 }
3129
3130 bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64;
3131 bool isInt = Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType();
3132 llvm::Type *CharPtr = CGF.Int8PtrTy;
3133 llvm::Type *CharPtrPtr = CGF.Int8PtrPtrTy;
3134
3135 CGBuilderTy &Builder = CGF.Builder;
3136 llvm::Value *GPRPtr = Builder.CreateBitCast(VAListAddr, CharPtr, "gprptr");
3137 llvm::Value *GPRPtrAsInt = Builder.CreatePtrToInt(GPRPtr, CGF.Int32Ty);
3138 llvm::Value *FPRPtrAsInt = Builder.CreateAdd(GPRPtrAsInt, Builder.getInt32(1));
3139 llvm::Value *FPRPtr = Builder.CreateIntToPtr(FPRPtrAsInt, CharPtr);
3140 llvm::Value *OverflowAreaPtrAsInt = Builder.CreateAdd(FPRPtrAsInt, Builder.getInt32(3));
3141 llvm::Value *OverflowAreaPtr = Builder.CreateIntToPtr(OverflowAreaPtrAsInt, CharPtrPtr);
3142 llvm::Value *RegsaveAreaPtrAsInt = Builder.CreateAdd(OverflowAreaPtrAsInt, Builder.getInt32(4));
3143 llvm::Value *RegsaveAreaPtr = Builder.CreateIntToPtr(RegsaveAreaPtrAsInt, CharPtrPtr);
3144 llvm::Value *GPR = Builder.CreateLoad(GPRPtr, false, "gpr");
3145 // Align GPR when TY is i64.
3146 if (isI64) {
3147 llvm::Value *GPRAnd = Builder.CreateAnd(GPR, Builder.getInt8(1));
3148 llvm::Value *CC64 = Builder.CreateICmpEQ(GPRAnd, Builder.getInt8(1));
3149 llvm::Value *GPRPlusOne = Builder.CreateAdd(GPR, Builder.getInt8(1));
3150 GPR = Builder.CreateSelect(CC64, GPRPlusOne, GPR);
3151 }
3152 llvm::Value *FPR = Builder.CreateLoad(FPRPtr, false, "fpr");
3153 llvm::Value *OverflowArea = Builder.CreateLoad(OverflowAreaPtr, false, "overflow_area");
3154 llvm::Value *OverflowAreaAsInt = Builder.CreatePtrToInt(OverflowArea, CGF.Int32Ty);
3155 llvm::Value *RegsaveArea = Builder.CreateLoad(RegsaveAreaPtr, false, "regsave_area");
3156 llvm::Value *RegsaveAreaAsInt = Builder.CreatePtrToInt(RegsaveArea, CGF.Int32Ty);
3157
3158 llvm::Value *CC = Builder.CreateICmpULT(isInt ? GPR : FPR,
3159 Builder.getInt8(8), "cond");
3160
3161 llvm::Value *RegConstant = Builder.CreateMul(isInt ? GPR : FPR,
3162 Builder.getInt8(isInt ? 4 : 8));
3163
3164 llvm::Value *OurReg = Builder.CreateAdd(RegsaveAreaAsInt, Builder.CreateSExt(RegConstant, CGF.Int32Ty));
3165
3166 if (Ty->isFloatingType())
3167 OurReg = Builder.CreateAdd(OurReg, Builder.getInt32(32));
3168
3169 llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs");
3170 llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow");
3171 llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
3172
3173 Builder.CreateCondBr(CC, UsingRegs, UsingOverflow);
3174
3175 CGF.EmitBlock(UsingRegs);
3176
3177 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3178 llvm::Value *Result1 = Builder.CreateIntToPtr(OurReg, PTy);
3179 // Increase the GPR/FPR indexes.
3180 if (isInt) {
3181 GPR = Builder.CreateAdd(GPR, Builder.getInt8(isI64 ? 2 : 1));
3182 Builder.CreateStore(GPR, GPRPtr);
3183 } else {
3184 FPR = Builder.CreateAdd(FPR, Builder.getInt8(1));
3185 Builder.CreateStore(FPR, FPRPtr);
3186 }
3187 CGF.EmitBranch(Cont);
3188
3189 CGF.EmitBlock(UsingOverflow);
3190
3191 // Increase the overflow area.
3192 llvm::Value *Result2 = Builder.CreateIntToPtr(OverflowAreaAsInt, PTy);
3193 OverflowAreaAsInt = Builder.CreateAdd(OverflowAreaAsInt, Builder.getInt32(isInt ? 4 : 8));
3194 Builder.CreateStore(Builder.CreateIntToPtr(OverflowAreaAsInt, CharPtr), OverflowAreaPtr);
3195 CGF.EmitBranch(Cont);
3196
3197 CGF.EmitBlock(Cont);
3198
3199 llvm::PHINode *Result = CGF.Builder.CreatePHI(PTy, 2, "vaarg.addr");
3200 Result->addIncoming(Result1, UsingRegs);
3201 Result->addIncoming(Result2, UsingOverflow);
3202
3203 if (Ty->isAggregateType()) {
3204 llvm::Value *AGGPtr = Builder.CreateBitCast(Result, CharPtrPtr, "aggrptr") ;
3205 return Builder.CreateLoad(AGGPtr, false, "aggr");
3206 }
3207
3208 return Result;
3209}
3210
John McCallea8d8bb2010-03-11 00:10:12 +00003211bool
3212PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3213 llvm::Value *Address) const {
3214 // This is calculated from the LLVM and GCC tables and verified
3215 // against gcc output. AFAIK all ABIs use the same encoding.
3216
3217 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallea8d8bb2010-03-11 00:10:12 +00003218
Chris Lattnerece04092012-02-07 00:39:47 +00003219 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallea8d8bb2010-03-11 00:10:12 +00003220 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
3221 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
3222 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
3223
3224 // 0-31: r0-31, the 4-byte general-purpose registers
John McCall943fae92010-05-27 06:19:26 +00003225 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallea8d8bb2010-03-11 00:10:12 +00003226
3227 // 32-63: fp0-31, the 8-byte floating-point registers
John McCall943fae92010-05-27 06:19:26 +00003228 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallea8d8bb2010-03-11 00:10:12 +00003229
3230 // 64-76 are various 4-byte special-purpose registers:
3231 // 64: mq
3232 // 65: lr
3233 // 66: ctr
3234 // 67: ap
3235 // 68-75 cr0-7
3236 // 76: xer
John McCall943fae92010-05-27 06:19:26 +00003237 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallea8d8bb2010-03-11 00:10:12 +00003238
3239 // 77-108: v0-31, the 16-byte vector registers
John McCall943fae92010-05-27 06:19:26 +00003240 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallea8d8bb2010-03-11 00:10:12 +00003241
3242 // 109: vrsave
3243 // 110: vscr
3244 // 111: spe_acc
3245 // 112: spefscr
3246 // 113: sfp
John McCall943fae92010-05-27 06:19:26 +00003247 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallea8d8bb2010-03-11 00:10:12 +00003248
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003249 return false;
John McCallea8d8bb2010-03-11 00:10:12 +00003250}
3251
Roman Divackyd966e722012-05-09 18:22:46 +00003252// PowerPC-64
3253
3254namespace {
Bill Schmidt25cb3492012-10-03 19:18:57 +00003255/// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
3256class PPC64_SVR4_ABIInfo : public DefaultABIInfo {
Ulrich Weigandb7122372014-07-21 00:48:09 +00003257public:
3258 enum ABIKind {
3259 ELFv1 = 0,
3260 ELFv2
3261 };
3262
3263private:
3264 static const unsigned GPRBits = 64;
3265 ABIKind Kind;
Bill Schmidt25cb3492012-10-03 19:18:57 +00003266
3267public:
Ulrich Weigandb7122372014-07-21 00:48:09 +00003268 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind)
3269 : DefaultABIInfo(CGT), Kind(Kind) {}
Bill Schmidt25cb3492012-10-03 19:18:57 +00003270
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00003271 bool isPromotableTypeForABI(QualType Ty) const;
Ulrich Weigand581badc2014-07-10 17:20:07 +00003272 bool isAlignedParamType(QualType Ty) const;
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00003273
3274 ABIArgInfo classifyReturnType(QualType RetTy) const;
3275 ABIArgInfo classifyArgumentType(QualType Ty) const;
3276
Reid Klecknere9f6a712014-10-31 17:10:41 +00003277 bool isHomogeneousAggregateBaseType(QualType Ty) const override;
3278 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
3279 uint64_t Members) const override;
3280
Bill Schmidt84d37792012-10-12 19:26:17 +00003281 // TODO: We can add more logic to computeInfo to improve performance.
3282 // Example: For aggregate arguments that fit in a register, we could
3283 // use getDirectInReg (as is done below for structs containing a single
3284 // floating-point value) to avoid pushing them to memory on function
3285 // entry. This would require changing the logic in PPCISelLowering
3286 // when lowering the parameters in the caller and args in the callee.
Craig Topper4f12f102014-03-12 06:41:41 +00003287 void computeInfo(CGFunctionInfo &FI) const override {
Reid Kleckner40ca9132014-05-13 22:05:45 +00003288 if (!getCXXABI().classifyReturnType(FI))
3289 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Aaron Ballmanec47bc22014-03-17 18:10:01 +00003290 for (auto &I : FI.arguments()) {
Bill Schmidt84d37792012-10-12 19:26:17 +00003291 // We rely on the default argument classification for the most part.
3292 // One exception: An aggregate containing a single floating-point
Bill Schmidt179afae2013-07-23 22:15:57 +00003293 // or vector item must be passed in a register if one is available.
Aaron Ballmanec47bc22014-03-17 18:10:01 +00003294 const Type *T = isSingleElementStruct(I.type, getContext());
Bill Schmidt84d37792012-10-12 19:26:17 +00003295 if (T) {
3296 const BuiltinType *BT = T->getAs<BuiltinType>();
Ulrich Weigandf4eba982014-07-10 16:39:01 +00003297 if ((T->isVectorType() && getContext().getTypeSize(T) == 128) ||
3298 (BT && BT->isFloatingPoint())) {
Bill Schmidt84d37792012-10-12 19:26:17 +00003299 QualType QT(T, 0);
Aaron Ballmanec47bc22014-03-17 18:10:01 +00003300 I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
Bill Schmidt84d37792012-10-12 19:26:17 +00003301 continue;
3302 }
3303 }
Aaron Ballmanec47bc22014-03-17 18:10:01 +00003304 I.info = classifyArgumentType(I.type);
Bill Schmidt84d37792012-10-12 19:26:17 +00003305 }
3306 }
Bill Schmidt25cb3492012-10-03 19:18:57 +00003307
Craig Topper4f12f102014-03-12 06:41:41 +00003308 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3309 CodeGenFunction &CGF) const override;
Bill Schmidt25cb3492012-10-03 19:18:57 +00003310};
3311
3312class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
3313public:
Ulrich Weigandb7122372014-07-21 00:48:09 +00003314 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT,
3315 PPC64_SVR4_ABIInfo::ABIKind Kind)
3316 : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind)) {}
Bill Schmidt25cb3492012-10-03 19:18:57 +00003317
Craig Topper4f12f102014-03-12 06:41:41 +00003318 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
Bill Schmidt25cb3492012-10-03 19:18:57 +00003319 // This is recovered from gcc output.
3320 return 1; // r1 is the dedicated stack pointer
3321 }
3322
3323 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00003324 llvm::Value *Address) const override;
Hal Finkel92e31a52014-10-03 17:45:20 +00003325
3326 unsigned getOpenMPSimdDefaultAlignment(QualType) const override {
3327 return 16; // Natural alignment for Altivec and VSX vectors.
3328 }
Bill Schmidt25cb3492012-10-03 19:18:57 +00003329};
3330
Roman Divackyd966e722012-05-09 18:22:46 +00003331class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
3332public:
3333 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
3334
Craig Topper4f12f102014-03-12 06:41:41 +00003335 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
Roman Divackyd966e722012-05-09 18:22:46 +00003336 // This is recovered from gcc output.
3337 return 1; // r1 is the dedicated stack pointer
3338 }
3339
3340 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00003341 llvm::Value *Address) const override;
Hal Finkel92e31a52014-10-03 17:45:20 +00003342
3343 unsigned getOpenMPSimdDefaultAlignment(QualType) const override {
3344 return 16; // Natural alignment for Altivec vectors.
3345 }
Roman Divackyd966e722012-05-09 18:22:46 +00003346};
3347
3348}
3349
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00003350// Return true if the ABI requires Ty to be passed sign- or zero-
3351// extended to 64 bits.
3352bool
3353PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
3354 // Treat an enum type as its underlying type.
3355 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3356 Ty = EnumTy->getDecl()->getIntegerType();
3357
3358 // Promotable integer types are required to be promoted by the ABI.
3359 if (Ty->isPromotableIntegerType())
3360 return true;
3361
3362 // In addition to the usual promotable integer types, we also need to
3363 // extend all 32-bit types, since the ABI requires promotion to 64 bits.
3364 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
3365 switch (BT->getKind()) {
3366 case BuiltinType::Int:
3367 case BuiltinType::UInt:
3368 return true;
3369 default:
3370 break;
3371 }
3372
3373 return false;
3374}
3375
Ulrich Weigand581badc2014-07-10 17:20:07 +00003376/// isAlignedParamType - Determine whether a type requires 16-byte
3377/// alignment in the parameter area.
3378bool
3379PPC64_SVR4_ABIInfo::isAlignedParamType(QualType Ty) const {
3380 // Complex types are passed just like their elements.
3381 if (const ComplexType *CTy = Ty->getAs<ComplexType>())
3382 Ty = CTy->getElementType();
3383
3384 // Only vector types of size 16 bytes need alignment (larger types are
3385 // passed via reference, smaller types are not aligned).
3386 if (Ty->isVectorType())
3387 return getContext().getTypeSize(Ty) == 128;
3388
3389 // For single-element float/vector structs, we consider the whole type
3390 // to have the same alignment requirements as its single element.
3391 const Type *AlignAsType = nullptr;
3392 const Type *EltType = isSingleElementStruct(Ty, getContext());
3393 if (EltType) {
3394 const BuiltinType *BT = EltType->getAs<BuiltinType>();
3395 if ((EltType->isVectorType() &&
3396 getContext().getTypeSize(EltType) == 128) ||
3397 (BT && BT->isFloatingPoint()))
3398 AlignAsType = EltType;
3399 }
3400
Ulrich Weigandb7122372014-07-21 00:48:09 +00003401 // Likewise for ELFv2 homogeneous aggregates.
3402 const Type *Base = nullptr;
3403 uint64_t Members = 0;
3404 if (!AlignAsType && Kind == ELFv2 &&
3405 isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members))
3406 AlignAsType = Base;
3407
Ulrich Weigand581badc2014-07-10 17:20:07 +00003408 // With special case aggregates, only vector base types need alignment.
3409 if (AlignAsType)
3410 return AlignAsType->isVectorType();
3411
3412 // Otherwise, we only need alignment for any aggregate type that
3413 // has an alignment requirement of >= 16 bytes.
3414 if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128)
3415 return true;
3416
3417 return false;
3418}
3419
Ulrich Weigandb7122372014-07-21 00:48:09 +00003420/// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous
3421/// aggregate. Base is set to the base element type, and Members is set
3422/// to the number of base elements.
Reid Klecknere9f6a712014-10-31 17:10:41 +00003423bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base,
3424 uint64_t &Members) const {
Ulrich Weigandb7122372014-07-21 00:48:09 +00003425 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
3426 uint64_t NElements = AT->getSize().getZExtValue();
3427 if (NElements == 0)
3428 return false;
3429 if (!isHomogeneousAggregate(AT->getElementType(), Base, Members))
3430 return false;
3431 Members *= NElements;
3432 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
3433 const RecordDecl *RD = RT->getDecl();
3434 if (RD->hasFlexibleArrayMember())
3435 return false;
3436
3437 Members = 0;
Ulrich Weiganda094f042014-10-29 13:23:20 +00003438
3439 // If this is a C++ record, check the bases first.
3440 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
3441 for (const auto &I : CXXRD->bases()) {
3442 // Ignore empty records.
3443 if (isEmptyRecord(getContext(), I.getType(), true))
3444 continue;
3445
3446 uint64_t FldMembers;
3447 if (!isHomogeneousAggregate(I.getType(), Base, FldMembers))
3448 return false;
3449
3450 Members += FldMembers;
3451 }
3452 }
3453
Ulrich Weigandb7122372014-07-21 00:48:09 +00003454 for (const auto *FD : RD->fields()) {
3455 // Ignore (non-zero arrays of) empty records.
3456 QualType FT = FD->getType();
3457 while (const ConstantArrayType *AT =
3458 getContext().getAsConstantArrayType(FT)) {
3459 if (AT->getSize().getZExtValue() == 0)
3460 return false;
3461 FT = AT->getElementType();
3462 }
3463 if (isEmptyRecord(getContext(), FT, true))
3464 continue;
3465
3466 // For compatibility with GCC, ignore empty bitfields in C++ mode.
3467 if (getContext().getLangOpts().CPlusPlus &&
3468 FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
3469 continue;
3470
3471 uint64_t FldMembers;
3472 if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers))
3473 return false;
3474
3475 Members = (RD->isUnion() ?
3476 std::max(Members, FldMembers) : Members + FldMembers);
3477 }
3478
3479 if (!Base)
3480 return false;
3481
3482 // Ensure there is no padding.
3483 if (getContext().getTypeSize(Base) * Members !=
3484 getContext().getTypeSize(Ty))
3485 return false;
3486 } else {
3487 Members = 1;
3488 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
3489 Members = 2;
3490 Ty = CT->getElementType();
3491 }
3492
Reid Klecknere9f6a712014-10-31 17:10:41 +00003493 // Most ABIs only support float, double, and some vector type widths.
3494 if (!isHomogeneousAggregateBaseType(Ty))
Ulrich Weigandb7122372014-07-21 00:48:09 +00003495 return false;
Ulrich Weigandb7122372014-07-21 00:48:09 +00003496
3497 // The base type must be the same for all members. Types that
3498 // agree in both total size and mode (float vs. vector) are
3499 // treated as being equivalent here.
3500 const Type *TyPtr = Ty.getTypePtr();
3501 if (!Base)
3502 Base = TyPtr;
3503
3504 if (Base->isVectorType() != TyPtr->isVectorType() ||
3505 getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr))
3506 return false;
3507 }
Reid Klecknere9f6a712014-10-31 17:10:41 +00003508 return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members);
3509}
Ulrich Weigandb7122372014-07-21 00:48:09 +00003510
Reid Klecknere9f6a712014-10-31 17:10:41 +00003511bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
3512 // Homogeneous aggregates for ELFv2 must have base types of float,
3513 // double, long double, or 128-bit vectors.
3514 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3515 if (BT->getKind() == BuiltinType::Float ||
3516 BT->getKind() == BuiltinType::Double ||
3517 BT->getKind() == BuiltinType::LongDouble)
3518 return true;
3519 }
3520 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3521 if (getContext().getTypeSize(VT) == 128)
3522 return true;
3523 }
3524 return false;
3525}
3526
3527bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough(
3528 const Type *Base, uint64_t Members) const {
Ulrich Weigandb7122372014-07-21 00:48:09 +00003529 // Vector types require one register, floating point types require one
3530 // or two registers depending on their size.
Reid Klecknere9f6a712014-10-31 17:10:41 +00003531 uint32_t NumRegs =
3532 Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64;
Ulrich Weigandb7122372014-07-21 00:48:09 +00003533
3534 // Homogeneous Aggregates may occupy at most 8 registers.
Reid Klecknere9f6a712014-10-31 17:10:41 +00003535 return Members * NumRegs <= 8;
Ulrich Weigandb7122372014-07-21 00:48:09 +00003536}
3537
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00003538ABIArgInfo
3539PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
Reid Klecknerb1be6832014-11-15 01:41:41 +00003540 Ty = useFirstFieldIfTransparentUnion(Ty);
3541
Bill Schmidt90b22c92012-11-27 02:46:43 +00003542 if (Ty->isAnyComplexType())
3543 return ABIArgInfo::getDirect();
3544
Ulrich Weigandf4eba982014-07-10 16:39:01 +00003545 // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes)
3546 // or via reference (larger than 16 bytes).
3547 if (Ty->isVectorType()) {
3548 uint64_t Size = getContext().getTypeSize(Ty);
3549 if (Size > 128)
3550 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3551 else if (Size < 128) {
3552 llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
3553 return ABIArgInfo::getDirect(CoerceTy);
3554 }
3555 }
3556
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00003557 if (isAggregateTypeForABI(Ty)) {
Mark Lacey3825e832013-10-06 01:33:34 +00003558 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00003559 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00003560
Ulrich Weigand581badc2014-07-10 17:20:07 +00003561 uint64_t ABIAlign = isAlignedParamType(Ty)? 16 : 8;
3562 uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8;
Ulrich Weigandb7122372014-07-21 00:48:09 +00003563
3564 // ELFv2 homogeneous aggregates are passed as array types.
3565 const Type *Base = nullptr;
3566 uint64_t Members = 0;
3567 if (Kind == ELFv2 &&
3568 isHomogeneousAggregate(Ty, Base, Members)) {
3569 llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
3570 llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
3571 return ABIArgInfo::getDirect(CoerceTy);
3572 }
3573
Ulrich Weigand601957f2014-07-21 00:56:36 +00003574 // If an aggregate may end up fully in registers, we do not
3575 // use the ByVal method, but pass the aggregate as array.
3576 // This is usually beneficial since we avoid forcing the
3577 // back-end to store the argument to memory.
3578 uint64_t Bits = getContext().getTypeSize(Ty);
3579 if (Bits > 0 && Bits <= 8 * GPRBits) {
3580 llvm::Type *CoerceTy;
3581
3582 // Types up to 8 bytes are passed as integer type (which will be
3583 // properly aligned in the argument save area doubleword).
3584 if (Bits <= GPRBits)
3585 CoerceTy = llvm::IntegerType::get(getVMContext(),
3586 llvm::RoundUpToAlignment(Bits, 8));
3587 // Larger types are passed as arrays, with the base type selected
3588 // according to the required alignment in the save area.
3589 else {
3590 uint64_t RegBits = ABIAlign * 8;
3591 uint64_t NumRegs = llvm::RoundUpToAlignment(Bits, RegBits) / RegBits;
3592 llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits);
3593 CoerceTy = llvm::ArrayType::get(RegTy, NumRegs);
3594 }
3595
3596 return ABIArgInfo::getDirect(CoerceTy);
3597 }
3598
Ulrich Weigandb7122372014-07-21 00:48:09 +00003599 // All other aggregates are passed ByVal.
Ulrich Weigand581badc2014-07-10 17:20:07 +00003600 return ABIArgInfo::getIndirect(ABIAlign, /*ByVal=*/true,
3601 /*Realign=*/TyAlign > ABIAlign);
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00003602 }
3603
3604 return (isPromotableTypeForABI(Ty) ?
3605 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3606}
3607
3608ABIArgInfo
3609PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
3610 if (RetTy->isVoidType())
3611 return ABIArgInfo::getIgnore();
3612
Bill Schmidta3d121c2012-12-17 04:20:17 +00003613 if (RetTy->isAnyComplexType())
3614 return ABIArgInfo::getDirect();
3615
Ulrich Weigandf4eba982014-07-10 16:39:01 +00003616 // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes)
3617 // or via reference (larger than 16 bytes).
3618 if (RetTy->isVectorType()) {
3619 uint64_t Size = getContext().getTypeSize(RetTy);
3620 if (Size > 128)
3621 return ABIArgInfo::getIndirect(0);
3622 else if (Size < 128) {
3623 llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
3624 return ABIArgInfo::getDirect(CoerceTy);
3625 }
3626 }
3627
Ulrich Weigandb7122372014-07-21 00:48:09 +00003628 if (isAggregateTypeForABI(RetTy)) {
3629 // ELFv2 homogeneous aggregates are returned as array types.
3630 const Type *Base = nullptr;
3631 uint64_t Members = 0;
3632 if (Kind == ELFv2 &&
3633 isHomogeneousAggregate(RetTy, Base, Members)) {
3634 llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
3635 llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
3636 return ABIArgInfo::getDirect(CoerceTy);
3637 }
3638
3639 // ELFv2 small aggregates are returned in up to two registers.
3640 uint64_t Bits = getContext().getTypeSize(RetTy);
3641 if (Kind == ELFv2 && Bits <= 2 * GPRBits) {
3642 if (Bits == 0)
3643 return ABIArgInfo::getIgnore();
3644
3645 llvm::Type *CoerceTy;
3646 if (Bits > GPRBits) {
3647 CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits);
Reid Kleckneree7cf842014-12-01 22:02:27 +00003648 CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy, nullptr);
Ulrich Weigandb7122372014-07-21 00:48:09 +00003649 } else
3650 CoerceTy = llvm::IntegerType::get(getVMContext(),
3651 llvm::RoundUpToAlignment(Bits, 8));
3652 return ABIArgInfo::getDirect(CoerceTy);
3653 }
3654
3655 // All other aggregates are returned indirectly.
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00003656 return ABIArgInfo::getIndirect(0);
Ulrich Weigandb7122372014-07-21 00:48:09 +00003657 }
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00003658
3659 return (isPromotableTypeForABI(RetTy) ?
3660 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3661}
3662
Bill Schmidt25cb3492012-10-03 19:18:57 +00003663// Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
3664llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr,
3665 QualType Ty,
3666 CodeGenFunction &CGF) const {
3667 llvm::Type *BP = CGF.Int8PtrTy;
3668 llvm::Type *BPP = CGF.Int8PtrPtrTy;
3669
3670 CGBuilderTy &Builder = CGF.Builder;
3671 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3672 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
3673
Ulrich Weigand581badc2014-07-10 17:20:07 +00003674 // Handle types that require 16-byte alignment in the parameter save area.
3675 if (isAlignedParamType(Ty)) {
3676 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
3677 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(15));
3678 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt64(-16));
3679 Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align");
3680 }
3681
Bill Schmidt924c4782013-01-14 17:45:36 +00003682 // Update the va_list pointer. The pointer should be bumped by the
3683 // size of the object. We can trust getTypeSize() except for a complex
3684 // type whose base type is smaller than a doubleword. For these, the
3685 // size of the object is 16 bytes; see below for further explanation.
Bill Schmidt25cb3492012-10-03 19:18:57 +00003686 unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8;
Bill Schmidt924c4782013-01-14 17:45:36 +00003687 QualType BaseTy;
3688 unsigned CplxBaseSize = 0;
3689
3690 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
3691 BaseTy = CTy->getElementType();
3692 CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8;
3693 if (CplxBaseSize < 8)
3694 SizeInBytes = 16;
3695 }
3696
Bill Schmidt25cb3492012-10-03 19:18:57 +00003697 unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8);
3698 llvm::Value *NextAddr =
3699 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset),
3700 "ap.next");
3701 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3702
Bill Schmidt924c4782013-01-14 17:45:36 +00003703 // If we have a complex type and the base type is smaller than 8 bytes,
3704 // the ABI calls for the real and imaginary parts to be right-adjusted
3705 // in separate doublewords. However, Clang expects us to produce a
3706 // pointer to a structure with the two parts packed tightly. So generate
3707 // loads of the real and imaginary parts relative to the va_list pointer,
3708 // and store them to a temporary structure.
3709 if (CplxBaseSize && CplxBaseSize < 8) {
3710 llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
3711 llvm::Value *ImagAddr = RealAddr;
Ulrich Weigandbebc55b2014-06-20 16:37:40 +00003712 if (CGF.CGM.getDataLayout().isBigEndian()) {
3713 RealAddr = Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize));
3714 ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize));
3715 } else {
3716 ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(8));
3717 }
Bill Schmidt924c4782013-01-14 17:45:36 +00003718 llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy));
3719 RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy);
3720 ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy);
3721 llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal");
3722 llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag");
3723 llvm::Value *Ptr = CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty),
3724 "vacplx");
3725 llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, ".real");
3726 llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, ".imag");
3727 Builder.CreateStore(Real, RealPtr, false);
3728 Builder.CreateStore(Imag, ImagPtr, false);
3729 return Ptr;
3730 }
3731
Bill Schmidt25cb3492012-10-03 19:18:57 +00003732 // If the argument is smaller than 8 bytes, it is right-adjusted in
3733 // its doubleword slot. Adjust the pointer to pick it up from the
3734 // correct offset.
Ulrich Weigandbebc55b2014-06-20 16:37:40 +00003735 if (SizeInBytes < 8 && CGF.CGM.getDataLayout().isBigEndian()) {
Bill Schmidt25cb3492012-10-03 19:18:57 +00003736 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
3737 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes));
3738 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
3739 }
3740
3741 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3742 return Builder.CreateBitCast(Addr, PTy);
3743}
3744
3745static bool
3746PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3747 llvm::Value *Address) {
Roman Divackyd966e722012-05-09 18:22:46 +00003748 // This is calculated from the LLVM and GCC tables and verified
3749 // against gcc output. AFAIK all ABIs use the same encoding.
3750
3751 CodeGen::CGBuilderTy &Builder = CGF.Builder;
3752
3753 llvm::IntegerType *i8 = CGF.Int8Ty;
3754 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
3755 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
3756 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
3757
3758 // 0-31: r0-31, the 8-byte general-purpose registers
3759 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
3760
3761 // 32-63: fp0-31, the 8-byte floating-point registers
3762 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
3763
3764 // 64-76 are various 4-byte special-purpose registers:
3765 // 64: mq
3766 // 65: lr
3767 // 66: ctr
3768 // 67: ap
3769 // 68-75 cr0-7
3770 // 76: xer
3771 AssignToArrayRange(Builder, Address, Four8, 64, 76);
3772
3773 // 77-108: v0-31, the 16-byte vector registers
3774 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
3775
3776 // 109: vrsave
3777 // 110: vscr
3778 // 111: spe_acc
3779 // 112: spefscr
3780 // 113: sfp
3781 AssignToArrayRange(Builder, Address, Four8, 109, 113);
3782
3783 return false;
3784}
John McCallea8d8bb2010-03-11 00:10:12 +00003785
Bill Schmidt25cb3492012-10-03 19:18:57 +00003786bool
3787PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
3788 CodeGen::CodeGenFunction &CGF,
3789 llvm::Value *Address) const {
3790
3791 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
3792}
3793
3794bool
3795PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3796 llvm::Value *Address) const {
3797
3798 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
3799}
3800
Chris Lattner0cf24192010-06-28 20:05:43 +00003801//===----------------------------------------------------------------------===//
Tim Northover573cbee2014-05-24 12:52:07 +00003802// AArch64 ABI Implementation
Tim Northovera2ee4332014-03-29 15:09:45 +00003803//===----------------------------------------------------------------------===//
3804
3805namespace {
3806
Tim Northover573cbee2014-05-24 12:52:07 +00003807class AArch64ABIInfo : public ABIInfo {
Tim Northovera2ee4332014-03-29 15:09:45 +00003808public:
3809 enum ABIKind {
3810 AAPCS = 0,
3811 DarwinPCS
3812 };
3813
3814private:
3815 ABIKind Kind;
3816
3817public:
Tim Northover573cbee2014-05-24 12:52:07 +00003818 AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) : ABIInfo(CGT), Kind(Kind) {}
Tim Northovera2ee4332014-03-29 15:09:45 +00003819
3820private:
3821 ABIKind getABIKind() const { return Kind; }
3822 bool isDarwinPCS() const { return Kind == DarwinPCS; }
3823
3824 ABIArgInfo classifyReturnType(QualType RetTy) const;
Tim Northoverb047bfa2014-11-27 21:02:49 +00003825 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Reid Klecknere9f6a712014-10-31 17:10:41 +00003826 bool isHomogeneousAggregateBaseType(QualType Ty) const override;
3827 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
3828 uint64_t Members) const override;
3829
Tim Northovera2ee4332014-03-29 15:09:45 +00003830 bool isIllegalVectorType(QualType Ty) const;
3831
David Blaikie1cbb9712014-11-14 19:09:44 +00003832 void computeInfo(CGFunctionInfo &FI) const override {
Reid Kleckner40ca9132014-05-13 22:05:45 +00003833 if (!getCXXABI().classifyReturnType(FI))
3834 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Tim Northover5ffc0922014-04-17 10:20:38 +00003835
Tim Northoverb047bfa2014-11-27 21:02:49 +00003836 for (auto &it : FI.arguments())
3837 it.info = classifyArgumentType(it.type);
Tim Northovera2ee4332014-03-29 15:09:45 +00003838 }
3839
3840 llvm::Value *EmitDarwinVAArg(llvm::Value *VAListAddr, QualType Ty,
3841 CodeGenFunction &CGF) const;
3842
3843 llvm::Value *EmitAAPCSVAArg(llvm::Value *VAListAddr, QualType Ty,
3844 CodeGenFunction &CGF) const;
3845
3846 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
NAKAMURA Takumi8c894962014-11-01 01:32:27 +00003847 CodeGenFunction &CGF) const override {
Tim Northovera2ee4332014-03-29 15:09:45 +00003848 return isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF)
3849 : EmitAAPCSVAArg(VAListAddr, Ty, CGF);
3850 }
3851};
3852
Tim Northover573cbee2014-05-24 12:52:07 +00003853class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
Tim Northovera2ee4332014-03-29 15:09:45 +00003854public:
Tim Northover573cbee2014-05-24 12:52:07 +00003855 AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind)
3856 : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {}
Tim Northovera2ee4332014-03-29 15:09:45 +00003857
3858 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
3859 return "mov\tfp, fp\t\t; marker for objc_retainAutoreleaseReturnValue";
3860 }
3861
3862 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { return 31; }
3863
3864 virtual bool doesReturnSlotInterfereWithArgs() const { return false; }
3865};
3866}
3867
Tim Northoverb047bfa2014-11-27 21:02:49 +00003868ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const {
Reid Klecknerb1be6832014-11-15 01:41:41 +00003869 Ty = useFirstFieldIfTransparentUnion(Ty);
3870
Tim Northovera2ee4332014-03-29 15:09:45 +00003871 // Handle illegal vector types here.
3872 if (isIllegalVectorType(Ty)) {
3873 uint64_t Size = getContext().getTypeSize(Ty);
3874 if (Size <= 32) {
3875 llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext());
Tim Northovera2ee4332014-03-29 15:09:45 +00003876 return ABIArgInfo::getDirect(ResType);
3877 }
3878 if (Size == 64) {
3879 llvm::Type *ResType =
3880 llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2);
Tim Northovera2ee4332014-03-29 15:09:45 +00003881 return ABIArgInfo::getDirect(ResType);
3882 }
3883 if (Size == 128) {
3884 llvm::Type *ResType =
3885 llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4);
Tim Northovera2ee4332014-03-29 15:09:45 +00003886 return ABIArgInfo::getDirect(ResType);
3887 }
Tim Northovera2ee4332014-03-29 15:09:45 +00003888 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3889 }
Tim Northovera2ee4332014-03-29 15:09:45 +00003890
3891 if (!isAggregateTypeForABI(Ty)) {
3892 // Treat an enum type as its underlying type.
3893 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3894 Ty = EnumTy->getDecl()->getIntegerType();
3895
Tim Northovera2ee4332014-03-29 15:09:45 +00003896 return (Ty->isPromotableIntegerType() && isDarwinPCS()
3897 ? ABIArgInfo::getExtend()
3898 : ABIArgInfo::getDirect());
3899 }
3900
3901 // Structures with either a non-trivial destructor or a non-trivial
3902 // copy constructor are always indirect.
Reid Kleckner40ca9132014-05-13 22:05:45 +00003903 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
Reid Kleckner40ca9132014-05-13 22:05:45 +00003904 return ABIArgInfo::getIndirect(0, /*ByVal=*/RAA ==
Tim Northoverb047bfa2014-11-27 21:02:49 +00003905 CGCXXABI::RAA_DirectInMemory);
Tim Northovera2ee4332014-03-29 15:09:45 +00003906 }
3907
3908 // Empty records are always ignored on Darwin, but actually passed in C++ mode
3909 // elsewhere for GNU compatibility.
3910 if (isEmptyRecord(getContext(), Ty, true)) {
3911 if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS())
3912 return ABIArgInfo::getIgnore();
3913
Tim Northovera2ee4332014-03-29 15:09:45 +00003914 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
3915 }
3916
3917 // Homogeneous Floating-point Aggregates (HFAs) need to be expanded.
Craig Topper8a13c412014-05-21 05:09:00 +00003918 const Type *Base = nullptr;
Tim Northovera2ee4332014-03-29 15:09:45 +00003919 uint64_t Members = 0;
Reid Klecknere9f6a712014-10-31 17:10:41 +00003920 if (isHomogeneousAggregate(Ty, Base, Members)) {
Tim Northoverb047bfa2014-11-27 21:02:49 +00003921 return ABIArgInfo::getDirect(
3922 llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members));
Tim Northovera2ee4332014-03-29 15:09:45 +00003923 }
3924
3925 // Aggregates <= 16 bytes are passed directly in registers or on the stack.
3926 uint64_t Size = getContext().getTypeSize(Ty);
3927 if (Size <= 128) {
Tim Northoverc801b4a2014-04-15 14:55:11 +00003928 unsigned Alignment = getContext().getTypeAlign(Ty);
Tim Northovera2ee4332014-03-29 15:09:45 +00003929 Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes
Tim Northoverb047bfa2014-11-27 21:02:49 +00003930
Tim Northovera2ee4332014-03-29 15:09:45 +00003931 // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
3932 // For aggregates with 16-byte alignment, we use i128.
Tim Northoverc801b4a2014-04-15 14:55:11 +00003933 if (Alignment < 128 && Size == 128) {
Tim Northovera2ee4332014-03-29 15:09:45 +00003934 llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext());
3935 return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64));
3936 }
3937 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
3938 }
3939
Tim Northovera2ee4332014-03-29 15:09:45 +00003940 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3941}
3942
Tim Northover573cbee2014-05-24 12:52:07 +00003943ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const {
Tim Northovera2ee4332014-03-29 15:09:45 +00003944 if (RetTy->isVoidType())
3945 return ABIArgInfo::getIgnore();
3946
3947 // Large vector types should be returned via memory.
3948 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
3949 return ABIArgInfo::getIndirect(0);
3950
3951 if (!isAggregateTypeForABI(RetTy)) {
3952 // Treat an enum type as its underlying type.
3953 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3954 RetTy = EnumTy->getDecl()->getIntegerType();
3955
Tim Northover4dab6982014-04-18 13:46:08 +00003956 return (RetTy->isPromotableIntegerType() && isDarwinPCS()
3957 ? ABIArgInfo::getExtend()
3958 : ABIArgInfo::getDirect());
Tim Northovera2ee4332014-03-29 15:09:45 +00003959 }
3960
Tim Northovera2ee4332014-03-29 15:09:45 +00003961 if (isEmptyRecord(getContext(), RetTy, true))
3962 return ABIArgInfo::getIgnore();
3963
Craig Topper8a13c412014-05-21 05:09:00 +00003964 const Type *Base = nullptr;
Reid Klecknere9f6a712014-10-31 17:10:41 +00003965 uint64_t Members = 0;
3966 if (isHomogeneousAggregate(RetTy, Base, Members))
Tim Northovera2ee4332014-03-29 15:09:45 +00003967 // Homogeneous Floating-point Aggregates (HFAs) are returned directly.
3968 return ABIArgInfo::getDirect();
3969
3970 // Aggregates <= 16 bytes are returned directly in registers or on the stack.
3971 uint64_t Size = getContext().getTypeSize(RetTy);
3972 if (Size <= 128) {
3973 Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes
3974 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
3975 }
3976
3977 return ABIArgInfo::getIndirect(0);
3978}
3979
Tim Northover573cbee2014-05-24 12:52:07 +00003980/// isIllegalVectorType - check whether the vector type is legal for AArch64.
3981bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const {
Tim Northovera2ee4332014-03-29 15:09:45 +00003982 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3983 // Check whether VT is legal.
3984 unsigned NumElements = VT->getNumElements();
3985 uint64_t Size = getContext().getTypeSize(VT);
3986 // NumElements should be power of 2 between 1 and 16.
3987 if ((NumElements & (NumElements - 1)) != 0 || NumElements > 16)
3988 return true;
3989 return Size != 64 && (Size != 128 || NumElements == 1);
3990 }
3991 return false;
3992}
3993
Reid Klecknere9f6a712014-10-31 17:10:41 +00003994bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
3995 // Homogeneous aggregates for AAPCS64 must have base types of a floating
3996 // point type or a short-vector type. This is the same as the 32-bit ABI,
3997 // but with the difference that any floating-point type is allowed,
3998 // including __fp16.
3999 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
4000 if (BT->isFloatingPoint())
4001 return true;
4002 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
4003 unsigned VecSize = getContext().getTypeSize(VT);
4004 if (VecSize == 64 || VecSize == 128)
4005 return true;
4006 }
4007 return false;
4008}
4009
4010bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
4011 uint64_t Members) const {
4012 return Members <= 4;
4013}
4014
Tim Northoverb047bfa2014-11-27 21:02:49 +00004015llvm::Value *AArch64ABIInfo::EmitAAPCSVAArg(llvm::Value *VAListAddr,
4016 QualType Ty,
4017 CodeGenFunction &CGF) const {
4018 ABIArgInfo AI = classifyArgumentType(Ty);
Reid Klecknere9f6a712014-10-31 17:10:41 +00004019 bool IsIndirect = AI.isIndirect();
4020
Tim Northoverb047bfa2014-11-27 21:02:49 +00004021 llvm::Type *BaseTy = CGF.ConvertType(Ty);
4022 if (IsIndirect)
4023 BaseTy = llvm::PointerType::getUnqual(BaseTy);
4024 else if (AI.getCoerceToType())
4025 BaseTy = AI.getCoerceToType();
4026
4027 unsigned NumRegs = 1;
4028 if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) {
4029 BaseTy = ArrTy->getElementType();
4030 NumRegs = ArrTy->getNumElements();
4031 }
4032 bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy();
4033
Tim Northovera2ee4332014-03-29 15:09:45 +00004034 // The AArch64 va_list type and handling is specified in the Procedure Call
4035 // Standard, section B.4:
4036 //
4037 // struct {
4038 // void *__stack;
4039 // void *__gr_top;
4040 // void *__vr_top;
4041 // int __gr_offs;
4042 // int __vr_offs;
4043 // };
4044
4045 llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
4046 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
4047 llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
4048 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
4049 auto &Ctx = CGF.getContext();
4050
Craig Topper8a13c412014-05-21 05:09:00 +00004051 llvm::Value *reg_offs_p = nullptr, *reg_offs = nullptr;
Tim Northovera2ee4332014-03-29 15:09:45 +00004052 int reg_top_index;
Tim Northoverb047bfa2014-11-27 21:02:49 +00004053 int RegSize = IsIndirect ? 8 : getContext().getTypeSize(Ty) / 8;
4054 if (!IsFPR) {
Tim Northovera2ee4332014-03-29 15:09:45 +00004055 // 3 is the field number of __gr_offs
4056 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p");
4057 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
4058 reg_top_index = 1; // field number for __gr_top
Tim Northoverb047bfa2014-11-27 21:02:49 +00004059 RegSize = llvm::RoundUpToAlignment(RegSize, 8);
Tim Northovera2ee4332014-03-29 15:09:45 +00004060 } else {
Tim Northovera2ee4332014-03-29 15:09:45 +00004061 // 4 is the field number of __vr_offs.
4062 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p");
4063 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
4064 reg_top_index = 2; // field number for __vr_top
Tim Northoverb047bfa2014-11-27 21:02:49 +00004065 RegSize = 16 * NumRegs;
Tim Northovera2ee4332014-03-29 15:09:45 +00004066 }
4067
4068 //=======================================
4069 // Find out where argument was passed
4070 //=======================================
4071
4072 // If reg_offs >= 0 we're already using the stack for this type of
4073 // argument. We don't want to keep updating reg_offs (in case it overflows,
4074 // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
4075 // whatever they get).
Craig Topper8a13c412014-05-21 05:09:00 +00004076 llvm::Value *UsingStack = nullptr;
Tim Northovera2ee4332014-03-29 15:09:45 +00004077 UsingStack = CGF.Builder.CreateICmpSGE(
4078 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0));
4079
4080 CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
4081
4082 // Otherwise, at least some kind of argument could go in these registers, the
Bob Wilson3abf1692014-04-21 01:23:36 +00004083 // question is whether this particular type is too big.
Tim Northovera2ee4332014-03-29 15:09:45 +00004084 CGF.EmitBlock(MaybeRegBlock);
4085
4086 // Integer arguments may need to correct register alignment (for example a
4087 // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
4088 // align __gr_offs to calculate the potential address.
Tim Northoverb047bfa2014-11-27 21:02:49 +00004089 if (!IsFPR && !IsIndirect && Ctx.getTypeAlign(Ty) > 64) {
Tim Northovera2ee4332014-03-29 15:09:45 +00004090 int Align = Ctx.getTypeAlign(Ty) / 8;
4091
4092 reg_offs = CGF.Builder.CreateAdd(
4093 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
4094 "align_regoffs");
4095 reg_offs = CGF.Builder.CreateAnd(
4096 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align),
4097 "aligned_regoffs");
4098 }
4099
4100 // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
Craig Topper8a13c412014-05-21 05:09:00 +00004101 llvm::Value *NewOffset = nullptr;
Tim Northovera2ee4332014-03-29 15:09:45 +00004102 NewOffset = CGF.Builder.CreateAdd(
4103 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs");
4104 CGF.Builder.CreateStore(NewOffset, reg_offs_p);
4105
4106 // Now we're in a position to decide whether this argument really was in
4107 // registers or not.
Craig Topper8a13c412014-05-21 05:09:00 +00004108 llvm::Value *InRegs = nullptr;
Tim Northovera2ee4332014-03-29 15:09:45 +00004109 InRegs = CGF.Builder.CreateICmpSLE(
4110 NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg");
4111
4112 CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
4113
4114 //=======================================
4115 // Argument was in registers
4116 //=======================================
4117
4118 // Now we emit the code for if the argument was originally passed in
4119 // registers. First start the appropriate block:
4120 CGF.EmitBlock(InRegBlock);
4121
Craig Topper8a13c412014-05-21 05:09:00 +00004122 llvm::Value *reg_top_p = nullptr, *reg_top = nullptr;
Tim Northovera2ee4332014-03-29 15:09:45 +00004123 reg_top_p =
4124 CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p");
4125 reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
4126 llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs);
Craig Topper8a13c412014-05-21 05:09:00 +00004127 llvm::Value *RegAddr = nullptr;
Tim Northovera2ee4332014-03-29 15:09:45 +00004128 llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
4129
4130 if (IsIndirect) {
4131 // If it's been passed indirectly (actually a struct), whatever we find from
4132 // stored registers or on the stack will actually be a struct **.
4133 MemTy = llvm::PointerType::getUnqual(MemTy);
4134 }
4135
Craig Topper8a13c412014-05-21 05:09:00 +00004136 const Type *Base = nullptr;
Reid Klecknere9f6a712014-10-31 17:10:41 +00004137 uint64_t NumMembers = 0;
4138 bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers);
James Molloy467be602014-05-07 14:45:55 +00004139 if (IsHFA && NumMembers > 1) {
Tim Northovera2ee4332014-03-29 15:09:45 +00004140 // Homogeneous aggregates passed in registers will have their elements split
4141 // and stored 16-bytes apart regardless of size (they're notionally in qN,
4142 // qN+1, ...). We reload and store into a temporary local variable
4143 // contiguously.
4144 assert(!IsIndirect && "Homogeneous aggregates should be passed directly");
4145 llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
4146 llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
4147 llvm::Value *Tmp = CGF.CreateTempAlloca(HFATy);
4148 int Offset = 0;
4149
4150 if (CGF.CGM.getDataLayout().isBigEndian() && Ctx.getTypeSize(Base) < 128)
4151 Offset = 16 - Ctx.getTypeSize(Base) / 8;
4152 for (unsigned i = 0; i < NumMembers; ++i) {
4153 llvm::Value *BaseOffset =
4154 llvm::ConstantInt::get(CGF.Int32Ty, 16 * i + Offset);
4155 llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset);
4156 LoadAddr = CGF.Builder.CreateBitCast(
4157 LoadAddr, llvm::PointerType::getUnqual(BaseTy));
4158 llvm::Value *StoreAddr = CGF.Builder.CreateStructGEP(Tmp, i);
4159
4160 llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
4161 CGF.Builder.CreateStore(Elem, StoreAddr);
4162 }
4163
4164 RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy);
4165 } else {
4166 // Otherwise the object is contiguous in memory
4167 unsigned BeAlign = reg_top_index == 2 ? 16 : 8;
James Molloy467be602014-05-07 14:45:55 +00004168 if (CGF.CGM.getDataLayout().isBigEndian() &&
4169 (IsHFA || !isAggregateTypeForABI(Ty)) &&
Tim Northovera2ee4332014-03-29 15:09:45 +00004170 Ctx.getTypeSize(Ty) < (BeAlign * 8)) {
4171 int Offset = BeAlign - Ctx.getTypeSize(Ty) / 8;
4172 BaseAddr = CGF.Builder.CreatePtrToInt(BaseAddr, CGF.Int64Ty);
4173
4174 BaseAddr = CGF.Builder.CreateAdd(
4175 BaseAddr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), "align_be");
4176
4177 BaseAddr = CGF.Builder.CreateIntToPtr(BaseAddr, CGF.Int8PtrTy);
4178 }
4179
4180 RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy);
4181 }
4182
4183 CGF.EmitBranch(ContBlock);
4184
4185 //=======================================
4186 // Argument was on the stack
4187 //=======================================
4188 CGF.EmitBlock(OnStackBlock);
4189
Craig Topper8a13c412014-05-21 05:09:00 +00004190 llvm::Value *stack_p = nullptr, *OnStackAddr = nullptr;
Tim Northovera2ee4332014-03-29 15:09:45 +00004191 stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p");
4192 OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack");
4193
4194 // Again, stack arguments may need realigmnent. In this case both integer and
4195 // floating-point ones might be affected.
4196 if (!IsIndirect && Ctx.getTypeAlign(Ty) > 64) {
4197 int Align = Ctx.getTypeAlign(Ty) / 8;
4198
4199 OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty);
4200
4201 OnStackAddr = CGF.Builder.CreateAdd(
4202 OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
4203 "align_stack");
4204 OnStackAddr = CGF.Builder.CreateAnd(
4205 OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, -Align),
4206 "align_stack");
4207
4208 OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy);
4209 }
4210
4211 uint64_t StackSize;
4212 if (IsIndirect)
4213 StackSize = 8;
4214 else
4215 StackSize = Ctx.getTypeSize(Ty) / 8;
4216
4217 // All stack slots are 8 bytes
4218 StackSize = llvm::RoundUpToAlignment(StackSize, 8);
4219
4220 llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize);
4221 llvm::Value *NewStack =
4222 CGF.Builder.CreateGEP(OnStackAddr, StackSizeC, "new_stack");
4223
4224 // Write the new value of __stack for the next call to va_arg
4225 CGF.Builder.CreateStore(NewStack, stack_p);
4226
4227 if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) &&
4228 Ctx.getTypeSize(Ty) < 64) {
4229 int Offset = 8 - Ctx.getTypeSize(Ty) / 8;
4230 OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty);
4231
4232 OnStackAddr = CGF.Builder.CreateAdd(
4233 OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), "align_be");
4234
4235 OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy);
4236 }
4237
4238 OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy);
4239
4240 CGF.EmitBranch(ContBlock);
4241
4242 //=======================================
4243 // Tidy up
4244 //=======================================
4245 CGF.EmitBlock(ContBlock);
4246
4247 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr");
4248 ResAddr->addIncoming(RegAddr, InRegBlock);
4249 ResAddr->addIncoming(OnStackAddr, OnStackBlock);
4250
4251 if (IsIndirect)
4252 return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr");
4253
4254 return ResAddr;
4255}
4256
Tim Northover573cbee2014-05-24 12:52:07 +00004257llvm::Value *AArch64ABIInfo::EmitDarwinVAArg(llvm::Value *VAListAddr, QualType Ty,
Tim Northovera2ee4332014-03-29 15:09:45 +00004258 CodeGenFunction &CGF) const {
4259 // We do not support va_arg for aggregates or illegal vector types.
4260 // Lower VAArg here for these cases and use the LLVM va_arg instruction for
4261 // other cases.
4262 if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty))
Craig Topper8a13c412014-05-21 05:09:00 +00004263 return nullptr;
Tim Northovera2ee4332014-03-29 15:09:45 +00004264
4265 uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8;
4266 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
4267
Craig Topper8a13c412014-05-21 05:09:00 +00004268 const Type *Base = nullptr;
Reid Klecknere9f6a712014-10-31 17:10:41 +00004269 uint64_t Members = 0;
4270 bool isHA = isHomogeneousAggregate(Ty, Base, Members);
Tim Northovera2ee4332014-03-29 15:09:45 +00004271
4272 bool isIndirect = false;
4273 // Arguments bigger than 16 bytes which aren't homogeneous aggregates should
4274 // be passed indirectly.
4275 if (Size > 16 && !isHA) {
4276 isIndirect = true;
4277 Size = 8;
4278 Align = 8;
4279 }
4280
4281 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
4282 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
4283
4284 CGBuilderTy &Builder = CGF.Builder;
4285 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
4286 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
4287
4288 if (isEmptyRecord(getContext(), Ty, true)) {
4289 // These are ignored for parameter passing purposes.
4290 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
4291 return Builder.CreateBitCast(Addr, PTy);
4292 }
4293
4294 const uint64_t MinABIAlign = 8;
4295 if (Align > MinABIAlign) {
4296 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
4297 Addr = Builder.CreateGEP(Addr, Offset);
4298 llvm::Value *AsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
4299 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~(Align - 1));
4300 llvm::Value *Aligned = Builder.CreateAnd(AsInt, Mask);
4301 Addr = Builder.CreateIntToPtr(Aligned, BP, "ap.align");
4302 }
4303
4304 uint64_t Offset = llvm::RoundUpToAlignment(Size, MinABIAlign);
4305 llvm::Value *NextAddr = Builder.CreateGEP(
4306 Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), "ap.next");
4307 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
4308
4309 if (isIndirect)
4310 Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP));
4311 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
4312 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
4313
4314 return AddrTyped;
4315}
4316
4317//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00004318// ARM ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00004319//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00004320
4321namespace {
4322
Anton Korobeynikov244360d2009-06-05 22:08:42 +00004323class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00004324public:
4325 enum ABIKind {
4326 APCS = 0,
4327 AAPCS = 1,
4328 AAPCS_VFP
4329 };
4330
4331private:
4332 ABIKind Kind;
Oliver Stannard405bded2014-02-11 09:25:50 +00004333 mutable int VFPRegs[16];
4334 const unsigned NumVFPs;
4335 const unsigned NumGPRs;
4336 mutable unsigned AllocatedGPRs;
4337 mutable unsigned AllocatedVFPs;
Daniel Dunbar020daa92009-09-12 01:00:39 +00004338
4339public:
Oliver Stannard405bded2014-02-11 09:25:50 +00004340 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind),
4341 NumVFPs(16), NumGPRs(4) {
Anton Korobeynikovd90dd792014-12-02 16:04:58 +00004342 setCCs();
Oliver Stannard405bded2014-02-11 09:25:50 +00004343 resetAllocatedRegs();
John McCall882987f2013-02-28 19:01:20 +00004344 }
Daniel Dunbar020daa92009-09-12 01:00:39 +00004345
John McCall3480ef22011-08-30 01:42:09 +00004346 bool isEABI() const {
Joerg Sonnenberger782e6aa2013-12-12 21:29:27 +00004347 switch (getTarget().getTriple().getEnvironment()) {
4348 case llvm::Triple::Android:
4349 case llvm::Triple::EABI:
Joerg Sonnenbergerd75a1f82013-12-16 19:16:04 +00004350 case llvm::Triple::EABIHF:
Joerg Sonnenberger782e6aa2013-12-12 21:29:27 +00004351 case llvm::Triple::GNUEABI:
Joerg Sonnenberger0c1652d2013-12-16 18:30:28 +00004352 case llvm::Triple::GNUEABIHF:
Joerg Sonnenberger782e6aa2013-12-12 21:29:27 +00004353 return true;
4354 default:
4355 return false;
4356 }
John McCall3480ef22011-08-30 01:42:09 +00004357 }
4358
Joerg Sonnenbergerd75a1f82013-12-16 19:16:04 +00004359 bool isEABIHF() const {
4360 switch (getTarget().getTriple().getEnvironment()) {
4361 case llvm::Triple::EABIHF:
4362 case llvm::Triple::GNUEABIHF:
4363 return true;
4364 default:
4365 return false;
4366 }
4367 }
4368
Daniel Dunbar020daa92009-09-12 01:00:39 +00004369 ABIKind getABIKind() const { return Kind; }
4370
Tim Northovera484bc02013-10-01 14:34:25 +00004371private:
Amara Emerson9dc78782014-01-28 10:56:36 +00004372 ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const;
James Molloy6f244b62014-05-09 16:21:39 +00004373 ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic,
Oliver Stannard405bded2014-02-11 09:25:50 +00004374 bool &IsCPRC) const;
Manman Renfef9e312012-10-16 19:18:39 +00004375 bool isIllegalVectorType(QualType Ty) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00004376
Reid Klecknere9f6a712014-10-31 17:10:41 +00004377 bool isHomogeneousAggregateBaseType(QualType Ty) const override;
4378 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
4379 uint64_t Members) const override;
4380
Craig Topper4f12f102014-03-12 06:41:41 +00004381 void computeInfo(CGFunctionInfo &FI) const override;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00004382
Craig Topper4f12f102014-03-12 06:41:41 +00004383 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4384 CodeGenFunction &CGF) const override;
John McCall882987f2013-02-28 19:01:20 +00004385
4386 llvm::CallingConv::ID getLLVMDefaultCC() const;
4387 llvm::CallingConv::ID getABIDefaultCC() const;
Anton Korobeynikovd90dd792014-12-02 16:04:58 +00004388 void setCCs();
Oliver Stannard405bded2014-02-11 09:25:50 +00004389
4390 void markAllocatedGPRs(unsigned Alignment, unsigned NumRequired) const;
4391 void markAllocatedVFPs(unsigned Alignment, unsigned NumRequired) const;
4392 void resetAllocatedRegs(void) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00004393};
4394
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004395class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
4396public:
Chris Lattner2b037972010-07-29 02:01:43 +00004397 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
4398 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00004399
John McCall3480ef22011-08-30 01:42:09 +00004400 const ARMABIInfo &getABIInfo() const {
4401 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
4402 }
4403
Craig Topper4f12f102014-03-12 06:41:41 +00004404 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
John McCallbeec5a02010-03-06 00:35:14 +00004405 return 13;
4406 }
Roman Divackyc1617352011-05-18 19:36:54 +00004407
Craig Topper4f12f102014-03-12 06:41:41 +00004408 StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
John McCall31168b02011-06-15 23:02:42 +00004409 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
4410 }
4411
Roman Divackyc1617352011-05-18 19:36:54 +00004412 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00004413 llvm::Value *Address) const override {
Chris Lattnerece04092012-02-07 00:39:47 +00004414 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divackyc1617352011-05-18 19:36:54 +00004415
4416 // 0-15 are the 16 integer registers.
Chris Lattnerece04092012-02-07 00:39:47 +00004417 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divackyc1617352011-05-18 19:36:54 +00004418 return false;
4419 }
John McCall3480ef22011-08-30 01:42:09 +00004420
Craig Topper4f12f102014-03-12 06:41:41 +00004421 unsigned getSizeOfUnwindException() const override {
John McCall3480ef22011-08-30 01:42:09 +00004422 if (getABIInfo().isEABI()) return 88;
4423 return TargetCodeGenInfo::getSizeOfUnwindException();
4424 }
Tim Northovera484bc02013-10-01 14:34:25 +00004425
4426 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Craig Topper4f12f102014-03-12 06:41:41 +00004427 CodeGen::CodeGenModule &CGM) const override {
Tim Northovera484bc02013-10-01 14:34:25 +00004428 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4429 if (!FD)
4430 return;
4431
4432 const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>();
4433 if (!Attr)
4434 return;
4435
4436 const char *Kind;
4437 switch (Attr->getInterrupt()) {
4438 case ARMInterruptAttr::Generic: Kind = ""; break;
4439 case ARMInterruptAttr::IRQ: Kind = "IRQ"; break;
4440 case ARMInterruptAttr::FIQ: Kind = "FIQ"; break;
4441 case ARMInterruptAttr::SWI: Kind = "SWI"; break;
4442 case ARMInterruptAttr::ABORT: Kind = "ABORT"; break;
4443 case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break;
4444 }
4445
4446 llvm::Function *Fn = cast<llvm::Function>(GV);
4447
4448 Fn->addFnAttr("interrupt", Kind);
4449
4450 if (cast<ARMABIInfo>(getABIInfo()).getABIKind() == ARMABIInfo::APCS)
4451 return;
4452
4453 // AAPCS guarantees that sp will be 8-byte aligned on any public interface,
4454 // however this is not necessarily true on taking any interrupt. Instruct
4455 // the backend to perform a realignment as part of the function prologue.
4456 llvm::AttrBuilder B;
4457 B.addStackAlignmentAttr(8);
4458 Fn->addAttributes(llvm::AttributeSet::FunctionIndex,
4459 llvm::AttributeSet::get(CGM.getLLVMContext(),
4460 llvm::AttributeSet::FunctionIndex,
4461 B));
4462 }
4463
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004464};
4465
Daniel Dunbard59655c2009-09-12 00:59:49 +00004466}
4467
Chris Lattner22326a12010-07-29 02:31:05 +00004468void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Manman Ren2a523d82012-10-30 23:21:41 +00004469 // To correctly handle Homogeneous Aggregate, we need to keep track of the
Manman Renb505d332012-10-31 19:02:26 +00004470 // VFP registers allocated so far.
Manman Ren2a523d82012-10-30 23:21:41 +00004471 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
4472 // VFP registers of the appropriate type unallocated then the argument is
4473 // allocated to the lowest-numbered sequence of such registers.
4474 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
4475 // unallocated are marked as unavailable.
Oliver Stannard405bded2014-02-11 09:25:50 +00004476 resetAllocatedRegs();
4477
Reid Kleckner40ca9132014-05-13 22:05:45 +00004478 if (getCXXABI().classifyReturnType(FI)) {
4479 if (FI.getReturnInfo().isIndirect())
4480 markAllocatedGPRs(1, 1);
4481 } else {
4482 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), FI.isVariadic());
4483 }
Aaron Ballmanec47bc22014-03-17 18:10:01 +00004484 for (auto &I : FI.arguments()) {
Oliver Stannard405bded2014-02-11 09:25:50 +00004485 unsigned PreAllocationVFPs = AllocatedVFPs;
4486 unsigned PreAllocationGPRs = AllocatedGPRs;
Oliver Stannard405bded2014-02-11 09:25:50 +00004487 bool IsCPRC = false;
Manman Ren2a523d82012-10-30 23:21:41 +00004488 // 6.1.2.3 There is one VFP co-processor register class using registers
4489 // s0-s15 (d0-d7) for passing arguments.
James Molloy6f244b62014-05-09 16:21:39 +00004490 I.info = classifyArgumentType(I.type, FI.isVariadic(), IsCPRC);
Oliver Stannard405bded2014-02-11 09:25:50 +00004491
4492 // If we have allocated some arguments onto the stack (due to running
4493 // out of VFP registers), we cannot split an argument between GPRs and
4494 // the stack. If this situation occurs, we add padding to prevent the
Oliver Stannarda3afc692014-05-19 13:10:05 +00004495 // GPRs from being used. In this situation, the current argument could
Oliver Stannard405bded2014-02-11 09:25:50 +00004496 // only be allocated by rule C.8, so rule C.6 would mark these GPRs as
4497 // unusable anyway.
Oliver Stannarde0228512014-07-18 09:09:31 +00004498 // We do not have to do this if the argument is being passed ByVal, as the
4499 // backend can handle that situation correctly.
Oliver Stannard405bded2014-02-11 09:25:50 +00004500 const bool StackUsed = PreAllocationGPRs > NumGPRs || PreAllocationVFPs > NumVFPs;
Oliver Stannarde0228512014-07-18 09:09:31 +00004501 const bool IsByVal = I.info.isIndirect() && I.info.getIndirectByVal();
4502 if (!IsCPRC && PreAllocationGPRs < NumGPRs && AllocatedGPRs > NumGPRs &&
4503 StackUsed && !IsByVal) {
Oliver Stannard405bded2014-02-11 09:25:50 +00004504 llvm::Type *PaddingTy = llvm::ArrayType::get(
4505 llvm::Type::getInt32Ty(getVMContext()), NumGPRs - PreAllocationGPRs);
Oliver Stannarda3afc692014-05-19 13:10:05 +00004506 if (I.info.canHaveCoerceToType()) {
Tim Northover5a1558e2014-11-07 22:30:50 +00004507 I.info = ABIArgInfo::getDirect(I.info.getCoerceToType() /* type */,
4508 0 /* offset */, PaddingTy, true);
Oliver Stannarda3afc692014-05-19 13:10:05 +00004509 } else {
4510 I.info = ABIArgInfo::getDirect(nullptr /* type */, 0 /* offset */,
Tim Northover5a1558e2014-11-07 22:30:50 +00004511 PaddingTy, true);
Oliver Stannarda3afc692014-05-19 13:10:05 +00004512 }
Manman Ren2a523d82012-10-30 23:21:41 +00004513 }
4514 }
Daniel Dunbar020daa92009-09-12 01:00:39 +00004515
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004516 // Always honor user-specified calling convention.
4517 if (FI.getCallingConvention() != llvm::CallingConv::C)
4518 return;
4519
John McCall882987f2013-02-28 19:01:20 +00004520 llvm::CallingConv::ID cc = getRuntimeCC();
4521 if (cc != llvm::CallingConv::C)
4522 FI.setEffectiveCallingConvention(cc);
4523}
Rafael Espindolaa92c4422010-06-16 16:13:39 +00004524
John McCall882987f2013-02-28 19:01:20 +00004525/// Return the default calling convention that LLVM will use.
4526llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
4527 // The default calling convention that LLVM will infer.
Joerg Sonnenbergerd75a1f82013-12-16 19:16:04 +00004528 if (isEABIHF())
John McCall882987f2013-02-28 19:01:20 +00004529 return llvm::CallingConv::ARM_AAPCS_VFP;
4530 else if (isEABI())
4531 return llvm::CallingConv::ARM_AAPCS;
4532 else
4533 return llvm::CallingConv::ARM_APCS;
4534}
4535
4536/// Return the calling convention that our ABI would like us to use
4537/// as the C calling convention.
4538llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
Daniel Dunbar020daa92009-09-12 01:00:39 +00004539 switch (getABIKind()) {
John McCall882987f2013-02-28 19:01:20 +00004540 case APCS: return llvm::CallingConv::ARM_APCS;
4541 case AAPCS: return llvm::CallingConv::ARM_AAPCS;
4542 case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
Daniel Dunbar020daa92009-09-12 01:00:39 +00004543 }
John McCall882987f2013-02-28 19:01:20 +00004544 llvm_unreachable("bad ABI kind");
4545}
4546
Anton Korobeynikovd90dd792014-12-02 16:04:58 +00004547void ARMABIInfo::setCCs() {
John McCall882987f2013-02-28 19:01:20 +00004548 assert(getRuntimeCC() == llvm::CallingConv::C);
4549
4550 // Don't muddy up the IR with a ton of explicit annotations if
4551 // they'd just match what LLVM will infer from the triple.
4552 llvm::CallingConv::ID abiCC = getABIDefaultCC();
4553 if (abiCC != getLLVMDefaultCC())
4554 RuntimeCC = abiCC;
Anton Korobeynikovd90dd792014-12-02 16:04:58 +00004555
4556 BuiltinCC = (getABIKind() == APCS ?
4557 llvm::CallingConv::ARM_APCS : llvm::CallingConv::ARM_AAPCS);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00004558}
4559
Manman Renb505d332012-10-31 19:02:26 +00004560/// markAllocatedVFPs - update VFPRegs according to the alignment and
4561/// number of VFP registers (unit is S register) requested.
Oliver Stannard405bded2014-02-11 09:25:50 +00004562void ARMABIInfo::markAllocatedVFPs(unsigned Alignment,
4563 unsigned NumRequired) const {
Manman Renb505d332012-10-31 19:02:26 +00004564 // Early Exit.
Oliver Stannard405bded2014-02-11 09:25:50 +00004565 if (AllocatedVFPs >= 16) {
4566 // We use AllocatedVFP > 16 to signal that some CPRCs were allocated on
4567 // the stack.
4568 AllocatedVFPs = 17;
Manman Renb505d332012-10-31 19:02:26 +00004569 return;
Oliver Stannard405bded2014-02-11 09:25:50 +00004570 }
Manman Renb505d332012-10-31 19:02:26 +00004571 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
4572 // VFP registers of the appropriate type unallocated then the argument is
4573 // allocated to the lowest-numbered sequence of such registers.
4574 for (unsigned I = 0; I < 16; I += Alignment) {
4575 bool FoundSlot = true;
4576 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
4577 if (J >= 16 || VFPRegs[J]) {
4578 FoundSlot = false;
4579 break;
4580 }
4581 if (FoundSlot) {
4582 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
4583 VFPRegs[J] = 1;
Oliver Stannard405bded2014-02-11 09:25:50 +00004584 AllocatedVFPs += NumRequired;
Manman Renb505d332012-10-31 19:02:26 +00004585 return;
4586 }
4587 }
4588 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
4589 // unallocated are marked as unavailable.
4590 for (unsigned I = 0; I < 16; I++)
4591 VFPRegs[I] = 1;
Oliver Stannard405bded2014-02-11 09:25:50 +00004592 AllocatedVFPs = 17; // We do not have enough VFP registers.
Manman Renb505d332012-10-31 19:02:26 +00004593}
4594
Oliver Stannard405bded2014-02-11 09:25:50 +00004595/// Update AllocatedGPRs to record the number of general purpose registers
4596/// which have been allocated. It is valid for AllocatedGPRs to go above 4,
4597/// this represents arguments being stored on the stack.
4598void ARMABIInfo::markAllocatedGPRs(unsigned Alignment,
Oliver Stannard3f32b9b2014-06-27 13:59:27 +00004599 unsigned NumRequired) const {
Oliver Stannard405bded2014-02-11 09:25:50 +00004600 assert((Alignment == 1 || Alignment == 2) && "Alignment must be 4 or 8 bytes");
4601
4602 if (Alignment == 2 && AllocatedGPRs & 0x1)
4603 AllocatedGPRs += 1;
4604
4605 AllocatedGPRs += NumRequired;
4606}
4607
4608void ARMABIInfo::resetAllocatedRegs(void) const {
4609 AllocatedGPRs = 0;
4610 AllocatedVFPs = 0;
4611 for (unsigned i = 0; i < NumVFPs; ++i)
4612 VFPRegs[i] = 0;
4613}
4614
James Molloy6f244b62014-05-09 16:21:39 +00004615ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, bool isVariadic,
Oliver Stannard405bded2014-02-11 09:25:50 +00004616 bool &IsCPRC) const {
Manman Ren2a523d82012-10-30 23:21:41 +00004617 // We update number of allocated VFPs according to
4618 // 6.1.2.1 The following argument types are VFP CPRCs:
4619 // A single-precision floating-point type (including promoted
4620 // half-precision types); A double-precision floating-point type;
4621 // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
4622 // with a Base Type of a single- or double-precision floating-point type,
4623 // 64-bit containerized vectors or 128-bit containerized vectors with one
4624 // to four Elements.
Tim Northover5a1558e2014-11-07 22:30:50 +00004625 bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic;
Oliver Stannard2bfdc5b2014-08-27 10:43:15 +00004626
Reid Klecknerb1be6832014-11-15 01:41:41 +00004627 Ty = useFirstFieldIfTransparentUnion(Ty);
4628
Manman Renfef9e312012-10-16 19:18:39 +00004629 // Handle illegal vector types here.
4630 if (isIllegalVectorType(Ty)) {
4631 uint64_t Size = getContext().getTypeSize(Ty);
4632 if (Size <= 32) {
4633 llvm::Type *ResType =
4634 llvm::Type::getInt32Ty(getVMContext());
Oliver Stannard405bded2014-02-11 09:25:50 +00004635 markAllocatedGPRs(1, 1);
Tim Northover5a1558e2014-11-07 22:30:50 +00004636 return ABIArgInfo::getDirect(ResType);
Manman Renfef9e312012-10-16 19:18:39 +00004637 }
4638 if (Size == 64) {
4639 llvm::Type *ResType = llvm::VectorType::get(
4640 llvm::Type::getInt32Ty(getVMContext()), 2);
Oliver Stannard405bded2014-02-11 09:25:50 +00004641 if (getABIKind() == ARMABIInfo::AAPCS || isVariadic){
4642 markAllocatedGPRs(2, 2);
4643 } else {
4644 markAllocatedVFPs(2, 2);
4645 IsCPRC = true;
4646 }
Tim Northover5a1558e2014-11-07 22:30:50 +00004647 return ABIArgInfo::getDirect(ResType);
Manman Renfef9e312012-10-16 19:18:39 +00004648 }
4649 if (Size == 128) {
4650 llvm::Type *ResType = llvm::VectorType::get(
4651 llvm::Type::getInt32Ty(getVMContext()), 4);
Oliver Stannard405bded2014-02-11 09:25:50 +00004652 if (getABIKind() == ARMABIInfo::AAPCS || isVariadic) {
4653 markAllocatedGPRs(2, 4);
4654 } else {
4655 markAllocatedVFPs(4, 4);
4656 IsCPRC = true;
4657 }
Tim Northover5a1558e2014-11-07 22:30:50 +00004658 return ABIArgInfo::getDirect(ResType);
Manman Renfef9e312012-10-16 19:18:39 +00004659 }
Oliver Stannard405bded2014-02-11 09:25:50 +00004660 markAllocatedGPRs(1, 1);
Manman Renfef9e312012-10-16 19:18:39 +00004661 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
4662 }
Manman Renb505d332012-10-31 19:02:26 +00004663 // Update VFPRegs for legal vector types.
Oliver Stannard405bded2014-02-11 09:25:50 +00004664 if (getABIKind() == ARMABIInfo::AAPCS_VFP && !isVariadic) {
4665 if (const VectorType *VT = Ty->getAs<VectorType>()) {
4666 uint64_t Size = getContext().getTypeSize(VT);
4667 // Size of a legal vector should be power of 2 and above 64.
4668 markAllocatedVFPs(Size >= 128 ? 4 : 2, Size / 32);
4669 IsCPRC = true;
4670 }
Manman Ren2a523d82012-10-30 23:21:41 +00004671 }
Manman Renb505d332012-10-31 19:02:26 +00004672 // Update VFPRegs for floating point types.
Oliver Stannard405bded2014-02-11 09:25:50 +00004673 if (getABIKind() == ARMABIInfo::AAPCS_VFP && !isVariadic) {
4674 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
4675 if (BT->getKind() == BuiltinType::Half ||
4676 BT->getKind() == BuiltinType::Float) {
4677 markAllocatedVFPs(1, 1);
4678 IsCPRC = true;
4679 }
4680 if (BT->getKind() == BuiltinType::Double ||
4681 BT->getKind() == BuiltinType::LongDouble) {
4682 markAllocatedVFPs(2, 2);
4683 IsCPRC = true;
4684 }
4685 }
Manman Ren2a523d82012-10-30 23:21:41 +00004686 }
Manman Renfef9e312012-10-16 19:18:39 +00004687
John McCalla1dee5302010-08-22 10:59:02 +00004688 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00004689 // Treat an enum type as its underlying type.
Oliver Stannard405bded2014-02-11 09:25:50 +00004690 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
Douglas Gregora71cc152010-02-02 20:10:50 +00004691 Ty = EnumTy->getDecl()->getIntegerType();
Oliver Stannard405bded2014-02-11 09:25:50 +00004692 }
Douglas Gregora71cc152010-02-02 20:10:50 +00004693
Oliver Stannard405bded2014-02-11 09:25:50 +00004694 unsigned Size = getContext().getTypeSize(Ty);
4695 if (!IsCPRC)
4696 markAllocatedGPRs(Size > 32 ? 2 : 1, (Size + 31) / 32);
Tim Northover5a1558e2014-11-07 22:30:50 +00004697 return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend()
4698 : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00004699 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00004700
Oliver Stannard405bded2014-02-11 09:25:50 +00004701 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
4702 markAllocatedGPRs(1, 1);
Tim Northover1060eae2013-06-21 22:49:34 +00004703 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Oliver Stannard405bded2014-02-11 09:25:50 +00004704 }
Tim Northover1060eae2013-06-21 22:49:34 +00004705
Daniel Dunbar09d33622009-09-14 21:54:03 +00004706 // Ignore empty records.
Chris Lattner458b2aa2010-07-29 02:16:43 +00004707 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar09d33622009-09-14 21:54:03 +00004708 return ABIArgInfo::getIgnore();
4709
Tim Northover5a1558e2014-11-07 22:30:50 +00004710 if (IsEffectivelyAAPCS_VFP) {
Manman Ren2a523d82012-10-30 23:21:41 +00004711 // Homogeneous Aggregates need to be expanded when we can fit the aggregate
4712 // into VFP registers.
Craig Topper8a13c412014-05-21 05:09:00 +00004713 const Type *Base = nullptr;
Manman Ren2a523d82012-10-30 23:21:41 +00004714 uint64_t Members = 0;
Reid Klecknere9f6a712014-10-31 17:10:41 +00004715 if (isHomogeneousAggregate(Ty, Base, Members)) {
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00004716 assert(Base && "Base class should be set for homogeneous aggregate");
Manman Ren2a523d82012-10-30 23:21:41 +00004717 // Base can be a floating-point or a vector.
4718 if (Base->isVectorType()) {
4719 // ElementSize is in number of floats.
4720 unsigned ElementSize = getContext().getTypeSize(Base) == 64 ? 2 : 4;
Oliver Stannard405bded2014-02-11 09:25:50 +00004721 markAllocatedVFPs(ElementSize,
Manman Ren77b02382012-11-06 19:05:29 +00004722 Members * ElementSize);
Manman Ren2a523d82012-10-30 23:21:41 +00004723 } else if (Base->isSpecificBuiltinType(BuiltinType::Float))
Oliver Stannard405bded2014-02-11 09:25:50 +00004724 markAllocatedVFPs(1, Members);
Manman Ren2a523d82012-10-30 23:21:41 +00004725 else {
4726 assert(Base->isSpecificBuiltinType(BuiltinType::Double) ||
4727 Base->isSpecificBuiltinType(BuiltinType::LongDouble));
Oliver Stannard405bded2014-02-11 09:25:50 +00004728 markAllocatedVFPs(2, Members * 2);
Manman Ren2a523d82012-10-30 23:21:41 +00004729 }
Oliver Stannard405bded2014-02-11 09:25:50 +00004730 IsCPRC = true;
Tim Northover5a1558e2014-11-07 22:30:50 +00004731 return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00004732 }
Bob Wilsone826a2a2011-08-03 05:58:22 +00004733 }
4734
Manman Ren6c30e132012-08-13 21:23:55 +00004735 // Support byval for ARM.
Manman Ren77b02382012-11-06 19:05:29 +00004736 // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
4737 // most 8-byte. We realign the indirect argument if type alignment is bigger
4738 // than ABI alignment.
Manman Ren505d68f2012-11-05 22:42:46 +00004739 uint64_t ABIAlign = 4;
4740 uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8;
4741 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
4742 getABIKind() == ARMABIInfo::AAPCS)
4743 ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
Manman Ren8cd99812012-11-06 04:58:01 +00004744 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
Oliver Stannard3f32b9b2014-06-27 13:59:27 +00004745 // Update Allocated GPRs. Since this is only used when the size of the
4746 // argument is greater than 64 bytes, this will always use up any available
4747 // registers (of which there are 4). We also don't care about getting the
4748 // alignment right, because general-purpose registers cannot be back-filled.
4749 markAllocatedGPRs(1, 4);
Oliver Stannard7c3c09e2014-03-12 14:02:50 +00004750 return ABIArgInfo::getIndirect(TyAlign, /*ByVal=*/true,
Manman Ren77b02382012-11-06 19:05:29 +00004751 /*Realign=*/TyAlign > ABIAlign);
Eli Friedmane66abda2012-08-09 00:31:40 +00004752 }
4753
Daniel Dunbarb34b0802010-09-23 01:54:28 +00004754 // Otherwise, pass by coercing to a structure of the appropriate size.
Chris Lattner2192fe52011-07-18 04:24:23 +00004755 llvm::Type* ElemTy;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00004756 unsigned SizeRegs;
Eli Friedmane66abda2012-08-09 00:31:40 +00004757 // FIXME: Try to match the types of the arguments more accurately where
4758 // we can.
4759 if (getContext().getTypeAlign(Ty) <= 32) {
Bob Wilson8e2b75d2011-08-01 23:39:04 +00004760 ElemTy = llvm::Type::getInt32Ty(getVMContext());
4761 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Oliver Stannard405bded2014-02-11 09:25:50 +00004762 markAllocatedGPRs(1, SizeRegs);
Manman Ren6fdb1582012-06-25 22:04:00 +00004763 } else {
Manman Ren6fdb1582012-06-25 22:04:00 +00004764 ElemTy = llvm::Type::getInt64Ty(getVMContext());
4765 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Oliver Stannard405bded2014-02-11 09:25:50 +00004766 markAllocatedGPRs(2, SizeRegs * 2);
Stuart Hastingsf2752a32011-04-27 17:24:02 +00004767 }
Stuart Hastings4b214952011-04-28 18:16:06 +00004768
Tim Northover5a1558e2014-11-07 22:30:50 +00004769 return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00004770}
4771
Chris Lattner458b2aa2010-07-29 02:16:43 +00004772static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar626f1d82009-09-13 08:03:58 +00004773 llvm::LLVMContext &VMContext) {
4774 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
4775 // is called integer-like if its size is less than or equal to one word, and
4776 // the offset of each of its addressable sub-fields is zero.
4777
4778 uint64_t Size = Context.getTypeSize(Ty);
4779
4780 // Check that the type fits in a word.
4781 if (Size > 32)
4782 return false;
4783
4784 // FIXME: Handle vector types!
4785 if (Ty->isVectorType())
4786 return false;
4787
Daniel Dunbard53bac72009-09-14 02:20:34 +00004788 // Float types are never treated as "integer like".
4789 if (Ty->isRealFloatingType())
4790 return false;
4791
Daniel Dunbar626f1d82009-09-13 08:03:58 +00004792 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00004793 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00004794 return true;
4795
Daniel Dunbar96ebba52010-02-01 23:31:26 +00004796 // Small complex integer types are "integer like".
4797 if (const ComplexType *CT = Ty->getAs<ComplexType>())
4798 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00004799
4800 // Single element and zero sized arrays should be allowed, by the definition
4801 // above, but they are not.
4802
4803 // Otherwise, it must be a record type.
4804 const RecordType *RT = Ty->getAs<RecordType>();
4805 if (!RT) return false;
4806
4807 // Ignore records with flexible arrays.
4808 const RecordDecl *RD = RT->getDecl();
4809 if (RD->hasFlexibleArrayMember())
4810 return false;
4811
4812 // Check that all sub-fields are at offset 0, and are themselves "integer
4813 // like".
4814 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
4815
4816 bool HadField = false;
4817 unsigned idx = 0;
4818 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
4819 i != e; ++i, ++idx) {
David Blaikie40ed2972012-06-06 20:45:41 +00004820 const FieldDecl *FD = *i;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00004821
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00004822 // Bit-fields are not addressable, we only need to verify they are "integer
4823 // like". We still have to disallow a subsequent non-bitfield, for example:
4824 // struct { int : 0; int x }
4825 // is non-integer like according to gcc.
4826 if (FD->isBitField()) {
4827 if (!RD->isUnion())
4828 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00004829
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00004830 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
4831 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00004832
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00004833 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00004834 }
4835
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00004836 // Check if this field is at offset 0.
4837 if (Layout.getFieldOffset(idx) != 0)
4838 return false;
4839
Daniel Dunbar626f1d82009-09-13 08:03:58 +00004840 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
4841 return false;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00004842
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00004843 // Only allow at most one field in a structure. This doesn't match the
4844 // wording above, but follows gcc in situations with a field following an
4845 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00004846 if (!RD->isUnion()) {
4847 if (HadField)
4848 return false;
4849
4850 HadField = true;
4851 }
4852 }
4853
4854 return true;
4855}
4856
Oliver Stannard405bded2014-02-11 09:25:50 +00004857ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
4858 bool isVariadic) const {
Tim Northover5a1558e2014-11-07 22:30:50 +00004859 bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic;
Oliver Stannard2bfdc5b2014-08-27 10:43:15 +00004860
Daniel Dunbar626f1d82009-09-13 08:03:58 +00004861 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00004862 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00004863
Daniel Dunbar19964db2010-09-23 01:54:32 +00004864 // Large vector types should be returned via memory.
Oliver Stannard405bded2014-02-11 09:25:50 +00004865 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) {
4866 markAllocatedGPRs(1, 1);
Daniel Dunbar19964db2010-09-23 01:54:32 +00004867 return ABIArgInfo::getIndirect(0);
Oliver Stannard405bded2014-02-11 09:25:50 +00004868 }
Daniel Dunbar19964db2010-09-23 01:54:32 +00004869
John McCalla1dee5302010-08-22 10:59:02 +00004870 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00004871 // Treat an enum type as its underlying type.
4872 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4873 RetTy = EnumTy->getDecl()->getIntegerType();
4874
Tim Northover5a1558e2014-11-07 22:30:50 +00004875 return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend()
4876 : ABIArgInfo::getDirect();
Douglas Gregora71cc152010-02-02 20:10:50 +00004877 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00004878
4879 // Are we following APCS?
4880 if (getABIKind() == APCS) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00004881 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar626f1d82009-09-13 08:03:58 +00004882 return ABIArgInfo::getIgnore();
4883
Daniel Dunbareedf1512010-02-01 23:31:19 +00004884 // Complex types are all returned as packed integers.
4885 //
4886 // FIXME: Consider using 2 x vector types if the back end handles them
4887 // correctly.
4888 if (RetTy->isAnyComplexType())
Oliver Stannard2bfdc5b2014-08-27 10:43:15 +00004889 return ABIArgInfo::getDirect(llvm::IntegerType::get(
4890 getVMContext(), getContext().getTypeSize(RetTy)));
Daniel Dunbareedf1512010-02-01 23:31:19 +00004891
Daniel Dunbar626f1d82009-09-13 08:03:58 +00004892 // Integer like structures are returned in r0.
Chris Lattner458b2aa2010-07-29 02:16:43 +00004893 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00004894 // Return in the smallest viable integer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +00004895 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00004896 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00004897 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00004898 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00004899 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
4900 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00004901 }
4902
4903 // Otherwise return in memory.
Oliver Stannard405bded2014-02-11 09:25:50 +00004904 markAllocatedGPRs(1, 1);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00004905 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00004906 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00004907
4908 // Otherwise this is an AAPCS variant.
4909
Chris Lattner458b2aa2010-07-29 02:16:43 +00004910 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar1ce72512009-09-14 00:56:55 +00004911 return ABIArgInfo::getIgnore();
4912
Bob Wilson1d9269a2011-11-02 04:51:36 +00004913 // Check for homogeneous aggregates with AAPCS-VFP.
Tim Northover5a1558e2014-11-07 22:30:50 +00004914 if (IsEffectivelyAAPCS_VFP) {
Craig Topper8a13c412014-05-21 05:09:00 +00004915 const Type *Base = nullptr;
Reid Klecknere9f6a712014-10-31 17:10:41 +00004916 uint64_t Members;
4917 if (isHomogeneousAggregate(RetTy, Base, Members)) {
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00004918 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson1d9269a2011-11-02 04:51:36 +00004919 // Homogeneous Aggregates are returned directly.
Tim Northover5a1558e2014-11-07 22:30:50 +00004920 return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00004921 }
Bob Wilson1d9269a2011-11-02 04:51:36 +00004922 }
4923
Daniel Dunbar626f1d82009-09-13 08:03:58 +00004924 // Aggregates <= 4 bytes are returned in r0; other aggregates
4925 // are returned indirectly.
Chris Lattner458b2aa2010-07-29 02:16:43 +00004926 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00004927 if (Size <= 32) {
Christian Pirkerc3d32172014-07-03 09:28:12 +00004928 if (getDataLayout().isBigEndian())
4929 // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4)
Tim Northover5a1558e2014-11-07 22:30:50 +00004930 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Christian Pirkerc3d32172014-07-03 09:28:12 +00004931
Daniel Dunbar1ce72512009-09-14 00:56:55 +00004932 // Return in the smallest viable integer type.
4933 if (Size <= 8)
Tim Northover5a1558e2014-11-07 22:30:50 +00004934 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00004935 if (Size <= 16)
Tim Northover5a1558e2014-11-07 22:30:50 +00004936 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
4937 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00004938 }
4939
Oliver Stannard405bded2014-02-11 09:25:50 +00004940 markAllocatedGPRs(1, 1);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00004941 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00004942}
4943
Manman Renfef9e312012-10-16 19:18:39 +00004944/// isIllegalVector - check whether Ty is an illegal vector type.
4945bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
4946 if (const VectorType *VT = Ty->getAs<VectorType>()) {
4947 // Check whether VT is legal.
4948 unsigned NumElements = VT->getNumElements();
4949 uint64_t Size = getContext().getTypeSize(VT);
4950 // NumElements should be power of 2.
4951 if ((NumElements & (NumElements - 1)) != 0)
4952 return true;
4953 // Size should be greater than 32 bits.
4954 return Size <= 32;
4955 }
4956 return false;
4957}
4958
Reid Klecknere9f6a712014-10-31 17:10:41 +00004959bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
4960 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
4961 // double, or 64-bit or 128-bit vectors.
4962 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
4963 if (BT->getKind() == BuiltinType::Float ||
4964 BT->getKind() == BuiltinType::Double ||
4965 BT->getKind() == BuiltinType::LongDouble)
4966 return true;
4967 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
4968 unsigned VecSize = getContext().getTypeSize(VT);
4969 if (VecSize == 64 || VecSize == 128)
4970 return true;
4971 }
4972 return false;
4973}
4974
4975bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
4976 uint64_t Members) const {
4977 return Members <= 4;
4978}
4979
Anton Korobeynikov244360d2009-06-05 22:08:42 +00004980llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00004981 CodeGenFunction &CGF) const {
Chris Lattnerece04092012-02-07 00:39:47 +00004982 llvm::Type *BP = CGF.Int8PtrTy;
4983 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00004984
4985 CGBuilderTy &Builder = CGF.Builder;
Chris Lattnerece04092012-02-07 00:39:47 +00004986 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00004987 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Manman Rencca54d02012-10-16 19:01:37 +00004988
Tim Northover1711cc92013-06-21 23:05:33 +00004989 if (isEmptyRecord(getContext(), Ty, true)) {
4990 // These are ignored for parameter passing purposes.
4991 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
4992 return Builder.CreateBitCast(Addr, PTy);
4993 }
4994
Manman Rencca54d02012-10-16 19:01:37 +00004995 uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8;
Rafael Espindola11d994b2011-08-02 22:33:37 +00004996 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
Manman Renfef9e312012-10-16 19:18:39 +00004997 bool IsIndirect = false;
Manman Rencca54d02012-10-16 19:01:37 +00004998
4999 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
5000 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
Manman Ren67effb92012-10-16 19:51:48 +00005001 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
5002 getABIKind() == ARMABIInfo::AAPCS)
5003 TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
5004 else
5005 TyAlign = 4;
Manman Renfef9e312012-10-16 19:18:39 +00005006 // Use indirect if size of the illegal vector is bigger than 16 bytes.
5007 if (isIllegalVectorType(Ty) && Size > 16) {
5008 IsIndirect = true;
5009 Size = 4;
5010 TyAlign = 4;
5011 }
Manman Rencca54d02012-10-16 19:01:37 +00005012
5013 // Handle address alignment for ABI alignment > 4 bytes.
Rafael Espindola11d994b2011-08-02 22:33:37 +00005014 if (TyAlign > 4) {
5015 assert((TyAlign & (TyAlign - 1)) == 0 &&
5016 "Alignment is not power of 2!");
5017 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
5018 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
5019 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
Manman Rencca54d02012-10-16 19:01:37 +00005020 Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align");
Rafael Espindola11d994b2011-08-02 22:33:37 +00005021 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005022
5023 uint64_t Offset =
Manman Rencca54d02012-10-16 19:01:37 +00005024 llvm::RoundUpToAlignment(Size, 4);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005025 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +00005026 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005027 "ap.next");
5028 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
5029
Manman Renfef9e312012-10-16 19:18:39 +00005030 if (IsIndirect)
5031 Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP));
Manman Ren67effb92012-10-16 19:51:48 +00005032 else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) {
Manman Rencca54d02012-10-16 19:01:37 +00005033 // We can't directly cast ap.cur to pointer to a vector type, since ap.cur
5034 // may not be correctly aligned for the vector type. We create an aligned
5035 // temporary space and copy the content over from ap.cur to the temporary
5036 // space. This is necessary if the natural alignment of the type is greater
5037 // than the ABI alignment.
5038 llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
5039 CharUnits CharSize = getContext().getTypeSizeInChars(Ty);
5040 llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty),
5041 "var.align");
5042 llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
5043 llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy);
5044 Builder.CreateMemCpy(Dst, Src,
5045 llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()),
5046 TyAlign, false);
5047 Addr = AlignedTemp; //The content is in aligned location.
5048 }
5049 llvm::Type *PTy =
5050 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
5051 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
5052
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005053 return AddrTyped;
5054}
5055
Benjamin Kramer1cdb23d2012-10-20 13:02:06 +00005056namespace {
5057
Derek Schuffa2020962012-10-16 22:30:41 +00005058class NaClARMABIInfo : public ABIInfo {
5059 public:
5060 NaClARMABIInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
5061 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, Kind) {}
Craig Topper4f12f102014-03-12 06:41:41 +00005062 void computeInfo(CGFunctionInfo &FI) const override;
5063 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5064 CodeGenFunction &CGF) const override;
Derek Schuffa2020962012-10-16 22:30:41 +00005065 private:
5066 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
5067 ARMABIInfo NInfo; // Used for everything else.
5068};
5069
5070class NaClARMTargetCodeGenInfo : public TargetCodeGenInfo {
5071 public:
5072 NaClARMTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
5073 : TargetCodeGenInfo(new NaClARMABIInfo(CGT, Kind)) {}
5074};
5075
Benjamin Kramer1cdb23d2012-10-20 13:02:06 +00005076}
5077
Derek Schuffa2020962012-10-16 22:30:41 +00005078void NaClARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
5079 if (FI.getASTCallingConvention() == CC_PnaclCall)
5080 PInfo.computeInfo(FI);
5081 else
5082 static_cast<const ABIInfo&>(NInfo).computeInfo(FI);
5083}
5084
5085llvm::Value *NaClARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5086 CodeGenFunction &CGF) const {
5087 // Always use the native convention; calling pnacl-style varargs functions
5088 // is unsupported.
5089 return static_cast<const ABIInfo&>(NInfo).EmitVAArg(VAListAddr, Ty, CGF);
5090}
5091
Chris Lattner0cf24192010-06-28 20:05:43 +00005092//===----------------------------------------------------------------------===//
Justin Holewinski83e96682012-05-24 17:43:12 +00005093// NVPTX ABI Implementation
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00005094//===----------------------------------------------------------------------===//
5095
5096namespace {
5097
Justin Holewinski83e96682012-05-24 17:43:12 +00005098class NVPTXABIInfo : public ABIInfo {
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00005099public:
Justin Holewinski36837432013-03-30 14:38:24 +00005100 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00005101
5102 ABIArgInfo classifyReturnType(QualType RetTy) const;
5103 ABIArgInfo classifyArgumentType(QualType Ty) const;
5104
Craig Topper4f12f102014-03-12 06:41:41 +00005105 void computeInfo(CGFunctionInfo &FI) const override;
5106 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5107 CodeGenFunction &CFG) const override;
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00005108};
5109
Justin Holewinski83e96682012-05-24 17:43:12 +00005110class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00005111public:
Justin Holewinski83e96682012-05-24 17:43:12 +00005112 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
5113 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
Craig Topper4f12f102014-03-12 06:41:41 +00005114
5115 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
5116 CodeGen::CodeGenModule &M) const override;
Justin Holewinski36837432013-03-30 14:38:24 +00005117private:
Eli Benderskye06a2c42014-04-15 16:57:05 +00005118 // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the
5119 // resulting MDNode to the nvvm.annotations MDNode.
5120 static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand);
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00005121};
5122
Justin Holewinski83e96682012-05-24 17:43:12 +00005123ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00005124 if (RetTy->isVoidType())
5125 return ABIArgInfo::getIgnore();
Justin Holewinskif9329ff2013-11-20 20:35:34 +00005126
5127 // note: this is different from default ABI
5128 if (!RetTy->isScalarType())
5129 return ABIArgInfo::getDirect();
5130
5131 // Treat an enum type as its underlying type.
5132 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5133 RetTy = EnumTy->getDecl()->getIntegerType();
5134
5135 return (RetTy->isPromotableIntegerType() ?
5136 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00005137}
5138
Justin Holewinski83e96682012-05-24 17:43:12 +00005139ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
Justin Holewinskif9329ff2013-11-20 20:35:34 +00005140 // Treat an enum type as its underlying type.
5141 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5142 Ty = EnumTy->getDecl()->getIntegerType();
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00005143
Eli Bendersky95338a02014-10-29 13:43:21 +00005144 // Return aggregates type as indirect by value
5145 if (isAggregateTypeForABI(Ty))
5146 return ABIArgInfo::getIndirect(0, /* byval */ true);
5147
Justin Holewinskif9329ff2013-11-20 20:35:34 +00005148 return (Ty->isPromotableIntegerType() ?
5149 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00005150}
5151
Justin Holewinski83e96682012-05-24 17:43:12 +00005152void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
Reid Kleckner40ca9132014-05-13 22:05:45 +00005153 if (!getCXXABI().classifyReturnType(FI))
5154 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Aaron Ballmanec47bc22014-03-17 18:10:01 +00005155 for (auto &I : FI.arguments())
5156 I.info = classifyArgumentType(I.type);
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00005157
5158 // Always honor user-specified calling convention.
5159 if (FI.getCallingConvention() != llvm::CallingConv::C)
5160 return;
5161
John McCall882987f2013-02-28 19:01:20 +00005162 FI.setEffectiveCallingConvention(getRuntimeCC());
5163}
5164
Justin Holewinski83e96682012-05-24 17:43:12 +00005165llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5166 CodeGenFunction &CFG) const {
5167 llvm_unreachable("NVPTX does not support varargs");
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00005168}
5169
Justin Holewinski83e96682012-05-24 17:43:12 +00005170void NVPTXTargetCodeGenInfo::
5171SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
5172 CodeGen::CodeGenModule &M) const{
Justin Holewinski38031972011-10-05 17:58:44 +00005173 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
5174 if (!FD) return;
5175
5176 llvm::Function *F = cast<llvm::Function>(GV);
5177
5178 // Perform special handling in OpenCL mode
David Blaikiebbafb8a2012-03-11 07:00:24 +00005179 if (M.getLangOpts().OpenCL) {
Justin Holewinski36837432013-03-30 14:38:24 +00005180 // Use OpenCL function attributes to check for kernel functions
Justin Holewinski38031972011-10-05 17:58:44 +00005181 // By default, all functions are device functions
Justin Holewinski38031972011-10-05 17:58:44 +00005182 if (FD->hasAttr<OpenCLKernelAttr>()) {
Justin Holewinski36837432013-03-30 14:38:24 +00005183 // OpenCL __kernel functions get kernel metadata
Eli Benderskye06a2c42014-04-15 16:57:05 +00005184 // Create !{<func-ref>, metadata !"kernel", i32 1} node
5185 addNVVMMetadata(F, "kernel", 1);
Justin Holewinski38031972011-10-05 17:58:44 +00005186 // And kernel functions are not subject to inlining
Bill Wendling207f0532012-12-20 19:27:06 +00005187 F->addFnAttr(llvm::Attribute::NoInline);
Justin Holewinski38031972011-10-05 17:58:44 +00005188 }
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00005189 }
Justin Holewinski38031972011-10-05 17:58:44 +00005190
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00005191 // Perform special handling in CUDA mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00005192 if (M.getLangOpts().CUDA) {
Justin Holewinski36837432013-03-30 14:38:24 +00005193 // CUDA __global__ functions get a kernel metadata entry. Since
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00005194 // __global__ functions cannot be called from the device, we do not
5195 // need to set the noinline attribute.
Eli Benderskye06a2c42014-04-15 16:57:05 +00005196 if (FD->hasAttr<CUDAGlobalAttr>()) {
5197 // Create !{<func-ref>, metadata !"kernel", i32 1} node
5198 addNVVMMetadata(F, "kernel", 1);
5199 }
5200 if (FD->hasAttr<CUDALaunchBoundsAttr>()) {
5201 // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node
5202 addNVVMMetadata(F, "maxntidx",
5203 FD->getAttr<CUDALaunchBoundsAttr>()->getMaxThreads());
5204 // min blocks is a default argument for CUDALaunchBoundsAttr, so getting a
5205 // zero value from getMinBlocks either means it was not specified in
5206 // __launch_bounds__ or the user specified a 0 value. In both cases, we
5207 // don't have to add a PTX directive.
5208 int MinCTASM = FD->getAttr<CUDALaunchBoundsAttr>()->getMinBlocks();
5209 if (MinCTASM > 0) {
5210 // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node
5211 addNVVMMetadata(F, "minctasm", MinCTASM);
5212 }
5213 }
Justin Holewinski38031972011-10-05 17:58:44 +00005214 }
5215}
5216
Eli Benderskye06a2c42014-04-15 16:57:05 +00005217void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name,
5218 int Operand) {
Justin Holewinski36837432013-03-30 14:38:24 +00005219 llvm::Module *M = F->getParent();
5220 llvm::LLVMContext &Ctx = M->getContext();
5221
5222 // Get "nvvm.annotations" metadata node
5223 llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
5224
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00005225 llvm::Metadata *MDVals[] = {
5226 llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name),
5227 llvm::ConstantAsMetadata::get(
5228 llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))};
Justin Holewinski36837432013-03-30 14:38:24 +00005229 // Append metadata to nvvm.annotations
5230 MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
5231}
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00005232}
5233
5234//===----------------------------------------------------------------------===//
Ulrich Weigand47445072013-05-06 16:26:41 +00005235// SystemZ ABI Implementation
5236//===----------------------------------------------------------------------===//
5237
5238namespace {
5239
5240class SystemZABIInfo : public ABIInfo {
5241public:
5242 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
5243
5244 bool isPromotableIntegerType(QualType Ty) const;
5245 bool isCompoundType(QualType Ty) const;
5246 bool isFPArgumentType(QualType Ty) const;
5247
5248 ABIArgInfo classifyReturnType(QualType RetTy) const;
5249 ABIArgInfo classifyArgumentType(QualType ArgTy) const;
5250
Craig Topper4f12f102014-03-12 06:41:41 +00005251 void computeInfo(CGFunctionInfo &FI) const override {
Reid Kleckner40ca9132014-05-13 22:05:45 +00005252 if (!getCXXABI().classifyReturnType(FI))
5253 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Aaron Ballmanec47bc22014-03-17 18:10:01 +00005254 for (auto &I : FI.arguments())
5255 I.info = classifyArgumentType(I.type);
Ulrich Weigand47445072013-05-06 16:26:41 +00005256 }
5257
Craig Topper4f12f102014-03-12 06:41:41 +00005258 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5259 CodeGenFunction &CGF) const override;
Ulrich Weigand47445072013-05-06 16:26:41 +00005260};
5261
5262class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
5263public:
5264 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
5265 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
5266};
5267
5268}
5269
5270bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
5271 // Treat an enum type as its underlying type.
5272 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5273 Ty = EnumTy->getDecl()->getIntegerType();
5274
5275 // Promotable integer types are required to be promoted by the ABI.
5276 if (Ty->isPromotableIntegerType())
5277 return true;
5278
5279 // 32-bit values must also be promoted.
5280 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
5281 switch (BT->getKind()) {
5282 case BuiltinType::Int:
5283 case BuiltinType::UInt:
5284 return true;
5285 default:
5286 return false;
5287 }
5288 return false;
5289}
5290
5291bool SystemZABIInfo::isCompoundType(QualType Ty) const {
5292 return Ty->isAnyComplexType() || isAggregateTypeForABI(Ty);
5293}
5294
5295bool SystemZABIInfo::isFPArgumentType(QualType Ty) const {
5296 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
5297 switch (BT->getKind()) {
5298 case BuiltinType::Float:
5299 case BuiltinType::Double:
5300 return true;
5301 default:
5302 return false;
5303 }
5304
5305 if (const RecordType *RT = Ty->getAsStructureType()) {
5306 const RecordDecl *RD = RT->getDecl();
5307 bool Found = false;
5308
5309 // If this is a C++ record, check the bases first.
5310 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Aaron Ballman574705e2014-03-13 15:41:46 +00005311 for (const auto &I : CXXRD->bases()) {
5312 QualType Base = I.getType();
Ulrich Weigand47445072013-05-06 16:26:41 +00005313
5314 // Empty bases don't affect things either way.
5315 if (isEmptyRecord(getContext(), Base, true))
5316 continue;
5317
5318 if (Found)
5319 return false;
5320 Found = isFPArgumentType(Base);
5321 if (!Found)
5322 return false;
5323 }
5324
5325 // Check the fields.
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00005326 for (const auto *FD : RD->fields()) {
Ulrich Weigand47445072013-05-06 16:26:41 +00005327 // Empty bitfields don't affect things either way.
5328 // Unlike isSingleElementStruct(), empty structure and array fields
5329 // do count. So do anonymous bitfields that aren't zero-sized.
5330 if (FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
5331 return true;
5332
5333 // Unlike isSingleElementStruct(), arrays do not count.
5334 // Nested isFPArgumentType structures still do though.
5335 if (Found)
5336 return false;
5337 Found = isFPArgumentType(FD->getType());
5338 if (!Found)
5339 return false;
5340 }
5341
5342 // Unlike isSingleElementStruct(), trailing padding is allowed.
5343 // An 8-byte aligned struct s { float f; } is passed as a double.
5344 return Found;
5345 }
5346
5347 return false;
5348}
5349
5350llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5351 CodeGenFunction &CGF) const {
5352 // Assume that va_list type is correct; should be pointer to LLVM type:
5353 // struct {
5354 // i64 __gpr;
5355 // i64 __fpr;
5356 // i8 *__overflow_arg_area;
5357 // i8 *__reg_save_area;
5358 // };
5359
5360 // Every argument occupies 8 bytes and is passed by preference in either
5361 // GPRs or FPRs.
5362 Ty = CGF.getContext().getCanonicalType(Ty);
5363 ABIArgInfo AI = classifyArgumentType(Ty);
5364 bool InFPRs = isFPArgumentType(Ty);
5365
5366 llvm::Type *APTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
5367 bool IsIndirect = AI.isIndirect();
5368 unsigned UnpaddedBitSize;
5369 if (IsIndirect) {
5370 APTy = llvm::PointerType::getUnqual(APTy);
5371 UnpaddedBitSize = 64;
5372 } else
5373 UnpaddedBitSize = getContext().getTypeSize(Ty);
5374 unsigned PaddedBitSize = 64;
5375 assert((UnpaddedBitSize <= PaddedBitSize) && "Invalid argument size.");
5376
5377 unsigned PaddedSize = PaddedBitSize / 8;
5378 unsigned Padding = (PaddedBitSize - UnpaddedBitSize) / 8;
5379
5380 unsigned MaxRegs, RegCountField, RegSaveIndex, RegPadding;
5381 if (InFPRs) {
5382 MaxRegs = 4; // Maximum of 4 FPR arguments
5383 RegCountField = 1; // __fpr
5384 RegSaveIndex = 16; // save offset for f0
5385 RegPadding = 0; // floats are passed in the high bits of an FPR
5386 } else {
5387 MaxRegs = 5; // Maximum of 5 GPR arguments
5388 RegCountField = 0; // __gpr
5389 RegSaveIndex = 2; // save offset for r2
5390 RegPadding = Padding; // values are passed in the low bits of a GPR
5391 }
5392
5393 llvm::Value *RegCountPtr =
5394 CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr");
5395 llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count");
5396 llvm::Type *IndexTy = RegCount->getType();
5397 llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs);
5398 llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV,
Oliver Stannard405bded2014-02-11 09:25:50 +00005399 "fits_in_regs");
Ulrich Weigand47445072013-05-06 16:26:41 +00005400
5401 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
5402 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
5403 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
5404 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
5405
5406 // Emit code to load the value if it was passed in registers.
5407 CGF.EmitBlock(InRegBlock);
5408
5409 // Work out the address of an argument register.
5410 llvm::Value *PaddedSizeV = llvm::ConstantInt::get(IndexTy, PaddedSize);
5411 llvm::Value *ScaledRegCount =
5412 CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count");
5413 llvm::Value *RegBase =
5414 llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize + RegPadding);
5415 llvm::Value *RegOffset =
5416 CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset");
5417 llvm::Value *RegSaveAreaPtr =
5418 CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr");
5419 llvm::Value *RegSaveArea =
5420 CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area");
5421 llvm::Value *RawRegAddr =
5422 CGF.Builder.CreateGEP(RegSaveArea, RegOffset, "raw_reg_addr");
5423 llvm::Value *RegAddr =
5424 CGF.Builder.CreateBitCast(RawRegAddr, APTy, "reg_addr");
5425
5426 // Update the register count
5427 llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1);
5428 llvm::Value *NewRegCount =
5429 CGF.Builder.CreateAdd(RegCount, One, "reg_count");
5430 CGF.Builder.CreateStore(NewRegCount, RegCountPtr);
5431 CGF.EmitBranch(ContBlock);
5432
5433 // Emit code to load the value if it was passed in memory.
5434 CGF.EmitBlock(InMemBlock);
5435
5436 // Work out the address of a stack argument.
5437 llvm::Value *OverflowArgAreaPtr =
5438 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr");
5439 llvm::Value *OverflowArgArea =
5440 CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area");
5441 llvm::Value *PaddingV = llvm::ConstantInt::get(IndexTy, Padding);
5442 llvm::Value *RawMemAddr =
5443 CGF.Builder.CreateGEP(OverflowArgArea, PaddingV, "raw_mem_addr");
5444 llvm::Value *MemAddr =
5445 CGF.Builder.CreateBitCast(RawMemAddr, APTy, "mem_addr");
5446
5447 // Update overflow_arg_area_ptr pointer
5448 llvm::Value *NewOverflowArgArea =
5449 CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area");
5450 CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
5451 CGF.EmitBranch(ContBlock);
5452
5453 // Return the appropriate result.
5454 CGF.EmitBlock(ContBlock);
5455 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(APTy, 2, "va_arg.addr");
5456 ResAddr->addIncoming(RegAddr, InRegBlock);
5457 ResAddr->addIncoming(MemAddr, InMemBlock);
5458
5459 if (IsIndirect)
5460 return CGF.Builder.CreateLoad(ResAddr, "indirect_arg");
5461
5462 return ResAddr;
5463}
5464
Ulrich Weigand47445072013-05-06 16:26:41 +00005465ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
5466 if (RetTy->isVoidType())
5467 return ABIArgInfo::getIgnore();
5468 if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64)
5469 return ABIArgInfo::getIndirect(0);
5470 return (isPromotableIntegerType(RetTy) ?
5471 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
5472}
5473
5474ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
5475 // Handle the generic C++ ABI.
Mark Lacey3825e832013-10-06 01:33:34 +00005476 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Ulrich Weigand47445072013-05-06 16:26:41 +00005477 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
5478
5479 // Integers and enums are extended to full register width.
5480 if (isPromotableIntegerType(Ty))
5481 return ABIArgInfo::getExtend();
5482
5483 // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly.
5484 uint64_t Size = getContext().getTypeSize(Ty);
5485 if (Size != 8 && Size != 16 && Size != 32 && Size != 64)
Richard Sandifordcdd86882013-12-04 09:59:57 +00005486 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Ulrich Weigand47445072013-05-06 16:26:41 +00005487
5488 // Handle small structures.
5489 if (const RecordType *RT = Ty->getAs<RecordType>()) {
5490 // Structures with flexible arrays have variable length, so really
5491 // fail the size test above.
5492 const RecordDecl *RD = RT->getDecl();
5493 if (RD->hasFlexibleArrayMember())
Richard Sandifordcdd86882013-12-04 09:59:57 +00005494 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Ulrich Weigand47445072013-05-06 16:26:41 +00005495
5496 // The structure is passed as an unextended integer, a float, or a double.
5497 llvm::Type *PassTy;
5498 if (isFPArgumentType(Ty)) {
5499 assert(Size == 32 || Size == 64);
5500 if (Size == 32)
5501 PassTy = llvm::Type::getFloatTy(getVMContext());
5502 else
5503 PassTy = llvm::Type::getDoubleTy(getVMContext());
5504 } else
5505 PassTy = llvm::IntegerType::get(getVMContext(), Size);
5506 return ABIArgInfo::getDirect(PassTy);
5507 }
5508
5509 // Non-structure compounds are passed indirectly.
5510 if (isCompoundType(Ty))
Richard Sandifordcdd86882013-12-04 09:59:57 +00005511 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Ulrich Weigand47445072013-05-06 16:26:41 +00005512
Craig Topper8a13c412014-05-21 05:09:00 +00005513 return ABIArgInfo::getDirect(nullptr);
Ulrich Weigand47445072013-05-06 16:26:41 +00005514}
5515
5516//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00005517// MSP430 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00005518//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00005519
5520namespace {
5521
5522class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
5523public:
Chris Lattner2b037972010-07-29 02:01:43 +00005524 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
5525 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00005526 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Craig Topper4f12f102014-03-12 06:41:41 +00005527 CodeGen::CodeGenModule &M) const override;
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00005528};
5529
5530}
5531
5532void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
5533 llvm::GlobalValue *GV,
5534 CodeGen::CodeGenModule &M) const {
5535 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5536 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
5537 // Handle 'interrupt' attribute:
5538 llvm::Function *F = cast<llvm::Function>(GV);
5539
5540 // Step 1: Set ISR calling convention.
5541 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
5542
5543 // Step 2: Add attributes goodness.
Bill Wendling207f0532012-12-20 19:27:06 +00005544 F->addFnAttr(llvm::Attribute::NoInline);
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00005545
5546 // Step 3: Emit ISR vector alias.
Anton Korobeynikovc5a7f922012-11-26 18:59:10 +00005547 unsigned Num = attr->getNumber() / 2;
Rafael Espindola234405b2014-05-17 21:30:14 +00005548 llvm::GlobalAlias::create(llvm::Function::ExternalLinkage,
5549 "__isr_" + Twine(Num), F);
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00005550 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005551 }
5552}
5553
Chris Lattner0cf24192010-06-28 20:05:43 +00005554//===----------------------------------------------------------------------===//
John McCall943fae92010-05-27 06:19:26 +00005555// MIPS ABI Implementation. This works for both little-endian and
5556// big-endian variants.
Chris Lattner0cf24192010-06-28 20:05:43 +00005557//===----------------------------------------------------------------------===//
5558
John McCall943fae92010-05-27 06:19:26 +00005559namespace {
Akira Hatanakab579fe52011-06-02 00:09:17 +00005560class MipsABIInfo : public ABIInfo {
Akira Hatanaka14378522011-11-02 23:14:57 +00005561 bool IsO32;
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00005562 unsigned MinABIStackAlignInBytes, StackAlignInBytes;
5563 void CoerceToIntArgs(uint64_t TySize,
Craig Topper5603df42013-07-05 19:34:19 +00005564 SmallVectorImpl<llvm::Type *> &ArgList) const;
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00005565 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00005566 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanaka1632af62012-01-09 19:31:25 +00005567 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanakab579fe52011-06-02 00:09:17 +00005568public:
Akira Hatanakac4baedd2013-11-11 22:10:46 +00005569 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00005570 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
Akira Hatanakac4baedd2013-11-11 22:10:46 +00005571 StackAlignInBytes(IsO32 ? 8 : 16) {}
Akira Hatanakab579fe52011-06-02 00:09:17 +00005572
5573 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00005574 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Craig Topper4f12f102014-03-12 06:41:41 +00005575 void computeInfo(CGFunctionInfo &FI) const override;
5576 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5577 CodeGenFunction &CGF) const override;
Akira Hatanakab579fe52011-06-02 00:09:17 +00005578};
5579
John McCall943fae92010-05-27 06:19:26 +00005580class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanaka0486db02011-09-20 18:23:28 +00005581 unsigned SizeOfUnwindException;
John McCall943fae92010-05-27 06:19:26 +00005582public:
Akira Hatanakac4baedd2013-11-11 22:10:46 +00005583 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
5584 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
Akira Hatanaka14378522011-11-02 23:14:57 +00005585 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCall943fae92010-05-27 06:19:26 +00005586
Craig Topper4f12f102014-03-12 06:41:41 +00005587 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
John McCall943fae92010-05-27 06:19:26 +00005588 return 29;
5589 }
5590
Reed Kotler373feca2013-01-16 17:10:28 +00005591 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Craig Topper4f12f102014-03-12 06:41:41 +00005592 CodeGen::CodeGenModule &CGM) const override {
Reed Kotler3d5966f2013-03-13 20:40:30 +00005593 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
5594 if (!FD) return;
Rafael Espindolaa0851a22013-03-19 14:32:23 +00005595 llvm::Function *Fn = cast<llvm::Function>(GV);
Reed Kotler3d5966f2013-03-13 20:40:30 +00005596 if (FD->hasAttr<Mips16Attr>()) {
5597 Fn->addFnAttr("mips16");
5598 }
5599 else if (FD->hasAttr<NoMips16Attr>()) {
5600 Fn->addFnAttr("nomips16");
5601 }
Reed Kotler373feca2013-01-16 17:10:28 +00005602 }
Reed Kotler3d5966f2013-03-13 20:40:30 +00005603
John McCall943fae92010-05-27 06:19:26 +00005604 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00005605 llvm::Value *Address) const override;
John McCall3480ef22011-08-30 01:42:09 +00005606
Craig Topper4f12f102014-03-12 06:41:41 +00005607 unsigned getSizeOfUnwindException() const override {
Akira Hatanaka0486db02011-09-20 18:23:28 +00005608 return SizeOfUnwindException;
John McCall3480ef22011-08-30 01:42:09 +00005609 }
John McCall943fae92010-05-27 06:19:26 +00005610};
5611}
5612
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00005613void MipsABIInfo::CoerceToIntArgs(uint64_t TySize,
Craig Topper5603df42013-07-05 19:34:19 +00005614 SmallVectorImpl<llvm::Type *> &ArgList) const {
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00005615 llvm::IntegerType *IntTy =
5616 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00005617
5618 // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
5619 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
5620 ArgList.push_back(IntTy);
5621
5622 // If necessary, add one more integer type to ArgList.
5623 unsigned R = TySize % (MinABIStackAlignInBytes * 8);
5624
5625 if (R)
5626 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00005627}
5628
Akira Hatanaka101f70d2011-11-02 23:54:49 +00005629// In N32/64, an aligned double precision floating point field is passed in
5630// a register.
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00005631llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00005632 SmallVector<llvm::Type*, 8> ArgList, IntArgList;
5633
5634 if (IsO32) {
5635 CoerceToIntArgs(TySize, ArgList);
5636 return llvm::StructType::get(getVMContext(), ArgList);
5637 }
Akira Hatanaka101f70d2011-11-02 23:54:49 +00005638
Akira Hatanaka02e13e52012-01-12 00:52:17 +00005639 if (Ty->isComplexType())
5640 return CGT.ConvertType(Ty);
Akira Hatanaka79f04612012-01-10 23:12:19 +00005641
Akira Hatanaka4984f5d2012-02-09 19:54:16 +00005642 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanaka101f70d2011-11-02 23:54:49 +00005643
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00005644 // Unions/vectors are passed in integer registers.
5645 if (!RT || !RT->isStructureOrClassType()) {
5646 CoerceToIntArgs(TySize, ArgList);
5647 return llvm::StructType::get(getVMContext(), ArgList);
5648 }
Akira Hatanaka101f70d2011-11-02 23:54:49 +00005649
5650 const RecordDecl *RD = RT->getDecl();
5651 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00005652 assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
Akira Hatanaka101f70d2011-11-02 23:54:49 +00005653
Akira Hatanaka101f70d2011-11-02 23:54:49 +00005654 uint64_t LastOffset = 0;
5655 unsigned idx = 0;
5656 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
5657
Akira Hatanaka4984f5d2012-02-09 19:54:16 +00005658 // Iterate over fields in the struct/class and check if there are any aligned
5659 // double fields.
Akira Hatanaka101f70d2011-11-02 23:54:49 +00005660 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
5661 i != e; ++i, ++idx) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00005662 const QualType Ty = i->getType();
Akira Hatanaka101f70d2011-11-02 23:54:49 +00005663 const BuiltinType *BT = Ty->getAs<BuiltinType>();
5664
5665 if (!BT || BT->getKind() != BuiltinType::Double)
5666 continue;
5667
5668 uint64_t Offset = Layout.getFieldOffset(idx);
5669 if (Offset % 64) // Ignore doubles that are not aligned.
5670 continue;
5671
5672 // Add ((Offset - LastOffset) / 64) args of type i64.
5673 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
5674 ArgList.push_back(I64);
5675
5676 // Add double type.
5677 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
5678 LastOffset = Offset + 64;
5679 }
5680
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00005681 CoerceToIntArgs(TySize - LastOffset, IntArgList);
5682 ArgList.append(IntArgList.begin(), IntArgList.end());
Akira Hatanaka101f70d2011-11-02 23:54:49 +00005683
5684 return llvm::StructType::get(getVMContext(), ArgList);
5685}
5686
Akira Hatanakaddd66342013-10-29 18:41:15 +00005687llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset,
5688 uint64_t Offset) const {
5689 if (OrigOffset + MinABIStackAlignInBytes > Offset)
Craig Topper8a13c412014-05-21 05:09:00 +00005690 return nullptr;
Akira Hatanaka1632af62012-01-09 19:31:25 +00005691
Akira Hatanakaddd66342013-10-29 18:41:15 +00005692 return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8);
Akira Hatanaka1632af62012-01-09 19:31:25 +00005693}
Akira Hatanaka21ee88c2012-01-10 22:44:52 +00005694
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00005695ABIArgInfo
5696MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Akira Hatanaka1632af62012-01-09 19:31:25 +00005697 uint64_t OrigOffset = Offset;
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00005698 uint64_t TySize = getContext().getTypeSize(Ty);
Akira Hatanaka1632af62012-01-09 19:31:25 +00005699 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00005700
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00005701 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
5702 (uint64_t)StackAlignInBytes);
Akira Hatanakaddd66342013-10-29 18:41:15 +00005703 unsigned CurrOffset = llvm::RoundUpToAlignment(Offset, Align);
5704 Offset = CurrOffset + llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
Akira Hatanaka1632af62012-01-09 19:31:25 +00005705
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00005706 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
Akira Hatanakab579fe52011-06-02 00:09:17 +00005707 // Ignore empty aggregates.
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00005708 if (TySize == 0)
Akira Hatanakab579fe52011-06-02 00:09:17 +00005709 return ABIArgInfo::getIgnore();
5710
Mark Lacey3825e832013-10-06 01:33:34 +00005711 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00005712 Offset = OrigOffset + MinABIStackAlignInBytes;
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00005713 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00005714 }
Akira Hatanakadf425db2011-08-01 18:09:58 +00005715
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00005716 // If we have reached here, aggregates are passed directly by coercing to
5717 // another structure type. Padding is inserted if the offset of the
5718 // aggregate is unaligned.
Daniel Sandersaa1b3552014-10-24 15:30:16 +00005719 ABIArgInfo ArgInfo =
5720 ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
5721 getPaddingType(OrigOffset, CurrOffset));
5722 ArgInfo.setInReg(true);
5723 return ArgInfo;
Akira Hatanakab579fe52011-06-02 00:09:17 +00005724 }
5725
5726 // Treat an enum type as its underlying type.
5727 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5728 Ty = EnumTy->getDecl()->getIntegerType();
5729
Daniel Sanders5b445b32014-10-24 14:42:42 +00005730 // All integral types are promoted to the GPR width.
5731 if (Ty->isIntegralOrEnumerationType())
Akira Hatanaka1632af62012-01-09 19:31:25 +00005732 return ABIArgInfo::getExtend();
5733
Akira Hatanakaddd66342013-10-29 18:41:15 +00005734 return ABIArgInfo::getDirect(
Craig Topper8a13c412014-05-21 05:09:00 +00005735 nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset));
Akira Hatanakab579fe52011-06-02 00:09:17 +00005736}
5737
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00005738llvm::Type*
5739MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakab6f74432012-02-09 18:49:26 +00005740 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00005741 SmallVector<llvm::Type*, 8> RTList;
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00005742
Akira Hatanakab6f74432012-02-09 18:49:26 +00005743 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00005744 const RecordDecl *RD = RT->getDecl();
Akira Hatanakab6f74432012-02-09 18:49:26 +00005745 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
5746 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00005747
Akira Hatanakab6f74432012-02-09 18:49:26 +00005748 // N32/64 returns struct/classes in floating point registers if the
5749 // following conditions are met:
5750 // 1. The size of the struct/class is no larger than 128-bit.
5751 // 2. The struct/class has one or two fields all of which are floating
5752 // point types.
5753 // 3. The offset of the first field is zero (this follows what gcc does).
5754 //
5755 // Any other composite results are returned in integer registers.
5756 //
5757 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
5758 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
5759 for (; b != e; ++b) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00005760 const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00005761
Akira Hatanakab6f74432012-02-09 18:49:26 +00005762 if (!BT || !BT->isFloatingPoint())
5763 break;
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00005764
David Blaikie2d7c57e2012-04-30 02:36:29 +00005765 RTList.push_back(CGT.ConvertType(b->getType()));
Akira Hatanakab6f74432012-02-09 18:49:26 +00005766 }
5767
5768 if (b == e)
5769 return llvm::StructType::get(getVMContext(), RTList,
5770 RD->hasAttr<PackedAttr>());
5771
5772 RTList.clear();
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00005773 }
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00005774 }
5775
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00005776 CoerceToIntArgs(Size, RTList);
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00005777 return llvm::StructType::get(getVMContext(), RTList);
5778}
5779
Akira Hatanakab579fe52011-06-02 00:09:17 +00005780ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanaka60f5fe62012-01-23 23:18:57 +00005781 uint64_t Size = getContext().getTypeSize(RetTy);
5782
Daniel Sandersed39f582014-09-04 13:28:14 +00005783 if (RetTy->isVoidType())
5784 return ABIArgInfo::getIgnore();
5785
5786 // O32 doesn't treat zero-sized structs differently from other structs.
5787 // However, N32/N64 ignores zero sized return values.
5788 if (!IsO32 && Size == 0)
Akira Hatanakab579fe52011-06-02 00:09:17 +00005789 return ABIArgInfo::getIgnore();
5790
Akira Hatanakac37eddf2012-05-11 21:01:17 +00005791 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00005792 if (Size <= 128) {
5793 if (RetTy->isAnyComplexType())
5794 return ABIArgInfo::getDirect();
5795
Daniel Sanderse5018b62014-09-04 15:05:39 +00005796 // O32 returns integer vectors in registers and N32/N64 returns all small
Daniel Sanders00a56ff2014-09-04 15:07:43 +00005797 // aggregates in registers.
Daniel Sanderse5018b62014-09-04 15:05:39 +00005798 if (!IsO32 ||
5799 (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) {
5800 ABIArgInfo ArgInfo =
5801 ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
5802 ArgInfo.setInReg(true);
5803 return ArgInfo;
5804 }
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00005805 }
Akira Hatanakab579fe52011-06-02 00:09:17 +00005806
5807 return ABIArgInfo::getIndirect(0);
5808 }
5809
5810 // Treat an enum type as its underlying type.
5811 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5812 RetTy = EnumTy->getDecl()->getIntegerType();
5813
5814 return (RetTy->isPromotableIntegerType() ?
5815 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
5816}
5817
5818void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanaka32604a92012-01-12 01:10:09 +00005819 ABIArgInfo &RetInfo = FI.getReturnInfo();
Reid Kleckner40ca9132014-05-13 22:05:45 +00005820 if (!getCXXABI().classifyReturnType(FI))
5821 RetInfo = classifyReturnType(FI.getReturnType());
Akira Hatanaka32604a92012-01-12 01:10:09 +00005822
5823 // Check if a pointer to an aggregate is passed as a hidden argument.
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00005824 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
Akira Hatanaka32604a92012-01-12 01:10:09 +00005825
Aaron Ballmanec47bc22014-03-17 18:10:01 +00005826 for (auto &I : FI.arguments())
5827 I.info = classifyArgumentType(I.type, Offset);
Akira Hatanakab579fe52011-06-02 00:09:17 +00005828}
5829
5830llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5831 CodeGenFunction &CGF) const {
Daniel Sanders2ef3cdd32014-08-01 13:26:28 +00005832 llvm::Type *BP = CGF.Int8PtrTy;
5833 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Daniel Sanders59229dc2014-11-19 10:01:35 +00005834
5835 // Integer arguments are promoted 32-bit on O32 and 64-bit on N32/N64.
5836 unsigned SlotSizeInBits = IsO32 ? 32 : 64;
5837 if (Ty->isIntegerType() &&
5838 CGF.getContext().getIntWidth(Ty) < SlotSizeInBits) {
5839 Ty = CGF.getContext().getIntTypeForBitwidth(SlotSizeInBits,
5840 Ty->isSignedIntegerType());
5841 }
Daniel Sanders2ef3cdd32014-08-01 13:26:28 +00005842
5843 CGBuilderTy &Builder = CGF.Builder;
5844 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
5845 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Daniel Sanders8d36a612014-09-22 13:27:06 +00005846 int64_t TypeAlign =
5847 std::min(getContext().getTypeAlign(Ty) / 8, StackAlignInBytes);
Daniel Sanders2ef3cdd32014-08-01 13:26:28 +00005848 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
5849 llvm::Value *AddrTyped;
5850 unsigned PtrWidth = getTarget().getPointerWidth(0);
5851 llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
5852
5853 if (TypeAlign > MinABIStackAlignInBytes) {
5854 llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
5855 llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
5856 llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
5857 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
5858 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
5859 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
5860 }
5861 else
5862 AddrTyped = Builder.CreateBitCast(Addr, PTy);
5863
5864 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
5865 TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
Daniel Sanders59229dc2014-11-19 10:01:35 +00005866 unsigned ArgSizeInBits = CGF.getContext().getTypeSize(Ty);
5867 uint64_t Offset = llvm::RoundUpToAlignment(ArgSizeInBits / 8, TypeAlign);
Daniel Sanders2ef3cdd32014-08-01 13:26:28 +00005868 llvm::Value *NextAddr =
5869 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
5870 "ap.next");
5871 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
5872
5873 return AddrTyped;
Akira Hatanakab579fe52011-06-02 00:09:17 +00005874}
5875
John McCall943fae92010-05-27 06:19:26 +00005876bool
5877MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
5878 llvm::Value *Address) const {
5879 // This information comes from gcc's implementation, which seems to
5880 // as canonical as it gets.
5881
John McCall943fae92010-05-27 06:19:26 +00005882 // Everything on MIPS is 4 bytes. Double-precision FP registers
5883 // are aliased to pairs of single-precision FP registers.
Chris Lattnerece04092012-02-07 00:39:47 +00005884 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCall943fae92010-05-27 06:19:26 +00005885
5886 // 0-31 are the general purpose registers, $0 - $31.
5887 // 32-63 are the floating-point registers, $f0 - $f31.
5888 // 64 and 65 are the multiply/divide registers, $hi and $lo.
5889 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattnerece04092012-02-07 00:39:47 +00005890 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCall943fae92010-05-27 06:19:26 +00005891
5892 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
5893 // They are one bit wide and ignored here.
5894
5895 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
5896 // (coprocessor 1 is the FP unit)
5897 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
5898 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
5899 // 176-181 are the DSP accumulator registers.
Chris Lattnerece04092012-02-07 00:39:47 +00005900 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCall943fae92010-05-27 06:19:26 +00005901 return false;
5902}
5903
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00005904//===----------------------------------------------------------------------===//
5905// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
5906// Currently subclassed only to implement custom OpenCL C function attribute
5907// handling.
5908//===----------------------------------------------------------------------===//
5909
5910namespace {
5911
5912class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
5913public:
5914 TCETargetCodeGenInfo(CodeGenTypes &CGT)
5915 : DefaultTargetCodeGenInfo(CGT) {}
5916
Craig Topper4f12f102014-03-12 06:41:41 +00005917 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
5918 CodeGen::CodeGenModule &M) const override;
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00005919};
5920
5921void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
5922 llvm::GlobalValue *GV,
5923 CodeGen::CodeGenModule &M) const {
5924 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
5925 if (!FD) return;
5926
5927 llvm::Function *F = cast<llvm::Function>(GV);
5928
David Blaikiebbafb8a2012-03-11 07:00:24 +00005929 if (M.getLangOpts().OpenCL) {
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00005930 if (FD->hasAttr<OpenCLKernelAttr>()) {
5931 // OpenCL C Kernel functions are not subject to inlining
Bill Wendling207f0532012-12-20 19:27:06 +00005932 F->addFnAttr(llvm::Attribute::NoInline);
Aaron Ballman36a18ff2013-12-19 13:16:35 +00005933 const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>();
5934 if (Attr) {
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00005935 // Convert the reqd_work_group_size() attributes to metadata.
5936 llvm::LLVMContext &Context = F->getContext();
5937 llvm::NamedMDNode *OpenCLMetadata =
5938 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
5939
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00005940 SmallVector<llvm::Metadata *, 5> Operands;
5941 Operands.push_back(llvm::ConstantAsMetadata::get(F));
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00005942
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00005943 Operands.push_back(
5944 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
5945 M.Int32Ty, llvm::APInt(32, Attr->getXDim()))));
5946 Operands.push_back(
5947 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
5948 M.Int32Ty, llvm::APInt(32, Attr->getYDim()))));
5949 Operands.push_back(
5950 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
5951 M.Int32Ty, llvm::APInt(32, Attr->getZDim()))));
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00005952
5953 // Add a boolean constant operand for "required" (true) or "hint" (false)
5954 // for implementing the work_group_size_hint attr later. Currently
5955 // always true as the hint is not yet implemented.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00005956 Operands.push_back(
5957 llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context)));
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00005958 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
5959 }
5960 }
5961 }
5962}
5963
5964}
John McCall943fae92010-05-27 06:19:26 +00005965
Tony Linthicum76329bf2011-12-12 21:14:55 +00005966//===----------------------------------------------------------------------===//
5967// Hexagon ABI Implementation
5968//===----------------------------------------------------------------------===//
5969
5970namespace {
5971
5972class HexagonABIInfo : public ABIInfo {
5973
5974
5975public:
5976 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
5977
5978private:
5979
5980 ABIArgInfo classifyReturnType(QualType RetTy) const;
5981 ABIArgInfo classifyArgumentType(QualType RetTy) const;
5982
Craig Topper4f12f102014-03-12 06:41:41 +00005983 void computeInfo(CGFunctionInfo &FI) const override;
Tony Linthicum76329bf2011-12-12 21:14:55 +00005984
Craig Topper4f12f102014-03-12 06:41:41 +00005985 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5986 CodeGenFunction &CGF) const override;
Tony Linthicum76329bf2011-12-12 21:14:55 +00005987};
5988
5989class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
5990public:
5991 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
5992 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
5993
Craig Topper4f12f102014-03-12 06:41:41 +00005994 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
Tony Linthicum76329bf2011-12-12 21:14:55 +00005995 return 29;
5996 }
5997};
5998
5999}
6000
6001void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
Reid Kleckner40ca9132014-05-13 22:05:45 +00006002 if (!getCXXABI().classifyReturnType(FI))
6003 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Aaron Ballmanec47bc22014-03-17 18:10:01 +00006004 for (auto &I : FI.arguments())
6005 I.info = classifyArgumentType(I.type);
Tony Linthicum76329bf2011-12-12 21:14:55 +00006006}
6007
6008ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
6009 if (!isAggregateTypeForABI(Ty)) {
6010 // Treat an enum type as its underlying type.
6011 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
6012 Ty = EnumTy->getDecl()->getIntegerType();
6013
6014 return (Ty->isPromotableIntegerType() ?
6015 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
6016 }
6017
6018 // Ignore empty records.
6019 if (isEmptyRecord(getContext(), Ty, true))
6020 return ABIArgInfo::getIgnore();
6021
Mark Lacey3825e832013-10-06 01:33:34 +00006022 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00006023 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
Tony Linthicum76329bf2011-12-12 21:14:55 +00006024
6025 uint64_t Size = getContext().getTypeSize(Ty);
6026 if (Size > 64)
6027 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
6028 // Pass in the smallest viable integer type.
6029 else if (Size > 32)
6030 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
6031 else if (Size > 16)
6032 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
6033 else if (Size > 8)
6034 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
6035 else
6036 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
6037}
6038
6039ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
6040 if (RetTy->isVoidType())
6041 return ABIArgInfo::getIgnore();
6042
6043 // Large vector types should be returned via memory.
6044 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
6045 return ABIArgInfo::getIndirect(0);
6046
6047 if (!isAggregateTypeForABI(RetTy)) {
6048 // Treat an enum type as its underlying type.
6049 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
6050 RetTy = EnumTy->getDecl()->getIntegerType();
6051
6052 return (RetTy->isPromotableIntegerType() ?
6053 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
6054 }
6055
Tony Linthicum76329bf2011-12-12 21:14:55 +00006056 if (isEmptyRecord(getContext(), RetTy, true))
6057 return ABIArgInfo::getIgnore();
6058
6059 // Aggregates <= 8 bytes are returned in r0; other aggregates
6060 // are returned indirectly.
6061 uint64_t Size = getContext().getTypeSize(RetTy);
6062 if (Size <= 64) {
6063 // Return in the smallest viable integer type.
6064 if (Size <= 8)
6065 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
6066 if (Size <= 16)
6067 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
6068 if (Size <= 32)
6069 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
6070 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
6071 }
6072
6073 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
6074}
6075
6076llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattnerece04092012-02-07 00:39:47 +00006077 CodeGenFunction &CGF) const {
Tony Linthicum76329bf2011-12-12 21:14:55 +00006078 // FIXME: Need to handle alignment
Chris Lattnerece04092012-02-07 00:39:47 +00006079 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Tony Linthicum76329bf2011-12-12 21:14:55 +00006080
6081 CGBuilderTy &Builder = CGF.Builder;
6082 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
6083 "ap");
6084 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
6085 llvm::Type *PTy =
6086 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
6087 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
6088
6089 uint64_t Offset =
6090 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
6091 llvm::Value *NextAddr =
6092 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
6093 "ap.next");
6094 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
6095
6096 return AddrTyped;
6097}
6098
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006099//===----------------------------------------------------------------------===//
6100// AMDGPU ABI Implementation
6101//===----------------------------------------------------------------------===//
6102
6103namespace {
6104
6105class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo {
6106public:
6107 AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT)
6108 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
6109 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
6110 CodeGen::CodeGenModule &M) const override;
6111};
6112
6113}
6114
6115void AMDGPUTargetCodeGenInfo::SetTargetAttributes(
6116 const Decl *D,
6117 llvm::GlobalValue *GV,
6118 CodeGen::CodeGenModule &M) const {
6119 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
6120 if (!FD)
6121 return;
6122
6123 if (const auto Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) {
6124 llvm::Function *F = cast<llvm::Function>(GV);
6125 uint32_t NumVGPR = Attr->getNumVGPR();
6126 if (NumVGPR != 0)
6127 F->addFnAttr("amdgpu_num_vgpr", llvm::utostr(NumVGPR));
6128 }
6129
6130 if (const auto Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) {
6131 llvm::Function *F = cast<llvm::Function>(GV);
6132 unsigned NumSGPR = Attr->getNumSGPR();
6133 if (NumSGPR != 0)
6134 F->addFnAttr("amdgpu_num_sgpr", llvm::utostr(NumSGPR));
6135 }
6136}
6137
Tony Linthicum76329bf2011-12-12 21:14:55 +00006138
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00006139//===----------------------------------------------------------------------===//
6140// SPARC v9 ABI Implementation.
6141// Based on the SPARC Compliance Definition version 2.4.1.
6142//
6143// Function arguments a mapped to a nominal "parameter array" and promoted to
6144// registers depending on their type. Each argument occupies 8 or 16 bytes in
6145// the array, structs larger than 16 bytes are passed indirectly.
6146//
6147// One case requires special care:
6148//
6149// struct mixed {
6150// int i;
6151// float f;
6152// };
6153//
6154// When a struct mixed is passed by value, it only occupies 8 bytes in the
6155// parameter array, but the int is passed in an integer register, and the float
6156// is passed in a floating point register. This is represented as two arguments
6157// with the LLVM IR inreg attribute:
6158//
6159// declare void f(i32 inreg %i, float inreg %f)
6160//
6161// The code generator will only allocate 4 bytes from the parameter array for
6162// the inreg arguments. All other arguments are allocated a multiple of 8
6163// bytes.
6164//
6165namespace {
6166class SparcV9ABIInfo : public ABIInfo {
6167public:
6168 SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
6169
6170private:
6171 ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const;
Craig Topper4f12f102014-03-12 06:41:41 +00006172 void computeInfo(CGFunctionInfo &FI) const override;
6173 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
6174 CodeGenFunction &CGF) const override;
Jakob Stoklund Olesen02dc6a12013-05-28 04:57:37 +00006175
6176 // Coercion type builder for structs passed in registers. The coercion type
6177 // serves two purposes:
6178 //
6179 // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned'
6180 // in registers.
6181 // 2. Expose aligned floating point elements as first-level elements, so the
6182 // code generator knows to pass them in floating point registers.
6183 //
6184 // We also compute the InReg flag which indicates that the struct contains
6185 // aligned 32-bit floats.
6186 //
6187 struct CoerceBuilder {
6188 llvm::LLVMContext &Context;
6189 const llvm::DataLayout &DL;
6190 SmallVector<llvm::Type*, 8> Elems;
6191 uint64_t Size;
6192 bool InReg;
6193
6194 CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl)
6195 : Context(c), DL(dl), Size(0), InReg(false) {}
6196
6197 // Pad Elems with integers until Size is ToSize.
6198 void pad(uint64_t ToSize) {
6199 assert(ToSize >= Size && "Cannot remove elements");
6200 if (ToSize == Size)
6201 return;
6202
6203 // Finish the current 64-bit word.
6204 uint64_t Aligned = llvm::RoundUpToAlignment(Size, 64);
6205 if (Aligned > Size && Aligned <= ToSize) {
6206 Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size));
6207 Size = Aligned;
6208 }
6209
6210 // Add whole 64-bit words.
6211 while (Size + 64 <= ToSize) {
6212 Elems.push_back(llvm::Type::getInt64Ty(Context));
6213 Size += 64;
6214 }
6215
6216 // Final in-word padding.
6217 if (Size < ToSize) {
6218 Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size));
6219 Size = ToSize;
6220 }
6221 }
6222
6223 // Add a floating point element at Offset.
6224 void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) {
6225 // Unaligned floats are treated as integers.
6226 if (Offset % Bits)
6227 return;
6228 // The InReg flag is only required if there are any floats < 64 bits.
6229 if (Bits < 64)
6230 InReg = true;
6231 pad(Offset);
6232 Elems.push_back(Ty);
6233 Size = Offset + Bits;
6234 }
6235
6236 // Add a struct type to the coercion type, starting at Offset (in bits).
6237 void addStruct(uint64_t Offset, llvm::StructType *StrTy) {
6238 const llvm::StructLayout *Layout = DL.getStructLayout(StrTy);
6239 for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) {
6240 llvm::Type *ElemTy = StrTy->getElementType(i);
6241 uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i);
6242 switch (ElemTy->getTypeID()) {
6243 case llvm::Type::StructTyID:
6244 addStruct(ElemOffset, cast<llvm::StructType>(ElemTy));
6245 break;
6246 case llvm::Type::FloatTyID:
6247 addFloat(ElemOffset, ElemTy, 32);
6248 break;
6249 case llvm::Type::DoubleTyID:
6250 addFloat(ElemOffset, ElemTy, 64);
6251 break;
6252 case llvm::Type::FP128TyID:
6253 addFloat(ElemOffset, ElemTy, 128);
6254 break;
6255 case llvm::Type::PointerTyID:
6256 if (ElemOffset % 64 == 0) {
6257 pad(ElemOffset);
6258 Elems.push_back(ElemTy);
6259 Size += 64;
6260 }
6261 break;
6262 default:
6263 break;
6264 }
6265 }
6266 }
6267
6268 // Check if Ty is a usable substitute for the coercion type.
6269 bool isUsableType(llvm::StructType *Ty) const {
6270 if (Ty->getNumElements() != Elems.size())
6271 return false;
6272 for (unsigned i = 0, e = Elems.size(); i != e; ++i)
6273 if (Elems[i] != Ty->getElementType(i))
6274 return false;
6275 return true;
6276 }
6277
6278 // Get the coercion type as a literal struct type.
6279 llvm::Type *getType() const {
6280 if (Elems.size() == 1)
6281 return Elems.front();
6282 else
6283 return llvm::StructType::get(Context, Elems);
6284 }
6285 };
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00006286};
6287} // end anonymous namespace
6288
6289ABIArgInfo
6290SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const {
6291 if (Ty->isVoidType())
6292 return ABIArgInfo::getIgnore();
6293
6294 uint64_t Size = getContext().getTypeSize(Ty);
6295
6296 // Anything too big to fit in registers is passed with an explicit indirect
6297 // pointer / sret pointer.
6298 if (Size > SizeLimit)
6299 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
6300
6301 // Treat an enum type as its underlying type.
6302 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
6303 Ty = EnumTy->getDecl()->getIntegerType();
6304
6305 // Integer types smaller than a register are extended.
6306 if (Size < 64 && Ty->isIntegerType())
6307 return ABIArgInfo::getExtend();
6308
6309 // Other non-aggregates go in registers.
6310 if (!isAggregateTypeForABI(Ty))
6311 return ABIArgInfo::getDirect();
6312
Jakob Stoklund Olesenb81eb3e2014-01-12 06:54:56 +00006313 // If a C++ object has either a non-trivial copy constructor or a non-trivial
6314 // destructor, it is passed with an explicit indirect pointer / sret pointer.
6315 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
6316 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
6317
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00006318 // This is a small aggregate type that should be passed in registers.
Jakob Stoklund Olesen02dc6a12013-05-28 04:57:37 +00006319 // Build a coercion type from the LLVM struct type.
6320 llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty));
6321 if (!StrTy)
6322 return ABIArgInfo::getDirect();
6323
6324 CoerceBuilder CB(getVMContext(), getDataLayout());
6325 CB.addStruct(0, StrTy);
6326 CB.pad(llvm::RoundUpToAlignment(CB.DL.getTypeSizeInBits(StrTy), 64));
6327
6328 // Try to use the original type for coercion.
6329 llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();
6330
6331 if (CB.InReg)
6332 return ABIArgInfo::getDirectInReg(CoerceTy);
6333 else
6334 return ABIArgInfo::getDirect(CoerceTy);
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00006335}
6336
6337llvm::Value *SparcV9ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
6338 CodeGenFunction &CGF) const {
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00006339 ABIArgInfo AI = classifyType(Ty, 16 * 8);
6340 llvm::Type *ArgTy = CGT.ConvertType(Ty);
6341 if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
6342 AI.setCoerceToType(ArgTy);
6343
6344 llvm::Type *BPP = CGF.Int8PtrPtrTy;
6345 CGBuilderTy &Builder = CGF.Builder;
6346 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
6347 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
6348 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
6349 llvm::Value *ArgAddr;
6350 unsigned Stride;
6351
6352 switch (AI.getKind()) {
6353 case ABIArgInfo::Expand:
Reid Kleckner314ef7b2014-02-01 00:04:45 +00006354 case ABIArgInfo::InAlloca:
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00006355 llvm_unreachable("Unsupported ABI kind for va_arg");
6356
6357 case ABIArgInfo::Extend:
6358 Stride = 8;
6359 ArgAddr = Builder
6360 .CreateConstGEP1_32(Addr, 8 - getDataLayout().getTypeAllocSize(ArgTy),
6361 "extend");
6362 break;
6363
6364 case ABIArgInfo::Direct:
6365 Stride = getDataLayout().getTypeAllocSize(AI.getCoerceToType());
6366 ArgAddr = Addr;
6367 break;
6368
6369 case ABIArgInfo::Indirect:
6370 Stride = 8;
6371 ArgAddr = Builder.CreateBitCast(Addr,
6372 llvm::PointerType::getUnqual(ArgPtrTy),
6373 "indirect");
6374 ArgAddr = Builder.CreateLoad(ArgAddr, "indirect.arg");
6375 break;
6376
6377 case ABIArgInfo::Ignore:
6378 return llvm::UndefValue::get(ArgPtrTy);
6379 }
6380
6381 // Update VAList.
6382 Addr = Builder.CreateConstGEP1_32(Addr, Stride, "ap.next");
6383 Builder.CreateStore(Addr, VAListAddrAsBPP);
6384
6385 return Builder.CreatePointerCast(ArgAddr, ArgPtrTy, "arg.addr");
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00006386}
6387
6388void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const {
6389 FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8);
Aaron Ballmanec47bc22014-03-17 18:10:01 +00006390 for (auto &I : FI.arguments())
6391 I.info = classifyType(I.type, 16 * 8);
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00006392}
6393
6394namespace {
6395class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo {
6396public:
6397 SparcV9TargetCodeGenInfo(CodeGenTypes &CGT)
6398 : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {}
Roman Divackyf02c9942014-02-24 18:46:27 +00006399
Craig Topper4f12f102014-03-12 06:41:41 +00006400 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
Roman Divackyf02c9942014-02-24 18:46:27 +00006401 return 14;
6402 }
6403
6404 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00006405 llvm::Value *Address) const override;
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00006406};
6407} // end anonymous namespace
6408
Roman Divackyf02c9942014-02-24 18:46:27 +00006409bool
6410SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
6411 llvm::Value *Address) const {
6412 // This is calculated from the LLVM and GCC tables and verified
6413 // against gcc output. AFAIK all ABIs use the same encoding.
6414
6415 CodeGen::CGBuilderTy &Builder = CGF.Builder;
6416
6417 llvm::IntegerType *i8 = CGF.Int8Ty;
6418 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
6419 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
6420
6421 // 0-31: the 8-byte general-purpose registers
6422 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
6423
6424 // 32-63: f0-31, the 4-byte floating-point registers
6425 AssignToArrayRange(Builder, Address, Four8, 32, 63);
6426
6427 // Y = 64
6428 // PSR = 65
6429 // WIM = 66
6430 // TBR = 67
6431 // PC = 68
6432 // NPC = 69
6433 // FSR = 70
6434 // CSR = 71
6435 AssignToArrayRange(Builder, Address, Eight8, 64, 71);
6436
6437 // 72-87: d0-15, the 8-byte floating-point registers
6438 AssignToArrayRange(Builder, Address, Eight8, 72, 87);
6439
6440 return false;
6441}
6442
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00006443
Robert Lytton0e076492013-08-13 09:43:10 +00006444//===----------------------------------------------------------------------===//
Robert Lyttond21e2d72014-03-03 13:45:29 +00006445// XCore ABI Implementation
Robert Lytton0e076492013-08-13 09:43:10 +00006446//===----------------------------------------------------------------------===//
Robert Lytton844aeeb2014-05-02 09:33:20 +00006447
Robert Lytton0e076492013-08-13 09:43:10 +00006448namespace {
Robert Lytton844aeeb2014-05-02 09:33:20 +00006449
6450/// A SmallStringEnc instance is used to build up the TypeString by passing
6451/// it by reference between functions that append to it.
6452typedef llvm::SmallString<128> SmallStringEnc;
6453
6454/// TypeStringCache caches the meta encodings of Types.
6455///
6456/// The reason for caching TypeStrings is two fold:
6457/// 1. To cache a type's encoding for later uses;
6458/// 2. As a means to break recursive member type inclusion.
6459///
6460/// A cache Entry can have a Status of:
6461/// NonRecursive: The type encoding is not recursive;
6462/// Recursive: The type encoding is recursive;
6463/// Incomplete: An incomplete TypeString;
6464/// IncompleteUsed: An incomplete TypeString that has been used in a
6465/// Recursive type encoding.
6466///
6467/// A NonRecursive entry will have all of its sub-members expanded as fully
6468/// as possible. Whilst it may contain types which are recursive, the type
6469/// itself is not recursive and thus its encoding may be safely used whenever
6470/// the type is encountered.
6471///
6472/// A Recursive entry will have all of its sub-members expanded as fully as
6473/// possible. The type itself is recursive and it may contain other types which
6474/// are recursive. The Recursive encoding must not be used during the expansion
6475/// of a recursive type's recursive branch. For simplicity the code uses
6476/// IncompleteCount to reject all usage of Recursive encodings for member types.
6477///
6478/// An Incomplete entry is always a RecordType and only encodes its
6479/// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and
6480/// are placed into the cache during type expansion as a means to identify and
6481/// handle recursive inclusion of types as sub-members. If there is recursion
6482/// the entry becomes IncompleteUsed.
6483///
6484/// During the expansion of a RecordType's members:
6485///
6486/// If the cache contains a NonRecursive encoding for the member type, the
6487/// cached encoding is used;
6488///
6489/// If the cache contains a Recursive encoding for the member type, the
6490/// cached encoding is 'Swapped' out, as it may be incorrect, and...
6491///
6492/// If the member is a RecordType, an Incomplete encoding is placed into the
6493/// cache to break potential recursive inclusion of itself as a sub-member;
6494///
6495/// Once a member RecordType has been expanded, its temporary incomplete
6496/// entry is removed from the cache. If a Recursive encoding was swapped out
6497/// it is swapped back in;
6498///
6499/// If an incomplete entry is used to expand a sub-member, the incomplete
6500/// entry is marked as IncompleteUsed. The cache keeps count of how many
6501/// IncompleteUsed entries it currently contains in IncompleteUsedCount;
6502///
6503/// If a member's encoding is found to be a NonRecursive or Recursive viz:
6504/// IncompleteUsedCount==0, the member's encoding is added to the cache.
6505/// Else the member is part of a recursive type and thus the recursion has
6506/// been exited too soon for the encoding to be correct for the member.
6507///
6508class TypeStringCache {
6509 enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed};
6510 struct Entry {
6511 std::string Str; // The encoded TypeString for the type.
6512 enum Status State; // Information about the encoding in 'Str'.
6513 std::string Swapped; // A temporary place holder for a Recursive encoding
6514 // during the expansion of RecordType's members.
6515 };
6516 std::map<const IdentifierInfo *, struct Entry> Map;
6517 unsigned IncompleteCount; // Number of Incomplete entries in the Map.
6518 unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map.
6519public:
Robert Lyttond263f142014-05-06 09:38:54 +00006520 TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {};
Robert Lytton844aeeb2014-05-02 09:33:20 +00006521 void addIncomplete(const IdentifierInfo *ID, std::string StubEnc);
6522 bool removeIncomplete(const IdentifierInfo *ID);
6523 void addIfComplete(const IdentifierInfo *ID, StringRef Str,
6524 bool IsRecursive);
6525 StringRef lookupStr(const IdentifierInfo *ID);
6526};
6527
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00006528/// TypeString encodings for enum & union fields must be order.
Robert Lytton844aeeb2014-05-02 09:33:20 +00006529/// FieldEncoding is a helper for this ordering process.
6530class FieldEncoding {
6531 bool HasName;
6532 std::string Enc;
6533public:
6534 FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {};
6535 StringRef str() {return Enc.c_str();};
6536 bool operator<(const FieldEncoding &rhs) const {
6537 if (HasName != rhs.HasName) return HasName;
6538 return Enc < rhs.Enc;
6539 }
6540};
6541
Robert Lytton7d1db152013-08-19 09:46:39 +00006542class XCoreABIInfo : public DefaultABIInfo {
6543public:
6544 XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
Craig Topper4f12f102014-03-12 06:41:41 +00006545 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
6546 CodeGenFunction &CGF) const override;
Robert Lytton7d1db152013-08-19 09:46:39 +00006547};
6548
Robert Lyttond21e2d72014-03-03 13:45:29 +00006549class XCoreTargetCodeGenInfo : public TargetCodeGenInfo {
Robert Lytton844aeeb2014-05-02 09:33:20 +00006550 mutable TypeStringCache TSC;
Robert Lytton0e076492013-08-13 09:43:10 +00006551public:
Robert Lyttond21e2d72014-03-03 13:45:29 +00006552 XCoreTargetCodeGenInfo(CodeGenTypes &CGT)
Robert Lytton7d1db152013-08-19 09:46:39 +00006553 :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {}
Rafael Espindola8dcd6e72014-05-08 15:01:48 +00006554 void emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
6555 CodeGen::CodeGenModule &M) const override;
Robert Lytton0e076492013-08-13 09:43:10 +00006556};
Robert Lytton844aeeb2014-05-02 09:33:20 +00006557
Robert Lytton2d196952013-10-11 10:29:34 +00006558} // End anonymous namespace.
Robert Lytton0e076492013-08-13 09:43:10 +00006559
Robert Lytton7d1db152013-08-19 09:46:39 +00006560llvm::Value *XCoreABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
6561 CodeGenFunction &CGF) const {
Robert Lytton7d1db152013-08-19 09:46:39 +00006562 CGBuilderTy &Builder = CGF.Builder;
Robert Lytton7d1db152013-08-19 09:46:39 +00006563
Robert Lytton2d196952013-10-11 10:29:34 +00006564 // Get the VAList.
Robert Lytton7d1db152013-08-19 09:46:39 +00006565 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr,
6566 CGF.Int8PtrPtrTy);
6567 llvm::Value *AP = Builder.CreateLoad(VAListAddrAsBPP);
Robert Lytton7d1db152013-08-19 09:46:39 +00006568
Robert Lytton2d196952013-10-11 10:29:34 +00006569 // Handle the argument.
6570 ABIArgInfo AI = classifyArgumentType(Ty);
6571 llvm::Type *ArgTy = CGT.ConvertType(Ty);
6572 if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
6573 AI.setCoerceToType(ArgTy);
Robert Lytton7d1db152013-08-19 09:46:39 +00006574 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
Robert Lytton2d196952013-10-11 10:29:34 +00006575 llvm::Value *Val;
Andy Gibbsd9ba4722013-10-14 07:02:04 +00006576 uint64_t ArgSize = 0;
Robert Lytton7d1db152013-08-19 09:46:39 +00006577 switch (AI.getKind()) {
Robert Lytton7d1db152013-08-19 09:46:39 +00006578 case ABIArgInfo::Expand:
Reid Kleckner314ef7b2014-02-01 00:04:45 +00006579 case ABIArgInfo::InAlloca:
Robert Lytton7d1db152013-08-19 09:46:39 +00006580 llvm_unreachable("Unsupported ABI kind for va_arg");
6581 case ABIArgInfo::Ignore:
Robert Lytton2d196952013-10-11 10:29:34 +00006582 Val = llvm::UndefValue::get(ArgPtrTy);
6583 ArgSize = 0;
6584 break;
Robert Lytton7d1db152013-08-19 09:46:39 +00006585 case ABIArgInfo::Extend:
6586 case ABIArgInfo::Direct:
Robert Lytton2d196952013-10-11 10:29:34 +00006587 Val = Builder.CreatePointerCast(AP, ArgPtrTy);
6588 ArgSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType());
6589 if (ArgSize < 4)
6590 ArgSize = 4;
6591 break;
Robert Lytton7d1db152013-08-19 09:46:39 +00006592 case ABIArgInfo::Indirect:
6593 llvm::Value *ArgAddr;
6594 ArgAddr = Builder.CreateBitCast(AP, llvm::PointerType::getUnqual(ArgPtrTy));
6595 ArgAddr = Builder.CreateLoad(ArgAddr);
Robert Lytton2d196952013-10-11 10:29:34 +00006596 Val = Builder.CreatePointerCast(ArgAddr, ArgPtrTy);
6597 ArgSize = 4;
6598 break;
Robert Lytton7d1db152013-08-19 09:46:39 +00006599 }
Robert Lytton2d196952013-10-11 10:29:34 +00006600
6601 // Increment the VAList.
6602 if (ArgSize) {
6603 llvm::Value *APN = Builder.CreateConstGEP1_32(AP, ArgSize);
6604 Builder.CreateStore(APN, VAListAddrAsBPP);
6605 }
6606 return Val;
Robert Lytton7d1db152013-08-19 09:46:39 +00006607}
Robert Lytton0e076492013-08-13 09:43:10 +00006608
Robert Lytton844aeeb2014-05-02 09:33:20 +00006609/// During the expansion of a RecordType, an incomplete TypeString is placed
6610/// into the cache as a means to identify and break recursion.
6611/// If there is a Recursive encoding in the cache, it is swapped out and will
6612/// be reinserted by removeIncomplete().
6613/// All other types of encoding should have been used rather than arriving here.
6614void TypeStringCache::addIncomplete(const IdentifierInfo *ID,
6615 std::string StubEnc) {
6616 if (!ID)
6617 return;
6618 Entry &E = Map[ID];
6619 assert( (E.Str.empty() || E.State == Recursive) &&
6620 "Incorrectly use of addIncomplete");
6621 assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()");
6622 E.Swapped.swap(E.Str); // swap out the Recursive
6623 E.Str.swap(StubEnc);
6624 E.State = Incomplete;
6625 ++IncompleteCount;
6626}
6627
6628/// Once the RecordType has been expanded, the temporary incomplete TypeString
6629/// must be removed from the cache.
6630/// If a Recursive was swapped out by addIncomplete(), it will be replaced.
6631/// Returns true if the RecordType was defined recursively.
6632bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) {
6633 if (!ID)
6634 return false;
6635 auto I = Map.find(ID);
6636 assert(I != Map.end() && "Entry not present");
6637 Entry &E = I->second;
6638 assert( (E.State == Incomplete ||
6639 E.State == IncompleteUsed) &&
6640 "Entry must be an incomplete type");
6641 bool IsRecursive = false;
6642 if (E.State == IncompleteUsed) {
6643 // We made use of our Incomplete encoding, thus we are recursive.
6644 IsRecursive = true;
6645 --IncompleteUsedCount;
6646 }
6647 if (E.Swapped.empty())
6648 Map.erase(I);
6649 else {
6650 // Swap the Recursive back.
6651 E.Swapped.swap(E.Str);
6652 E.Swapped.clear();
6653 E.State = Recursive;
6654 }
6655 --IncompleteCount;
6656 return IsRecursive;
6657}
6658
6659/// Add the encoded TypeString to the cache only if it is NonRecursive or
6660/// Recursive (viz: all sub-members were expanded as fully as possible).
6661void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str,
6662 bool IsRecursive) {
6663 if (!ID || IncompleteUsedCount)
6664 return; // No key or it is is an incomplete sub-type so don't add.
6665 Entry &E = Map[ID];
6666 if (IsRecursive && !E.Str.empty()) {
6667 assert(E.State==Recursive && E.Str.size() == Str.size() &&
6668 "This is not the same Recursive entry");
6669 // The parent container was not recursive after all, so we could have used
6670 // this Recursive sub-member entry after all, but we assumed the worse when
6671 // we started viz: IncompleteCount!=0.
6672 return;
6673 }
6674 assert(E.Str.empty() && "Entry already present");
6675 E.Str = Str.str();
6676 E.State = IsRecursive? Recursive : NonRecursive;
6677}
6678
6679/// Return a cached TypeString encoding for the ID. If there isn't one, or we
6680/// are recursively expanding a type (IncompleteCount != 0) and the cached
6681/// encoding is Recursive, return an empty StringRef.
6682StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) {
6683 if (!ID)
6684 return StringRef(); // We have no key.
6685 auto I = Map.find(ID);
6686 if (I == Map.end())
6687 return StringRef(); // We have no encoding.
6688 Entry &E = I->second;
6689 if (E.State == Recursive && IncompleteCount)
6690 return StringRef(); // We don't use Recursive encodings for member types.
6691
6692 if (E.State == Incomplete) {
6693 // The incomplete type is being used to break out of recursion.
6694 E.State = IncompleteUsed;
6695 ++IncompleteUsedCount;
6696 }
6697 return E.Str.c_str();
6698}
6699
6700/// The XCore ABI includes a type information section that communicates symbol
6701/// type information to the linker. The linker uses this information to verify
6702/// safety/correctness of things such as array bound and pointers et al.
6703/// The ABI only requires C (and XC) language modules to emit TypeStrings.
6704/// This type information (TypeString) is emitted into meta data for all global
6705/// symbols: definitions, declarations, functions & variables.
6706///
6707/// The TypeString carries type, qualifier, name, size & value details.
6708/// Please see 'Tools Development Guide' section 2.16.2 for format details:
6709/// <https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf>
6710/// The output is tested by test/CodeGen/xcore-stringtype.c.
6711///
6712static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
6713 CodeGen::CodeGenModule &CGM, TypeStringCache &TSC);
6714
6715/// XCore uses emitTargetMD to emit TypeString metadata for global symbols.
6716void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
6717 CodeGen::CodeGenModule &CGM) const {
6718 SmallStringEnc Enc;
6719 if (getTypeString(Enc, D, CGM, TSC)) {
6720 llvm::LLVMContext &Ctx = CGM.getModule().getContext();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00006721 llvm::SmallVector<llvm::Metadata *, 2> MDVals;
6722 MDVals.push_back(llvm::ConstantAsMetadata::get(GV));
Robert Lytton844aeeb2014-05-02 09:33:20 +00006723 MDVals.push_back(llvm::MDString::get(Ctx, Enc.str()));
6724 llvm::NamedMDNode *MD =
6725 CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings");
6726 MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
6727 }
6728}
6729
6730static bool appendType(SmallStringEnc &Enc, QualType QType,
6731 const CodeGen::CodeGenModule &CGM,
6732 TypeStringCache &TSC);
6733
6734/// Helper function for appendRecordType().
6735/// Builds a SmallVector containing the encoded field types in declaration order.
6736static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE,
6737 const RecordDecl *RD,
6738 const CodeGen::CodeGenModule &CGM,
6739 TypeStringCache &TSC) {
Hans Wennborga302cd92014-08-21 16:06:57 +00006740 for (const auto *Field : RD->fields()) {
Robert Lytton844aeeb2014-05-02 09:33:20 +00006741 SmallStringEnc Enc;
6742 Enc += "m(";
Hans Wennborga302cd92014-08-21 16:06:57 +00006743 Enc += Field->getName();
Robert Lytton844aeeb2014-05-02 09:33:20 +00006744 Enc += "){";
Hans Wennborga302cd92014-08-21 16:06:57 +00006745 if (Field->isBitField()) {
Robert Lytton844aeeb2014-05-02 09:33:20 +00006746 Enc += "b(";
6747 llvm::raw_svector_ostream OS(Enc);
6748 OS.resync();
Hans Wennborga302cd92014-08-21 16:06:57 +00006749 OS << Field->getBitWidthValue(CGM.getContext());
Robert Lytton844aeeb2014-05-02 09:33:20 +00006750 OS.flush();
6751 Enc += ':';
6752 }
Hans Wennborga302cd92014-08-21 16:06:57 +00006753 if (!appendType(Enc, Field->getType(), CGM, TSC))
Robert Lytton844aeeb2014-05-02 09:33:20 +00006754 return false;
Hans Wennborga302cd92014-08-21 16:06:57 +00006755 if (Field->isBitField())
Robert Lytton844aeeb2014-05-02 09:33:20 +00006756 Enc += ')';
6757 Enc += '}';
Hans Wennborga302cd92014-08-21 16:06:57 +00006758 FE.push_back(FieldEncoding(!Field->getName().empty(), Enc));
Robert Lytton844aeeb2014-05-02 09:33:20 +00006759 }
6760 return true;
6761}
6762
6763/// Appends structure and union types to Enc and adds encoding to cache.
6764/// Recursively calls appendType (via extractFieldType) for each field.
6765/// Union types have their fields ordered according to the ABI.
6766static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT,
6767 const CodeGen::CodeGenModule &CGM,
6768 TypeStringCache &TSC, const IdentifierInfo *ID) {
6769 // Append the cached TypeString if we have one.
6770 StringRef TypeString = TSC.lookupStr(ID);
6771 if (!TypeString.empty()) {
6772 Enc += TypeString;
6773 return true;
6774 }
6775
6776 // Start to emit an incomplete TypeString.
6777 size_t Start = Enc.size();
6778 Enc += (RT->isUnionType()? 'u' : 's');
6779 Enc += '(';
6780 if (ID)
6781 Enc += ID->getName();
6782 Enc += "){";
6783
6784 // We collect all encoded fields and order as necessary.
6785 bool IsRecursive = false;
Robert Lytton844aeeb2014-05-02 09:33:20 +00006786 const RecordDecl *RD = RT->getDecl()->getDefinition();
6787 if (RD && !RD->field_empty()) {
6788 // An incomplete TypeString stub is placed in the cache for this RecordType
6789 // so that recursive calls to this RecordType will use it whilst building a
6790 // complete TypeString for this RecordType.
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00006791 SmallVector<FieldEncoding, 16> FE;
Robert Lytton844aeeb2014-05-02 09:33:20 +00006792 std::string StubEnc(Enc.substr(Start).str());
6793 StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString.
6794 TSC.addIncomplete(ID, std::move(StubEnc));
6795 if (!extractFieldType(FE, RD, CGM, TSC)) {
6796 (void) TSC.removeIncomplete(ID);
6797 return false;
6798 }
6799 IsRecursive = TSC.removeIncomplete(ID);
6800 // The ABI requires unions to be sorted but not structures.
6801 // See FieldEncoding::operator< for sort algorithm.
6802 if (RT->isUnionType())
6803 std::sort(FE.begin(), FE.end());
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00006804 // We can now complete the TypeString.
6805 unsigned E = FE.size();
Robert Lytton844aeeb2014-05-02 09:33:20 +00006806 for (unsigned I = 0; I != E; ++I) {
6807 if (I)
6808 Enc += ',';
6809 Enc += FE[I].str();
6810 }
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00006811 }
Robert Lytton844aeeb2014-05-02 09:33:20 +00006812 Enc += '}';
6813 TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive);
6814 return true;
6815}
6816
6817/// Appends enum types to Enc and adds the encoding to the cache.
6818static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET,
6819 TypeStringCache &TSC,
6820 const IdentifierInfo *ID) {
6821 // Append the cached TypeString if we have one.
6822 StringRef TypeString = TSC.lookupStr(ID);
6823 if (!TypeString.empty()) {
6824 Enc += TypeString;
6825 return true;
6826 }
6827
6828 size_t Start = Enc.size();
6829 Enc += "e(";
6830 if (ID)
6831 Enc += ID->getName();
6832 Enc += "){";
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00006833
6834 // We collect all encoded enumerations and order them alphanumerically.
Robert Lytton844aeeb2014-05-02 09:33:20 +00006835 if (const EnumDecl *ED = ET->getDecl()->getDefinition()) {
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00006836 SmallVector<FieldEncoding, 16> FE;
6837 for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E;
6838 ++I) {
6839 SmallStringEnc EnumEnc;
6840 EnumEnc += "m(";
6841 EnumEnc += I->getName();
6842 EnumEnc += "){";
6843 I->getInitVal().toString(EnumEnc);
6844 EnumEnc += '}';
6845 FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc));
6846 }
6847 std::sort(FE.begin(), FE.end());
6848 unsigned E = FE.size();
6849 for (unsigned I = 0; I != E; ++I) {
6850 if (I)
Robert Lytton844aeeb2014-05-02 09:33:20 +00006851 Enc += ',';
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00006852 Enc += FE[I].str();
Robert Lytton844aeeb2014-05-02 09:33:20 +00006853 }
6854 }
6855 Enc += '}';
6856 TSC.addIfComplete(ID, Enc.substr(Start), false);
6857 return true;
6858}
6859
6860/// Appends type's qualifier to Enc.
6861/// This is done prior to appending the type's encoding.
6862static void appendQualifier(SmallStringEnc &Enc, QualType QT) {
6863 // Qualifiers are emitted in alphabetical order.
6864 static const char *Table[] = {"","c:","r:","cr:","v:","cv:","rv:","crv:"};
6865 int Lookup = 0;
6866 if (QT.isConstQualified())
6867 Lookup += 1<<0;
6868 if (QT.isRestrictQualified())
6869 Lookup += 1<<1;
6870 if (QT.isVolatileQualified())
6871 Lookup += 1<<2;
6872 Enc += Table[Lookup];
6873}
6874
6875/// Appends built-in types to Enc.
6876static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) {
6877 const char *EncType;
6878 switch (BT->getKind()) {
6879 case BuiltinType::Void:
6880 EncType = "0";
6881 break;
6882 case BuiltinType::Bool:
6883 EncType = "b";
6884 break;
6885 case BuiltinType::Char_U:
6886 EncType = "uc";
6887 break;
6888 case BuiltinType::UChar:
6889 EncType = "uc";
6890 break;
6891 case BuiltinType::SChar:
6892 EncType = "sc";
6893 break;
6894 case BuiltinType::UShort:
6895 EncType = "us";
6896 break;
6897 case BuiltinType::Short:
6898 EncType = "ss";
6899 break;
6900 case BuiltinType::UInt:
6901 EncType = "ui";
6902 break;
6903 case BuiltinType::Int:
6904 EncType = "si";
6905 break;
6906 case BuiltinType::ULong:
6907 EncType = "ul";
6908 break;
6909 case BuiltinType::Long:
6910 EncType = "sl";
6911 break;
6912 case BuiltinType::ULongLong:
6913 EncType = "ull";
6914 break;
6915 case BuiltinType::LongLong:
6916 EncType = "sll";
6917 break;
6918 case BuiltinType::Float:
6919 EncType = "ft";
6920 break;
6921 case BuiltinType::Double:
6922 EncType = "d";
6923 break;
6924 case BuiltinType::LongDouble:
6925 EncType = "ld";
6926 break;
6927 default:
6928 return false;
6929 }
6930 Enc += EncType;
6931 return true;
6932}
6933
6934/// Appends a pointer encoding to Enc before calling appendType for the pointee.
6935static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT,
6936 const CodeGen::CodeGenModule &CGM,
6937 TypeStringCache &TSC) {
6938 Enc += "p(";
6939 if (!appendType(Enc, PT->getPointeeType(), CGM, TSC))
6940 return false;
6941 Enc += ')';
6942 return true;
6943}
6944
6945/// Appends array encoding to Enc before calling appendType for the element.
Robert Lytton6adb20f2014-06-05 09:06:21 +00006946static bool appendArrayType(SmallStringEnc &Enc, QualType QT,
6947 const ArrayType *AT,
Robert Lytton844aeeb2014-05-02 09:33:20 +00006948 const CodeGen::CodeGenModule &CGM,
6949 TypeStringCache &TSC, StringRef NoSizeEnc) {
6950 if (AT->getSizeModifier() != ArrayType::Normal)
6951 return false;
6952 Enc += "a(";
6953 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
6954 CAT->getSize().toStringUnsigned(Enc);
6955 else
6956 Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "".
6957 Enc += ':';
Robert Lytton6adb20f2014-06-05 09:06:21 +00006958 // The Qualifiers should be attached to the type rather than the array.
6959 appendQualifier(Enc, QT);
Robert Lytton844aeeb2014-05-02 09:33:20 +00006960 if (!appendType(Enc, AT->getElementType(), CGM, TSC))
6961 return false;
6962 Enc += ')';
6963 return true;
6964}
6965
6966/// Appends a function encoding to Enc, calling appendType for the return type
6967/// and the arguments.
6968static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT,
6969 const CodeGen::CodeGenModule &CGM,
6970 TypeStringCache &TSC) {
6971 Enc += "f{";
6972 if (!appendType(Enc, FT->getReturnType(), CGM, TSC))
6973 return false;
6974 Enc += "}(";
6975 if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) {
6976 // N.B. we are only interested in the adjusted param types.
6977 auto I = FPT->param_type_begin();
6978 auto E = FPT->param_type_end();
6979 if (I != E) {
6980 do {
6981 if (!appendType(Enc, *I, CGM, TSC))
6982 return false;
6983 ++I;
6984 if (I != E)
6985 Enc += ',';
6986 } while (I != E);
6987 if (FPT->isVariadic())
6988 Enc += ",va";
6989 } else {
6990 if (FPT->isVariadic())
6991 Enc += "va";
6992 else
6993 Enc += '0';
6994 }
6995 }
6996 Enc += ')';
6997 return true;
6998}
6999
7000/// Handles the type's qualifier before dispatching a call to handle specific
7001/// type encodings.
7002static bool appendType(SmallStringEnc &Enc, QualType QType,
7003 const CodeGen::CodeGenModule &CGM,
7004 TypeStringCache &TSC) {
7005
7006 QualType QT = QType.getCanonicalType();
7007
Robert Lytton6adb20f2014-06-05 09:06:21 +00007008 if (const ArrayType *AT = QT->getAsArrayTypeUnsafe())
7009 // The Qualifiers should be attached to the type rather than the array.
7010 // Thus we don't call appendQualifier() here.
7011 return appendArrayType(Enc, QT, AT, CGM, TSC, "");
7012
Robert Lytton844aeeb2014-05-02 09:33:20 +00007013 appendQualifier(Enc, QT);
7014
7015 if (const BuiltinType *BT = QT->getAs<BuiltinType>())
7016 return appendBuiltinType(Enc, BT);
7017
Robert Lytton844aeeb2014-05-02 09:33:20 +00007018 if (const PointerType *PT = QT->getAs<PointerType>())
7019 return appendPointerType(Enc, PT, CGM, TSC);
7020
7021 if (const EnumType *ET = QT->getAs<EnumType>())
7022 return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier());
7023
7024 if (const RecordType *RT = QT->getAsStructureType())
7025 return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
7026
7027 if (const RecordType *RT = QT->getAsUnionType())
7028 return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
7029
7030 if (const FunctionType *FT = QT->getAs<FunctionType>())
7031 return appendFunctionType(Enc, FT, CGM, TSC);
7032
7033 return false;
7034}
7035
7036static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
7037 CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) {
7038 if (!D)
7039 return false;
7040
7041 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
7042 if (FD->getLanguageLinkage() != CLanguageLinkage)
7043 return false;
7044 return appendType(Enc, FD->getType(), CGM, TSC);
7045 }
7046
7047 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
7048 if (VD->getLanguageLinkage() != CLanguageLinkage)
7049 return false;
7050 QualType QT = VD->getType().getCanonicalType();
7051 if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) {
7052 // Global ArrayTypes are given a size of '*' if the size is unknown.
Robert Lytton6adb20f2014-06-05 09:06:21 +00007053 // The Qualifiers should be attached to the type rather than the array.
7054 // Thus we don't call appendQualifier() here.
7055 return appendArrayType(Enc, QT, AT, CGM, TSC, "*");
Robert Lytton844aeeb2014-05-02 09:33:20 +00007056 }
7057 return appendType(Enc, QT, CGM, TSC);
7058 }
7059 return false;
7060}
7061
7062
Robert Lytton0e076492013-08-13 09:43:10 +00007063//===----------------------------------------------------------------------===//
7064// Driver code
7065//===----------------------------------------------------------------------===//
7066
Rafael Espindola9f834732014-09-19 01:54:22 +00007067const llvm::Triple &CodeGenModule::getTriple() const {
7068 return getTarget().getTriple();
7069}
7070
7071bool CodeGenModule::supportsCOMDAT() const {
7072 return !getTriple().isOSBinFormatMachO();
7073}
7074
Chris Lattner2b037972010-07-29 02:01:43 +00007075const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00007076 if (TheTargetCodeGenInfo)
7077 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00007078
John McCallc8e01702013-04-16 22:48:15 +00007079 const llvm::Triple &Triple = getTarget().getTriple();
Daniel Dunbar40165182009-08-24 09:10:05 +00007080 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00007081 default:
Chris Lattner2b037972010-07-29 02:01:43 +00007082 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00007083
Derek Schuff09338a22012-09-06 17:37:28 +00007084 case llvm::Triple::le32:
7085 return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
John McCall943fae92010-05-27 06:19:26 +00007086 case llvm::Triple::mips:
7087 case llvm::Triple::mipsel:
Akira Hatanakac4baedd2013-11-11 22:10:46 +00007088 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
7089
Akira Hatanakaec11b4f2011-09-20 18:30:57 +00007090 case llvm::Triple::mips64:
7091 case llvm::Triple::mips64el:
Akira Hatanakac4baedd2013-11-11 22:10:46 +00007092 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
7093
Tim Northover25e8a672014-05-24 12:51:25 +00007094 case llvm::Triple::aarch64:
Tim Northover40956e62014-07-23 12:32:58 +00007095 case llvm::Triple::aarch64_be: {
Tim Northover573cbee2014-05-24 12:52:07 +00007096 AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS;
Alp Toker4925ba72014-06-07 23:30:42 +00007097 if (getTarget().getABI() == "darwinpcs")
Tim Northover573cbee2014-05-24 12:52:07 +00007098 Kind = AArch64ABIInfo::DarwinPCS;
Tim Northovera2ee4332014-03-29 15:09:45 +00007099
Tim Northover573cbee2014-05-24 12:52:07 +00007100 return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types, Kind));
Tim Northovera2ee4332014-03-29 15:09:45 +00007101 }
7102
Daniel Dunbard59655c2009-09-12 00:59:49 +00007103 case llvm::Triple::arm:
Christian Pirkerf01cd6f2014-03-28 14:40:46 +00007104 case llvm::Triple::armeb:
Daniel Dunbard59655c2009-09-12 00:59:49 +00007105 case llvm::Triple::thumb:
Christian Pirkerf01cd6f2014-03-28 14:40:46 +00007106 case llvm::Triple::thumbeb:
Sandeep Patel45df3dd2011-04-05 00:23:47 +00007107 {
7108 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Alp Toker4925ba72014-06-07 23:30:42 +00007109 if (getTarget().getABI() == "apcs-gnu")
Sandeep Patel45df3dd2011-04-05 00:23:47 +00007110 Kind = ARMABIInfo::APCS;
David Tweed8f676532012-10-25 13:33:01 +00007111 else if (CodeGenOpts.FloatABI == "hard" ||
John McCallc8e01702013-04-16 22:48:15 +00007112 (CodeGenOpts.FloatABI != "soft" &&
7113 Triple.getEnvironment() == llvm::Triple::GNUEABIHF))
Sandeep Patel45df3dd2011-04-05 00:23:47 +00007114 Kind = ARMABIInfo::AAPCS_VFP;
7115
Derek Schuffa2020962012-10-16 22:30:41 +00007116 switch (Triple.getOS()) {
Eli Benderskyd7c92032012-12-04 18:38:10 +00007117 case llvm::Triple::NaCl:
Derek Schuffa2020962012-10-16 22:30:41 +00007118 return *(TheTargetCodeGenInfo =
7119 new NaClARMTargetCodeGenInfo(Types, Kind));
7120 default:
7121 return *(TheTargetCodeGenInfo =
7122 new ARMTargetCodeGenInfo(Types, Kind));
7123 }
Sandeep Patel45df3dd2011-04-05 00:23:47 +00007124 }
Daniel Dunbard59655c2009-09-12 00:59:49 +00007125
John McCallea8d8bb2010-03-11 00:10:12 +00007126 case llvm::Triple::ppc:
Chris Lattner2b037972010-07-29 02:01:43 +00007127 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
Roman Divackyd966e722012-05-09 18:22:46 +00007128 case llvm::Triple::ppc64:
Ulrich Weigandb7122372014-07-21 00:48:09 +00007129 if (Triple.isOSBinFormatELF()) {
Ulrich Weigandb7122372014-07-21 00:48:09 +00007130 PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1;
Ulrich Weigand8afad612014-07-28 13:17:52 +00007131 if (getTarget().getABI() == "elfv2")
7132 Kind = PPC64_SVR4_ABIInfo::ELFv2;
7133
Ulrich Weigandb7122372014-07-21 00:48:09 +00007134 return *(TheTargetCodeGenInfo =
7135 new PPC64_SVR4_TargetCodeGenInfo(Types, Kind));
7136 } else
Bill Schmidt25cb3492012-10-03 19:18:57 +00007137 return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
Ulrich Weigandb7122372014-07-21 00:48:09 +00007138 case llvm::Triple::ppc64le: {
Bill Schmidt778d3872013-07-26 01:36:11 +00007139 assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!");
Ulrich Weigandb7122372014-07-21 00:48:09 +00007140 PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2;
Ulrich Weigand8afad612014-07-28 13:17:52 +00007141 if (getTarget().getABI() == "elfv1")
7142 Kind = PPC64_SVR4_ABIInfo::ELFv1;
7143
Ulrich Weigandb7122372014-07-21 00:48:09 +00007144 return *(TheTargetCodeGenInfo =
7145 new PPC64_SVR4_TargetCodeGenInfo(Types, Kind));
7146 }
John McCallea8d8bb2010-03-11 00:10:12 +00007147
Peter Collingbournec947aae2012-05-20 23:28:41 +00007148 case llvm::Triple::nvptx:
7149 case llvm::Triple::nvptx64:
Justin Holewinski83e96682012-05-24 17:43:12 +00007150 return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types));
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00007151
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00007152 case llvm::Triple::msp430:
Chris Lattner2b037972010-07-29 02:01:43 +00007153 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00007154
Ulrich Weigand47445072013-05-06 16:26:41 +00007155 case llvm::Triple::systemz:
7156 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
7157
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007158 case llvm::Triple::tce:
7159 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
7160
Eli Friedman33465822011-07-08 23:31:17 +00007161 case llvm::Triple::x86: {
John McCall1fe2a8c2013-06-18 02:46:29 +00007162 bool IsDarwinVectorABI = Triple.isOSDarwin();
7163 bool IsSmallStructInRegABI =
7164 X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
Saleem Abdulrasoolec5c6242014-11-23 02:16:24 +00007165 bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing();
Daniel Dunbar14ad22f2011-04-19 21:43:27 +00007166
John McCall1fe2a8c2013-06-18 02:46:29 +00007167 if (Triple.getOS() == llvm::Triple::Win32) {
Eli Friedmana98d1f82012-01-25 22:46:34 +00007168 return *(TheTargetCodeGenInfo =
Reid Klecknere43f0fe2013-05-08 13:44:39 +00007169 new WinX86_32TargetCodeGenInfo(Types,
John McCall1fe2a8c2013-06-18 02:46:29 +00007170 IsDarwinVectorABI, IsSmallStructInRegABI,
7171 IsWin32FloatStructABI,
Reid Klecknere43f0fe2013-05-08 13:44:39 +00007172 CodeGenOpts.NumRegisterParameters));
John McCall1fe2a8c2013-06-18 02:46:29 +00007173 } else {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00007174 return *(TheTargetCodeGenInfo =
John McCall1fe2a8c2013-06-18 02:46:29 +00007175 new X86_32TargetCodeGenInfo(Types,
7176 IsDarwinVectorABI, IsSmallStructInRegABI,
7177 IsWin32FloatStructABI,
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00007178 CodeGenOpts.NumRegisterParameters));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00007179 }
Eli Friedman33465822011-07-08 23:31:17 +00007180 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00007181
Eli Friedmanbfd5add2011-12-02 00:11:43 +00007182 case llvm::Triple::x86_64: {
Alp Toker4925ba72014-06-07 23:30:42 +00007183 bool HasAVX = getTarget().getABI() == "avx";
Eli Friedmanbfd5add2011-12-02 00:11:43 +00007184
Chris Lattner04dc9572010-08-31 16:44:54 +00007185 switch (Triple.getOS()) {
7186 case llvm::Triple::Win32:
Alexander Musman09184fe2014-09-30 05:29:28 +00007187 return *(TheTargetCodeGenInfo =
7188 new WinX86_64TargetCodeGenInfo(Types, HasAVX));
Eli Benderskyd7c92032012-12-04 18:38:10 +00007189 case llvm::Triple::NaCl:
Alexander Musman09184fe2014-09-30 05:29:28 +00007190 return *(TheTargetCodeGenInfo =
7191 new NaClX86_64TargetCodeGenInfo(Types, HasAVX));
Chris Lattner04dc9572010-08-31 16:44:54 +00007192 default:
Alexander Musman09184fe2014-09-30 05:29:28 +00007193 return *(TheTargetCodeGenInfo =
7194 new X86_64TargetCodeGenInfo(Types, HasAVX));
Chris Lattner04dc9572010-08-31 16:44:54 +00007195 }
Daniel Dunbare3532f82009-08-24 08:52:16 +00007196 }
Tony Linthicum76329bf2011-12-12 21:14:55 +00007197 case llvm::Triple::hexagon:
7198 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00007199 case llvm::Triple::r600:
7200 return *(TheTargetCodeGenInfo = new AMDGPUTargetCodeGenInfo(Types));
Tom Stellardd8e38a32015-01-06 20:34:47 +00007201 case llvm::Triple::amdgcn:
7202 return *(TheTargetCodeGenInfo = new AMDGPUTargetCodeGenInfo(Types));
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00007203 case llvm::Triple::sparcv9:
7204 return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types));
Robert Lytton0e076492013-08-13 09:43:10 +00007205 case llvm::Triple::xcore:
Robert Lyttond21e2d72014-03-03 13:45:29 +00007206 return *(TheTargetCodeGenInfo = new XCoreTargetCodeGenInfo(Types));
Eli Friedmanbfd5add2011-12-02 00:11:43 +00007207 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00007208}