blob: 7aed35956f3f42c2abb2c2ea361e7363b2d6ef4c [file] [log] [blame]
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000016#include "ABIInfo.h"
17#include "CodeGenFunction.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000018#include "clang/AST/RecordLayout.h"
Sandeep Patel34c1af82011-04-05 00:23:47 +000019#include "clang/Frontend/CodeGenOptions.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000020#include "llvm/Type.h"
Micah Villmow25a6a842012-10-08 16:25:52 +000021#include "llvm/DataLayout.h"
Daniel Dunbar2c0843f2009-08-24 08:52:16 +000022#include "llvm/ADT/Triple.h"
Daniel Dunbar28df7a52009-12-03 09:13:49 +000023#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000024using namespace clang;
25using namespace CodeGen;
26
John McCallaeeb7012010-05-27 06:19:26 +000027static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
28 llvm::Value *Array,
29 llvm::Value *Value,
30 unsigned FirstIndex,
31 unsigned LastIndex) {
32 // Alternatively, we could emit this as a loop in the source.
33 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
34 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
35 Builder.CreateStore(Value, Cell);
36 }
37}
38
John McCalld608cdb2010-08-22 10:59:02 +000039static bool isAggregateTypeForABI(QualType T) {
40 return CodeGenFunction::hasAggregateLLVMType(T) ||
41 T->isMemberFunctionPointerType();
42}
43
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000044ABIInfo::~ABIInfo() {}
45
Chris Lattnerea044322010-07-29 02:01:43 +000046ASTContext &ABIInfo::getContext() const {
47 return CGT.getContext();
48}
49
50llvm::LLVMContext &ABIInfo::getVMContext() const {
51 return CGT.getLLVMContext();
52}
53
Micah Villmow25a6a842012-10-08 16:25:52 +000054const llvm::DataLayout &ABIInfo::getDataLayout() const {
55 return CGT.getDataLayout();
Chris Lattnerea044322010-07-29 02:01:43 +000056}
57
58
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000059void ABIArgInfo::dump() const {
Chris Lattner5f9e2722011-07-23 10:55:15 +000060 raw_ostream &OS = llvm::errs();
Daniel Dunbar28df7a52009-12-03 09:13:49 +000061 OS << "(ABIArgInfo Kind=";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000062 switch (TheKind) {
63 case Direct:
Chris Lattner800588f2010-07-29 06:26:06 +000064 OS << "Direct Type=";
Chris Lattner2acc6e32011-07-18 04:24:23 +000065 if (llvm::Type *Ty = getCoerceToType())
Chris Lattner800588f2010-07-29 06:26:06 +000066 Ty->print(OS);
67 else
68 OS << "null";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000069 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000070 case Extend:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000071 OS << "Extend";
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000072 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000073 case Ignore:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000074 OS << "Ignore";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000075 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000076 case Indirect:
Daniel Dunbardc6d5742010-04-21 19:10:51 +000077 OS << "Indirect Align=" << getIndirectAlign()
Joerg Sonnenbergere9b5d772011-07-15 18:23:44 +000078 << " ByVal=" << getIndirectByVal()
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +000079 << " Realign=" << getIndirectRealign();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000080 break;
81 case Expand:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000082 OS << "Expand";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000083 break;
84 }
Daniel Dunbar28df7a52009-12-03 09:13:49 +000085 OS << ")\n";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000086}
87
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000088TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
89
John McCall49e34be2011-08-30 01:42:09 +000090// If someone can figure out a general rule for this, that would be great.
91// It's probably just doomed to be platform-dependent, though.
92unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
93 // Verified for:
94 // x86-64 FreeBSD, Linux, Darwin
95 // x86-32 FreeBSD, Linux, Darwin
96 // PowerPC Linux, Darwin
97 // ARM Darwin (*not* EABI)
98 return 32;
99}
100
John McCallde5d3c72012-02-17 03:33:10 +0000101bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
102 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +0000103 // The following conventions are known to require this to be false:
104 // x86_stdcall
105 // MIPS
106 // For everything else, we just prefer false unless we opt out.
107 return false;
108}
109
Daniel Dunbar98303b92009-09-13 08:03:58 +0000110static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000111
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000112/// isEmptyField - Return true iff a the field is "empty", that is it
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000113/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +0000114static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
115 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000116 if (FD->isUnnamedBitfield())
117 return true;
118
119 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000120
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000121 // Constant arrays of empty records count as empty, strip them off.
122 // Constant arrays of zero length always count as empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000123 if (AllowArrays)
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000124 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
125 if (AT->getSize() == 0)
126 return true;
Daniel Dunbar98303b92009-09-13 08:03:58 +0000127 FT = AT->getElementType();
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000128 }
Daniel Dunbar98303b92009-09-13 08:03:58 +0000129
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000130 const RecordType *RT = FT->getAs<RecordType>();
131 if (!RT)
132 return false;
133
134 // C++ record fields are never empty, at least in the Itanium ABI.
135 //
136 // FIXME: We should use a predicate for whether this behavior is true in the
137 // current ABI.
138 if (isa<CXXRecordDecl>(RT->getDecl()))
139 return false;
140
Daniel Dunbar98303b92009-09-13 08:03:58 +0000141 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000142}
143
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000144/// isEmptyRecord - Return true iff a structure contains only empty
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000145/// fields. Note that a structure with a flexible array member is not
146/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000147static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000148 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000149 if (!RT)
150 return 0;
151 const RecordDecl *RD = RT->getDecl();
152 if (RD->hasFlexibleArrayMember())
153 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000154
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000155 // If this is a C++ record, check the bases first.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000156 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000157 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
158 e = CXXRD->bases_end(); i != e; ++i)
159 if (!isEmptyRecord(Context, i->getType(), true))
160 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000161
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000162 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
163 i != e; ++i)
David Blaikie581deb32012-06-06 20:45:41 +0000164 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000165 return false;
166 return true;
167}
168
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000169/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
170/// a non-trivial destructor or a non-trivial copy constructor.
171static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
172 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
173 if (!RD)
174 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000175
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000176 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
177}
178
179/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
180/// a record type with either a non-trivial destructor or a non-trivial copy
181/// constructor.
182static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
183 const RecordType *RT = T->getAs<RecordType>();
184 if (!RT)
185 return false;
186
187 return hasNonTrivialDestructorOrCopyConstructor(RT);
188}
189
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000190/// isSingleElementStruct - Determine if a structure is a "single
191/// element struct", i.e. it has exactly one non-empty field or
192/// exactly one field which is itself a single element
193/// struct. Structures with flexible array members are never
194/// considered single element structs.
195///
196/// \return The field declaration for the single non-empty field, if
197/// it exists.
198static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
199 const RecordType *RT = T->getAsStructureType();
200 if (!RT)
201 return 0;
202
203 const RecordDecl *RD = RT->getDecl();
204 if (RD->hasFlexibleArrayMember())
205 return 0;
206
207 const Type *Found = 0;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000208
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000209 // If this is a C++ record, check the bases first.
210 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
211 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
212 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000213 // Ignore empty records.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000214 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000215 continue;
216
217 // If we already found an element then this isn't a single-element struct.
218 if (Found)
219 return 0;
220
221 // If this is non-empty and not a single element struct, the composite
222 // cannot be a single element struct.
223 Found = isSingleElementStruct(i->getType(), Context);
224 if (!Found)
225 return 0;
226 }
227 }
228
229 // Check for single element.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000230 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
231 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000232 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000233 QualType FT = FD->getType();
234
235 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000236 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000237 continue;
238
239 // If we already found an element then this isn't a single-element
240 // struct.
241 if (Found)
242 return 0;
243
244 // Treat single element arrays as the element.
245 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
246 if (AT->getSize().getZExtValue() != 1)
247 break;
248 FT = AT->getElementType();
249 }
250
John McCalld608cdb2010-08-22 10:59:02 +0000251 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000252 Found = FT.getTypePtr();
253 } else {
254 Found = isSingleElementStruct(FT, Context);
255 if (!Found)
256 return 0;
257 }
258 }
259
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000260 // We don't consider a struct a single-element struct if it has
261 // padding beyond the element type.
262 if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
263 return 0;
264
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000265 return Found;
266}
267
268static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbara1842d32010-05-14 03:40:53 +0000269 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000270 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
271 !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000272 return false;
273
274 uint64_t Size = Context.getTypeSize(Ty);
275 return Size == 32 || Size == 64;
276}
277
Daniel Dunbar53012f42009-11-09 01:33:53 +0000278/// canExpandIndirectArgument - Test whether an argument type which is to be
279/// passed indirectly (on the stack) would have the equivalent layout if it was
280/// expanded into separate arguments. If so, we prefer to do the latter to avoid
281/// inhibiting optimizations.
282///
283// FIXME: This predicate is missing many cases, currently it just follows
284// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
285// should probably make this smarter, or better yet make the LLVM backend
286// capable of handling it.
287static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
288 // We can only expand structure types.
289 const RecordType *RT = Ty->getAs<RecordType>();
290 if (!RT)
291 return false;
292
293 // We can only expand (C) structures.
294 //
295 // FIXME: This needs to be generalized to handle classes as well.
296 const RecordDecl *RD = RT->getDecl();
297 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
298 return false;
299
Eli Friedman506d4e32011-11-18 01:32:26 +0000300 uint64_t Size = 0;
301
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000302 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
303 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000304 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000305
306 if (!is32Or64BitBasicType(FD->getType(), Context))
307 return false;
308
309 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
310 // how to expand them yet, and the predicate for telling if a bitfield still
311 // counts as "basic" is more complicated than what we were doing previously.
312 if (FD->isBitField())
313 return false;
Eli Friedman506d4e32011-11-18 01:32:26 +0000314
315 Size += Context.getTypeSize(FD->getType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000316 }
317
Eli Friedman506d4e32011-11-18 01:32:26 +0000318 // Make sure there are not any holes in the struct.
319 if (Size != Context.getTypeSize(Ty))
320 return false;
321
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000322 return true;
323}
324
325namespace {
326/// DefaultABIInfo - The default implementation for ABI specific
327/// details. This implementation provides information which results in
328/// self-consistent and sensible LLVM IR generation, but does not
329/// conform to any particular ABI.
330class DefaultABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +0000331public:
332 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000333
Chris Lattnera3c109b2010-07-29 02:16:43 +0000334 ABIArgInfo classifyReturnType(QualType RetTy) const;
335 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000336
Chris Lattneree5dcd02010-07-29 02:31:05 +0000337 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000338 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000339 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
340 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000341 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000342 }
343
344 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
345 CodeGenFunction &CGF) const;
346};
347
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000348class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
349public:
Chris Lattnerea044322010-07-29 02:01:43 +0000350 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
351 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000352};
353
354llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
355 CodeGenFunction &CGF) const {
356 return 0;
357}
358
Chris Lattnera3c109b2010-07-29 02:16:43 +0000359ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
Jan Wen Voung90306932011-11-03 00:59:44 +0000360 if (isAggregateTypeForABI(Ty)) {
361 // Records with non trivial destructors/constructors should not be passed
362 // by value.
363 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
364 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
365
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000366 return ABIArgInfo::getIndirect(0);
Jan Wen Voung90306932011-11-03 00:59:44 +0000367 }
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000368
Chris Lattnera14db752010-03-11 18:19:55 +0000369 // Treat an enum type as its underlying type.
370 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
371 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000372
Chris Lattnera14db752010-03-11 18:19:55 +0000373 return (Ty->isPromotableIntegerType() ?
374 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000375}
376
Bob Wilson0024f942011-01-10 23:54:17 +0000377ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
378 if (RetTy->isVoidType())
379 return ABIArgInfo::getIgnore();
380
381 if (isAggregateTypeForABI(RetTy))
382 return ABIArgInfo::getIndirect(0);
383
384 // Treat an enum type as its underlying type.
385 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
386 RetTy = EnumTy->getDecl()->getIntegerType();
387
388 return (RetTy->isPromotableIntegerType() ?
389 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
390}
391
Derek Schuff9ed63f82012-09-06 17:37:28 +0000392//===----------------------------------------------------------------------===//
393// le32/PNaCl bitcode ABI Implementation
394//===----------------------------------------------------------------------===//
395
396class PNaClABIInfo : public ABIInfo {
397 public:
398 PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
399
400 ABIArgInfo classifyReturnType(QualType RetTy) const;
401 ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &FreeRegs) const;
402
403 virtual void computeInfo(CGFunctionInfo &FI) const;
404 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
405 CodeGenFunction &CGF) const;
406};
407
408class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
409 public:
410 PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
411 : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {}
412};
413
414void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
415 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
416
417 unsigned FreeRegs = FI.getHasRegParm() ? FI.getRegParm() : 0;
418
419 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
420 it != ie; ++it)
421 it->info = classifyArgumentType(it->type, FreeRegs);
422 }
423
424llvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
425 CodeGenFunction &CGF) const {
426 return 0;
427}
428
429ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty,
430 unsigned &FreeRegs) const {
431 if (isAggregateTypeForABI(Ty)) {
432 // Records with non trivial destructors/constructors should not be passed
433 // by value.
434 FreeRegs = 0;
435 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
436 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
437
438 return ABIArgInfo::getIndirect(0);
439 }
440
441 // Treat an enum type as its underlying type.
442 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
443 Ty = EnumTy->getDecl()->getIntegerType();
444
445 ABIArgInfo BaseInfo = (Ty->isPromotableIntegerType() ?
446 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
447
448 // Regparm regs hold 32 bits.
449 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
450 if (SizeInRegs == 0) return BaseInfo;
451 if (SizeInRegs > FreeRegs) {
452 FreeRegs = 0;
453 return BaseInfo;
454 }
455 FreeRegs -= SizeInRegs;
456 return BaseInfo.isDirect() ?
457 ABIArgInfo::getDirectInReg(BaseInfo.getCoerceToType()) :
458 ABIArgInfo::getExtendInReg(BaseInfo.getCoerceToType());
459}
460
461ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
462 if (RetTy->isVoidType())
463 return ABIArgInfo::getIgnore();
464
465 if (isAggregateTypeForABI(RetTy))
466 return ABIArgInfo::getIndirect(0);
467
468 // Treat an enum type as its underlying type.
469 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
470 RetTy = EnumTy->getDecl()->getIntegerType();
471
472 return (RetTy->isPromotableIntegerType() ?
473 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
474}
475
Eli Friedman55fc7e22012-01-25 22:46:34 +0000476/// UseX86_MMXType - Return true if this is an MMX type that should use the
477/// special x86_mmx type.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000478bool UseX86_MMXType(llvm::Type *IRType) {
Bill Wendlingbb465d72010-10-18 03:41:31 +0000479 // If the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>, use the
480 // special x86_mmx type.
481 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
482 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
483 IRType->getScalarSizeInBits() != 64;
484}
485
Jay Foadef6de3d2011-07-11 09:56:20 +0000486static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000487 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000488 llvm::Type* Ty) {
Bill Wendling0507be62011-03-07 22:47:14 +0000489 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy())
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000490 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
491 return Ty;
492}
493
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000494//===----------------------------------------------------------------------===//
495// X86-32 ABI Implementation
496//===----------------------------------------------------------------------===//
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000497
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000498/// X86_32ABIInfo - The X86-32 ABI information.
499class X86_32ABIInfo : public ABIInfo {
Rafael Espindolab48280b2012-07-31 02:44:24 +0000500 enum Class {
501 Integer,
502 Float
503 };
504
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000505 static const unsigned MinABIStackAlignInBytes = 4;
506
David Chisnall1e4249c2009-08-17 23:08:21 +0000507 bool IsDarwinVectorABI;
508 bool IsSmallStructInRegABI;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000509 bool IsMMXDisabled;
Eli Friedman55fc7e22012-01-25 22:46:34 +0000510 bool IsWin32FloatStructABI;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000511 unsigned DefaultNumRegisterParameters;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000512
513 static bool isRegisterSize(unsigned Size) {
514 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
515 }
516
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000517 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context,
518 unsigned callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000519
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000520 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
521 /// such that the argument will be passed in memory.
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000522 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal,
523 unsigned &FreeRegs) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000524
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000525 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbare59d8582010-09-16 20:42:06 +0000526 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000527
Rafael Espindolab48280b2012-07-31 02:44:24 +0000528 Class classify(QualType Ty) const;
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000529 ABIArgInfo classifyReturnType(QualType RetTy,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000530 unsigned callingConvention) const;
Rafael Espindolab6932692012-10-24 01:58:58 +0000531 ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &FreeRegs,
532 bool IsFastCall) const;
533 bool shouldUseInReg(QualType Ty, unsigned &FreeRegs,
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000534 bool IsFastCall, bool &NeedsPadding) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000535
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000536public:
537
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000538 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000539 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
540 CodeGenFunction &CGF) const;
541
Rafael Espindolab48280b2012-07-31 02:44:24 +0000542 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m, bool w,
543 unsigned r)
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000544 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
Rafael Espindolab48280b2012-07-31 02:44:24 +0000545 IsMMXDisabled(m), IsWin32FloatStructABI(w),
546 DefaultNumRegisterParameters(r) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000547};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000548
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000549class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
550public:
Eli Friedman55fc7e22012-01-25 22:46:34 +0000551 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
Rafael Espindolab48280b2012-07-31 02:44:24 +0000552 bool d, bool p, bool m, bool w, unsigned r)
553 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, m, w, r)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000554
555 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
556 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000557
558 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
559 // Darwin uses different dwarf register numbers for EH.
560 if (CGM.isTargetDarwin()) return 5;
561
562 return 4;
563 }
564
565 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
566 llvm::Value *Address) const;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000567
Jay Foadef6de3d2011-07-11 09:56:20 +0000568 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000569 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000570 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000571 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
572 }
573
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000574};
575
576}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000577
578/// shouldReturnTypeInRegister - Determine if the given type should be
579/// passed in a register (for the Darwin ABI).
580bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000581 ASTContext &Context,
582 unsigned callingConvention) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000583 uint64_t Size = Context.getTypeSize(Ty);
584
585 // Type must be register sized.
586 if (!isRegisterSize(Size))
587 return false;
588
589 if (Ty->isVectorType()) {
590 // 64- and 128- bit vectors inside structures are not returned in
591 // registers.
592 if (Size == 64 || Size == 128)
593 return false;
594
595 return true;
596 }
597
Daniel Dunbar77115232010-05-15 00:00:30 +0000598 // If this is a builtin, pointer, enum, complex type, member pointer, or
599 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000600 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000601 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000602 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000603 return true;
604
605 // Arrays are treated like records.
606 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000607 return shouldReturnTypeInRegister(AT->getElementType(), Context,
608 callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000609
610 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000611 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000612 if (!RT) return false;
613
Anders Carlssona8874232010-01-27 03:25:19 +0000614 // FIXME: Traverse bases here too.
615
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000616 // For thiscall conventions, structures will never be returned in
617 // a register. This is for compatibility with the MSVC ABI
618 if (callingConvention == llvm::CallingConv::X86_ThisCall &&
619 RT->isStructureType()) {
620 return false;
621 }
622
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000623 // Structure types are passed in register if all fields would be
624 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000625 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
626 e = RT->getDecl()->field_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000627 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000628
629 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000630 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000631 continue;
632
633 // Check fields recursively.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000634 if (!shouldReturnTypeInRegister(FD->getType(), Context,
635 callingConvention))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000636 return false;
637 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000638 return true;
639}
640
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000641ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
642 unsigned callingConvention) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000643 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000644 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000645
Chris Lattnera3c109b2010-07-29 02:16:43 +0000646 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000647 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000648 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000649 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000650
651 // 128-bit vectors are a special case; they are returned in
652 // registers and we need to make sure to pick a type the LLVM
653 // backend will like.
654 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +0000655 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000656 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000657
658 // Always return in register if it fits in a general purpose
659 // register, or if it is 64 bits and has a single element.
660 if ((Size == 8 || Size == 16 || Size == 32) ||
661 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +0000662 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000663 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000664
665 return ABIArgInfo::getIndirect(0);
666 }
667
668 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000669 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000670
John McCalld608cdb2010-08-22 10:59:02 +0000671 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000672 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000673 // Structures with either a non-trivial destructor or a non-trivial
674 // copy constructor are always indirect.
675 if (hasNonTrivialDestructorOrCopyConstructor(RT))
676 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000677
Anders Carlsson40092972009-10-20 22:07:59 +0000678 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000679 if (RT->getDecl()->hasFlexibleArrayMember())
680 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000681 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000682
David Chisnall1e4249c2009-08-17 23:08:21 +0000683 // If specified, structs and unions are always indirect.
684 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000685 return ABIArgInfo::getIndirect(0);
686
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000687 // Small structures which are register sized are generally returned
688 // in a register.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000689 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(),
690 callingConvention)) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000691 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000692
693 // As a special-case, if the struct is a "single-element" struct, and
694 // the field is of type "float" or "double", return it in a
Eli Friedman55fc7e22012-01-25 22:46:34 +0000695 // floating-point register. (MSVC does not apply this special case.)
696 // We apply a similar transformation for pointer types to improve the
697 // quality of the generated IR.
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000698 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
Eli Friedman55fc7e22012-01-25 22:46:34 +0000699 if ((!IsWin32FloatStructABI && SeltTy->isRealFloatingType())
700 || SeltTy->hasPointerRepresentation())
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000701 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
702
703 // FIXME: We should be able to narrow this integer in cases with dead
704 // padding.
Chris Lattner800588f2010-07-29 06:26:06 +0000705 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000706 }
707
708 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000709 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000710
Chris Lattnera3c109b2010-07-29 02:16:43 +0000711 // Treat an enum type as its underlying type.
712 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
713 RetTy = EnumTy->getDecl()->getIntegerType();
714
715 return (RetTy->isPromotableIntegerType() ?
716 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000717}
718
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000719static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
720 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
721}
722
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000723static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
724 const RecordType *RT = Ty->getAs<RecordType>();
725 if (!RT)
726 return 0;
727 const RecordDecl *RD = RT->getDecl();
728
729 // If this is a C++ record, check the bases first.
730 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
731 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
732 e = CXXRD->bases_end(); i != e; ++i)
733 if (!isRecordWithSSEVectorType(Context, i->getType()))
734 return false;
735
736 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
737 i != e; ++i) {
738 QualType FT = i->getType();
739
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000740 if (isSSEVectorType(Context, FT))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000741 return true;
742
743 if (isRecordWithSSEVectorType(Context, FT))
744 return true;
745 }
746
747 return false;
748}
749
Daniel Dunbare59d8582010-09-16 20:42:06 +0000750unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
751 unsigned Align) const {
752 // Otherwise, if the alignment is less than or equal to the minimum ABI
753 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000754 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +0000755 return 0; // Use default alignment.
756
757 // On non-Darwin, the stack type alignment is always 4.
758 if (!IsDarwinVectorABI) {
759 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000760 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +0000761 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000762
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000763 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000764 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
765 isRecordWithSSEVectorType(getContext(), Ty)))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000766 return 16;
767
768 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000769}
770
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000771ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
772 unsigned &FreeRegs) const {
773 if (!ByVal) {
774 if (FreeRegs) {
775 --FreeRegs; // Non byval indirects just use one pointer.
776 return ABIArgInfo::getIndirectInReg(0, false);
777 }
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000778 return ABIArgInfo::getIndirect(0, false);
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000779 }
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000780
Daniel Dunbare59d8582010-09-16 20:42:06 +0000781 // Compute the byval alignment.
782 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
783 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
784 if (StackAlign == 0)
Chris Lattnerde92d732011-05-22 23:35:00 +0000785 return ABIArgInfo::getIndirect(4);
Daniel Dunbare59d8582010-09-16 20:42:06 +0000786
787 // If the stack alignment is less than the type alignment, realign the
788 // argument.
789 if (StackAlign < TypeAlign)
790 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
791 /*Realign=*/true);
792
793 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000794}
795
Rafael Espindolab48280b2012-07-31 02:44:24 +0000796X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
797 const Type *T = isSingleElementStruct(Ty, getContext());
798 if (!T)
799 T = Ty.getTypePtr();
800
801 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
802 BuiltinType::Kind K = BT->getKind();
803 if (K == BuiltinType::Float || K == BuiltinType::Double)
804 return Float;
805 }
806 return Integer;
807}
808
Rafael Espindolab6932692012-10-24 01:58:58 +0000809bool X86_32ABIInfo::shouldUseInReg(QualType Ty, unsigned &FreeRegs,
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000810 bool IsFastCall, bool &NeedsPadding) const {
811 NeedsPadding = false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000812 Class C = classify(Ty);
813 if (C == Float)
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000814 return false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000815
Rafael Espindolab6932692012-10-24 01:58:58 +0000816 unsigned Size = getContext().getTypeSize(Ty);
817 unsigned SizeInRegs = (Size + 31) / 32;
Rafael Espindola5f14fcb2012-10-23 02:04:01 +0000818
819 if (SizeInRegs == 0)
820 return false;
821
Rafael Espindolab48280b2012-07-31 02:44:24 +0000822 if (SizeInRegs > FreeRegs) {
823 FreeRegs = 0;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000824 return false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000825 }
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000826
Rafael Espindolab48280b2012-07-31 02:44:24 +0000827 FreeRegs -= SizeInRegs;
Rafael Espindolab6932692012-10-24 01:58:58 +0000828
829 if (IsFastCall) {
830 if (Size > 32)
831 return false;
832
833 if (Ty->isIntegralOrEnumerationType())
834 return true;
835
836 if (Ty->isPointerType())
837 return true;
838
839 if (Ty->isReferenceType())
840 return true;
841
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000842 if (FreeRegs)
843 NeedsPadding = true;
844
Rafael Espindolab6932692012-10-24 01:58:58 +0000845 return false;
846 }
847
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000848 return true;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000849}
850
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000851ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Rafael Espindolab6932692012-10-24 01:58:58 +0000852 unsigned &FreeRegs,
853 bool IsFastCall) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000854 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000855 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000856 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000857 if (const RecordType *RT = Ty->getAs<RecordType>()) {
858 // Structures with either a non-trivial destructor or a non-trivial
859 // copy constructor are always indirect.
860 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000861 return getIndirectResult(Ty, false, FreeRegs);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000862
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000863 if (RT->getDecl()->hasFlexibleArrayMember())
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000864 return getIndirectResult(Ty, true, FreeRegs);
Anders Carlssona8874232010-01-27 03:25:19 +0000865 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000866
Eli Friedman5a4d3522011-11-18 00:28:11 +0000867 // Ignore empty structs/unions.
Eli Friedman5a1ac892011-11-18 04:01:36 +0000868 if (isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000869 return ABIArgInfo::getIgnore();
870
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000871 llvm::LLVMContext &LLVMContext = getVMContext();
872 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
873 bool NeedsPadding;
874 if (shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding)) {
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000875 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000876 SmallVector<llvm::Type*, 3> Elements;
877 for (unsigned I = 0; I < SizeInRegs; ++I)
878 Elements.push_back(Int32);
879 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
880 return ABIArgInfo::getDirectInReg(Result);
881 }
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000882 llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : 0;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000883
Daniel Dunbar53012f42009-11-09 01:33:53 +0000884 // Expand small (<= 128-bit) record types when we know that the stack layout
885 // of those arguments will match the struct. This is important because the
886 // LLVM backend isn't smart enough to remove byval, which inhibits many
887 // optimizations.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000888 if (getContext().getTypeSize(Ty) <= 4*32 &&
889 canExpandIndirectArgument(Ty, getContext()))
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000890 return ABIArgInfo::getExpandWithPadding(IsFastCall, PaddingType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000891
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000892 return getIndirectResult(Ty, true, FreeRegs);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000893 }
894
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000895 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +0000896 // On Darwin, some vectors are passed in memory, we handle this by passing
897 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000898 if (IsDarwinVectorABI) {
899 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000900 if ((Size == 8 || Size == 16 || Size == 32) ||
901 (Size == 64 && VT->getNumElements() == 1))
902 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
903 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000904 }
Bill Wendlingbb465d72010-10-18 03:41:31 +0000905
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000906 llvm::Type *IRType = CGT.ConvertType(Ty);
Bill Wendlingbb465d72010-10-18 03:41:31 +0000907 if (UseX86_MMXType(IRType)) {
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000908 if (IsMMXDisabled)
909 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
910 64));
Bill Wendlingbb465d72010-10-18 03:41:31 +0000911 ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
912 AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
913 return AAI;
914 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000915
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000916 return ABIArgInfo::getDirect();
917 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000918
919
Chris Lattnera3c109b2010-07-29 02:16:43 +0000920 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
921 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000922
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000923 bool NeedsPadding;
924 bool InReg = shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding);
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000925
926 if (Ty->isPromotableIntegerType()) {
927 if (InReg)
928 return ABIArgInfo::getExtendInReg();
929 return ABIArgInfo::getExtend();
930 }
931 if (InReg)
932 return ABIArgInfo::getDirectInReg();
933 return ABIArgInfo::getDirect();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000934}
935
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000936void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
937 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
938 FI.getCallingConvention());
Rafael Espindolab48280b2012-07-31 02:44:24 +0000939
Rafael Espindolab6932692012-10-24 01:58:58 +0000940 unsigned CC = FI.getCallingConvention();
941 bool IsFastCall = CC == llvm::CallingConv::X86_FastCall;
942 unsigned FreeRegs;
943 if (IsFastCall)
944 FreeRegs = 2;
945 else if (FI.getHasRegParm())
946 FreeRegs = FI.getRegParm();
947 else
948 FreeRegs = DefaultNumRegisterParameters;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000949
950 // If the return value is indirect, then the hidden argument is consuming one
951 // integer register.
952 if (FI.getReturnInfo().isIndirect() && FreeRegs) {
953 --FreeRegs;
954 ABIArgInfo &Old = FI.getReturnInfo();
955 Old = ABIArgInfo::getIndirectInReg(Old.getIndirectAlign(),
956 Old.getIndirectByVal(),
957 Old.getIndirectRealign());
958 }
959
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000960 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
961 it != ie; ++it)
Rafael Espindolab6932692012-10-24 01:58:58 +0000962 it->info = classifyArgumentType(it->type, FreeRegs, IsFastCall);
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000963}
964
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000965llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
966 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +0000967 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000968
969 CGBuilderTy &Builder = CGF.Builder;
970 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
971 "ap");
972 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Eli Friedman7b1fb812011-11-18 02:12:09 +0000973
974 // Compute if the address needs to be aligned
975 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
976 Align = getTypeStackAlignInBytes(Ty, Align);
977 Align = std::max(Align, 4U);
978 if (Align > 4) {
979 // addr = (addr + align - 1) & -align;
980 llvm::Value *Offset =
981 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
982 Addr = CGF.Builder.CreateGEP(Addr, Offset);
983 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
984 CGF.Int32Ty);
985 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
986 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
987 Addr->getType(),
988 "ap.cur.aligned");
989 }
990
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000991 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000992 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000993 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
994
995 uint64_t Offset =
Eli Friedman7b1fb812011-11-18 02:12:09 +0000996 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000997 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +0000998 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000999 "ap.next");
1000 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1001
1002 return AddrTyped;
1003}
1004
Charles Davis74f72932010-02-13 15:54:06 +00001005void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
1006 llvm::GlobalValue *GV,
1007 CodeGen::CodeGenModule &CGM) const {
1008 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1009 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
1010 // Get the LLVM function.
1011 llvm::Function *Fn = cast<llvm::Function>(GV);
1012
1013 // Now add the 'alignstack' attribute with a value of 16.
Bill Wendling0d583392012-10-15 20:36:26 +00001014 llvm::AttrBuilder B;
Bill Wendlinge91e9ec2012-10-14 03:28:14 +00001015 B.addStackAlignmentAttr(16);
Bill Wendling75d37b42012-10-15 07:31:59 +00001016 Fn->addAttribute(llvm::AttrListPtr::FunctionIndex,
1017 llvm::Attributes::get(CGM.getLLVMContext(), B));
Charles Davis74f72932010-02-13 15:54:06 +00001018 }
1019 }
1020}
1021
John McCall6374c332010-03-06 00:35:14 +00001022bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
1023 CodeGen::CodeGenFunction &CGF,
1024 llvm::Value *Address) const {
1025 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCall6374c332010-03-06 00:35:14 +00001026
Chris Lattner8b418682012-02-07 00:39:47 +00001027 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001028
John McCall6374c332010-03-06 00:35:14 +00001029 // 0-7 are the eight integer registers; the order is different
1030 // on Darwin (for EH), but the range is the same.
1031 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +00001032 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +00001033
1034 if (CGF.CGM.isTargetDarwin()) {
1035 // 12-16 are st(0..4). Not sure why we stop at 4.
1036 // These have size 16, which is sizeof(long double) on
1037 // platforms with 8-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001038 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCallaeeb7012010-05-27 06:19:26 +00001039 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001040
John McCall6374c332010-03-06 00:35:14 +00001041 } else {
1042 // 9 is %eflags, which doesn't get a size on Darwin for some
1043 // reason.
1044 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
1045
1046 // 11-16 are st(0..5). Not sure why we stop at 5.
1047 // These have size 12, which is sizeof(long double) on
1048 // platforms with 4-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001049 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCallaeeb7012010-05-27 06:19:26 +00001050 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1051 }
John McCall6374c332010-03-06 00:35:14 +00001052
1053 return false;
1054}
1055
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001056//===----------------------------------------------------------------------===//
1057// X86-64 ABI Implementation
1058//===----------------------------------------------------------------------===//
1059
1060
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001061namespace {
1062/// X86_64ABIInfo - The X86_64 ABI information.
1063class X86_64ABIInfo : public ABIInfo {
1064 enum Class {
1065 Integer = 0,
1066 SSE,
1067 SSEUp,
1068 X87,
1069 X87Up,
1070 ComplexX87,
1071 NoClass,
1072 Memory
1073 };
1074
1075 /// merge - Implement the X86_64 ABI merging algorithm.
1076 ///
1077 /// Merge an accumulating classification \arg Accum with a field
1078 /// classification \arg Field.
1079 ///
1080 /// \param Accum - The accumulating classification. This should
1081 /// always be either NoClass or the result of a previous merge
1082 /// call. In addition, this should never be Memory (the caller
1083 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001084 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001085
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001086 /// postMerge - Implement the X86_64 ABI post merging algorithm.
1087 ///
1088 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
1089 /// final MEMORY or SSE classes when necessary.
1090 ///
1091 /// \param AggregateSize - The size of the current aggregate in
1092 /// the classification process.
1093 ///
1094 /// \param Lo - The classification for the parts of the type
1095 /// residing in the low word of the containing object.
1096 ///
1097 /// \param Hi - The classification for the parts of the type
1098 /// residing in the higher words of the containing object.
1099 ///
1100 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
1101
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001102 /// classify - Determine the x86_64 register classes in which the
1103 /// given type T should be passed.
1104 ///
1105 /// \param Lo - The classification for the parts of the type
1106 /// residing in the low word of the containing object.
1107 ///
1108 /// \param Hi - The classification for the parts of the type
1109 /// residing in the high word of the containing object.
1110 ///
1111 /// \param OffsetBase - The bit offset of this type in the
1112 /// containing object. Some parameters are classified different
1113 /// depending on whether they straddle an eightbyte boundary.
1114 ///
1115 /// If a word is unused its result will be NoClass; if a type should
1116 /// be passed in Memory then at least the classification of \arg Lo
1117 /// will be Memory.
1118 ///
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001119 /// The \arg Lo class will be NoClass iff the argument is ignored.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001120 ///
1121 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
1122 /// also be ComplexX87.
Chris Lattner9c254f02010-06-29 06:01:59 +00001123 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001124
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001125 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001126 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
1127 unsigned IROffset, QualType SourceTy,
1128 unsigned SourceOffset) const;
1129 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
1130 unsigned IROffset, QualType SourceTy,
1131 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001132
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001133 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001134 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +00001135 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001136
1137 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001138 /// such that the argument will be passed in memory.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001139 ///
1140 /// \param freeIntRegs - The number of free integer registers remaining
1141 /// available.
1142 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001143
Chris Lattnera3c109b2010-07-29 02:16:43 +00001144 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001145
Bill Wendlingbb465d72010-10-18 03:41:31 +00001146 ABIArgInfo classifyArgumentType(QualType Ty,
Daniel Dunbaredfac032012-03-10 01:03:58 +00001147 unsigned freeIntRegs,
Bill Wendlingbb465d72010-10-18 03:41:31 +00001148 unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +00001149 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001150
Eli Friedmanee1ad992011-12-02 00:11:43 +00001151 bool IsIllegalVectorType(QualType Ty) const;
1152
John McCall67a57732011-04-21 01:20:55 +00001153 /// The 0.98 ABI revision clarified a lot of ambiguities,
1154 /// unfortunately in ways that were not always consistent with
1155 /// certain previous compilers. In particular, platforms which
1156 /// required strict binary compatibility with older versions of GCC
1157 /// may need to exempt themselves.
1158 bool honorsRevision0_98() const {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001159 return !getContext().getTargetInfo().getTriple().isOSDarwin();
John McCall67a57732011-04-21 01:20:55 +00001160 }
1161
Eli Friedmanee1ad992011-12-02 00:11:43 +00001162 bool HasAVX;
Derek Schuffbabaf312012-10-11 15:52:22 +00001163 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
1164 // 64-bit hardware.
1165 bool Has64BitPointers;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001166
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001167public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001168 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
Derek Schuffbabaf312012-10-11 15:52:22 +00001169 ABIInfo(CGT), HasAVX(hasavx),
Derek Schuff90da80c2012-10-11 18:21:13 +00001170 Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001171 }
Chris Lattner9c254f02010-06-29 06:01:59 +00001172
John McCallde5d3c72012-02-17 03:33:10 +00001173 bool isPassedUsingAVXType(QualType type) const {
1174 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00001175 // The freeIntRegs argument doesn't matter here.
1176 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE);
John McCallde5d3c72012-02-17 03:33:10 +00001177 if (info.isDirect()) {
1178 llvm::Type *ty = info.getCoerceToType();
1179 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
1180 return (vectorTy->getBitWidth() > 128);
1181 }
1182 return false;
1183 }
1184
Chris Lattneree5dcd02010-07-29 02:31:05 +00001185 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001186
1187 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1188 CodeGenFunction &CGF) const;
1189};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001190
Chris Lattnerf13721d2010-08-31 16:44:54 +00001191/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001192class WinX86_64ABIInfo : public ABIInfo {
1193
1194 ABIArgInfo classify(QualType Ty) const;
1195
Chris Lattnerf13721d2010-08-31 16:44:54 +00001196public:
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001197 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
1198
1199 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattnerf13721d2010-08-31 16:44:54 +00001200
1201 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1202 CodeGenFunction &CGF) const;
1203};
1204
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001205class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1206public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001207 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
Derek Schuffbabaf312012-10-11 15:52:22 +00001208 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
John McCall6374c332010-03-06 00:35:14 +00001209
John McCallde5d3c72012-02-17 03:33:10 +00001210 const X86_64ABIInfo &getABIInfo() const {
1211 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
1212 }
1213
John McCall6374c332010-03-06 00:35:14 +00001214 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1215 return 7;
1216 }
1217
1218 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1219 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001220 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001221
John McCallaeeb7012010-05-27 06:19:26 +00001222 // 0-15 are the 16 integer registers.
1223 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001224 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +00001225 return false;
1226 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001227
Jay Foadef6de3d2011-07-11 09:56:20 +00001228 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001229 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +00001230 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001231 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1232 }
1233
John McCallde5d3c72012-02-17 03:33:10 +00001234 bool isNoProtoCallVariadic(const CallArgList &args,
1235 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +00001236 // The default CC on x86-64 sets %al to the number of SSA
1237 // registers used, and GCC sets this when calling an unprototyped
Eli Friedman3ed79032011-12-01 04:53:19 +00001238 // function, so we override the default behavior. However, don't do
Eli Friedman68805fe2011-12-06 03:08:26 +00001239 // that when AVX types are involved: the ABI explicitly states it is
1240 // undefined, and it doesn't work in practice because of how the ABI
1241 // defines varargs anyway.
John McCallde5d3c72012-02-17 03:33:10 +00001242 if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) {
Eli Friedman3ed79032011-12-01 04:53:19 +00001243 bool HasAVXType = false;
John McCallde5d3c72012-02-17 03:33:10 +00001244 for (CallArgList::const_iterator
1245 it = args.begin(), ie = args.end(); it != ie; ++it) {
1246 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1247 HasAVXType = true;
1248 break;
Eli Friedman3ed79032011-12-01 04:53:19 +00001249 }
1250 }
John McCallde5d3c72012-02-17 03:33:10 +00001251
Eli Friedman3ed79032011-12-01 04:53:19 +00001252 if (!HasAVXType)
1253 return true;
1254 }
John McCall01f151e2011-09-21 08:08:30 +00001255
John McCallde5d3c72012-02-17 03:33:10 +00001256 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
John McCall01f151e2011-09-21 08:08:30 +00001257 }
1258
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001259};
1260
Chris Lattnerf13721d2010-08-31 16:44:54 +00001261class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1262public:
1263 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1264 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1265
1266 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1267 return 7;
1268 }
1269
1270 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1271 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001272 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001273
Chris Lattnerf13721d2010-08-31 16:44:54 +00001274 // 0-15 are the 16 integer registers.
1275 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001276 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattnerf13721d2010-08-31 16:44:54 +00001277 return false;
1278 }
1279};
1280
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001281}
1282
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001283void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1284 Class &Hi) const {
1285 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1286 //
1287 // (a) If one of the classes is Memory, the whole argument is passed in
1288 // memory.
1289 //
1290 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1291 // memory.
1292 //
1293 // (c) If the size of the aggregate exceeds two eightbytes and the first
1294 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1295 // argument is passed in memory. NOTE: This is necessary to keep the
1296 // ABI working for processors that don't support the __m256 type.
1297 //
1298 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1299 //
1300 // Some of these are enforced by the merging logic. Others can arise
1301 // only with unions; for example:
1302 // union { _Complex double; unsigned; }
1303 //
1304 // Note that clauses (b) and (c) were added in 0.98.
1305 //
1306 if (Hi == Memory)
1307 Lo = Memory;
1308 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1309 Lo = Memory;
1310 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1311 Lo = Memory;
1312 if (Hi == SSEUp && Lo != SSE)
1313 Hi = SSE;
1314}
1315
Chris Lattner1090a9b2010-06-28 21:43:59 +00001316X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001317 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1318 // classified recursively so that always two fields are
1319 // considered. The resulting class is calculated according to
1320 // the classes of the fields in the eightbyte:
1321 //
1322 // (a) If both classes are equal, this is the resulting class.
1323 //
1324 // (b) If one of the classes is NO_CLASS, the resulting class is
1325 // the other class.
1326 //
1327 // (c) If one of the classes is MEMORY, the result is the MEMORY
1328 // class.
1329 //
1330 // (d) If one of the classes is INTEGER, the result is the
1331 // INTEGER.
1332 //
1333 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1334 // MEMORY is used as class.
1335 //
1336 // (f) Otherwise class SSE is used.
1337
1338 // Accum should never be memory (we should have returned) or
1339 // ComplexX87 (because this cannot be passed in a structure).
1340 assert((Accum != Memory && Accum != ComplexX87) &&
1341 "Invalid accumulated classification during merge.");
1342 if (Accum == Field || Field == NoClass)
1343 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001344 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001345 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001346 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001347 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001348 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001349 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001350 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1351 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001352 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001353 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001354}
1355
Chris Lattnerbcaedae2010-06-30 19:14:05 +00001356void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001357 Class &Lo, Class &Hi) const {
1358 // FIXME: This code can be simplified by introducing a simple value class for
1359 // Class pairs with appropriate constructor methods for the various
1360 // situations.
1361
1362 // FIXME: Some of the split computations are wrong; unaligned vectors
1363 // shouldn't be passed in registers for example, so there is no chance they
1364 // can straddle an eightbyte. Verify & simplify.
1365
1366 Lo = Hi = NoClass;
1367
1368 Class &Current = OffsetBase < 64 ? Lo : Hi;
1369 Current = Memory;
1370
John McCall183700f2009-09-21 23:43:11 +00001371 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001372 BuiltinType::Kind k = BT->getKind();
1373
1374 if (k == BuiltinType::Void) {
1375 Current = NoClass;
1376 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1377 Lo = Integer;
1378 Hi = Integer;
1379 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1380 Current = Integer;
Derek Schuff7da46f92012-10-11 16:55:58 +00001381 } else if ((k == BuiltinType::Float || k == BuiltinType::Double) ||
1382 (k == BuiltinType::LongDouble &&
1383 getContext().getTargetInfo().getTriple().getOS() ==
1384 llvm::Triple::NativeClient)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001385 Current = SSE;
1386 } else if (k == BuiltinType::LongDouble) {
1387 Lo = X87;
1388 Hi = X87Up;
1389 }
1390 // FIXME: _Decimal32 and _Decimal64 are SSE.
1391 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001392 return;
1393 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001394
Chris Lattner1090a9b2010-06-28 21:43:59 +00001395 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001396 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +00001397 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001398 return;
1399 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001400
Chris Lattner1090a9b2010-06-28 21:43:59 +00001401 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001402 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001403 return;
1404 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001405
Chris Lattner1090a9b2010-06-28 21:43:59 +00001406 if (Ty->isMemberPointerType()) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001407 if (Ty->isMemberFunctionPointerType() && Has64BitPointers)
Daniel Dunbar67d438d2010-05-15 00:00:37 +00001408 Lo = Hi = Integer;
1409 else
1410 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001411 return;
1412 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001413
Chris Lattner1090a9b2010-06-28 21:43:59 +00001414 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001415 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001416 if (Size == 32) {
1417 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1418 // float> as integer.
1419 Current = Integer;
1420
1421 // If this type crosses an eightbyte boundary, it should be
1422 // split.
1423 uint64_t EB_Real = (OffsetBase) / 64;
1424 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1425 if (EB_Real != EB_Imag)
1426 Hi = Lo;
1427 } else if (Size == 64) {
1428 // gcc passes <1 x double> in memory. :(
1429 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1430 return;
1431
1432 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001433 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001434 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1435 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1436 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001437 Current = Integer;
1438 else
1439 Current = SSE;
1440
1441 // If this type crosses an eightbyte boundary, it should be
1442 // split.
1443 if (OffsetBase && OffsetBase != 64)
1444 Hi = Lo;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001445 } else if (Size == 128 || (HasAVX && Size == 256)) {
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001446 // Arguments of 256-bits are split into four eightbyte chunks. The
1447 // least significant one belongs to class SSE and all the others to class
1448 // SSEUP. The original Lo and Hi design considers that types can't be
1449 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1450 // This design isn't correct for 256-bits, but since there're no cases
1451 // where the upper parts would need to be inspected, avoid adding
1452 // complexity and just consider Hi to match the 64-256 part.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001453 Lo = SSE;
1454 Hi = SSEUp;
1455 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001456 return;
1457 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001458
Chris Lattner1090a9b2010-06-28 21:43:59 +00001459 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001460 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001461
Chris Lattnerea044322010-07-29 02:01:43 +00001462 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001463 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001464 if (Size <= 64)
1465 Current = Integer;
1466 else if (Size <= 128)
1467 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001468 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001469 Current = SSE;
Derek Schuff7da46f92012-10-11 16:55:58 +00001470 else if (ET == getContext().DoubleTy ||
1471 (ET == getContext().LongDoubleTy &&
1472 getContext().getTargetInfo().getTriple().getOS() ==
1473 llvm::Triple::NativeClient))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001474 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001475 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001476 Current = ComplexX87;
1477
1478 // If this complex type crosses an eightbyte boundary then it
1479 // should be split.
1480 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001481 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001482 if (Hi == NoClass && EB_Real != EB_Imag)
1483 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001484
Chris Lattner1090a9b2010-06-28 21:43:59 +00001485 return;
1486 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001487
Chris Lattnerea044322010-07-29 02:01:43 +00001488 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001489 // Arrays are treated like structures.
1490
Chris Lattnerea044322010-07-29 02:01:43 +00001491 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001492
1493 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001494 // than four eightbytes, ..., it has class MEMORY.
1495 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001496 return;
1497
1498 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1499 // fields, it has class MEMORY.
1500 //
1501 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001502 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001503 return;
1504
1505 // Otherwise implement simplified merge. We could be smarter about
1506 // this, but it isn't worth it and would be harder to verify.
1507 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001508 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001509 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes089d8922011-07-12 01:27:38 +00001510
1511 // The only case a 256-bit wide vector could be used is when the array
1512 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1513 // to work for sizes wider than 128, early check and fallback to memory.
1514 if (Size > 128 && EltSize != 256)
1515 return;
1516
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001517 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1518 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001519 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001520 Lo = merge(Lo, FieldLo);
1521 Hi = merge(Hi, FieldHi);
1522 if (Lo == Memory || Hi == Memory)
1523 break;
1524 }
1525
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001526 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001527 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001528 return;
1529 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001530
Chris Lattner1090a9b2010-06-28 21:43:59 +00001531 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001532 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001533
1534 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001535 // than four eightbytes, ..., it has class MEMORY.
1536 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001537 return;
1538
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001539 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1540 // copy constructor or a non-trivial destructor, it is passed by invisible
1541 // reference.
1542 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1543 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001544
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001545 const RecordDecl *RD = RT->getDecl();
1546
1547 // Assume variable sized types are passed in memory.
1548 if (RD->hasFlexibleArrayMember())
1549 return;
1550
Chris Lattnerea044322010-07-29 02:01:43 +00001551 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001552
1553 // Reset Lo class, this will be recomputed.
1554 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001555
1556 // If this is a C++ record, classify the bases first.
1557 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1558 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1559 e = CXXRD->bases_end(); i != e; ++i) {
1560 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1561 "Unexpected base class!");
1562 const CXXRecordDecl *Base =
1563 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1564
1565 // Classify this field.
1566 //
1567 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1568 // single eightbyte, each is classified separately. Each eightbyte gets
1569 // initialized to class NO_CLASS.
1570 Class FieldLo, FieldHi;
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001571 uint64_t Offset =
1572 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
Chris Lattner9c254f02010-06-29 06:01:59 +00001573 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001574 Lo = merge(Lo, FieldLo);
1575 Hi = merge(Hi, FieldHi);
1576 if (Lo == Memory || Hi == Memory)
1577 break;
1578 }
1579 }
1580
1581 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001582 unsigned idx = 0;
Bruno Cardoso Lopes548e4782011-07-12 22:30:58 +00001583 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001584 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001585 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1586 bool BitField = i->isBitField();
1587
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001588 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1589 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001590 //
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001591 // The only case a 256-bit wide vector could be used is when the struct
1592 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1593 // to work for sizes wider than 128, early check and fallback to memory.
1594 //
1595 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1596 Lo = Memory;
1597 return;
1598 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001599 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001600 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001601 Lo = Memory;
1602 return;
1603 }
1604
1605 // Classify this field.
1606 //
1607 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1608 // exceeds a single eightbyte, each is classified
1609 // separately. Each eightbyte gets initialized to class
1610 // NO_CLASS.
1611 Class FieldLo, FieldHi;
1612
1613 // Bit-fields require special handling, they do not force the
1614 // structure to be passed in memory even if unaligned, and
1615 // therefore they can straddle an eightbyte.
1616 if (BitField) {
1617 // Ignore padding bit-fields.
1618 if (i->isUnnamedBitfield())
1619 continue;
1620
1621 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001622 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001623
1624 uint64_t EB_Lo = Offset / 64;
1625 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1626 FieldLo = FieldHi = NoClass;
1627 if (EB_Lo) {
1628 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1629 FieldLo = NoClass;
1630 FieldHi = Integer;
1631 } else {
1632 FieldLo = Integer;
1633 FieldHi = EB_Hi ? Integer : NoClass;
1634 }
1635 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001636 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001637 Lo = merge(Lo, FieldLo);
1638 Hi = merge(Hi, FieldHi);
1639 if (Lo == Memory || Hi == Memory)
1640 break;
1641 }
1642
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001643 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001644 }
1645}
1646
Chris Lattner9c254f02010-06-29 06:01:59 +00001647ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001648 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1649 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001650 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001651 // Treat an enum type as its underlying type.
1652 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1653 Ty = EnumTy->getDecl()->getIntegerType();
1654
1655 return (Ty->isPromotableIntegerType() ?
1656 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1657 }
1658
1659 return ABIArgInfo::getIndirect(0);
1660}
1661
Eli Friedmanee1ad992011-12-02 00:11:43 +00001662bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1663 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1664 uint64_t Size = getContext().getTypeSize(VecTy);
1665 unsigned LargestVector = HasAVX ? 256 : 128;
1666 if (Size <= 64 || Size > LargestVector)
1667 return true;
1668 }
1669
1670 return false;
1671}
1672
Daniel Dunbaredfac032012-03-10 01:03:58 +00001673ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1674 unsigned freeIntRegs) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001675 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1676 // place naturally.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001677 //
1678 // This assumption is optimistic, as there could be free registers available
1679 // when we need to pass this argument in memory, and LLVM could try to pass
1680 // the argument in the free register. This does not seem to happen currently,
1681 // but this code would be much safer if we could mark the argument with
1682 // 'onstack'. See PR12193.
Eli Friedmanee1ad992011-12-02 00:11:43 +00001683 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001684 // Treat an enum type as its underlying type.
1685 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1686 Ty = EnumTy->getDecl()->getIntegerType();
1687
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001688 return (Ty->isPromotableIntegerType() ?
1689 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001690 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001691
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001692 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1693 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001694
Chris Lattner855d2272011-05-22 23:21:23 +00001695 // Compute the byval alignment. We specify the alignment of the byval in all
1696 // cases so that the mid-level optimizer knows the alignment of the byval.
1697 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
Daniel Dunbaredfac032012-03-10 01:03:58 +00001698
1699 // Attempt to avoid passing indirect results using byval when possible. This
1700 // is important for good codegen.
1701 //
1702 // We do this by coercing the value into a scalar type which the backend can
1703 // handle naturally (i.e., without using byval).
1704 //
1705 // For simplicity, we currently only do this when we have exhausted all of the
1706 // free integer registers. Doing this when there are free integer registers
1707 // would require more care, as we would have to ensure that the coerced value
1708 // did not claim the unused register. That would require either reording the
1709 // arguments to the function (so that any subsequent inreg values came first),
1710 // or only doing this optimization when there were no following arguments that
1711 // might be inreg.
1712 //
1713 // We currently expect it to be rare (particularly in well written code) for
1714 // arguments to be passed on the stack when there are still free integer
1715 // registers available (this would typically imply large structs being passed
1716 // by value), so this seems like a fair tradeoff for now.
1717 //
1718 // We can revisit this if the backend grows support for 'onstack' parameter
1719 // attributes. See PR12193.
1720 if (freeIntRegs == 0) {
1721 uint64_t Size = getContext().getTypeSize(Ty);
1722
1723 // If this type fits in an eightbyte, coerce it into the matching integral
1724 // type, which will end up on the stack (with alignment 8).
1725 if (Align == 8 && Size <= 64)
1726 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1727 Size));
1728 }
1729
Chris Lattner855d2272011-05-22 23:21:23 +00001730 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001731}
1732
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001733/// GetByteVectorType - The ABI specifies that a value should be passed in an
1734/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner0f408f52010-07-29 04:56:46 +00001735/// vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001736llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001737 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001738
Chris Lattner15842bd2010-07-29 05:02:29 +00001739 // Wrapper structs that just contain vectors are passed just like vectors,
1740 // strip them off if present.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001741 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner15842bd2010-07-29 05:02:29 +00001742 while (STy && STy->getNumElements() == 1) {
1743 IRType = STy->getElementType(0);
1744 STy = dyn_cast<llvm::StructType>(IRType);
1745 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001746
Bruno Cardoso Lopes528a8c72011-07-08 22:57:35 +00001747 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001748 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1749 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001750 unsigned BitWidth = VT->getBitWidth();
Tanya Lattnerce275672011-11-28 23:18:11 +00001751 if ((BitWidth >= 128 && BitWidth <= 256) &&
Chris Lattner0f408f52010-07-29 04:56:46 +00001752 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1753 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1754 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1755 EltTy->isIntegerTy(128)))
1756 return VT;
1757 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001758
Chris Lattner0f408f52010-07-29 04:56:46 +00001759 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1760}
1761
Chris Lattnere2962be2010-07-29 07:30:00 +00001762/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1763/// is known to either be off the end of the specified type or being in
1764/// alignment padding. The user type specified is known to be at most 128 bits
1765/// in size, and have passed through X86_64ABIInfo::classify with a successful
1766/// classification that put one of the two halves in the INTEGER class.
1767///
1768/// It is conservatively correct to return false.
1769static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1770 unsigned EndBit, ASTContext &Context) {
1771 // If the bytes being queried are off the end of the type, there is no user
1772 // data hiding here. This handles analysis of builtins, vectors and other
1773 // types that don't contain interesting padding.
1774 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1775 if (TySize <= StartBit)
1776 return true;
1777
Chris Lattner021c3a32010-07-29 07:43:55 +00001778 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1779 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1780 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1781
1782 // Check each element to see if the element overlaps with the queried range.
1783 for (unsigned i = 0; i != NumElts; ++i) {
1784 // If the element is after the span we care about, then we're done..
1785 unsigned EltOffset = i*EltSize;
1786 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001787
Chris Lattner021c3a32010-07-29 07:43:55 +00001788 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1789 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1790 EndBit-EltOffset, Context))
1791 return false;
1792 }
1793 // If it overlaps no elements, then it is safe to process as padding.
1794 return true;
1795 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001796
Chris Lattnere2962be2010-07-29 07:30:00 +00001797 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1798 const RecordDecl *RD = RT->getDecl();
1799 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001800
Chris Lattnere2962be2010-07-29 07:30:00 +00001801 // If this is a C++ record, check the bases first.
1802 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1803 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1804 e = CXXRD->bases_end(); i != e; ++i) {
1805 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1806 "Unexpected base class!");
1807 const CXXRecordDecl *Base =
1808 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001809
Chris Lattnere2962be2010-07-29 07:30:00 +00001810 // If the base is after the span we care about, ignore it.
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001811 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
Chris Lattnere2962be2010-07-29 07:30:00 +00001812 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001813
Chris Lattnere2962be2010-07-29 07:30:00 +00001814 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1815 if (!BitsContainNoUserData(i->getType(), BaseStart,
1816 EndBit-BaseOffset, Context))
1817 return false;
1818 }
1819 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001820
Chris Lattnere2962be2010-07-29 07:30:00 +00001821 // Verify that no field has data that overlaps the region of interest. Yes
1822 // this could be sped up a lot by being smarter about queried fields,
1823 // however we're only looking at structs up to 16 bytes, so we don't care
1824 // much.
1825 unsigned idx = 0;
1826 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1827 i != e; ++i, ++idx) {
1828 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001829
Chris Lattnere2962be2010-07-29 07:30:00 +00001830 // If we found a field after the region we care about, then we're done.
1831 if (FieldOffset >= EndBit) break;
1832
1833 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1834 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1835 Context))
1836 return false;
1837 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001838
Chris Lattnere2962be2010-07-29 07:30:00 +00001839 // If nothing in this record overlapped the area of interest, then we're
1840 // clean.
1841 return true;
1842 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001843
Chris Lattnere2962be2010-07-29 07:30:00 +00001844 return false;
1845}
1846
Chris Lattner0b362002010-07-29 18:39:32 +00001847/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1848/// float member at the specified offset. For example, {int,{float}} has a
1849/// float at offset 4. It is conservatively correct for this routine to return
1850/// false.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001851static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Micah Villmow25a6a842012-10-08 16:25:52 +00001852 const llvm::DataLayout &TD) {
Chris Lattner0b362002010-07-29 18:39:32 +00001853 // Base case if we find a float.
1854 if (IROffset == 0 && IRType->isFloatTy())
1855 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001856
Chris Lattner0b362002010-07-29 18:39:32 +00001857 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001858 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner0b362002010-07-29 18:39:32 +00001859 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1860 unsigned Elt = SL->getElementContainingOffset(IROffset);
1861 IROffset -= SL->getElementOffset(Elt);
1862 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1863 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001864
Chris Lattner0b362002010-07-29 18:39:32 +00001865 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001866 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1867 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner0b362002010-07-29 18:39:32 +00001868 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1869 IROffset -= IROffset/EltSize*EltSize;
1870 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1871 }
1872
1873 return false;
1874}
1875
Chris Lattnerf47c9442010-07-29 18:13:09 +00001876
1877/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1878/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001879llvm::Type *X86_64ABIInfo::
1880GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00001881 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001882 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001883 // pass as float if the last 4 bytes is just padding. This happens for
1884 // structs that contain 3 floats.
1885 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1886 SourceOffset*8+64, getContext()))
1887 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001888
Chris Lattner0b362002010-07-29 18:39:32 +00001889 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1890 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1891 // case.
Micah Villmow25a6a842012-10-08 16:25:52 +00001892 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
1893 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001894 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001895
Chris Lattnerf47c9442010-07-29 18:13:09 +00001896 return llvm::Type::getDoubleTy(getVMContext());
1897}
1898
1899
Chris Lattner0d2656d2010-07-29 17:40:35 +00001900/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1901/// an 8-byte GPR. This means that we either have a scalar or we are talking
1902/// about the high or low part of an up-to-16-byte struct. This routine picks
1903/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001904/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1905/// etc).
1906///
1907/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1908/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1909/// the 8-byte value references. PrefType may be null.
1910///
1911/// SourceTy is the source level type for the entire argument. SourceOffset is
1912/// an offset into this that we're processing (which is always either 0 or 8).
1913///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001914llvm::Type *X86_64ABIInfo::
1915GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00001916 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001917 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1918 // returning an 8-byte unit starting with it. See if we can safely use it.
1919 if (IROffset == 0) {
1920 // Pointers and int64's always fill the 8-byte unit.
Derek Schuffbabaf312012-10-11 15:52:22 +00001921 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
1922 IRType->isIntegerTy(64))
Chris Lattnere2962be2010-07-29 07:30:00 +00001923 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001924
Chris Lattnere2962be2010-07-29 07:30:00 +00001925 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1926 // goodness in the source type is just tail padding. This is allowed to
1927 // kick in for struct {double,int} on the int, but not on
1928 // struct{double,int,int} because we wouldn't return the second int. We
1929 // have to do this analysis on the source type because we can't depend on
1930 // unions being lowered a specific way etc.
1931 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
Derek Schuffbabaf312012-10-11 15:52:22 +00001932 IRType->isIntegerTy(32) ||
1933 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
1934 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
1935 cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001936
Chris Lattnere2962be2010-07-29 07:30:00 +00001937 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1938 SourceOffset*8+64, getContext()))
1939 return IRType;
1940 }
1941 }
Chris Lattner49382de2010-07-28 22:44:07 +00001942
Chris Lattner2acc6e32011-07-18 04:24:23 +00001943 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001944 // If this is a struct, recurse into the field at the specified offset.
Micah Villmow25a6a842012-10-08 16:25:52 +00001945 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001946 if (IROffset < SL->getSizeInBytes()) {
1947 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1948 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001949
Chris Lattner0d2656d2010-07-29 17:40:35 +00001950 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1951 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001952 }
Chris Lattner49382de2010-07-28 22:44:07 +00001953 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001954
Chris Lattner2acc6e32011-07-18 04:24:23 +00001955 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001956 llvm::Type *EltTy = ATy->getElementType();
Micah Villmow25a6a842012-10-08 16:25:52 +00001957 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
Chris Lattner021c3a32010-07-29 07:43:55 +00001958 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00001959 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1960 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00001961 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001962
Chris Lattner49382de2010-07-28 22:44:07 +00001963 // Okay, we don't have any better idea of what to pass, so we pass this in an
1964 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001965 unsigned TySizeInBytes =
1966 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00001967
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001968 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001969
Chris Lattner49382de2010-07-28 22:44:07 +00001970 // It is always safe to classify this as an integer type up to i64 that
1971 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001972 return llvm::IntegerType::get(getVMContext(),
1973 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00001974}
1975
Chris Lattner66e7b682010-09-01 00:50:20 +00001976
1977/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1978/// be used as elements of a two register pair to pass or return, return a
1979/// first class aggregate to represent them. For example, if the low part of
1980/// a by-value argument should be passed as i32* and the high part as float,
1981/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001982static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00001983GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Micah Villmow25a6a842012-10-08 16:25:52 +00001984 const llvm::DataLayout &TD) {
Chris Lattner66e7b682010-09-01 00:50:20 +00001985 // In order to correctly satisfy the ABI, we need to the high part to start
1986 // at offset 8. If the high and low parts we inferred are both 4-byte types
1987 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1988 // the second element at offset 8. Check for this:
1989 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1990 unsigned HiAlign = TD.getABITypeAlignment(Hi);
Micah Villmow25a6a842012-10-08 16:25:52 +00001991 unsigned HiStart = llvm::DataLayout::RoundUpAlignment(LoSize, HiAlign);
Chris Lattner66e7b682010-09-01 00:50:20 +00001992 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001993
Chris Lattner66e7b682010-09-01 00:50:20 +00001994 // To handle this, we have to increase the size of the low part so that the
1995 // second element will start at an 8 byte offset. We can't increase the size
1996 // of the second element because it might make us access off the end of the
1997 // struct.
1998 if (HiStart != 8) {
1999 // There are only two sorts of types the ABI generation code can produce for
2000 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
2001 // Promote these to a larger type.
2002 if (Lo->isFloatTy())
2003 Lo = llvm::Type::getDoubleTy(Lo->getContext());
2004 else {
2005 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
2006 Lo = llvm::Type::getInt64Ty(Lo->getContext());
2007 }
2008 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002009
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002010 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002011
2012
Chris Lattner66e7b682010-09-01 00:50:20 +00002013 // Verify that the second element is at an 8-byte offset.
2014 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
2015 "Invalid x86-64 argument pair!");
2016 return Result;
2017}
2018
Chris Lattner519f68c2010-07-28 23:06:14 +00002019ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00002020classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00002021 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
2022 // classification algorithm.
2023 X86_64ABIInfo::Class Lo, Hi;
2024 classify(RetTy, 0, Lo, Hi);
2025
2026 // Check some invariants.
2027 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002028 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2029
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002030 llvm::Type *ResType = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002031 switch (Lo) {
2032 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002033 if (Hi == NoClass)
2034 return ABIArgInfo::getIgnore();
2035 // If the low part is just padding, it takes no register, leave ResType
2036 // null.
2037 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2038 "Unknown missing lo part");
2039 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002040
2041 case SSEUp:
2042 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002043 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002044
2045 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
2046 // hidden argument.
2047 case Memory:
2048 return getIndirectReturnResult(RetTy);
2049
2050 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
2051 // available register of the sequence %rax, %rdx is used.
2052 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002053 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002054
Chris Lattnereb518b42010-07-29 21:42:50 +00002055 // If we have a sign or zero extended integer, make sure to return Extend
2056 // so that the parameter gets the right LLVM IR attributes.
2057 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2058 // Treat an enum type as its underlying type.
2059 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2060 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002061
Chris Lattnereb518b42010-07-29 21:42:50 +00002062 if (RetTy->isIntegralOrEnumerationType() &&
2063 RetTy->isPromotableIntegerType())
2064 return ABIArgInfo::getExtend();
2065 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002066 break;
2067
2068 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
2069 // available SSE register of the sequence %xmm0, %xmm1 is used.
2070 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002071 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00002072 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002073
2074 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
2075 // returned on the X87 stack in %st0 as 80-bit x87 number.
2076 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00002077 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00002078 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002079
2080 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
2081 // part of the value is returned in %st0 and the imaginary part in
2082 // %st1.
2083 case ComplexX87:
2084 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00002085 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00002086 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00002087 NULL);
2088 break;
2089 }
2090
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002091 llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002092 switch (Hi) {
2093 // Memory was handled previously and X87 should
2094 // never occur as a hi class.
2095 case Memory:
2096 case X87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002097 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002098
2099 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00002100 case NoClass:
2101 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002102
Chris Lattner3db4dde2010-09-01 00:20:33 +00002103 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002104 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002105 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2106 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002107 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00002108 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002109 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002110 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2111 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002112 break;
2113
2114 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002115 // is passed in the next available eightbyte chunk if the last used
2116 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00002117 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002118 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00002119 case SSEUp:
2120 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002121 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00002122 break;
2123
2124 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
2125 // returned together with the previous X87 value in %st0.
2126 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002127 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00002128 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002129 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00002130 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00002131 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002132 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002133 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2134 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00002135 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002136 break;
2137 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002138
Chris Lattner3db4dde2010-09-01 00:20:33 +00002139 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00002140 // known to pass in the high eightbyte of the result. We do this by forming a
2141 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00002142 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002143 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Chris Lattner519f68c2010-07-28 23:06:14 +00002144
Chris Lattnereb518b42010-07-29 21:42:50 +00002145 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00002146}
2147
Daniel Dunbaredfac032012-03-10 01:03:58 +00002148ABIArgInfo X86_64ABIInfo::classifyArgumentType(
2149 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE)
2150 const
2151{
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002152 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00002153 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002154
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002155 // Check some invariants.
2156 // FIXME: Enforce these by construction.
2157 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002158 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2159
2160 neededInt = 0;
2161 neededSSE = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002162 llvm::Type *ResType = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002163 switch (Lo) {
2164 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002165 if (Hi == NoClass)
2166 return ABIArgInfo::getIgnore();
2167 // If the low part is just padding, it takes no register, leave ResType
2168 // null.
2169 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2170 "Unknown missing lo part");
2171 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002172
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002173 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
2174 // on the stack.
2175 case Memory:
2176
2177 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
2178 // COMPLEX_X87, it is passed in memory.
2179 case X87:
2180 case ComplexX87:
Eli Friedmanded137f2011-06-29 07:04:55 +00002181 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2182 ++neededInt;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002183 return getIndirectResult(Ty, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002184
2185 case SSEUp:
2186 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002187 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002188
2189 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
2190 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
2191 // and %r9 is used.
2192 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00002193 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002194
Chris Lattner49382de2010-07-28 22:44:07 +00002195 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002196 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00002197
2198 // If we have a sign or zero extended integer, make sure to return Extend
2199 // so that the parameter gets the right LLVM IR attributes.
2200 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2201 // Treat an enum type as its underlying type.
2202 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2203 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002204
Chris Lattnereb518b42010-07-29 21:42:50 +00002205 if (Ty->isIntegralOrEnumerationType() &&
2206 Ty->isPromotableIntegerType())
2207 return ABIArgInfo::getExtend();
2208 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002209
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002210 break;
2211
2212 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
2213 // available SSE register is used, the registers are taken in the
2214 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00002215 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002216 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00002217 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00002218 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002219 break;
2220 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00002221 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002222
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002223 llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002224 switch (Hi) {
2225 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002226 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002227 // which is passed in memory.
2228 case Memory:
2229 case X87:
2230 case ComplexX87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002231 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002232
2233 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002234
Chris Lattner645406a2010-09-01 00:24:35 +00002235 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002236 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00002237 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002238 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002239
Chris Lattner645406a2010-09-01 00:24:35 +00002240 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2241 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002242 break;
2243
2244 // X87Up generally doesn't occur here (long double is passed in
2245 // memory), except in situations involving unions.
2246 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00002247 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002248 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002249
Chris Lattner645406a2010-09-01 00:24:35 +00002250 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2251 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00002252
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002253 ++neededSSE;
2254 break;
2255
2256 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
2257 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002258 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002259 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00002260 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002261 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002262 break;
2263 }
2264
Chris Lattner645406a2010-09-01 00:24:35 +00002265 // If a high part was specified, merge it together with the low part. It is
2266 // known to pass in the high eightbyte of the result. We do this by forming a
2267 // first class struct aggregate with the high and low part: {low, high}
2268 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002269 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002270
Chris Lattnereb518b42010-07-29 21:42:50 +00002271 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002272}
2273
Chris Lattneree5dcd02010-07-29 02:31:05 +00002274void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002275
Chris Lattnera3c109b2010-07-29 02:16:43 +00002276 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002277
2278 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00002279 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002280
2281 // If the return value is indirect, then the hidden argument is consuming one
2282 // integer register.
2283 if (FI.getReturnInfo().isIndirect())
2284 --freeIntRegs;
2285
2286 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2287 // get assigned (in left-to-right order) for passing as follows...
2288 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2289 it != ie; ++it) {
Bill Wendling99aaae82010-10-18 23:51:38 +00002290 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002291 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
2292 neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002293
2294 // AMD64-ABI 3.2.3p3: If there are no registers available for any
2295 // eightbyte of an argument, the whole argument is passed on the
2296 // stack. If registers have already been assigned for some
2297 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00002298 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002299 freeIntRegs -= neededInt;
2300 freeSSERegs -= neededSSE;
2301 } else {
Daniel Dunbaredfac032012-03-10 01:03:58 +00002302 it->info = getIndirectResult(it->type, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002303 }
2304 }
2305}
2306
2307static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2308 QualType Ty,
2309 CodeGenFunction &CGF) {
2310 llvm::Value *overflow_arg_area_p =
2311 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2312 llvm::Value *overflow_arg_area =
2313 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2314
2315 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2316 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedman8d2fe422011-11-18 02:44:19 +00002317 // It isn't stated explicitly in the standard, but in practice we use
2318 // alignment greater than 16 where necessary.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002319 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2320 if (Align > 8) {
Eli Friedman8d2fe422011-11-18 02:44:19 +00002321 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
Owen Anderson0032b272009-08-13 21:57:51 +00002322 llvm::Value *Offset =
Eli Friedman8d2fe422011-11-18 02:44:19 +00002323 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002324 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2325 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00002326 CGF.Int64Ty);
Eli Friedman8d2fe422011-11-18 02:44:19 +00002327 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002328 overflow_arg_area =
2329 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2330 overflow_arg_area->getType(),
2331 "overflow_arg_area.align");
2332 }
2333
2334 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002335 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002336 llvm::Value *Res =
2337 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002338 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002339
2340 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2341 // l->overflow_arg_area + sizeof(type).
2342 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2343 // an 8 byte boundary.
2344
2345 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00002346 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00002347 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002348 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2349 "overflow_arg_area.next");
2350 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2351
2352 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2353 return Res;
2354}
2355
2356llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2357 CodeGenFunction &CGF) const {
2358 // Assume that va_list type is correct; should be pointer to LLVM type:
2359 // struct {
2360 // i32 gp_offset;
2361 // i32 fp_offset;
2362 // i8* overflow_arg_area;
2363 // i8* reg_save_area;
2364 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00002365 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002366
Chris Lattnera14db752010-03-11 18:19:55 +00002367 Ty = CGF.getContext().getCanonicalType(Ty);
Daniel Dunbaredfac032012-03-10 01:03:58 +00002368 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002369
2370 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2371 // in the registers. If not go to step 7.
2372 if (!neededInt && !neededSSE)
2373 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2374
2375 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2376 // general purpose registers needed to pass type and num_fp to hold
2377 // the number of floating point registers needed.
2378
2379 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2380 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2381 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2382 //
2383 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2384 // register save space).
2385
2386 llvm::Value *InRegs = 0;
2387 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2388 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2389 if (neededInt) {
2390 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2391 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002392 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2393 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002394 }
2395
2396 if (neededSSE) {
2397 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2398 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2399 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00002400 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2401 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002402 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2403 }
2404
2405 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2406 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2407 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2408 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2409
2410 // Emit code to load the value if it was passed in registers.
2411
2412 CGF.EmitBlock(InRegBlock);
2413
2414 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2415 // an offset of l->gp_offset and/or l->fp_offset. This may require
2416 // copying to a temporary location in case the parameter is passed
2417 // in different register classes or requires an alignment greater
2418 // than 8 for general purpose registers and 16 for XMM registers.
2419 //
2420 // FIXME: This really results in shameful code when we end up needing to
2421 // collect arguments from different places; often what should result in a
2422 // simple assembling of a structure from scattered addresses has many more
2423 // loads than necessary. Can we clean this up?
Chris Lattner2acc6e32011-07-18 04:24:23 +00002424 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002425 llvm::Value *RegAddr =
2426 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2427 "reg_save_area");
2428 if (neededInt && neededSSE) {
2429 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002430 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002431 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002432 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2433 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002434 llvm::Type *TyLo = ST->getElementType(0);
2435 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002436 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002437 "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002438 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2439 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002440 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2441 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002442 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2443 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002444 llvm::Value *V =
2445 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2446 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2447 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2448 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2449
Owen Andersona1cf15f2009-07-14 23:10:40 +00002450 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002451 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002452 } else if (neededInt) {
2453 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2454 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002455 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002456 } else if (neededSSE == 1) {
2457 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2458 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2459 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002460 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002461 assert(neededSSE == 2 && "Invalid number of needed registers!");
2462 // SSE registers are spaced 16 bytes apart in the register save
2463 // area, we need to collect the two eightbytes together.
2464 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002465 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner8b418682012-02-07 00:39:47 +00002466 llvm::Type *DoubleTy = CGF.DoubleTy;
Chris Lattner2acc6e32011-07-18 04:24:23 +00002467 llvm::Type *DblPtrTy =
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002468 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002469 llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002470 DoubleTy, NULL);
2471 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2472 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2473 DblPtrTy));
2474 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2475 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2476 DblPtrTy));
2477 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2478 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2479 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002480 }
2481
2482 // AMD64-ABI 3.5.7p5: Step 5. Set:
2483 // l->gp_offset = l->gp_offset + num_gp * 8
2484 // l->fp_offset = l->fp_offset + num_fp * 16.
2485 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002486 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002487 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2488 gp_offset_p);
2489 }
2490 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002491 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002492 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2493 fp_offset_p);
2494 }
2495 CGF.EmitBranch(ContBlock);
2496
2497 // Emit code to load the value if it was passed in memory.
2498
2499 CGF.EmitBlock(InMemBlock);
2500 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2501
2502 // Return the appropriate result.
2503
2504 CGF.EmitBlock(ContBlock);
Jay Foadbbf3bac2011-03-30 11:28:58 +00002505 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002506 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002507 ResAddr->addIncoming(RegAddr, InRegBlock);
2508 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002509 return ResAddr;
2510}
2511
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002512ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2513
2514 if (Ty->isVoidType())
2515 return ABIArgInfo::getIgnore();
2516
2517 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2518 Ty = EnumTy->getDecl()->getIntegerType();
2519
2520 uint64_t Size = getContext().getTypeSize(Ty);
2521
2522 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002523 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2524 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002525 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2526
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002527 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2528 if (Size == 128 &&
Eli Friedman55fc7e22012-01-25 22:46:34 +00002529 getContext().getTargetInfo().getTriple().getOS()
2530 == llvm::Triple::MinGW32)
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002531 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2532 Size));
2533
2534 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2535 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2536 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002537 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002538 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2539 Size));
2540
2541 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2542 }
2543
2544 if (Ty->isPromotableIntegerType())
2545 return ABIArgInfo::getExtend();
2546
2547 return ABIArgInfo::getDirect();
2548}
2549
2550void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2551
2552 QualType RetTy = FI.getReturnType();
2553 FI.getReturnInfo() = classify(RetTy);
2554
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002555 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2556 it != ie; ++it)
2557 it->info = classify(it->type);
2558}
2559
Chris Lattnerf13721d2010-08-31 16:44:54 +00002560llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2561 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002562 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002563
Chris Lattnerf13721d2010-08-31 16:44:54 +00002564 CGBuilderTy &Builder = CGF.Builder;
2565 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2566 "ap");
2567 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2568 llvm::Type *PTy =
2569 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2570 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2571
2572 uint64_t Offset =
2573 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2574 llvm::Value *NextAddr =
2575 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2576 "ap.next");
2577 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2578
2579 return AddrTyped;
2580}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002581
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00002582namespace {
2583
Derek Schuff263366f2012-10-16 22:30:41 +00002584class NaClX86_64ABIInfo : public ABIInfo {
2585 public:
2586 NaClX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2587 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, HasAVX) {}
2588 virtual void computeInfo(CGFunctionInfo &FI) const;
2589 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2590 CodeGenFunction &CGF) const;
2591 private:
2592 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
2593 X86_64ABIInfo NInfo; // Used for everything else.
2594};
2595
2596class NaClX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2597 public:
2598 NaClX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2599 : TargetCodeGenInfo(new NaClX86_64ABIInfo(CGT, HasAVX)) {}
2600};
2601
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00002602}
2603
Derek Schuff263366f2012-10-16 22:30:41 +00002604void NaClX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2605 if (FI.getASTCallingConvention() == CC_PnaclCall)
2606 PInfo.computeInfo(FI);
2607 else
2608 NInfo.computeInfo(FI);
2609}
2610
2611llvm::Value *NaClX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2612 CodeGenFunction &CGF) const {
2613 // Always use the native convention; calling pnacl-style varargs functions
2614 // is unuspported.
2615 return NInfo.EmitVAArg(VAListAddr, Ty, CGF);
2616}
2617
2618
John McCallec853ba2010-03-11 00:10:12 +00002619// PowerPC-32
2620
2621namespace {
2622class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2623public:
Chris Lattnerea044322010-07-29 02:01:43 +00002624 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002625
John McCallec853ba2010-03-11 00:10:12 +00002626 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2627 // This is recovered from gcc output.
2628 return 1; // r1 is the dedicated stack pointer
2629 }
2630
2631 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002632 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002633};
2634
2635}
2636
2637bool
2638PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2639 llvm::Value *Address) const {
2640 // This is calculated from the LLVM and GCC tables and verified
2641 // against gcc output. AFAIK all ABIs use the same encoding.
2642
2643 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallec853ba2010-03-11 00:10:12 +00002644
Chris Lattner8b418682012-02-07 00:39:47 +00002645 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallec853ba2010-03-11 00:10:12 +00002646 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2647 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2648 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2649
2650 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002651 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002652
2653 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002654 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002655
2656 // 64-76 are various 4-byte special-purpose registers:
2657 // 64: mq
2658 // 65: lr
2659 // 66: ctr
2660 // 67: ap
2661 // 68-75 cr0-7
2662 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002663 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002664
2665 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002666 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002667
2668 // 109: vrsave
2669 // 110: vscr
2670 // 111: spe_acc
2671 // 112: spefscr
2672 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002673 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002674
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002675 return false;
John McCallec853ba2010-03-11 00:10:12 +00002676}
2677
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002678// PowerPC-64
2679
2680namespace {
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002681/// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
2682class PPC64_SVR4_ABIInfo : public DefaultABIInfo {
2683
2684public:
2685 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
2686
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00002687 // TODO: We can add more logic to computeInfo to improve performance.
2688 // Example: For aggregate arguments that fit in a register, we could
2689 // use getDirectInReg (as is done below for structs containing a single
2690 // floating-point value) to avoid pushing them to memory on function
2691 // entry. This would require changing the logic in PPCISelLowering
2692 // when lowering the parameters in the caller and args in the callee.
2693 virtual void computeInfo(CGFunctionInfo &FI) const {
2694 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2695 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2696 it != ie; ++it) {
2697 // We rely on the default argument classification for the most part.
2698 // One exception: An aggregate containing a single floating-point
2699 // item must be passed in a register if one is available.
2700 const Type *T = isSingleElementStruct(it->type, getContext());
2701 if (T) {
2702 const BuiltinType *BT = T->getAs<BuiltinType>();
2703 if (BT && BT->isFloatingPoint()) {
2704 QualType QT(T, 0);
2705 it->info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
2706 continue;
2707 }
2708 }
2709 it->info = classifyArgumentType(it->type);
2710 }
2711 }
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002712
2713 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr,
2714 QualType Ty,
2715 CodeGenFunction &CGF) const;
2716};
2717
2718class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
2719public:
2720 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT)
2721 : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT)) {}
2722
2723 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2724 // This is recovered from gcc output.
2725 return 1; // r1 is the dedicated stack pointer
2726 }
2727
2728 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2729 llvm::Value *Address) const;
2730};
2731
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002732class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2733public:
2734 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
2735
2736 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2737 // This is recovered from gcc output.
2738 return 1; // r1 is the dedicated stack pointer
2739 }
2740
2741 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2742 llvm::Value *Address) const;
2743};
2744
2745}
2746
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002747// Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
2748llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr,
2749 QualType Ty,
2750 CodeGenFunction &CGF) const {
2751 llvm::Type *BP = CGF.Int8PtrTy;
2752 llvm::Type *BPP = CGF.Int8PtrPtrTy;
2753
2754 CGBuilderTy &Builder = CGF.Builder;
2755 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
2756 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2757
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002758 // Update the va_list pointer.
2759 unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8;
2760 unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8);
2761 llvm::Value *NextAddr =
2762 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset),
2763 "ap.next");
2764 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2765
2766 // If the argument is smaller than 8 bytes, it is right-adjusted in
2767 // its doubleword slot. Adjust the pointer to pick it up from the
2768 // correct offset.
2769 if (SizeInBytes < 8) {
2770 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2771 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes));
2772 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2773 }
2774
2775 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2776 return Builder.CreateBitCast(Addr, PTy);
2777}
2778
2779static bool
2780PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2781 llvm::Value *Address) {
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002782 // This is calculated from the LLVM and GCC tables and verified
2783 // against gcc output. AFAIK all ABIs use the same encoding.
2784
2785 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2786
2787 llvm::IntegerType *i8 = CGF.Int8Ty;
2788 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2789 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2790 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2791
2792 // 0-31: r0-31, the 8-byte general-purpose registers
2793 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
2794
2795 // 32-63: fp0-31, the 8-byte floating-point registers
2796 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
2797
2798 // 64-76 are various 4-byte special-purpose registers:
2799 // 64: mq
2800 // 65: lr
2801 // 66: ctr
2802 // 67: ap
2803 // 68-75 cr0-7
2804 // 76: xer
2805 AssignToArrayRange(Builder, Address, Four8, 64, 76);
2806
2807 // 77-108: v0-31, the 16-byte vector registers
2808 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
2809
2810 // 109: vrsave
2811 // 110: vscr
2812 // 111: spe_acc
2813 // 112: spefscr
2814 // 113: sfp
2815 AssignToArrayRange(Builder, Address, Four8, 109, 113);
2816
2817 return false;
2818}
John McCallec853ba2010-03-11 00:10:12 +00002819
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002820bool
2821PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
2822 CodeGen::CodeGenFunction &CGF,
2823 llvm::Value *Address) const {
2824
2825 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
2826}
2827
2828bool
2829PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2830 llvm::Value *Address) const {
2831
2832 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
2833}
2834
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002835//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002836// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002837//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002838
2839namespace {
2840
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002841class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002842public:
2843 enum ABIKind {
2844 APCS = 0,
2845 AAPCS = 1,
2846 AAPCS_VFP
2847 };
2848
2849private:
2850 ABIKind Kind;
2851
2852public:
Chris Lattnerea044322010-07-29 02:01:43 +00002853 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002854
John McCall49e34be2011-08-30 01:42:09 +00002855 bool isEABI() const {
Eli Friedman55fc7e22012-01-25 22:46:34 +00002856 StringRef Env =
2857 getContext().getTargetInfo().getTriple().getEnvironmentName();
Logan Chien94a71422012-09-02 09:30:11 +00002858 return (Env == "gnueabi" || Env == "eabi" ||
2859 Env == "android" || Env == "androideabi");
John McCall49e34be2011-08-30 01:42:09 +00002860 }
2861
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002862private:
2863 ABIKind getABIKind() const { return Kind; }
2864
Chris Lattnera3c109b2010-07-29 02:16:43 +00002865 ABIArgInfo classifyReturnType(QualType RetTy) const;
Manman Ren710c5172012-10-31 19:02:26 +00002866 ABIArgInfo classifyArgumentType(QualType RetTy, int *VFPRegs,
2867 unsigned &AllocatedVFP,
Manman Renb3fa55f2012-10-30 23:21:41 +00002868 bool &IsHA) const;
Manman Ren97f81572012-10-16 19:18:39 +00002869 bool isIllegalVectorType(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002870
Chris Lattneree5dcd02010-07-29 02:31:05 +00002871 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002872
2873 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2874 CodeGenFunction &CGF) const;
2875};
2876
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002877class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2878public:
Chris Lattnerea044322010-07-29 02:01:43 +00002879 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2880 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00002881
John McCall49e34be2011-08-30 01:42:09 +00002882 const ARMABIInfo &getABIInfo() const {
2883 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
2884 }
2885
John McCall6374c332010-03-06 00:35:14 +00002886 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2887 return 13;
2888 }
Roman Divacky09345d12011-05-18 19:36:54 +00002889
Chris Lattner5f9e2722011-07-23 10:55:15 +00002890 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCallf85e1932011-06-15 23:02:42 +00002891 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2892 }
2893
Roman Divacky09345d12011-05-18 19:36:54 +00002894 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2895 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002896 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divacky09345d12011-05-18 19:36:54 +00002897
2898 // 0-15 are the 16 integer registers.
Chris Lattner8b418682012-02-07 00:39:47 +00002899 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divacky09345d12011-05-18 19:36:54 +00002900 return false;
2901 }
John McCall49e34be2011-08-30 01:42:09 +00002902
2903 unsigned getSizeOfUnwindException() const {
2904 if (getABIInfo().isEABI()) return 88;
2905 return TargetCodeGenInfo::getSizeOfUnwindException();
2906 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002907};
2908
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002909}
2910
Chris Lattneree5dcd02010-07-29 02:31:05 +00002911void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Manman Renb3fa55f2012-10-30 23:21:41 +00002912 // To correctly handle Homogeneous Aggregate, we need to keep track of the
Manman Ren710c5172012-10-31 19:02:26 +00002913 // VFP registers allocated so far.
Manman Renb3fa55f2012-10-30 23:21:41 +00002914 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
2915 // VFP registers of the appropriate type unallocated then the argument is
2916 // allocated to the lowest-numbered sequence of such registers.
2917 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
2918 // unallocated are marked as unavailable.
2919 unsigned AllocatedVFP = 0;
Manman Ren710c5172012-10-31 19:02:26 +00002920 int VFPRegs[16] = { 0 };
Chris Lattnera3c109b2010-07-29 02:16:43 +00002921 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002922 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Manman Renb3fa55f2012-10-30 23:21:41 +00002923 it != ie; ++it) {
2924 unsigned PreAllocation = AllocatedVFP;
2925 bool IsHA = false;
2926 // 6.1.2.3 There is one VFP co-processor register class using registers
2927 // s0-s15 (d0-d7) for passing arguments.
2928 const unsigned NumVFPs = 16;
Manman Ren710c5172012-10-31 19:02:26 +00002929 it->info = classifyArgumentType(it->type, VFPRegs, AllocatedVFP, IsHA);
Manman Renb3fa55f2012-10-30 23:21:41 +00002930 // If we do not have enough VFP registers for the HA, any VFP registers
2931 // that are unallocated are marked as unavailable. To achieve this, we add
2932 // padding of (NumVFPs - PreAllocation) floats.
2933 if (IsHA && AllocatedVFP > NumVFPs && PreAllocation < NumVFPs) {
2934 llvm::Type *PaddingTy = llvm::ArrayType::get(
2935 llvm::Type::getFloatTy(getVMContext()), NumVFPs - PreAllocation);
2936 it->info = ABIArgInfo::getExpandWithPadding(false, PaddingTy);
2937 }
2938 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002939
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002940 // Always honor user-specified calling convention.
2941 if (FI.getCallingConvention() != llvm::CallingConv::C)
2942 return;
2943
2944 // Calling convention as default by an ABI.
Rafael Espindola25117ab2010-06-16 16:13:39 +00002945 llvm::CallingConv::ID DefaultCC;
David Tweedb16abb12012-10-25 13:33:01 +00002946 if (getContext().getTargetInfo().getTriple().getEnvironmentName()=="gnueabihf")
2947 DefaultCC = llvm::CallingConv::ARM_AAPCS_VFP;
2948 else if (isEABI())
Rafael Espindola25117ab2010-06-16 16:13:39 +00002949 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00002950 else
2951 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00002952
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002953 // If user did not ask for specific calling convention explicitly (e.g. via
2954 // pcs attribute), set effective calling convention if it's different than ABI
2955 // default.
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002956 switch (getABIKind()) {
2957 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002958 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2959 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002960 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002961 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002962 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2963 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002964 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002965 case AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002966 if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
2967 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002968 break;
2969 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002970}
2971
Bob Wilson194f06a2011-08-03 05:58:22 +00002972/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
2973/// aggregate. If HAMembers is non-null, the number of base elements
2974/// contained in the type is returned through it; this is used for the
2975/// recursive calls that check aggregate component types.
2976static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
2977 ASTContext &Context,
2978 uint64_t *HAMembers = 0) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002979 uint64_t Members = 0;
Bob Wilson194f06a2011-08-03 05:58:22 +00002980 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2981 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
2982 return false;
2983 Members *= AT->getSize().getZExtValue();
2984 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
2985 const RecordDecl *RD = RT->getDecl();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002986 if (RD->hasFlexibleArrayMember())
Bob Wilson194f06a2011-08-03 05:58:22 +00002987 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002988
Bob Wilson194f06a2011-08-03 05:58:22 +00002989 Members = 0;
2990 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2991 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00002992 const FieldDecl *FD = *i;
Bob Wilson194f06a2011-08-03 05:58:22 +00002993 uint64_t FldMembers;
2994 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
2995 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002996
2997 Members = (RD->isUnion() ?
2998 std::max(Members, FldMembers) : Members + FldMembers);
Bob Wilson194f06a2011-08-03 05:58:22 +00002999 }
3000 } else {
3001 Members = 1;
3002 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
3003 Members = 2;
3004 Ty = CT->getElementType();
3005 }
3006
3007 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
3008 // double, or 64-bit or 128-bit vectors.
3009 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3010 if (BT->getKind() != BuiltinType::Float &&
Tim Northoveradfa45f2012-07-20 22:29:29 +00003011 BT->getKind() != BuiltinType::Double &&
3012 BT->getKind() != BuiltinType::LongDouble)
Bob Wilson194f06a2011-08-03 05:58:22 +00003013 return false;
3014 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
3015 unsigned VecSize = Context.getTypeSize(VT);
3016 if (VecSize != 64 && VecSize != 128)
3017 return false;
3018 } else {
3019 return false;
3020 }
3021
3022 // The base type must be the same for all members. Vector types of the
3023 // same total size are treated as being equivalent here.
3024 const Type *TyPtr = Ty.getTypePtr();
3025 if (!Base)
3026 Base = TyPtr;
3027 if (Base != TyPtr &&
3028 (!Base->isVectorType() || !TyPtr->isVectorType() ||
3029 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
3030 return false;
3031 }
3032
3033 // Homogeneous Aggregates can have at most 4 members of the base type.
3034 if (HAMembers)
3035 *HAMembers = Members;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003036
3037 return (Members > 0 && Members <= 4);
Bob Wilson194f06a2011-08-03 05:58:22 +00003038}
3039
Manman Ren710c5172012-10-31 19:02:26 +00003040/// markAllocatedVFPs - update VFPRegs according to the alignment and
3041/// number of VFP registers (unit is S register) requested.
3042static void markAllocatedVFPs(int *VFPRegs, unsigned &AllocatedVFP,
3043 unsigned Alignment,
3044 unsigned NumRequired) {
3045 // Early Exit.
3046 if (AllocatedVFP >= 16)
3047 return;
3048 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
3049 // VFP registers of the appropriate type unallocated then the argument is
3050 // allocated to the lowest-numbered sequence of such registers.
3051 for (unsigned I = 0; I < 16; I += Alignment) {
3052 bool FoundSlot = true;
3053 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3054 if (J >= 16 || VFPRegs[J]) {
3055 FoundSlot = false;
3056 break;
3057 }
3058 if (FoundSlot) {
3059 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3060 VFPRegs[J] = 1;
3061 AllocatedVFP += NumRequired;
3062 return;
3063 }
3064 }
3065 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
3066 // unallocated are marked as unavailable.
3067 for (unsigned I = 0; I < 16; I++)
3068 VFPRegs[I] = 1;
3069 AllocatedVFP = 17; // We do not have enough VFP registers.
3070}
3071
3072ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, int *VFPRegs,
3073 unsigned &AllocatedVFP,
Manman Renb3fa55f2012-10-30 23:21:41 +00003074 bool &IsHA) const {
3075 // We update number of allocated VFPs according to
3076 // 6.1.2.1 The following argument types are VFP CPRCs:
3077 // A single-precision floating-point type (including promoted
3078 // half-precision types); A double-precision floating-point type;
3079 // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
3080 // with a Base Type of a single- or double-precision floating-point type,
3081 // 64-bit containerized vectors or 128-bit containerized vectors with one
3082 // to four Elements.
3083
Manman Ren97f81572012-10-16 19:18:39 +00003084 // Handle illegal vector types here.
3085 if (isIllegalVectorType(Ty)) {
3086 uint64_t Size = getContext().getTypeSize(Ty);
3087 if (Size <= 32) {
3088 llvm::Type *ResType =
3089 llvm::Type::getInt32Ty(getVMContext());
3090 return ABIArgInfo::getDirect(ResType);
3091 }
3092 if (Size == 64) {
3093 llvm::Type *ResType = llvm::VectorType::get(
3094 llvm::Type::getInt32Ty(getVMContext()), 2);
Manman Ren710c5172012-10-31 19:02:26 +00003095 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
Manman Ren97f81572012-10-16 19:18:39 +00003096 return ABIArgInfo::getDirect(ResType);
3097 }
3098 if (Size == 128) {
3099 llvm::Type *ResType = llvm::VectorType::get(
3100 llvm::Type::getInt32Ty(getVMContext()), 4);
Manman Ren710c5172012-10-31 19:02:26 +00003101 markAllocatedVFPs(VFPRegs, AllocatedVFP, 4, 4);
Manman Ren97f81572012-10-16 19:18:39 +00003102 return ABIArgInfo::getDirect(ResType);
3103 }
3104 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3105 }
Manman Ren710c5172012-10-31 19:02:26 +00003106 // Update VFPRegs for legal vector types.
Manman Renb3fa55f2012-10-30 23:21:41 +00003107 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3108 uint64_t Size = getContext().getTypeSize(VT);
3109 // Size of a legal vector should be power of 2 and above 64.
Manman Ren710c5172012-10-31 19:02:26 +00003110 markAllocatedVFPs(VFPRegs, AllocatedVFP, Size >= 128 ? 4 : 2, Size / 32);
Manman Renb3fa55f2012-10-30 23:21:41 +00003111 }
Manman Ren710c5172012-10-31 19:02:26 +00003112 // Update VFPRegs for floating point types.
Manman Renb3fa55f2012-10-30 23:21:41 +00003113 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3114 if (BT->getKind() == BuiltinType::Half ||
3115 BT->getKind() == BuiltinType::Float)
Manman Ren710c5172012-10-31 19:02:26 +00003116 markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, 1);
Manman Renb3fa55f2012-10-30 23:21:41 +00003117 if (BT->getKind() == BuiltinType::Double ||
Manman Ren710c5172012-10-31 19:02:26 +00003118 BT->getKind() == BuiltinType::LongDouble)
3119 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
Manman Renb3fa55f2012-10-30 23:21:41 +00003120 }
Manman Ren97f81572012-10-16 19:18:39 +00003121
John McCalld608cdb2010-08-22 10:59:02 +00003122 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003123 // Treat an enum type as its underlying type.
3124 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3125 Ty = EnumTy->getDecl()->getIntegerType();
3126
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003127 return (Ty->isPromotableIntegerType() ?
3128 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003129 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003130
Daniel Dunbar42025572009-09-14 21:54:03 +00003131 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003132 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00003133 return ABIArgInfo::getIgnore();
3134
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003135 // Structures with either a non-trivial destructor or a non-trivial
3136 // copy constructor are always indirect.
3137 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
3138 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3139
Bob Wilson194f06a2011-08-03 05:58:22 +00003140 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
Manman Renb3fa55f2012-10-30 23:21:41 +00003141 // Homogeneous Aggregates need to be expanded when we can fit the aggregate
3142 // into VFP registers.
Bob Wilson194f06a2011-08-03 05:58:22 +00003143 const Type *Base = 0;
Manman Renb3fa55f2012-10-30 23:21:41 +00003144 uint64_t Members = 0;
3145 if (isHomogeneousAggregate(Ty, Base, getContext(), &Members)) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003146 assert(Base && "Base class should be set for homogeneous aggregate");
Manman Renb3fa55f2012-10-30 23:21:41 +00003147 // Base can be a floating-point or a vector.
3148 if (Base->isVectorType()) {
3149 // ElementSize is in number of floats.
3150 unsigned ElementSize = getContext().getTypeSize(Base) == 64 ? 2 : 4;
Manman Ren710c5172012-10-31 19:02:26 +00003151 markAllocatedVFPs(VFPRegs, AllocatedVFP, ElementSize, Members * ElementSize);
Manman Renb3fa55f2012-10-30 23:21:41 +00003152 } else if (Base->isSpecificBuiltinType(BuiltinType::Float))
Manman Ren710c5172012-10-31 19:02:26 +00003153 markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, Members);
Manman Renb3fa55f2012-10-30 23:21:41 +00003154 else {
3155 assert(Base->isSpecificBuiltinType(BuiltinType::Double) ||
3156 Base->isSpecificBuiltinType(BuiltinType::LongDouble));
Manman Ren710c5172012-10-31 19:02:26 +00003157 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, Members * 2);
Manman Renb3fa55f2012-10-30 23:21:41 +00003158 }
3159 IsHA = true;
Bob Wilson194f06a2011-08-03 05:58:22 +00003160 return ABIArgInfo::getExpand();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003161 }
Bob Wilson194f06a2011-08-03 05:58:22 +00003162 }
3163
Manman Ren634b3d22012-08-13 21:23:55 +00003164 // Support byval for ARM.
3165 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64) ||
3166 getContext().getTypeAlign(Ty) > 64) {
3167 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
Eli Friedman79f30982012-08-09 00:31:40 +00003168 }
3169
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00003170 // Otherwise, pass by coercing to a structure of the appropriate size.
Chris Lattner2acc6e32011-07-18 04:24:23 +00003171 llvm::Type* ElemTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003172 unsigned SizeRegs;
Eli Friedman79f30982012-08-09 00:31:40 +00003173 // FIXME: Try to match the types of the arguments more accurately where
3174 // we can.
3175 if (getContext().getTypeAlign(Ty) <= 32) {
Bob Wilson53fc1a62011-08-01 23:39:04 +00003176 ElemTy = llvm::Type::getInt32Ty(getVMContext());
3177 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Manman Ren78eb76e2012-06-25 22:04:00 +00003178 } else {
Manman Ren78eb76e2012-06-25 22:04:00 +00003179 ElemTy = llvm::Type::getInt64Ty(getVMContext());
3180 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastings67d097e2011-04-27 17:24:02 +00003181 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003182
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003183 llvm::Type *STy =
Chris Lattner7650d952011-06-18 22:49:11 +00003184 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003185 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003186}
3187
Chris Lattnera3c109b2010-07-29 02:16:43 +00003188static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00003189 llvm::LLVMContext &VMContext) {
3190 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
3191 // is called integer-like if its size is less than or equal to one word, and
3192 // the offset of each of its addressable sub-fields is zero.
3193
3194 uint64_t Size = Context.getTypeSize(Ty);
3195
3196 // Check that the type fits in a word.
3197 if (Size > 32)
3198 return false;
3199
3200 // FIXME: Handle vector types!
3201 if (Ty->isVectorType())
3202 return false;
3203
Daniel Dunbarb0d58192009-09-14 02:20:34 +00003204 // Float types are never treated as "integer like".
3205 if (Ty->isRealFloatingType())
3206 return false;
3207
Daniel Dunbar98303b92009-09-13 08:03:58 +00003208 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00003209 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00003210 return true;
3211
Daniel Dunbar45815812010-02-01 23:31:26 +00003212 // Small complex integer types are "integer like".
3213 if (const ComplexType *CT = Ty->getAs<ComplexType>())
3214 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003215
3216 // Single element and zero sized arrays should be allowed, by the definition
3217 // above, but they are not.
3218
3219 // Otherwise, it must be a record type.
3220 const RecordType *RT = Ty->getAs<RecordType>();
3221 if (!RT) return false;
3222
3223 // Ignore records with flexible arrays.
3224 const RecordDecl *RD = RT->getDecl();
3225 if (RD->hasFlexibleArrayMember())
3226 return false;
3227
3228 // Check that all sub-fields are at offset 0, and are themselves "integer
3229 // like".
3230 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3231
3232 bool HadField = false;
3233 unsigned idx = 0;
3234 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3235 i != e; ++i, ++idx) {
David Blaikie581deb32012-06-06 20:45:41 +00003236 const FieldDecl *FD = *i;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003237
Daniel Dunbar679855a2010-01-29 03:22:29 +00003238 // Bit-fields are not addressable, we only need to verify they are "integer
3239 // like". We still have to disallow a subsequent non-bitfield, for example:
3240 // struct { int : 0; int x }
3241 // is non-integer like according to gcc.
3242 if (FD->isBitField()) {
3243 if (!RD->isUnion())
3244 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003245
Daniel Dunbar679855a2010-01-29 03:22:29 +00003246 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3247 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003248
Daniel Dunbar679855a2010-01-29 03:22:29 +00003249 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003250 }
3251
Daniel Dunbar679855a2010-01-29 03:22:29 +00003252 // Check if this field is at offset 0.
3253 if (Layout.getFieldOffset(idx) != 0)
3254 return false;
3255
Daniel Dunbar98303b92009-09-13 08:03:58 +00003256 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3257 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003258
Daniel Dunbar679855a2010-01-29 03:22:29 +00003259 // Only allow at most one field in a structure. This doesn't match the
3260 // wording above, but follows gcc in situations with a field following an
3261 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00003262 if (!RD->isUnion()) {
3263 if (HadField)
3264 return false;
3265
3266 HadField = true;
3267 }
3268 }
3269
3270 return true;
3271}
3272
Chris Lattnera3c109b2010-07-29 02:16:43 +00003273ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003274 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003275 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00003276
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00003277 // Large vector types should be returned via memory.
3278 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
3279 return ABIArgInfo::getIndirect(0);
3280
John McCalld608cdb2010-08-22 10:59:02 +00003281 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003282 // Treat an enum type as its underlying type.
3283 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3284 RetTy = EnumTy->getDecl()->getIntegerType();
3285
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003286 return (RetTy->isPromotableIntegerType() ?
3287 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003288 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003289
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003290 // Structures with either a non-trivial destructor or a non-trivial
3291 // copy constructor are always indirect.
3292 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
3293 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3294
Daniel Dunbar98303b92009-09-13 08:03:58 +00003295 // Are we following APCS?
3296 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00003297 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00003298 return ABIArgInfo::getIgnore();
3299
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003300 // Complex types are all returned as packed integers.
3301 //
3302 // FIXME: Consider using 2 x vector types if the back end handles them
3303 // correctly.
3304 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00003305 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00003306 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003307
Daniel Dunbar98303b92009-09-13 08:03:58 +00003308 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003309 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003310 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003311 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003312 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003313 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003314 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003315 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3316 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003317 }
3318
3319 // Otherwise return in memory.
3320 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003321 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003322
3323 // Otherwise this is an AAPCS variant.
3324
Chris Lattnera3c109b2010-07-29 02:16:43 +00003325 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00003326 return ABIArgInfo::getIgnore();
3327
Bob Wilson3b694fa2011-11-02 04:51:36 +00003328 // Check for homogeneous aggregates with AAPCS-VFP.
3329 if (getABIKind() == AAPCS_VFP) {
3330 const Type *Base = 0;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003331 if (isHomogeneousAggregate(RetTy, Base, getContext())) {
3332 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson3b694fa2011-11-02 04:51:36 +00003333 // Homogeneous Aggregates are returned directly.
3334 return ABIArgInfo::getDirect();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003335 }
Bob Wilson3b694fa2011-11-02 04:51:36 +00003336 }
3337
Daniel Dunbar98303b92009-09-13 08:03:58 +00003338 // Aggregates <= 4 bytes are returned in r0; other aggregates
3339 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003340 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00003341 if (Size <= 32) {
3342 // Return in the smallest viable integer type.
3343 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003344 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003345 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003346 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3347 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003348 }
3349
Daniel Dunbar98303b92009-09-13 08:03:58 +00003350 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003351}
3352
Manman Ren97f81572012-10-16 19:18:39 +00003353/// isIllegalVector - check whether Ty is an illegal vector type.
3354bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
3355 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3356 // Check whether VT is legal.
3357 unsigned NumElements = VT->getNumElements();
3358 uint64_t Size = getContext().getTypeSize(VT);
3359 // NumElements should be power of 2.
3360 if ((NumElements & (NumElements - 1)) != 0)
3361 return true;
3362 // Size should be greater than 32 bits.
3363 return Size <= 32;
3364 }
3365 return false;
3366}
3367
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003368llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00003369 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003370 llvm::Type *BP = CGF.Int8PtrTy;
3371 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003372
3373 CGBuilderTy &Builder = CGF.Builder;
Chris Lattner8b418682012-02-07 00:39:47 +00003374 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003375 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Manman Rend105e732012-10-16 19:01:37 +00003376
3377 uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8;
Rafael Espindolae164c182011-08-02 22:33:37 +00003378 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
Manman Ren97f81572012-10-16 19:18:39 +00003379 bool IsIndirect = false;
Manman Rend105e732012-10-16 19:01:37 +00003380
3381 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
3382 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
Manman Ren93371022012-10-16 19:51:48 +00003383 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3384 getABIKind() == ARMABIInfo::AAPCS)
3385 TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
3386 else
3387 TyAlign = 4;
Manman Ren97f81572012-10-16 19:18:39 +00003388 // Use indirect if size of the illegal vector is bigger than 16 bytes.
3389 if (isIllegalVectorType(Ty) && Size > 16) {
3390 IsIndirect = true;
3391 Size = 4;
3392 TyAlign = 4;
3393 }
Manman Rend105e732012-10-16 19:01:37 +00003394
3395 // Handle address alignment for ABI alignment > 4 bytes.
Rafael Espindolae164c182011-08-02 22:33:37 +00003396 if (TyAlign > 4) {
3397 assert((TyAlign & (TyAlign - 1)) == 0 &&
3398 "Alignment is not power of 2!");
3399 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3400 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
3401 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
Manman Rend105e732012-10-16 19:01:37 +00003402 Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align");
Rafael Espindolae164c182011-08-02 22:33:37 +00003403 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003404
3405 uint64_t Offset =
Manman Rend105e732012-10-16 19:01:37 +00003406 llvm::RoundUpToAlignment(Size, 4);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003407 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00003408 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003409 "ap.next");
3410 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3411
Manman Ren97f81572012-10-16 19:18:39 +00003412 if (IsIndirect)
3413 Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP));
Manman Ren93371022012-10-16 19:51:48 +00003414 else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) {
Manman Rend105e732012-10-16 19:01:37 +00003415 // We can't directly cast ap.cur to pointer to a vector type, since ap.cur
3416 // may not be correctly aligned for the vector type. We create an aligned
3417 // temporary space and copy the content over from ap.cur to the temporary
3418 // space. This is necessary if the natural alignment of the type is greater
3419 // than the ABI alignment.
3420 llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
3421 CharUnits CharSize = getContext().getTypeSizeInChars(Ty);
3422 llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty),
3423 "var.align");
3424 llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
3425 llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy);
3426 Builder.CreateMemCpy(Dst, Src,
3427 llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()),
3428 TyAlign, false);
3429 Addr = AlignedTemp; //The content is in aligned location.
3430 }
3431 llvm::Type *PTy =
3432 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3433 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3434
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003435 return AddrTyped;
3436}
3437
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00003438namespace {
3439
Derek Schuff263366f2012-10-16 22:30:41 +00003440class NaClARMABIInfo : public ABIInfo {
3441 public:
3442 NaClARMABIInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3443 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, Kind) {}
3444 virtual void computeInfo(CGFunctionInfo &FI) const;
3445 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3446 CodeGenFunction &CGF) const;
3447 private:
3448 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
3449 ARMABIInfo NInfo; // Used for everything else.
3450};
3451
3452class NaClARMTargetCodeGenInfo : public TargetCodeGenInfo {
3453 public:
3454 NaClARMTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3455 : TargetCodeGenInfo(new NaClARMABIInfo(CGT, Kind)) {}
3456};
3457
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00003458}
3459
Derek Schuff263366f2012-10-16 22:30:41 +00003460void NaClARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
3461 if (FI.getASTCallingConvention() == CC_PnaclCall)
3462 PInfo.computeInfo(FI);
3463 else
3464 static_cast<const ABIInfo&>(NInfo).computeInfo(FI);
3465}
3466
3467llvm::Value *NaClARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3468 CodeGenFunction &CGF) const {
3469 // Always use the native convention; calling pnacl-style varargs functions
3470 // is unsupported.
3471 return static_cast<const ABIInfo&>(NInfo).EmitVAArg(VAListAddr, Ty, CGF);
3472}
3473
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003474//===----------------------------------------------------------------------===//
Justin Holewinski2c585b92012-05-24 17:43:12 +00003475// NVPTX ABI Implementation
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003476//===----------------------------------------------------------------------===//
3477
3478namespace {
3479
Justin Holewinski2c585b92012-05-24 17:43:12 +00003480class NVPTXABIInfo : public ABIInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003481public:
Justin Holewinski2c585b92012-05-24 17:43:12 +00003482 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003483
3484 ABIArgInfo classifyReturnType(QualType RetTy) const;
3485 ABIArgInfo classifyArgumentType(QualType Ty) const;
3486
3487 virtual void computeInfo(CGFunctionInfo &FI) const;
3488 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3489 CodeGenFunction &CFG) const;
3490};
3491
Justin Holewinski2c585b92012-05-24 17:43:12 +00003492class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003493public:
Justin Holewinski2c585b92012-05-24 17:43:12 +00003494 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
3495 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
Justin Holewinski818eafb2011-10-05 17:58:44 +00003496
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003497 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3498 CodeGen::CodeGenModule &M) const;
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003499};
3500
Justin Holewinski2c585b92012-05-24 17:43:12 +00003501ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003502 if (RetTy->isVoidType())
3503 return ABIArgInfo::getIgnore();
3504 if (isAggregateTypeForABI(RetTy))
3505 return ABIArgInfo::getIndirect(0);
3506 return ABIArgInfo::getDirect();
3507}
3508
Justin Holewinski2c585b92012-05-24 17:43:12 +00003509ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003510 if (isAggregateTypeForABI(Ty))
3511 return ABIArgInfo::getIndirect(0);
3512
3513 return ABIArgInfo::getDirect();
3514}
3515
Justin Holewinski2c585b92012-05-24 17:43:12 +00003516void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003517 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3518 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3519 it != ie; ++it)
3520 it->info = classifyArgumentType(it->type);
3521
3522 // Always honor user-specified calling convention.
3523 if (FI.getCallingConvention() != llvm::CallingConv::C)
3524 return;
3525
3526 // Calling convention as default by an ABI.
Justin Holewinski2c585b92012-05-24 17:43:12 +00003527 // We're still using the PTX_Kernel/PTX_Device calling conventions here,
3528 // but we should switch to NVVM metadata later on.
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003529 llvm::CallingConv::ID DefaultCC;
David Blaikie4e4d0842012-03-11 07:00:24 +00003530 const LangOptions &LangOpts = getContext().getLangOpts();
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003531 if (LangOpts.OpenCL || LangOpts.CUDA) {
3532 // If we are in OpenCL or CUDA mode, then default to device functions
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003533 DefaultCC = llvm::CallingConv::PTX_Device;
Justin Holewinski818eafb2011-10-05 17:58:44 +00003534 } else {
3535 // If we are in standard C/C++ mode, use the triple to decide on the default
3536 StringRef Env =
3537 getContext().getTargetInfo().getTriple().getEnvironmentName();
3538 if (Env == "device")
3539 DefaultCC = llvm::CallingConv::PTX_Device;
3540 else
3541 DefaultCC = llvm::CallingConv::PTX_Kernel;
3542 }
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003543 FI.setEffectiveCallingConvention(DefaultCC);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003544
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003545}
3546
Justin Holewinski2c585b92012-05-24 17:43:12 +00003547llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3548 CodeGenFunction &CFG) const {
3549 llvm_unreachable("NVPTX does not support varargs");
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003550}
3551
Justin Holewinski2c585b92012-05-24 17:43:12 +00003552void NVPTXTargetCodeGenInfo::
3553SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3554 CodeGen::CodeGenModule &M) const{
Justin Holewinski818eafb2011-10-05 17:58:44 +00003555 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3556 if (!FD) return;
3557
3558 llvm::Function *F = cast<llvm::Function>(GV);
3559
3560 // Perform special handling in OpenCL mode
David Blaikie4e4d0842012-03-11 07:00:24 +00003561 if (M.getLangOpts().OpenCL) {
Justin Holewinski818eafb2011-10-05 17:58:44 +00003562 // Use OpenCL function attributes to set proper calling conventions
3563 // By default, all functions are device functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00003564 if (FD->hasAttr<OpenCLKernelAttr>()) {
3565 // OpenCL __kernel functions get a kernel calling convention
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003566 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003567 // And kernel functions are not subject to inlining
Bill Wendlingfac63102012-10-10 03:13:20 +00003568 F->addFnAttr(llvm::Attributes::NoInline);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003569 }
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003570 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00003571
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003572 // Perform special handling in CUDA mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00003573 if (M.getLangOpts().CUDA) {
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003574 // CUDA __global__ functions get a kernel calling convention. Since
3575 // __global__ functions cannot be called from the device, we do not
3576 // need to set the noinline attribute.
3577 if (FD->getAttr<CUDAGlobalAttr>())
3578 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003579 }
3580}
3581
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003582}
3583
3584//===----------------------------------------------------------------------===//
Wesley Peck276fdf42010-12-19 19:57:51 +00003585// MBlaze ABI Implementation
3586//===----------------------------------------------------------------------===//
3587
3588namespace {
3589
3590class MBlazeABIInfo : public ABIInfo {
3591public:
3592 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3593
3594 bool isPromotableIntegerType(QualType Ty) const;
3595
3596 ABIArgInfo classifyReturnType(QualType RetTy) const;
3597 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3598
3599 virtual void computeInfo(CGFunctionInfo &FI) const {
3600 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3601 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3602 it != ie; ++it)
3603 it->info = classifyArgumentType(it->type);
3604 }
3605
3606 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3607 CodeGenFunction &CGF) const;
3608};
3609
3610class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
3611public:
3612 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
3613 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
3614 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3615 CodeGen::CodeGenModule &M) const;
3616};
3617
3618}
3619
3620bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
3621 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
3622 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
3623 switch (BT->getKind()) {
3624 case BuiltinType::Bool:
3625 case BuiltinType::Char_S:
3626 case BuiltinType::Char_U:
3627 case BuiltinType::SChar:
3628 case BuiltinType::UChar:
3629 case BuiltinType::Short:
3630 case BuiltinType::UShort:
3631 return true;
3632 default:
3633 return false;
3634 }
3635 return false;
3636}
3637
3638llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3639 CodeGenFunction &CGF) const {
3640 // FIXME: Implement
3641 return 0;
3642}
3643
3644
3645ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
3646 if (RetTy->isVoidType())
3647 return ABIArgInfo::getIgnore();
3648 if (isAggregateTypeForABI(RetTy))
3649 return ABIArgInfo::getIndirect(0);
3650
3651 return (isPromotableIntegerType(RetTy) ?
3652 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3653}
3654
3655ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
3656 if (isAggregateTypeForABI(Ty))
3657 return ABIArgInfo::getIndirect(0);
3658
3659 return (isPromotableIntegerType(Ty) ?
3660 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3661}
3662
3663void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3664 llvm::GlobalValue *GV,
3665 CodeGen::CodeGenModule &M)
3666 const {
3667 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3668 if (!FD) return;
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00003669
Wesley Peck276fdf42010-12-19 19:57:51 +00003670 llvm::CallingConv::ID CC = llvm::CallingConv::C;
3671 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
3672 CC = llvm::CallingConv::MBLAZE_INTR;
3673 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
3674 CC = llvm::CallingConv::MBLAZE_SVOL;
3675
3676 if (CC != llvm::CallingConv::C) {
3677 // Handle 'interrupt_handler' attribute:
3678 llvm::Function *F = cast<llvm::Function>(GV);
3679
3680 // Step 1: Set ISR calling convention.
3681 F->setCallingConv(CC);
3682
3683 // Step 2: Add attributes goodness.
Bill Wendlingfac63102012-10-10 03:13:20 +00003684 F->addFnAttr(llvm::Attributes::NoInline);
Wesley Peck276fdf42010-12-19 19:57:51 +00003685 }
3686
3687 // Step 3: Emit _interrupt_handler alias.
3688 if (CC == llvm::CallingConv::MBLAZE_INTR)
3689 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
3690 "_interrupt_handler", GV, &M.getModule());
3691}
3692
3693
3694//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003695// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003696//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003697
3698namespace {
3699
3700class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
3701public:
Chris Lattnerea044322010-07-29 02:01:43 +00003702 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
3703 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003704 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3705 CodeGen::CodeGenModule &M) const;
3706};
3707
3708}
3709
3710void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3711 llvm::GlobalValue *GV,
3712 CodeGen::CodeGenModule &M) const {
3713 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
3714 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
3715 // Handle 'interrupt' attribute:
3716 llvm::Function *F = cast<llvm::Function>(GV);
3717
3718 // Step 1: Set ISR calling convention.
3719 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
3720
3721 // Step 2: Add attributes goodness.
Bill Wendlingfac63102012-10-10 03:13:20 +00003722 F->addFnAttr(llvm::Attributes::NoInline);
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003723
3724 // Step 3: Emit ISR vector alias.
3725 unsigned Num = attr->getNumber() + 0xffe0;
3726 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003727 "vector_" + Twine::utohexstr(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003728 GV, &M.getModule());
3729 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003730 }
3731}
3732
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003733//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00003734// MIPS ABI Implementation. This works for both little-endian and
3735// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003736//===----------------------------------------------------------------------===//
3737
John McCallaeeb7012010-05-27 06:19:26 +00003738namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00003739class MipsABIInfo : public ABIInfo {
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003740 bool IsO32;
Akira Hatanakac359f202012-07-03 19:24:06 +00003741 unsigned MinABIStackAlignInBytes, StackAlignInBytes;
3742 void CoerceToIntArgs(uint64_t TySize,
3743 SmallVector<llvm::Type*, 8> &ArgList) const;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003744 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003745 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003746 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003747public:
Akira Hatanakab551dd32011-11-03 00:05:50 +00003748 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
Akira Hatanakac359f202012-07-03 19:24:06 +00003749 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
3750 StackAlignInBytes(IsO32 ? 8 : 16) {}
Akira Hatanaka619e8872011-06-02 00:09:17 +00003751
3752 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003753 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003754 virtual void computeInfo(CGFunctionInfo &FI) const;
3755 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3756 CodeGenFunction &CGF) const;
3757};
3758
John McCallaeeb7012010-05-27 06:19:26 +00003759class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003760 unsigned SizeOfUnwindException;
John McCallaeeb7012010-05-27 06:19:26 +00003761public:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003762 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
3763 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
3764 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCallaeeb7012010-05-27 06:19:26 +00003765
3766 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
3767 return 29;
3768 }
3769
3770 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003771 llvm::Value *Address) const;
John McCall49e34be2011-08-30 01:42:09 +00003772
3773 unsigned getSizeOfUnwindException() const {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003774 return SizeOfUnwindException;
John McCall49e34be2011-08-30 01:42:09 +00003775 }
John McCallaeeb7012010-05-27 06:19:26 +00003776};
3777}
3778
Akira Hatanakac359f202012-07-03 19:24:06 +00003779void MipsABIInfo::CoerceToIntArgs(uint64_t TySize,
3780 SmallVector<llvm::Type*, 8> &ArgList) const {
3781 llvm::IntegerType *IntTy =
3782 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003783
3784 // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
3785 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
3786 ArgList.push_back(IntTy);
3787
3788 // If necessary, add one more integer type to ArgList.
3789 unsigned R = TySize % (MinABIStackAlignInBytes * 8);
3790
3791 if (R)
3792 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003793}
3794
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003795// In N32/64, an aligned double precision floating point field is passed in
3796// a register.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003797llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
Akira Hatanakac359f202012-07-03 19:24:06 +00003798 SmallVector<llvm::Type*, 8> ArgList, IntArgList;
3799
3800 if (IsO32) {
3801 CoerceToIntArgs(TySize, ArgList);
3802 return llvm::StructType::get(getVMContext(), ArgList);
3803 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003804
Akira Hatanaka2afd23d2012-01-12 00:52:17 +00003805 if (Ty->isComplexType())
3806 return CGT.ConvertType(Ty);
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00003807
Akira Hatanakaa34e9212012-02-09 19:54:16 +00003808 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003809
Akira Hatanakac359f202012-07-03 19:24:06 +00003810 // Unions/vectors are passed in integer registers.
3811 if (!RT || !RT->isStructureOrClassType()) {
3812 CoerceToIntArgs(TySize, ArgList);
3813 return llvm::StructType::get(getVMContext(), ArgList);
3814 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003815
3816 const RecordDecl *RD = RT->getDecl();
3817 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003818 assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003819
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003820 uint64_t LastOffset = 0;
3821 unsigned idx = 0;
3822 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
3823
Akira Hatanakaa34e9212012-02-09 19:54:16 +00003824 // Iterate over fields in the struct/class and check if there are any aligned
3825 // double fields.
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003826 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3827 i != e; ++i, ++idx) {
David Blaikie262bc182012-04-30 02:36:29 +00003828 const QualType Ty = i->getType();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003829 const BuiltinType *BT = Ty->getAs<BuiltinType>();
3830
3831 if (!BT || BT->getKind() != BuiltinType::Double)
3832 continue;
3833
3834 uint64_t Offset = Layout.getFieldOffset(idx);
3835 if (Offset % 64) // Ignore doubles that are not aligned.
3836 continue;
3837
3838 // Add ((Offset - LastOffset) / 64) args of type i64.
3839 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
3840 ArgList.push_back(I64);
3841
3842 // Add double type.
3843 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
3844 LastOffset = Offset + 64;
3845 }
3846
Akira Hatanakac359f202012-07-03 19:24:06 +00003847 CoerceToIntArgs(TySize - LastOffset, IntArgList);
3848 ArgList.append(IntArgList.begin(), IntArgList.end());
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003849
3850 return llvm::StructType::get(getVMContext(), ArgList);
3851}
3852
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003853llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003854 assert((Offset % MinABIStackAlignInBytes) == 0);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003855
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003856 if ((Align - 1) & Offset)
3857 return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
3858
3859 return 0;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003860}
Akira Hatanaka9659d592012-01-10 22:44:52 +00003861
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003862ABIArgInfo
3863MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003864 uint64_t OrigOffset = Offset;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003865 uint64_t TySize = getContext().getTypeSize(Ty);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003866 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003867
Akira Hatanakac359f202012-07-03 19:24:06 +00003868 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
3869 (uint64_t)StackAlignInBytes);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003870 Offset = llvm::RoundUpToAlignment(Offset, Align);
3871 Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003872
Akira Hatanakac359f202012-07-03 19:24:06 +00003873 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
Akira Hatanaka619e8872011-06-02 00:09:17 +00003874 // Ignore empty aggregates.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003875 if (TySize == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00003876 return ABIArgInfo::getIgnore();
3877
Akira Hatanaka511949b2011-08-01 18:09:58 +00003878 // Records with non trivial destructors/constructors should not be passed
3879 // by value.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003880 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003881 Offset = OrigOffset + MinABIStackAlignInBytes;
Akira Hatanaka511949b2011-08-01 18:09:58 +00003882 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003883 }
Akira Hatanaka511949b2011-08-01 18:09:58 +00003884
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003885 // If we have reached here, aggregates are passed directly by coercing to
3886 // another structure type. Padding is inserted if the offset of the
3887 // aggregate is unaligned.
3888 return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
3889 getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00003890 }
3891
3892 // Treat an enum type as its underlying type.
3893 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3894 Ty = EnumTy->getDecl()->getIntegerType();
3895
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003896 if (Ty->isPromotableIntegerType())
3897 return ABIArgInfo::getExtend();
3898
3899 return ABIArgInfo::getDirect(0, 0, getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00003900}
3901
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003902llvm::Type*
3903MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakada54ff32012-02-09 18:49:26 +00003904 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakac359f202012-07-03 19:24:06 +00003905 SmallVector<llvm::Type*, 8> RTList;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003906
Akira Hatanakada54ff32012-02-09 18:49:26 +00003907 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003908 const RecordDecl *RD = RT->getDecl();
Akira Hatanakada54ff32012-02-09 18:49:26 +00003909 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3910 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003911
Akira Hatanakada54ff32012-02-09 18:49:26 +00003912 // N32/64 returns struct/classes in floating point registers if the
3913 // following conditions are met:
3914 // 1. The size of the struct/class is no larger than 128-bit.
3915 // 2. The struct/class has one or two fields all of which are floating
3916 // point types.
3917 // 3. The offset of the first field is zero (this follows what gcc does).
3918 //
3919 // Any other composite results are returned in integer registers.
3920 //
3921 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
3922 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
3923 for (; b != e; ++b) {
David Blaikie262bc182012-04-30 02:36:29 +00003924 const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003925
Akira Hatanakada54ff32012-02-09 18:49:26 +00003926 if (!BT || !BT->isFloatingPoint())
3927 break;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003928
David Blaikie262bc182012-04-30 02:36:29 +00003929 RTList.push_back(CGT.ConvertType(b->getType()));
Akira Hatanakada54ff32012-02-09 18:49:26 +00003930 }
3931
3932 if (b == e)
3933 return llvm::StructType::get(getVMContext(), RTList,
3934 RD->hasAttr<PackedAttr>());
3935
3936 RTList.clear();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003937 }
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003938 }
3939
Akira Hatanakac359f202012-07-03 19:24:06 +00003940 CoerceToIntArgs(Size, RTList);
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003941 return llvm::StructType::get(getVMContext(), RTList);
3942}
3943
Akira Hatanaka619e8872011-06-02 00:09:17 +00003944ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanakaa8536c02012-01-23 23:18:57 +00003945 uint64_t Size = getContext().getTypeSize(RetTy);
3946
3947 if (RetTy->isVoidType() || Size == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00003948 return ABIArgInfo::getIgnore();
3949
Akira Hatanaka8aeb1472012-05-11 21:01:17 +00003950 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003951 if (Size <= 128) {
3952 if (RetTy->isAnyComplexType())
3953 return ABIArgInfo::getDirect();
3954
Akira Hatanakac359f202012-07-03 19:24:06 +00003955 // O32 returns integer vectors in registers.
3956 if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())
3957 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
3958
Akira Hatanaka526cdfb2012-02-08 01:31:22 +00003959 if (!IsO32 && !isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003960 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
3961 }
Akira Hatanaka619e8872011-06-02 00:09:17 +00003962
3963 return ABIArgInfo::getIndirect(0);
3964 }
3965
3966 // Treat an enum type as its underlying type.
3967 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3968 RetTy = EnumTy->getDecl()->getIntegerType();
3969
3970 return (RetTy->isPromotableIntegerType() ?
3971 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3972}
3973
3974void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanakacc662542012-01-12 01:10:09 +00003975 ABIArgInfo &RetInfo = FI.getReturnInfo();
3976 RetInfo = classifyReturnType(FI.getReturnType());
3977
3978 // Check if a pointer to an aggregate is passed as a hidden argument.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003979 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
Akira Hatanakacc662542012-01-12 01:10:09 +00003980
Akira Hatanaka619e8872011-06-02 00:09:17 +00003981 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3982 it != ie; ++it)
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003983 it->info = classifyArgumentType(it->type, Offset);
Akira Hatanaka619e8872011-06-02 00:09:17 +00003984}
3985
3986llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3987 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003988 llvm::Type *BP = CGF.Int8PtrTy;
3989 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003990
3991 CGBuilderTy &Builder = CGF.Builder;
3992 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3993 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003994 int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003995 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3996 llvm::Value *AddrTyped;
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003997 unsigned PtrWidth = getContext().getTargetInfo().getPointerWidth(0);
3998 llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003999
4000 if (TypeAlign > MinABIStackAlignInBytes) {
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004001 llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
4002 llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
4003 llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
4004 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004005 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
4006 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
4007 }
4008 else
4009 AddrTyped = Builder.CreateBitCast(Addr, PTy);
4010
4011 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004012 TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004013 uint64_t Offset =
4014 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
4015 llvm::Value *NextAddr =
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004016 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004017 "ap.next");
4018 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
4019
4020 return AddrTyped;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004021}
4022
John McCallaeeb7012010-05-27 06:19:26 +00004023bool
4024MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4025 llvm::Value *Address) const {
4026 // This information comes from gcc's implementation, which seems to
4027 // as canonical as it gets.
4028
John McCallaeeb7012010-05-27 06:19:26 +00004029 // Everything on MIPS is 4 bytes. Double-precision FP registers
4030 // are aliased to pairs of single-precision FP registers.
Chris Lattner8b418682012-02-07 00:39:47 +00004031 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCallaeeb7012010-05-27 06:19:26 +00004032
4033 // 0-31 are the general purpose registers, $0 - $31.
4034 // 32-63 are the floating-point registers, $f0 - $f31.
4035 // 64 and 65 are the multiply/divide registers, $hi and $lo.
4036 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattner8b418682012-02-07 00:39:47 +00004037 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCallaeeb7012010-05-27 06:19:26 +00004038
4039 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
4040 // They are one bit wide and ignored here.
4041
4042 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
4043 // (coprocessor 1 is the FP unit)
4044 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
4045 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
4046 // 176-181 are the DSP accumulator registers.
Chris Lattner8b418682012-02-07 00:39:47 +00004047 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCallaeeb7012010-05-27 06:19:26 +00004048 return false;
4049}
4050
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004051//===----------------------------------------------------------------------===//
4052// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
4053// Currently subclassed only to implement custom OpenCL C function attribute
4054// handling.
4055//===----------------------------------------------------------------------===//
4056
4057namespace {
4058
4059class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
4060public:
4061 TCETargetCodeGenInfo(CodeGenTypes &CGT)
4062 : DefaultTargetCodeGenInfo(CGT) {}
4063
4064 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4065 CodeGen::CodeGenModule &M) const;
4066};
4067
4068void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4069 llvm::GlobalValue *GV,
4070 CodeGen::CodeGenModule &M) const {
4071 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4072 if (!FD) return;
4073
4074 llvm::Function *F = cast<llvm::Function>(GV);
4075
David Blaikie4e4d0842012-03-11 07:00:24 +00004076 if (M.getLangOpts().OpenCL) {
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004077 if (FD->hasAttr<OpenCLKernelAttr>()) {
4078 // OpenCL C Kernel functions are not subject to inlining
Bill Wendlingfac63102012-10-10 03:13:20 +00004079 F->addFnAttr(llvm::Attributes::NoInline);
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004080
4081 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
4082
4083 // Convert the reqd_work_group_size() attributes to metadata.
4084 llvm::LLVMContext &Context = F->getContext();
4085 llvm::NamedMDNode *OpenCLMetadata =
4086 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
4087
4088 SmallVector<llvm::Value*, 5> Operands;
4089 Operands.push_back(F);
4090
Chris Lattner8b418682012-02-07 00:39:47 +00004091 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4092 llvm::APInt(32,
4093 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
4094 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4095 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004096 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
Chris Lattner8b418682012-02-07 00:39:47 +00004097 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4098 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004099 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
4100
4101 // Add a boolean constant operand for "required" (true) or "hint" (false)
4102 // for implementing the work_group_size_hint attr later. Currently
4103 // always true as the hint is not yet implemented.
Chris Lattner8b418682012-02-07 00:39:47 +00004104 Operands.push_back(llvm::ConstantInt::getTrue(Context));
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004105 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
4106 }
4107 }
4108 }
4109}
4110
4111}
John McCallaeeb7012010-05-27 06:19:26 +00004112
Tony Linthicum96319392011-12-12 21:14:55 +00004113//===----------------------------------------------------------------------===//
4114// Hexagon ABI Implementation
4115//===----------------------------------------------------------------------===//
4116
4117namespace {
4118
4119class HexagonABIInfo : public ABIInfo {
4120
4121
4122public:
4123 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
4124
4125private:
4126
4127 ABIArgInfo classifyReturnType(QualType RetTy) const;
4128 ABIArgInfo classifyArgumentType(QualType RetTy) const;
4129
4130 virtual void computeInfo(CGFunctionInfo &FI) const;
4131
4132 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4133 CodeGenFunction &CGF) const;
4134};
4135
4136class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
4137public:
4138 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
4139 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
4140
4141 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
4142 return 29;
4143 }
4144};
4145
4146}
4147
4148void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
4149 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4150 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4151 it != ie; ++it)
4152 it->info = classifyArgumentType(it->type);
4153}
4154
4155ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
4156 if (!isAggregateTypeForABI(Ty)) {
4157 // Treat an enum type as its underlying type.
4158 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4159 Ty = EnumTy->getDecl()->getIntegerType();
4160
4161 return (Ty->isPromotableIntegerType() ?
4162 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4163 }
4164
4165 // Ignore empty records.
4166 if (isEmptyRecord(getContext(), Ty, true))
4167 return ABIArgInfo::getIgnore();
4168
4169 // Structures with either a non-trivial destructor or a non-trivial
4170 // copy constructor are always indirect.
4171 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
4172 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
4173
4174 uint64_t Size = getContext().getTypeSize(Ty);
4175 if (Size > 64)
4176 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
4177 // Pass in the smallest viable integer type.
4178 else if (Size > 32)
4179 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
4180 else if (Size > 16)
4181 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
4182 else if (Size > 8)
4183 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
4184 else
4185 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
4186}
4187
4188ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
4189 if (RetTy->isVoidType())
4190 return ABIArgInfo::getIgnore();
4191
4192 // Large vector types should be returned via memory.
4193 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
4194 return ABIArgInfo::getIndirect(0);
4195
4196 if (!isAggregateTypeForABI(RetTy)) {
4197 // Treat an enum type as its underlying type.
4198 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4199 RetTy = EnumTy->getDecl()->getIntegerType();
4200
4201 return (RetTy->isPromotableIntegerType() ?
4202 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4203 }
4204
4205 // Structures with either a non-trivial destructor or a non-trivial
4206 // copy constructor are always indirect.
4207 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
4208 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
4209
4210 if (isEmptyRecord(getContext(), RetTy, true))
4211 return ABIArgInfo::getIgnore();
4212
4213 // Aggregates <= 8 bytes are returned in r0; other aggregates
4214 // are returned indirectly.
4215 uint64_t Size = getContext().getTypeSize(RetTy);
4216 if (Size <= 64) {
4217 // Return in the smallest viable integer type.
4218 if (Size <= 8)
4219 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
4220 if (Size <= 16)
4221 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
4222 if (Size <= 32)
4223 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
4224 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
4225 }
4226
4227 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
4228}
4229
4230llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner8b418682012-02-07 00:39:47 +00004231 CodeGenFunction &CGF) const {
Tony Linthicum96319392011-12-12 21:14:55 +00004232 // FIXME: Need to handle alignment
Chris Lattner8b418682012-02-07 00:39:47 +00004233 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Tony Linthicum96319392011-12-12 21:14:55 +00004234
4235 CGBuilderTy &Builder = CGF.Builder;
4236 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
4237 "ap");
4238 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
4239 llvm::Type *PTy =
4240 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
4241 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
4242
4243 uint64_t Offset =
4244 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
4245 llvm::Value *NextAddr =
4246 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
4247 "ap.next");
4248 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
4249
4250 return AddrTyped;
4251}
4252
4253
Chris Lattnerea044322010-07-29 02:01:43 +00004254const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004255 if (TheTargetCodeGenInfo)
4256 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004257
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004258 const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00004259 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004260 default:
Chris Lattnerea044322010-07-29 02:01:43 +00004261 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004262
Derek Schuff9ed63f82012-09-06 17:37:28 +00004263 case llvm::Triple::le32:
4264 return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
John McCallaeeb7012010-05-27 06:19:26 +00004265 case llvm::Triple::mips:
4266 case llvm::Triple::mipsel:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004267 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCallaeeb7012010-05-27 06:19:26 +00004268
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00004269 case llvm::Triple::mips64:
4270 case llvm::Triple::mips64el:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004271 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00004272
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004273 case llvm::Triple::arm:
4274 case llvm::Triple::thumb:
Sandeep Patel34c1af82011-04-05 00:23:47 +00004275 {
4276 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004277 if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
Sandeep Patel34c1af82011-04-05 00:23:47 +00004278 Kind = ARMABIInfo::APCS;
David Tweedb16abb12012-10-25 13:33:01 +00004279 else if (CodeGenOpts.FloatABI == "hard" ||
4280 (CodeGenOpts.FloatABI != "soft" && Triple.getEnvironment()==llvm::Triple::GNUEABIHF))
Sandeep Patel34c1af82011-04-05 00:23:47 +00004281 Kind = ARMABIInfo::AAPCS_VFP;
4282
Derek Schuff263366f2012-10-16 22:30:41 +00004283 switch (Triple.getOS()) {
4284 case llvm::Triple::NativeClient:
4285 return *(TheTargetCodeGenInfo =
4286 new NaClARMTargetCodeGenInfo(Types, Kind));
4287 default:
4288 return *(TheTargetCodeGenInfo =
4289 new ARMTargetCodeGenInfo(Types, Kind));
4290 }
Sandeep Patel34c1af82011-04-05 00:23:47 +00004291 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004292
John McCallec853ba2010-03-11 00:10:12 +00004293 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00004294 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
Roman Divacky0fbc4b92012-05-09 18:22:46 +00004295 case llvm::Triple::ppc64:
Bill Schmidt2fc107f2012-10-03 19:18:57 +00004296 if (Triple.isOSBinFormatELF())
4297 return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types));
4298 else
4299 return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00004300
Peter Collingbourneedb66f32012-05-20 23:28:41 +00004301 case llvm::Triple::nvptx:
4302 case llvm::Triple::nvptx64:
Justin Holewinski2c585b92012-05-24 17:43:12 +00004303 return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types));
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004304
Wesley Peck276fdf42010-12-19 19:57:51 +00004305 case llvm::Triple::mblaze:
4306 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
4307
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004308 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00004309 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004310
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004311 case llvm::Triple::tce:
4312 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
4313
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004314 case llvm::Triple::x86: {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004315 bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004316
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00004317 if (Triple.isOSDarwin())
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004318 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004319 new X86_32TargetCodeGenInfo(Types, true, true, DisableMMX, false,
4320 CodeGenOpts.NumRegisterParameters));
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00004321
4322 switch (Triple.getOS()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004323 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004324 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00004325 case llvm::Triple::AuroraUX:
4326 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00004327 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004328 case llvm::Triple::OpenBSD:
Eli Friedman42f74f22012-08-08 23:57:20 +00004329 case llvm::Triple::Bitrig:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004330 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004331 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX,
4332 false,
4333 CodeGenOpts.NumRegisterParameters));
Eli Friedman55fc7e22012-01-25 22:46:34 +00004334
4335 case llvm::Triple::Win32:
4336 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004337 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX, true,
4338 CodeGenOpts.NumRegisterParameters));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004339
4340 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004341 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004342 new X86_32TargetCodeGenInfo(Types, false, false, DisableMMX,
4343 false,
4344 CodeGenOpts.NumRegisterParameters));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004345 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004346 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004347
Eli Friedmanee1ad992011-12-02 00:11:43 +00004348 case llvm::Triple::x86_64: {
4349 bool HasAVX = strcmp(getContext().getTargetInfo().getABI(), "avx") == 0;
4350
Chris Lattnerf13721d2010-08-31 16:44:54 +00004351 switch (Triple.getOS()) {
4352 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00004353 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00004354 case llvm::Triple::Cygwin:
4355 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
Derek Schuff263366f2012-10-16 22:30:41 +00004356 case llvm::Triple::NativeClient:
4357 return *(TheTargetCodeGenInfo = new NaClX86_64TargetCodeGenInfo(Types, HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00004358 default:
Eli Friedmanee1ad992011-12-02 00:11:43 +00004359 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
4360 HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00004361 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004362 }
Tony Linthicum96319392011-12-12 21:14:55 +00004363 case llvm::Triple::hexagon:
4364 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
Eli Friedmanee1ad992011-12-02 00:11:43 +00004365 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004366}