blob: ca9cfbbfef23e5eb5d8de2911f16a58859ead679 [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 Espindola0b4cc952012-10-19 05:04:37 +0000531 ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &FreeRegs) const;
532 bool shouldUseInReg(QualType Ty, unsigned &FreeRegs) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000533
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000534public:
535
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000536 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000537 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
538 CodeGenFunction &CGF) const;
539
Rafael Espindolab48280b2012-07-31 02:44:24 +0000540 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m, bool w,
541 unsigned r)
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000542 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
Rafael Espindolab48280b2012-07-31 02:44:24 +0000543 IsMMXDisabled(m), IsWin32FloatStructABI(w),
544 DefaultNumRegisterParameters(r) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000545};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000546
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000547class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
548public:
Eli Friedman55fc7e22012-01-25 22:46:34 +0000549 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
Rafael Espindolab48280b2012-07-31 02:44:24 +0000550 bool d, bool p, bool m, bool w, unsigned r)
551 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, m, w, r)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000552
553 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
554 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000555
556 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
557 // Darwin uses different dwarf register numbers for EH.
558 if (CGM.isTargetDarwin()) return 5;
559
560 return 4;
561 }
562
563 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
564 llvm::Value *Address) const;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000565
Jay Foadef6de3d2011-07-11 09:56:20 +0000566 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000567 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000568 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000569 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
570 }
571
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000572};
573
574}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000575
576/// shouldReturnTypeInRegister - Determine if the given type should be
577/// passed in a register (for the Darwin ABI).
578bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000579 ASTContext &Context,
580 unsigned callingConvention) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000581 uint64_t Size = Context.getTypeSize(Ty);
582
583 // Type must be register sized.
584 if (!isRegisterSize(Size))
585 return false;
586
587 if (Ty->isVectorType()) {
588 // 64- and 128- bit vectors inside structures are not returned in
589 // registers.
590 if (Size == 64 || Size == 128)
591 return false;
592
593 return true;
594 }
595
Daniel Dunbar77115232010-05-15 00:00:30 +0000596 // If this is a builtin, pointer, enum, complex type, member pointer, or
597 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000598 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000599 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000600 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000601 return true;
602
603 // Arrays are treated like records.
604 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000605 return shouldReturnTypeInRegister(AT->getElementType(), Context,
606 callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000607
608 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000609 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000610 if (!RT) return false;
611
Anders Carlssona8874232010-01-27 03:25:19 +0000612 // FIXME: Traverse bases here too.
613
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000614 // For thiscall conventions, structures will never be returned in
615 // a register. This is for compatibility with the MSVC ABI
616 if (callingConvention == llvm::CallingConv::X86_ThisCall &&
617 RT->isStructureType()) {
618 return false;
619 }
620
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000621 // Structure types are passed in register if all fields would be
622 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000623 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
624 e = RT->getDecl()->field_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000625 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000626
627 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000628 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000629 continue;
630
631 // Check fields recursively.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000632 if (!shouldReturnTypeInRegister(FD->getType(), Context,
633 callingConvention))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000634 return false;
635 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000636 return true;
637}
638
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000639ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
640 unsigned callingConvention) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000641 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000642 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000643
Chris Lattnera3c109b2010-07-29 02:16:43 +0000644 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000645 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000646 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000647 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000648
649 // 128-bit vectors are a special case; they are returned in
650 // registers and we need to make sure to pick a type the LLVM
651 // backend will like.
652 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +0000653 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000654 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000655
656 // Always return in register if it fits in a general purpose
657 // register, or if it is 64 bits and has a single element.
658 if ((Size == 8 || Size == 16 || Size == 32) ||
659 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +0000660 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000661 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000662
663 return ABIArgInfo::getIndirect(0);
664 }
665
666 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000667 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000668
John McCalld608cdb2010-08-22 10:59:02 +0000669 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000670 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000671 // Structures with either a non-trivial destructor or a non-trivial
672 // copy constructor are always indirect.
673 if (hasNonTrivialDestructorOrCopyConstructor(RT))
674 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000675
Anders Carlsson40092972009-10-20 22:07:59 +0000676 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000677 if (RT->getDecl()->hasFlexibleArrayMember())
678 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000679 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000680
David Chisnall1e4249c2009-08-17 23:08:21 +0000681 // If specified, structs and unions are always indirect.
682 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000683 return ABIArgInfo::getIndirect(0);
684
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000685 // Small structures which are register sized are generally returned
686 // in a register.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000687 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(),
688 callingConvention)) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000689 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000690
691 // As a special-case, if the struct is a "single-element" struct, and
692 // the field is of type "float" or "double", return it in a
Eli Friedman55fc7e22012-01-25 22:46:34 +0000693 // floating-point register. (MSVC does not apply this special case.)
694 // We apply a similar transformation for pointer types to improve the
695 // quality of the generated IR.
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000696 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
Eli Friedman55fc7e22012-01-25 22:46:34 +0000697 if ((!IsWin32FloatStructABI && SeltTy->isRealFloatingType())
698 || SeltTy->hasPointerRepresentation())
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000699 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
700
701 // FIXME: We should be able to narrow this integer in cases with dead
702 // padding.
Chris Lattner800588f2010-07-29 06:26:06 +0000703 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000704 }
705
706 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000707 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000708
Chris Lattnera3c109b2010-07-29 02:16:43 +0000709 // Treat an enum type as its underlying type.
710 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
711 RetTy = EnumTy->getDecl()->getIntegerType();
712
713 return (RetTy->isPromotableIntegerType() ?
714 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000715}
716
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000717static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
718 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
719}
720
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000721static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
722 const RecordType *RT = Ty->getAs<RecordType>();
723 if (!RT)
724 return 0;
725 const RecordDecl *RD = RT->getDecl();
726
727 // If this is a C++ record, check the bases first.
728 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
729 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
730 e = CXXRD->bases_end(); i != e; ++i)
731 if (!isRecordWithSSEVectorType(Context, i->getType()))
732 return false;
733
734 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
735 i != e; ++i) {
736 QualType FT = i->getType();
737
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000738 if (isSSEVectorType(Context, FT))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000739 return true;
740
741 if (isRecordWithSSEVectorType(Context, FT))
742 return true;
743 }
744
745 return false;
746}
747
Daniel Dunbare59d8582010-09-16 20:42:06 +0000748unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
749 unsigned Align) const {
750 // Otherwise, if the alignment is less than or equal to the minimum ABI
751 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000752 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +0000753 return 0; // Use default alignment.
754
755 // On non-Darwin, the stack type alignment is always 4.
756 if (!IsDarwinVectorABI) {
757 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000758 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +0000759 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000760
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000761 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000762 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
763 isRecordWithSSEVectorType(getContext(), Ty)))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000764 return 16;
765
766 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000767}
768
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000769ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
770 unsigned &FreeRegs) const {
771 if (!ByVal) {
772 if (FreeRegs) {
773 --FreeRegs; // Non byval indirects just use one pointer.
774 return ABIArgInfo::getIndirectInReg(0, false);
775 }
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000776 return ABIArgInfo::getIndirect(0, false);
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000777 }
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000778
Daniel Dunbare59d8582010-09-16 20:42:06 +0000779 // Compute the byval alignment.
780 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
781 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
782 if (StackAlign == 0)
Chris Lattnerde92d732011-05-22 23:35:00 +0000783 return ABIArgInfo::getIndirect(4);
Daniel Dunbare59d8582010-09-16 20:42:06 +0000784
785 // If the stack alignment is less than the type alignment, realign the
786 // argument.
787 if (StackAlign < TypeAlign)
788 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
789 /*Realign=*/true);
790
791 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000792}
793
Rafael Espindolab48280b2012-07-31 02:44:24 +0000794X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
795 const Type *T = isSingleElementStruct(Ty, getContext());
796 if (!T)
797 T = Ty.getTypePtr();
798
799 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
800 BuiltinType::Kind K = BT->getKind();
801 if (K == BuiltinType::Float || K == BuiltinType::Double)
802 return Float;
803 }
804 return Integer;
805}
806
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000807bool X86_32ABIInfo::shouldUseInReg(QualType Ty, unsigned &FreeRegs) const {
Rafael Espindolab48280b2012-07-31 02:44:24 +0000808 Class C = classify(Ty);
809 if (C == Float)
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000810 return false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000811
812 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000813 if (SizeInRegs > FreeRegs) {
814 FreeRegs = 0;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000815 return false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000816 }
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000817
Rafael Espindolab48280b2012-07-31 02:44:24 +0000818 FreeRegs -= SizeInRegs;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000819 return true;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000820}
821
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000822ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
823 unsigned &FreeRegs) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000824 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000825 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000826 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000827 if (const RecordType *RT = Ty->getAs<RecordType>()) {
828 // Structures with either a non-trivial destructor or a non-trivial
829 // copy constructor are always indirect.
830 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000831 return getIndirectResult(Ty, false, FreeRegs);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000832
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000833 if (RT->getDecl()->hasFlexibleArrayMember())
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000834 return getIndirectResult(Ty, true, FreeRegs);
Anders Carlssona8874232010-01-27 03:25:19 +0000835 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000836
Eli Friedman5a4d3522011-11-18 00:28:11 +0000837 // Ignore empty structs/unions.
Eli Friedman5a1ac892011-11-18 04:01:36 +0000838 if (isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000839 return ABIArgInfo::getIgnore();
840
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000841 if (shouldUseInReg(Ty, FreeRegs)) {
842 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
843 llvm::LLVMContext &LLVMContext = getVMContext();
844 llvm::Type *Int32 = llvm::Type::getInt32Ty(LLVMContext);
845 SmallVector<llvm::Type*, 3> Elements;
846 for (unsigned I = 0; I < SizeInRegs; ++I)
847 Elements.push_back(Int32);
848 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
849 return ABIArgInfo::getDirectInReg(Result);
850 }
851
Daniel Dunbar53012f42009-11-09 01:33:53 +0000852 // Expand small (<= 128-bit) record types when we know that the stack layout
853 // of those arguments will match the struct. This is important because the
854 // LLVM backend isn't smart enough to remove byval, which inhibits many
855 // optimizations.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000856 if (getContext().getTypeSize(Ty) <= 4*32 &&
857 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar53012f42009-11-09 01:33:53 +0000858 return ABIArgInfo::getExpand();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000859
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000860 return getIndirectResult(Ty, true, FreeRegs);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000861 }
862
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000863 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +0000864 // On Darwin, some vectors are passed in memory, we handle this by passing
865 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000866 if (IsDarwinVectorABI) {
867 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000868 if ((Size == 8 || Size == 16 || Size == 32) ||
869 (Size == 64 && VT->getNumElements() == 1))
870 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
871 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000872 }
Bill Wendlingbb465d72010-10-18 03:41:31 +0000873
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000874 llvm::Type *IRType = CGT.ConvertType(Ty);
Bill Wendlingbb465d72010-10-18 03:41:31 +0000875 if (UseX86_MMXType(IRType)) {
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000876 if (IsMMXDisabled)
877 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
878 64));
Bill Wendlingbb465d72010-10-18 03:41:31 +0000879 ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
880 AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
881 return AAI;
882 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000883
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000884 return ABIArgInfo::getDirect();
885 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000886
887
Chris Lattnera3c109b2010-07-29 02:16:43 +0000888 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
889 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000890
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000891 bool InReg = shouldUseInReg(Ty, FreeRegs);
892
893 if (Ty->isPromotableIntegerType()) {
894 if (InReg)
895 return ABIArgInfo::getExtendInReg();
896 return ABIArgInfo::getExtend();
897 }
898 if (InReg)
899 return ABIArgInfo::getDirectInReg();
900 return ABIArgInfo::getDirect();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000901}
902
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000903void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
904 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
905 FI.getCallingConvention());
Rafael Espindolab48280b2012-07-31 02:44:24 +0000906
907 unsigned FreeRegs = FI.getHasRegParm() ? FI.getRegParm() :
908 DefaultNumRegisterParameters;
909
910 // If the return value is indirect, then the hidden argument is consuming one
911 // integer register.
912 if (FI.getReturnInfo().isIndirect() && FreeRegs) {
913 --FreeRegs;
914 ABIArgInfo &Old = FI.getReturnInfo();
915 Old = ABIArgInfo::getIndirectInReg(Old.getIndirectAlign(),
916 Old.getIndirectByVal(),
917 Old.getIndirectRealign());
918 }
919
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000920 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
921 it != ie; ++it)
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000922 it->info = classifyArgumentType(it->type, FreeRegs);
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000923}
924
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000925llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
926 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +0000927 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000928
929 CGBuilderTy &Builder = CGF.Builder;
930 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
931 "ap");
932 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Eli Friedman7b1fb812011-11-18 02:12:09 +0000933
934 // Compute if the address needs to be aligned
935 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
936 Align = getTypeStackAlignInBytes(Ty, Align);
937 Align = std::max(Align, 4U);
938 if (Align > 4) {
939 // addr = (addr + align - 1) & -align;
940 llvm::Value *Offset =
941 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
942 Addr = CGF.Builder.CreateGEP(Addr, Offset);
943 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
944 CGF.Int32Ty);
945 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
946 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
947 Addr->getType(),
948 "ap.cur.aligned");
949 }
950
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000951 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000952 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000953 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
954
955 uint64_t Offset =
Eli Friedman7b1fb812011-11-18 02:12:09 +0000956 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000957 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +0000958 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000959 "ap.next");
960 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
961
962 return AddrTyped;
963}
964
Charles Davis74f72932010-02-13 15:54:06 +0000965void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
966 llvm::GlobalValue *GV,
967 CodeGen::CodeGenModule &CGM) const {
968 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
969 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
970 // Get the LLVM function.
971 llvm::Function *Fn = cast<llvm::Function>(GV);
972
973 // Now add the 'alignstack' attribute with a value of 16.
Bill Wendling0d583392012-10-15 20:36:26 +0000974 llvm::AttrBuilder B;
Bill Wendlinge91e9ec2012-10-14 03:28:14 +0000975 B.addStackAlignmentAttr(16);
Bill Wendling75d37b42012-10-15 07:31:59 +0000976 Fn->addAttribute(llvm::AttrListPtr::FunctionIndex,
977 llvm::Attributes::get(CGM.getLLVMContext(), B));
Charles Davis74f72932010-02-13 15:54:06 +0000978 }
979 }
980}
981
John McCall6374c332010-03-06 00:35:14 +0000982bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
983 CodeGen::CodeGenFunction &CGF,
984 llvm::Value *Address) const {
985 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCall6374c332010-03-06 00:35:14 +0000986
Chris Lattner8b418682012-02-07 00:39:47 +0000987 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000988
John McCall6374c332010-03-06 00:35:14 +0000989 // 0-7 are the eight integer registers; the order is different
990 // on Darwin (for EH), but the range is the same.
991 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +0000992 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +0000993
994 if (CGF.CGM.isTargetDarwin()) {
995 // 12-16 are st(0..4). Not sure why we stop at 4.
996 // These have size 16, which is sizeof(long double) on
997 // platforms with 8-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +0000998 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCallaeeb7012010-05-27 06:19:26 +0000999 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001000
John McCall6374c332010-03-06 00:35:14 +00001001 } else {
1002 // 9 is %eflags, which doesn't get a size on Darwin for some
1003 // reason.
1004 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
1005
1006 // 11-16 are st(0..5). Not sure why we stop at 5.
1007 // These have size 12, which is sizeof(long double) on
1008 // platforms with 4-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001009 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCallaeeb7012010-05-27 06:19:26 +00001010 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1011 }
John McCall6374c332010-03-06 00:35:14 +00001012
1013 return false;
1014}
1015
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001016//===----------------------------------------------------------------------===//
1017// X86-64 ABI Implementation
1018//===----------------------------------------------------------------------===//
1019
1020
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001021namespace {
1022/// X86_64ABIInfo - The X86_64 ABI information.
1023class X86_64ABIInfo : public ABIInfo {
1024 enum Class {
1025 Integer = 0,
1026 SSE,
1027 SSEUp,
1028 X87,
1029 X87Up,
1030 ComplexX87,
1031 NoClass,
1032 Memory
1033 };
1034
1035 /// merge - Implement the X86_64 ABI merging algorithm.
1036 ///
1037 /// Merge an accumulating classification \arg Accum with a field
1038 /// classification \arg Field.
1039 ///
1040 /// \param Accum - The accumulating classification. This should
1041 /// always be either NoClass or the result of a previous merge
1042 /// call. In addition, this should never be Memory (the caller
1043 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001044 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001045
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001046 /// postMerge - Implement the X86_64 ABI post merging algorithm.
1047 ///
1048 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
1049 /// final MEMORY or SSE classes when necessary.
1050 ///
1051 /// \param AggregateSize - The size of the current aggregate in
1052 /// the classification process.
1053 ///
1054 /// \param Lo - The classification for the parts of the type
1055 /// residing in the low word of the containing object.
1056 ///
1057 /// \param Hi - The classification for the parts of the type
1058 /// residing in the higher words of the containing object.
1059 ///
1060 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
1061
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001062 /// classify - Determine the x86_64 register classes in which the
1063 /// given type T should be passed.
1064 ///
1065 /// \param Lo - The classification for the parts of the type
1066 /// residing in the low word of the containing object.
1067 ///
1068 /// \param Hi - The classification for the parts of the type
1069 /// residing in the high word of the containing object.
1070 ///
1071 /// \param OffsetBase - The bit offset of this type in the
1072 /// containing object. Some parameters are classified different
1073 /// depending on whether they straddle an eightbyte boundary.
1074 ///
1075 /// If a word is unused its result will be NoClass; if a type should
1076 /// be passed in Memory then at least the classification of \arg Lo
1077 /// will be Memory.
1078 ///
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001079 /// The \arg Lo class will be NoClass iff the argument is ignored.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001080 ///
1081 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
1082 /// also be ComplexX87.
Chris Lattner9c254f02010-06-29 06:01:59 +00001083 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001084
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001085 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001086 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
1087 unsigned IROffset, QualType SourceTy,
1088 unsigned SourceOffset) const;
1089 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
1090 unsigned IROffset, QualType SourceTy,
1091 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001092
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001093 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001094 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +00001095 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001096
1097 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001098 /// such that the argument will be passed in memory.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001099 ///
1100 /// \param freeIntRegs - The number of free integer registers remaining
1101 /// available.
1102 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001103
Chris Lattnera3c109b2010-07-29 02:16:43 +00001104 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001105
Bill Wendlingbb465d72010-10-18 03:41:31 +00001106 ABIArgInfo classifyArgumentType(QualType Ty,
Daniel Dunbaredfac032012-03-10 01:03:58 +00001107 unsigned freeIntRegs,
Bill Wendlingbb465d72010-10-18 03:41:31 +00001108 unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +00001109 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001110
Eli Friedmanee1ad992011-12-02 00:11:43 +00001111 bool IsIllegalVectorType(QualType Ty) const;
1112
John McCall67a57732011-04-21 01:20:55 +00001113 /// The 0.98 ABI revision clarified a lot of ambiguities,
1114 /// unfortunately in ways that were not always consistent with
1115 /// certain previous compilers. In particular, platforms which
1116 /// required strict binary compatibility with older versions of GCC
1117 /// may need to exempt themselves.
1118 bool honorsRevision0_98() const {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001119 return !getContext().getTargetInfo().getTriple().isOSDarwin();
John McCall67a57732011-04-21 01:20:55 +00001120 }
1121
Eli Friedmanee1ad992011-12-02 00:11:43 +00001122 bool HasAVX;
Derek Schuffbabaf312012-10-11 15:52:22 +00001123 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
1124 // 64-bit hardware.
1125 bool Has64BitPointers;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001126
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001127public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001128 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
Derek Schuffbabaf312012-10-11 15:52:22 +00001129 ABIInfo(CGT), HasAVX(hasavx),
Derek Schuff90da80c2012-10-11 18:21:13 +00001130 Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001131 }
Chris Lattner9c254f02010-06-29 06:01:59 +00001132
John McCallde5d3c72012-02-17 03:33:10 +00001133 bool isPassedUsingAVXType(QualType type) const {
1134 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00001135 // The freeIntRegs argument doesn't matter here.
1136 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE);
John McCallde5d3c72012-02-17 03:33:10 +00001137 if (info.isDirect()) {
1138 llvm::Type *ty = info.getCoerceToType();
1139 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
1140 return (vectorTy->getBitWidth() > 128);
1141 }
1142 return false;
1143 }
1144
Chris Lattneree5dcd02010-07-29 02:31:05 +00001145 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001146
1147 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1148 CodeGenFunction &CGF) const;
1149};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001150
Chris Lattnerf13721d2010-08-31 16:44:54 +00001151/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001152class WinX86_64ABIInfo : public ABIInfo {
1153
1154 ABIArgInfo classify(QualType Ty) const;
1155
Chris Lattnerf13721d2010-08-31 16:44:54 +00001156public:
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001157 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
1158
1159 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattnerf13721d2010-08-31 16:44:54 +00001160
1161 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1162 CodeGenFunction &CGF) const;
1163};
1164
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001165class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1166public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001167 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
Derek Schuffbabaf312012-10-11 15:52:22 +00001168 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
John McCall6374c332010-03-06 00:35:14 +00001169
John McCallde5d3c72012-02-17 03:33:10 +00001170 const X86_64ABIInfo &getABIInfo() const {
1171 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
1172 }
1173
John McCall6374c332010-03-06 00:35:14 +00001174 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1175 return 7;
1176 }
1177
1178 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1179 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001180 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001181
John McCallaeeb7012010-05-27 06:19:26 +00001182 // 0-15 are the 16 integer registers.
1183 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001184 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +00001185 return false;
1186 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001187
Jay Foadef6de3d2011-07-11 09:56:20 +00001188 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001189 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +00001190 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001191 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1192 }
1193
John McCallde5d3c72012-02-17 03:33:10 +00001194 bool isNoProtoCallVariadic(const CallArgList &args,
1195 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +00001196 // The default CC on x86-64 sets %al to the number of SSA
1197 // registers used, and GCC sets this when calling an unprototyped
Eli Friedman3ed79032011-12-01 04:53:19 +00001198 // function, so we override the default behavior. However, don't do
Eli Friedman68805fe2011-12-06 03:08:26 +00001199 // that when AVX types are involved: the ABI explicitly states it is
1200 // undefined, and it doesn't work in practice because of how the ABI
1201 // defines varargs anyway.
John McCallde5d3c72012-02-17 03:33:10 +00001202 if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) {
Eli Friedman3ed79032011-12-01 04:53:19 +00001203 bool HasAVXType = false;
John McCallde5d3c72012-02-17 03:33:10 +00001204 for (CallArgList::const_iterator
1205 it = args.begin(), ie = args.end(); it != ie; ++it) {
1206 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1207 HasAVXType = true;
1208 break;
Eli Friedman3ed79032011-12-01 04:53:19 +00001209 }
1210 }
John McCallde5d3c72012-02-17 03:33:10 +00001211
Eli Friedman3ed79032011-12-01 04:53:19 +00001212 if (!HasAVXType)
1213 return true;
1214 }
John McCall01f151e2011-09-21 08:08:30 +00001215
John McCallde5d3c72012-02-17 03:33:10 +00001216 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
John McCall01f151e2011-09-21 08:08:30 +00001217 }
1218
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001219};
1220
Chris Lattnerf13721d2010-08-31 16:44:54 +00001221class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1222public:
1223 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1224 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1225
1226 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1227 return 7;
1228 }
1229
1230 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1231 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001232 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001233
Chris Lattnerf13721d2010-08-31 16:44:54 +00001234 // 0-15 are the 16 integer registers.
1235 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001236 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattnerf13721d2010-08-31 16:44:54 +00001237 return false;
1238 }
1239};
1240
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001241}
1242
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001243void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1244 Class &Hi) const {
1245 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1246 //
1247 // (a) If one of the classes is Memory, the whole argument is passed in
1248 // memory.
1249 //
1250 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1251 // memory.
1252 //
1253 // (c) If the size of the aggregate exceeds two eightbytes and the first
1254 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1255 // argument is passed in memory. NOTE: This is necessary to keep the
1256 // ABI working for processors that don't support the __m256 type.
1257 //
1258 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1259 //
1260 // Some of these are enforced by the merging logic. Others can arise
1261 // only with unions; for example:
1262 // union { _Complex double; unsigned; }
1263 //
1264 // Note that clauses (b) and (c) were added in 0.98.
1265 //
1266 if (Hi == Memory)
1267 Lo = Memory;
1268 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1269 Lo = Memory;
1270 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1271 Lo = Memory;
1272 if (Hi == SSEUp && Lo != SSE)
1273 Hi = SSE;
1274}
1275
Chris Lattner1090a9b2010-06-28 21:43:59 +00001276X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001277 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1278 // classified recursively so that always two fields are
1279 // considered. The resulting class is calculated according to
1280 // the classes of the fields in the eightbyte:
1281 //
1282 // (a) If both classes are equal, this is the resulting class.
1283 //
1284 // (b) If one of the classes is NO_CLASS, the resulting class is
1285 // the other class.
1286 //
1287 // (c) If one of the classes is MEMORY, the result is the MEMORY
1288 // class.
1289 //
1290 // (d) If one of the classes is INTEGER, the result is the
1291 // INTEGER.
1292 //
1293 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1294 // MEMORY is used as class.
1295 //
1296 // (f) Otherwise class SSE is used.
1297
1298 // Accum should never be memory (we should have returned) or
1299 // ComplexX87 (because this cannot be passed in a structure).
1300 assert((Accum != Memory && Accum != ComplexX87) &&
1301 "Invalid accumulated classification during merge.");
1302 if (Accum == Field || Field == NoClass)
1303 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001304 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001305 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001306 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001307 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001308 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001309 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001310 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1311 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001312 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001313 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001314}
1315
Chris Lattnerbcaedae2010-06-30 19:14:05 +00001316void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001317 Class &Lo, Class &Hi) const {
1318 // FIXME: This code can be simplified by introducing a simple value class for
1319 // Class pairs with appropriate constructor methods for the various
1320 // situations.
1321
1322 // FIXME: Some of the split computations are wrong; unaligned vectors
1323 // shouldn't be passed in registers for example, so there is no chance they
1324 // can straddle an eightbyte. Verify & simplify.
1325
1326 Lo = Hi = NoClass;
1327
1328 Class &Current = OffsetBase < 64 ? Lo : Hi;
1329 Current = Memory;
1330
John McCall183700f2009-09-21 23:43:11 +00001331 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001332 BuiltinType::Kind k = BT->getKind();
1333
1334 if (k == BuiltinType::Void) {
1335 Current = NoClass;
1336 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1337 Lo = Integer;
1338 Hi = Integer;
1339 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1340 Current = Integer;
Derek Schuff7da46f92012-10-11 16:55:58 +00001341 } else if ((k == BuiltinType::Float || k == BuiltinType::Double) ||
1342 (k == BuiltinType::LongDouble &&
1343 getContext().getTargetInfo().getTriple().getOS() ==
1344 llvm::Triple::NativeClient)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001345 Current = SSE;
1346 } else if (k == BuiltinType::LongDouble) {
1347 Lo = X87;
1348 Hi = X87Up;
1349 }
1350 // FIXME: _Decimal32 and _Decimal64 are SSE.
1351 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001352 return;
1353 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001354
Chris Lattner1090a9b2010-06-28 21:43:59 +00001355 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001356 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +00001357 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001358 return;
1359 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001360
Chris Lattner1090a9b2010-06-28 21:43:59 +00001361 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001362 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001363 return;
1364 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001365
Chris Lattner1090a9b2010-06-28 21:43:59 +00001366 if (Ty->isMemberPointerType()) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001367 if (Ty->isMemberFunctionPointerType() && Has64BitPointers)
Daniel Dunbar67d438d2010-05-15 00:00:37 +00001368 Lo = Hi = Integer;
1369 else
1370 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001371 return;
1372 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001373
Chris Lattner1090a9b2010-06-28 21:43:59 +00001374 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001375 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001376 if (Size == 32) {
1377 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1378 // float> as integer.
1379 Current = Integer;
1380
1381 // If this type crosses an eightbyte boundary, it should be
1382 // split.
1383 uint64_t EB_Real = (OffsetBase) / 64;
1384 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1385 if (EB_Real != EB_Imag)
1386 Hi = Lo;
1387 } else if (Size == 64) {
1388 // gcc passes <1 x double> in memory. :(
1389 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1390 return;
1391
1392 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001393 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001394 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1395 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1396 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001397 Current = Integer;
1398 else
1399 Current = SSE;
1400
1401 // If this type crosses an eightbyte boundary, it should be
1402 // split.
1403 if (OffsetBase && OffsetBase != 64)
1404 Hi = Lo;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001405 } else if (Size == 128 || (HasAVX && Size == 256)) {
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001406 // Arguments of 256-bits are split into four eightbyte chunks. The
1407 // least significant one belongs to class SSE and all the others to class
1408 // SSEUP. The original Lo and Hi design considers that types can't be
1409 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1410 // This design isn't correct for 256-bits, but since there're no cases
1411 // where the upper parts would need to be inspected, avoid adding
1412 // complexity and just consider Hi to match the 64-256 part.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001413 Lo = SSE;
1414 Hi = SSEUp;
1415 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001416 return;
1417 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001418
Chris Lattner1090a9b2010-06-28 21:43:59 +00001419 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001420 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001421
Chris Lattnerea044322010-07-29 02:01:43 +00001422 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001423 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001424 if (Size <= 64)
1425 Current = Integer;
1426 else if (Size <= 128)
1427 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001428 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001429 Current = SSE;
Derek Schuff7da46f92012-10-11 16:55:58 +00001430 else if (ET == getContext().DoubleTy ||
1431 (ET == getContext().LongDoubleTy &&
1432 getContext().getTargetInfo().getTriple().getOS() ==
1433 llvm::Triple::NativeClient))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001434 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001435 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001436 Current = ComplexX87;
1437
1438 // If this complex type crosses an eightbyte boundary then it
1439 // should be split.
1440 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001441 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001442 if (Hi == NoClass && EB_Real != EB_Imag)
1443 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001444
Chris Lattner1090a9b2010-06-28 21:43:59 +00001445 return;
1446 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001447
Chris Lattnerea044322010-07-29 02:01:43 +00001448 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001449 // Arrays are treated like structures.
1450
Chris Lattnerea044322010-07-29 02:01:43 +00001451 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001452
1453 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001454 // than four eightbytes, ..., it has class MEMORY.
1455 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001456 return;
1457
1458 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1459 // fields, it has class MEMORY.
1460 //
1461 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001462 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001463 return;
1464
1465 // Otherwise implement simplified merge. We could be smarter about
1466 // this, but it isn't worth it and would be harder to verify.
1467 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001468 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001469 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes089d8922011-07-12 01:27:38 +00001470
1471 // The only case a 256-bit wide vector could be used is when the array
1472 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1473 // to work for sizes wider than 128, early check and fallback to memory.
1474 if (Size > 128 && EltSize != 256)
1475 return;
1476
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001477 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1478 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001479 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001480 Lo = merge(Lo, FieldLo);
1481 Hi = merge(Hi, FieldHi);
1482 if (Lo == Memory || Hi == Memory)
1483 break;
1484 }
1485
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001486 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001487 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001488 return;
1489 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001490
Chris Lattner1090a9b2010-06-28 21:43:59 +00001491 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001492 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001493
1494 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001495 // than four eightbytes, ..., it has class MEMORY.
1496 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001497 return;
1498
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001499 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1500 // copy constructor or a non-trivial destructor, it is passed by invisible
1501 // reference.
1502 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1503 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001504
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001505 const RecordDecl *RD = RT->getDecl();
1506
1507 // Assume variable sized types are passed in memory.
1508 if (RD->hasFlexibleArrayMember())
1509 return;
1510
Chris Lattnerea044322010-07-29 02:01:43 +00001511 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001512
1513 // Reset Lo class, this will be recomputed.
1514 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001515
1516 // If this is a C++ record, classify the bases first.
1517 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1518 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1519 e = CXXRD->bases_end(); i != e; ++i) {
1520 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1521 "Unexpected base class!");
1522 const CXXRecordDecl *Base =
1523 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1524
1525 // Classify this field.
1526 //
1527 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1528 // single eightbyte, each is classified separately. Each eightbyte gets
1529 // initialized to class NO_CLASS.
1530 Class FieldLo, FieldHi;
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001531 uint64_t Offset =
1532 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
Chris Lattner9c254f02010-06-29 06:01:59 +00001533 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001534 Lo = merge(Lo, FieldLo);
1535 Hi = merge(Hi, FieldHi);
1536 if (Lo == Memory || Hi == Memory)
1537 break;
1538 }
1539 }
1540
1541 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001542 unsigned idx = 0;
Bruno Cardoso Lopes548e4782011-07-12 22:30:58 +00001543 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001544 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001545 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1546 bool BitField = i->isBitField();
1547
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001548 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1549 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001550 //
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001551 // The only case a 256-bit wide vector could be used is when the struct
1552 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1553 // to work for sizes wider than 128, early check and fallback to memory.
1554 //
1555 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1556 Lo = Memory;
1557 return;
1558 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001559 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001560 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001561 Lo = Memory;
1562 return;
1563 }
1564
1565 // Classify this field.
1566 //
1567 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1568 // exceeds a single eightbyte, each is classified
1569 // separately. Each eightbyte gets initialized to class
1570 // NO_CLASS.
1571 Class FieldLo, FieldHi;
1572
1573 // Bit-fields require special handling, they do not force the
1574 // structure to be passed in memory even if unaligned, and
1575 // therefore they can straddle an eightbyte.
1576 if (BitField) {
1577 // Ignore padding bit-fields.
1578 if (i->isUnnamedBitfield())
1579 continue;
1580
1581 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001582 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001583
1584 uint64_t EB_Lo = Offset / 64;
1585 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1586 FieldLo = FieldHi = NoClass;
1587 if (EB_Lo) {
1588 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1589 FieldLo = NoClass;
1590 FieldHi = Integer;
1591 } else {
1592 FieldLo = Integer;
1593 FieldHi = EB_Hi ? Integer : NoClass;
1594 }
1595 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001596 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001597 Lo = merge(Lo, FieldLo);
1598 Hi = merge(Hi, FieldHi);
1599 if (Lo == Memory || Hi == Memory)
1600 break;
1601 }
1602
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001603 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001604 }
1605}
1606
Chris Lattner9c254f02010-06-29 06:01:59 +00001607ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001608 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1609 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001610 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001611 // Treat an enum type as its underlying type.
1612 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1613 Ty = EnumTy->getDecl()->getIntegerType();
1614
1615 return (Ty->isPromotableIntegerType() ?
1616 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1617 }
1618
1619 return ABIArgInfo::getIndirect(0);
1620}
1621
Eli Friedmanee1ad992011-12-02 00:11:43 +00001622bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1623 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1624 uint64_t Size = getContext().getTypeSize(VecTy);
1625 unsigned LargestVector = HasAVX ? 256 : 128;
1626 if (Size <= 64 || Size > LargestVector)
1627 return true;
1628 }
1629
1630 return false;
1631}
1632
Daniel Dunbaredfac032012-03-10 01:03:58 +00001633ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1634 unsigned freeIntRegs) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001635 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1636 // place naturally.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001637 //
1638 // This assumption is optimistic, as there could be free registers available
1639 // when we need to pass this argument in memory, and LLVM could try to pass
1640 // the argument in the free register. This does not seem to happen currently,
1641 // but this code would be much safer if we could mark the argument with
1642 // 'onstack'. See PR12193.
Eli Friedmanee1ad992011-12-02 00:11:43 +00001643 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001644 // Treat an enum type as its underlying type.
1645 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1646 Ty = EnumTy->getDecl()->getIntegerType();
1647
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001648 return (Ty->isPromotableIntegerType() ?
1649 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001650 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001651
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001652 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1653 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001654
Chris Lattner855d2272011-05-22 23:21:23 +00001655 // Compute the byval alignment. We specify the alignment of the byval in all
1656 // cases so that the mid-level optimizer knows the alignment of the byval.
1657 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
Daniel Dunbaredfac032012-03-10 01:03:58 +00001658
1659 // Attempt to avoid passing indirect results using byval when possible. This
1660 // is important for good codegen.
1661 //
1662 // We do this by coercing the value into a scalar type which the backend can
1663 // handle naturally (i.e., without using byval).
1664 //
1665 // For simplicity, we currently only do this when we have exhausted all of the
1666 // free integer registers. Doing this when there are free integer registers
1667 // would require more care, as we would have to ensure that the coerced value
1668 // did not claim the unused register. That would require either reording the
1669 // arguments to the function (so that any subsequent inreg values came first),
1670 // or only doing this optimization when there were no following arguments that
1671 // might be inreg.
1672 //
1673 // We currently expect it to be rare (particularly in well written code) for
1674 // arguments to be passed on the stack when there are still free integer
1675 // registers available (this would typically imply large structs being passed
1676 // by value), so this seems like a fair tradeoff for now.
1677 //
1678 // We can revisit this if the backend grows support for 'onstack' parameter
1679 // attributes. See PR12193.
1680 if (freeIntRegs == 0) {
1681 uint64_t Size = getContext().getTypeSize(Ty);
1682
1683 // If this type fits in an eightbyte, coerce it into the matching integral
1684 // type, which will end up on the stack (with alignment 8).
1685 if (Align == 8 && Size <= 64)
1686 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1687 Size));
1688 }
1689
Chris Lattner855d2272011-05-22 23:21:23 +00001690 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001691}
1692
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001693/// GetByteVectorType - The ABI specifies that a value should be passed in an
1694/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner0f408f52010-07-29 04:56:46 +00001695/// vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001696llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001697 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001698
Chris Lattner15842bd2010-07-29 05:02:29 +00001699 // Wrapper structs that just contain vectors are passed just like vectors,
1700 // strip them off if present.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001701 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner15842bd2010-07-29 05:02:29 +00001702 while (STy && STy->getNumElements() == 1) {
1703 IRType = STy->getElementType(0);
1704 STy = dyn_cast<llvm::StructType>(IRType);
1705 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001706
Bruno Cardoso Lopes528a8c72011-07-08 22:57:35 +00001707 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001708 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1709 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001710 unsigned BitWidth = VT->getBitWidth();
Tanya Lattnerce275672011-11-28 23:18:11 +00001711 if ((BitWidth >= 128 && BitWidth <= 256) &&
Chris Lattner0f408f52010-07-29 04:56:46 +00001712 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1713 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1714 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1715 EltTy->isIntegerTy(128)))
1716 return VT;
1717 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001718
Chris Lattner0f408f52010-07-29 04:56:46 +00001719 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1720}
1721
Chris Lattnere2962be2010-07-29 07:30:00 +00001722/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1723/// is known to either be off the end of the specified type or being in
1724/// alignment padding. The user type specified is known to be at most 128 bits
1725/// in size, and have passed through X86_64ABIInfo::classify with a successful
1726/// classification that put one of the two halves in the INTEGER class.
1727///
1728/// It is conservatively correct to return false.
1729static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1730 unsigned EndBit, ASTContext &Context) {
1731 // If the bytes being queried are off the end of the type, there is no user
1732 // data hiding here. This handles analysis of builtins, vectors and other
1733 // types that don't contain interesting padding.
1734 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1735 if (TySize <= StartBit)
1736 return true;
1737
Chris Lattner021c3a32010-07-29 07:43:55 +00001738 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1739 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1740 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1741
1742 // Check each element to see if the element overlaps with the queried range.
1743 for (unsigned i = 0; i != NumElts; ++i) {
1744 // If the element is after the span we care about, then we're done..
1745 unsigned EltOffset = i*EltSize;
1746 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001747
Chris Lattner021c3a32010-07-29 07:43:55 +00001748 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1749 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1750 EndBit-EltOffset, Context))
1751 return false;
1752 }
1753 // If it overlaps no elements, then it is safe to process as padding.
1754 return true;
1755 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001756
Chris Lattnere2962be2010-07-29 07:30:00 +00001757 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1758 const RecordDecl *RD = RT->getDecl();
1759 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001760
Chris Lattnere2962be2010-07-29 07:30:00 +00001761 // If this is a C++ record, check the bases first.
1762 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1763 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1764 e = CXXRD->bases_end(); i != e; ++i) {
1765 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1766 "Unexpected base class!");
1767 const CXXRecordDecl *Base =
1768 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001769
Chris Lattnere2962be2010-07-29 07:30:00 +00001770 // If the base is after the span we care about, ignore it.
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001771 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
Chris Lattnere2962be2010-07-29 07:30:00 +00001772 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001773
Chris Lattnere2962be2010-07-29 07:30:00 +00001774 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1775 if (!BitsContainNoUserData(i->getType(), BaseStart,
1776 EndBit-BaseOffset, Context))
1777 return false;
1778 }
1779 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001780
Chris Lattnere2962be2010-07-29 07:30:00 +00001781 // Verify that no field has data that overlaps the region of interest. Yes
1782 // this could be sped up a lot by being smarter about queried fields,
1783 // however we're only looking at structs up to 16 bytes, so we don't care
1784 // much.
1785 unsigned idx = 0;
1786 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1787 i != e; ++i, ++idx) {
1788 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001789
Chris Lattnere2962be2010-07-29 07:30:00 +00001790 // If we found a field after the region we care about, then we're done.
1791 if (FieldOffset >= EndBit) break;
1792
1793 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1794 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1795 Context))
1796 return false;
1797 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001798
Chris Lattnere2962be2010-07-29 07:30:00 +00001799 // If nothing in this record overlapped the area of interest, then we're
1800 // clean.
1801 return true;
1802 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001803
Chris Lattnere2962be2010-07-29 07:30:00 +00001804 return false;
1805}
1806
Chris Lattner0b362002010-07-29 18:39:32 +00001807/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1808/// float member at the specified offset. For example, {int,{float}} has a
1809/// float at offset 4. It is conservatively correct for this routine to return
1810/// false.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001811static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Micah Villmow25a6a842012-10-08 16:25:52 +00001812 const llvm::DataLayout &TD) {
Chris Lattner0b362002010-07-29 18:39:32 +00001813 // Base case if we find a float.
1814 if (IROffset == 0 && IRType->isFloatTy())
1815 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001816
Chris Lattner0b362002010-07-29 18:39:32 +00001817 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001818 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner0b362002010-07-29 18:39:32 +00001819 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1820 unsigned Elt = SL->getElementContainingOffset(IROffset);
1821 IROffset -= SL->getElementOffset(Elt);
1822 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1823 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001824
Chris Lattner0b362002010-07-29 18:39:32 +00001825 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001826 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1827 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner0b362002010-07-29 18:39:32 +00001828 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1829 IROffset -= IROffset/EltSize*EltSize;
1830 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1831 }
1832
1833 return false;
1834}
1835
Chris Lattnerf47c9442010-07-29 18:13:09 +00001836
1837/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1838/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001839llvm::Type *X86_64ABIInfo::
1840GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00001841 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001842 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001843 // pass as float if the last 4 bytes is just padding. This happens for
1844 // structs that contain 3 floats.
1845 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1846 SourceOffset*8+64, getContext()))
1847 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001848
Chris Lattner0b362002010-07-29 18:39:32 +00001849 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1850 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1851 // case.
Micah Villmow25a6a842012-10-08 16:25:52 +00001852 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
1853 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001854 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001855
Chris Lattnerf47c9442010-07-29 18:13:09 +00001856 return llvm::Type::getDoubleTy(getVMContext());
1857}
1858
1859
Chris Lattner0d2656d2010-07-29 17:40:35 +00001860/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1861/// an 8-byte GPR. This means that we either have a scalar or we are talking
1862/// about the high or low part of an up-to-16-byte struct. This routine picks
1863/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001864/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1865/// etc).
1866///
1867/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1868/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1869/// the 8-byte value references. PrefType may be null.
1870///
1871/// SourceTy is the source level type for the entire argument. SourceOffset is
1872/// an offset into this that we're processing (which is always either 0 or 8).
1873///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001874llvm::Type *X86_64ABIInfo::
1875GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00001876 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001877 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1878 // returning an 8-byte unit starting with it. See if we can safely use it.
1879 if (IROffset == 0) {
1880 // Pointers and int64's always fill the 8-byte unit.
Derek Schuffbabaf312012-10-11 15:52:22 +00001881 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
1882 IRType->isIntegerTy(64))
Chris Lattnere2962be2010-07-29 07:30:00 +00001883 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001884
Chris Lattnere2962be2010-07-29 07:30:00 +00001885 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1886 // goodness in the source type is just tail padding. This is allowed to
1887 // kick in for struct {double,int} on the int, but not on
1888 // struct{double,int,int} because we wouldn't return the second int. We
1889 // have to do this analysis on the source type because we can't depend on
1890 // unions being lowered a specific way etc.
1891 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
Derek Schuffbabaf312012-10-11 15:52:22 +00001892 IRType->isIntegerTy(32) ||
1893 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
1894 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
1895 cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001896
Chris Lattnere2962be2010-07-29 07:30:00 +00001897 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1898 SourceOffset*8+64, getContext()))
1899 return IRType;
1900 }
1901 }
Chris Lattner49382de2010-07-28 22:44:07 +00001902
Chris Lattner2acc6e32011-07-18 04:24:23 +00001903 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001904 // If this is a struct, recurse into the field at the specified offset.
Micah Villmow25a6a842012-10-08 16:25:52 +00001905 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001906 if (IROffset < SL->getSizeInBytes()) {
1907 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1908 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001909
Chris Lattner0d2656d2010-07-29 17:40:35 +00001910 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1911 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001912 }
Chris Lattner49382de2010-07-28 22:44:07 +00001913 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001914
Chris Lattner2acc6e32011-07-18 04:24:23 +00001915 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001916 llvm::Type *EltTy = ATy->getElementType();
Micah Villmow25a6a842012-10-08 16:25:52 +00001917 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
Chris Lattner021c3a32010-07-29 07:43:55 +00001918 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00001919 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1920 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00001921 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001922
Chris Lattner49382de2010-07-28 22:44:07 +00001923 // Okay, we don't have any better idea of what to pass, so we pass this in an
1924 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001925 unsigned TySizeInBytes =
1926 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00001927
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001928 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001929
Chris Lattner49382de2010-07-28 22:44:07 +00001930 // It is always safe to classify this as an integer type up to i64 that
1931 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001932 return llvm::IntegerType::get(getVMContext(),
1933 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00001934}
1935
Chris Lattner66e7b682010-09-01 00:50:20 +00001936
1937/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1938/// be used as elements of a two register pair to pass or return, return a
1939/// first class aggregate to represent them. For example, if the low part of
1940/// a by-value argument should be passed as i32* and the high part as float,
1941/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001942static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00001943GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Micah Villmow25a6a842012-10-08 16:25:52 +00001944 const llvm::DataLayout &TD) {
Chris Lattner66e7b682010-09-01 00:50:20 +00001945 // In order to correctly satisfy the ABI, we need to the high part to start
1946 // at offset 8. If the high and low parts we inferred are both 4-byte types
1947 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1948 // the second element at offset 8. Check for this:
1949 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1950 unsigned HiAlign = TD.getABITypeAlignment(Hi);
Micah Villmow25a6a842012-10-08 16:25:52 +00001951 unsigned HiStart = llvm::DataLayout::RoundUpAlignment(LoSize, HiAlign);
Chris Lattner66e7b682010-09-01 00:50:20 +00001952 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001953
Chris Lattner66e7b682010-09-01 00:50:20 +00001954 // To handle this, we have to increase the size of the low part so that the
1955 // second element will start at an 8 byte offset. We can't increase the size
1956 // of the second element because it might make us access off the end of the
1957 // struct.
1958 if (HiStart != 8) {
1959 // There are only two sorts of types the ABI generation code can produce for
1960 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1961 // Promote these to a larger type.
1962 if (Lo->isFloatTy())
1963 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1964 else {
1965 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1966 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1967 }
1968 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001969
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001970 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001971
1972
Chris Lattner66e7b682010-09-01 00:50:20 +00001973 // Verify that the second element is at an 8-byte offset.
1974 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1975 "Invalid x86-64 argument pair!");
1976 return Result;
1977}
1978
Chris Lattner519f68c2010-07-28 23:06:14 +00001979ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00001980classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00001981 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1982 // classification algorithm.
1983 X86_64ABIInfo::Class Lo, Hi;
1984 classify(RetTy, 0, Lo, Hi);
1985
1986 // Check some invariants.
1987 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001988 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1989
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001990 llvm::Type *ResType = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001991 switch (Lo) {
1992 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001993 if (Hi == NoClass)
1994 return ABIArgInfo::getIgnore();
1995 // If the low part is just padding, it takes no register, leave ResType
1996 // null.
1997 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1998 "Unknown missing lo part");
1999 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002000
2001 case SSEUp:
2002 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002003 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002004
2005 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
2006 // hidden argument.
2007 case Memory:
2008 return getIndirectReturnResult(RetTy);
2009
2010 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
2011 // available register of the sequence %rax, %rdx is used.
2012 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002013 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002014
Chris Lattnereb518b42010-07-29 21:42:50 +00002015 // If we have a sign or zero extended integer, make sure to return Extend
2016 // so that the parameter gets the right LLVM IR attributes.
2017 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2018 // Treat an enum type as its underlying type.
2019 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2020 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002021
Chris Lattnereb518b42010-07-29 21:42:50 +00002022 if (RetTy->isIntegralOrEnumerationType() &&
2023 RetTy->isPromotableIntegerType())
2024 return ABIArgInfo::getExtend();
2025 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002026 break;
2027
2028 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
2029 // available SSE register of the sequence %xmm0, %xmm1 is used.
2030 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002031 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00002032 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002033
2034 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
2035 // returned on the X87 stack in %st0 as 80-bit x87 number.
2036 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00002037 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00002038 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002039
2040 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
2041 // part of the value is returned in %st0 and the imaginary part in
2042 // %st1.
2043 case ComplexX87:
2044 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00002045 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00002046 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00002047 NULL);
2048 break;
2049 }
2050
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002051 llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002052 switch (Hi) {
2053 // Memory was handled previously and X87 should
2054 // never occur as a hi class.
2055 case Memory:
2056 case X87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002057 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002058
2059 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00002060 case NoClass:
2061 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002062
Chris Lattner3db4dde2010-09-01 00:20:33 +00002063 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002064 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002065 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2066 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002067 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00002068 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002069 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002070 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2071 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002072 break;
2073
2074 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002075 // is passed in the next available eightbyte chunk if the last used
2076 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00002077 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002078 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00002079 case SSEUp:
2080 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002081 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00002082 break;
2083
2084 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
2085 // returned together with the previous X87 value in %st0.
2086 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002087 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00002088 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002089 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00002090 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00002091 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002092 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002093 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2094 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00002095 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002096 break;
2097 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002098
Chris Lattner3db4dde2010-09-01 00:20:33 +00002099 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00002100 // known to pass in the high eightbyte of the result. We do this by forming a
2101 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00002102 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002103 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Chris Lattner519f68c2010-07-28 23:06:14 +00002104
Chris Lattnereb518b42010-07-29 21:42:50 +00002105 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00002106}
2107
Daniel Dunbaredfac032012-03-10 01:03:58 +00002108ABIArgInfo X86_64ABIInfo::classifyArgumentType(
2109 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE)
2110 const
2111{
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002112 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00002113 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002114
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002115 // Check some invariants.
2116 // FIXME: Enforce these by construction.
2117 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002118 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2119
2120 neededInt = 0;
2121 neededSSE = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002122 llvm::Type *ResType = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002123 switch (Lo) {
2124 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002125 if (Hi == NoClass)
2126 return ABIArgInfo::getIgnore();
2127 // If the low part is just padding, it takes no register, leave ResType
2128 // null.
2129 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2130 "Unknown missing lo part");
2131 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002132
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002133 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
2134 // on the stack.
2135 case Memory:
2136
2137 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
2138 // COMPLEX_X87, it is passed in memory.
2139 case X87:
2140 case ComplexX87:
Eli Friedmanded137f2011-06-29 07:04:55 +00002141 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2142 ++neededInt;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002143 return getIndirectResult(Ty, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002144
2145 case SSEUp:
2146 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002147 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002148
2149 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
2150 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
2151 // and %r9 is used.
2152 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00002153 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002154
Chris Lattner49382de2010-07-28 22:44:07 +00002155 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002156 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00002157
2158 // If we have a sign or zero extended integer, make sure to return Extend
2159 // so that the parameter gets the right LLVM IR attributes.
2160 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2161 // Treat an enum type as its underlying type.
2162 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2163 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002164
Chris Lattnereb518b42010-07-29 21:42:50 +00002165 if (Ty->isIntegralOrEnumerationType() &&
2166 Ty->isPromotableIntegerType())
2167 return ABIArgInfo::getExtend();
2168 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002169
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002170 break;
2171
2172 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
2173 // available SSE register is used, the registers are taken in the
2174 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00002175 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002176 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00002177 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00002178 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002179 break;
2180 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00002181 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002182
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002183 llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002184 switch (Hi) {
2185 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002186 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002187 // which is passed in memory.
2188 case Memory:
2189 case X87:
2190 case ComplexX87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002191 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002192
2193 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002194
Chris Lattner645406a2010-09-01 00:24:35 +00002195 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002196 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00002197 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002198 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002199
Chris Lattner645406a2010-09-01 00:24:35 +00002200 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2201 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002202 break;
2203
2204 // X87Up generally doesn't occur here (long double is passed in
2205 // memory), except in situations involving unions.
2206 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00002207 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002208 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002209
Chris Lattner645406a2010-09-01 00:24:35 +00002210 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2211 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00002212
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002213 ++neededSSE;
2214 break;
2215
2216 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
2217 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002218 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002219 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00002220 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002221 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002222 break;
2223 }
2224
Chris Lattner645406a2010-09-01 00:24:35 +00002225 // If a high part was specified, merge it together with the low part. It is
2226 // known to pass in the high eightbyte of the result. We do this by forming a
2227 // first class struct aggregate with the high and low part: {low, high}
2228 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002229 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002230
Chris Lattnereb518b42010-07-29 21:42:50 +00002231 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002232}
2233
Chris Lattneree5dcd02010-07-29 02:31:05 +00002234void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002235
Chris Lattnera3c109b2010-07-29 02:16:43 +00002236 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002237
2238 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00002239 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002240
2241 // If the return value is indirect, then the hidden argument is consuming one
2242 // integer register.
2243 if (FI.getReturnInfo().isIndirect())
2244 --freeIntRegs;
2245
2246 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2247 // get assigned (in left-to-right order) for passing as follows...
2248 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2249 it != ie; ++it) {
Bill Wendling99aaae82010-10-18 23:51:38 +00002250 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002251 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
2252 neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002253
2254 // AMD64-ABI 3.2.3p3: If there are no registers available for any
2255 // eightbyte of an argument, the whole argument is passed on the
2256 // stack. If registers have already been assigned for some
2257 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00002258 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002259 freeIntRegs -= neededInt;
2260 freeSSERegs -= neededSSE;
2261 } else {
Daniel Dunbaredfac032012-03-10 01:03:58 +00002262 it->info = getIndirectResult(it->type, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002263 }
2264 }
2265}
2266
2267static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2268 QualType Ty,
2269 CodeGenFunction &CGF) {
2270 llvm::Value *overflow_arg_area_p =
2271 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2272 llvm::Value *overflow_arg_area =
2273 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2274
2275 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2276 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedman8d2fe422011-11-18 02:44:19 +00002277 // It isn't stated explicitly in the standard, but in practice we use
2278 // alignment greater than 16 where necessary.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002279 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2280 if (Align > 8) {
Eli Friedman8d2fe422011-11-18 02:44:19 +00002281 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
Owen Anderson0032b272009-08-13 21:57:51 +00002282 llvm::Value *Offset =
Eli Friedman8d2fe422011-11-18 02:44:19 +00002283 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002284 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2285 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00002286 CGF.Int64Ty);
Eli Friedman8d2fe422011-11-18 02:44:19 +00002287 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002288 overflow_arg_area =
2289 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2290 overflow_arg_area->getType(),
2291 "overflow_arg_area.align");
2292 }
2293
2294 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002295 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002296 llvm::Value *Res =
2297 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002298 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002299
2300 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2301 // l->overflow_arg_area + sizeof(type).
2302 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2303 // an 8 byte boundary.
2304
2305 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00002306 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00002307 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002308 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2309 "overflow_arg_area.next");
2310 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2311
2312 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2313 return Res;
2314}
2315
2316llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2317 CodeGenFunction &CGF) const {
2318 // Assume that va_list type is correct; should be pointer to LLVM type:
2319 // struct {
2320 // i32 gp_offset;
2321 // i32 fp_offset;
2322 // i8* overflow_arg_area;
2323 // i8* reg_save_area;
2324 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00002325 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002326
Chris Lattnera14db752010-03-11 18:19:55 +00002327 Ty = CGF.getContext().getCanonicalType(Ty);
Daniel Dunbaredfac032012-03-10 01:03:58 +00002328 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002329
2330 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2331 // in the registers. If not go to step 7.
2332 if (!neededInt && !neededSSE)
2333 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2334
2335 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2336 // general purpose registers needed to pass type and num_fp to hold
2337 // the number of floating point registers needed.
2338
2339 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2340 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2341 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2342 //
2343 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2344 // register save space).
2345
2346 llvm::Value *InRegs = 0;
2347 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2348 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2349 if (neededInt) {
2350 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2351 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002352 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2353 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002354 }
2355
2356 if (neededSSE) {
2357 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2358 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2359 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00002360 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2361 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002362 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2363 }
2364
2365 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2366 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2367 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2368 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2369
2370 // Emit code to load the value if it was passed in registers.
2371
2372 CGF.EmitBlock(InRegBlock);
2373
2374 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2375 // an offset of l->gp_offset and/or l->fp_offset. This may require
2376 // copying to a temporary location in case the parameter is passed
2377 // in different register classes or requires an alignment greater
2378 // than 8 for general purpose registers and 16 for XMM registers.
2379 //
2380 // FIXME: This really results in shameful code when we end up needing to
2381 // collect arguments from different places; often what should result in a
2382 // simple assembling of a structure from scattered addresses has many more
2383 // loads than necessary. Can we clean this up?
Chris Lattner2acc6e32011-07-18 04:24:23 +00002384 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002385 llvm::Value *RegAddr =
2386 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2387 "reg_save_area");
2388 if (neededInt && neededSSE) {
2389 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002390 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002391 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002392 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2393 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002394 llvm::Type *TyLo = ST->getElementType(0);
2395 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002396 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002397 "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002398 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2399 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002400 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2401 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002402 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2403 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002404 llvm::Value *V =
2405 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2406 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2407 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2408 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2409
Owen Andersona1cf15f2009-07-14 23:10:40 +00002410 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002411 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002412 } else if (neededInt) {
2413 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2414 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002415 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002416 } else if (neededSSE == 1) {
2417 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2418 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2419 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002420 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002421 assert(neededSSE == 2 && "Invalid number of needed registers!");
2422 // SSE registers are spaced 16 bytes apart in the register save
2423 // area, we need to collect the two eightbytes together.
2424 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002425 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner8b418682012-02-07 00:39:47 +00002426 llvm::Type *DoubleTy = CGF.DoubleTy;
Chris Lattner2acc6e32011-07-18 04:24:23 +00002427 llvm::Type *DblPtrTy =
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002428 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002429 llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002430 DoubleTy, NULL);
2431 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2432 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2433 DblPtrTy));
2434 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2435 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2436 DblPtrTy));
2437 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2438 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2439 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002440 }
2441
2442 // AMD64-ABI 3.5.7p5: Step 5. Set:
2443 // l->gp_offset = l->gp_offset + num_gp * 8
2444 // l->fp_offset = l->fp_offset + num_fp * 16.
2445 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002446 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002447 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2448 gp_offset_p);
2449 }
2450 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002451 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002452 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2453 fp_offset_p);
2454 }
2455 CGF.EmitBranch(ContBlock);
2456
2457 // Emit code to load the value if it was passed in memory.
2458
2459 CGF.EmitBlock(InMemBlock);
2460 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2461
2462 // Return the appropriate result.
2463
2464 CGF.EmitBlock(ContBlock);
Jay Foadbbf3bac2011-03-30 11:28:58 +00002465 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002466 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002467 ResAddr->addIncoming(RegAddr, InRegBlock);
2468 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002469 return ResAddr;
2470}
2471
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002472ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2473
2474 if (Ty->isVoidType())
2475 return ABIArgInfo::getIgnore();
2476
2477 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2478 Ty = EnumTy->getDecl()->getIntegerType();
2479
2480 uint64_t Size = getContext().getTypeSize(Ty);
2481
2482 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002483 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2484 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002485 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2486
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002487 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2488 if (Size == 128 &&
Eli Friedman55fc7e22012-01-25 22:46:34 +00002489 getContext().getTargetInfo().getTriple().getOS()
2490 == llvm::Triple::MinGW32)
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002491 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2492 Size));
2493
2494 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2495 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2496 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002497 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002498 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2499 Size));
2500
2501 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2502 }
2503
2504 if (Ty->isPromotableIntegerType())
2505 return ABIArgInfo::getExtend();
2506
2507 return ABIArgInfo::getDirect();
2508}
2509
2510void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2511
2512 QualType RetTy = FI.getReturnType();
2513 FI.getReturnInfo() = classify(RetTy);
2514
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002515 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2516 it != ie; ++it)
2517 it->info = classify(it->type);
2518}
2519
Chris Lattnerf13721d2010-08-31 16:44:54 +00002520llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2521 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002522 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002523
Chris Lattnerf13721d2010-08-31 16:44:54 +00002524 CGBuilderTy &Builder = CGF.Builder;
2525 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2526 "ap");
2527 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2528 llvm::Type *PTy =
2529 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2530 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2531
2532 uint64_t Offset =
2533 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2534 llvm::Value *NextAddr =
2535 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2536 "ap.next");
2537 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2538
2539 return AddrTyped;
2540}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002541
Derek Schuff263366f2012-10-16 22:30:41 +00002542class NaClX86_64ABIInfo : public ABIInfo {
2543 public:
2544 NaClX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2545 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, HasAVX) {}
2546 virtual void computeInfo(CGFunctionInfo &FI) const;
2547 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2548 CodeGenFunction &CGF) const;
2549 private:
2550 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
2551 X86_64ABIInfo NInfo; // Used for everything else.
2552};
2553
2554class NaClX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2555 public:
2556 NaClX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2557 : TargetCodeGenInfo(new NaClX86_64ABIInfo(CGT, HasAVX)) {}
2558};
2559
2560void NaClX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2561 if (FI.getASTCallingConvention() == CC_PnaclCall)
2562 PInfo.computeInfo(FI);
2563 else
2564 NInfo.computeInfo(FI);
2565}
2566
2567llvm::Value *NaClX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2568 CodeGenFunction &CGF) const {
2569 // Always use the native convention; calling pnacl-style varargs functions
2570 // is unuspported.
2571 return NInfo.EmitVAArg(VAListAddr, Ty, CGF);
2572}
2573
2574
John McCallec853ba2010-03-11 00:10:12 +00002575// PowerPC-32
2576
2577namespace {
2578class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2579public:
Chris Lattnerea044322010-07-29 02:01:43 +00002580 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002581
John McCallec853ba2010-03-11 00:10:12 +00002582 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2583 // This is recovered from gcc output.
2584 return 1; // r1 is the dedicated stack pointer
2585 }
2586
2587 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002588 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002589};
2590
2591}
2592
2593bool
2594PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2595 llvm::Value *Address) const {
2596 // This is calculated from the LLVM and GCC tables and verified
2597 // against gcc output. AFAIK all ABIs use the same encoding.
2598
2599 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallec853ba2010-03-11 00:10:12 +00002600
Chris Lattner8b418682012-02-07 00:39:47 +00002601 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallec853ba2010-03-11 00:10:12 +00002602 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2603 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2604 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2605
2606 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002607 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002608
2609 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002610 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002611
2612 // 64-76 are various 4-byte special-purpose registers:
2613 // 64: mq
2614 // 65: lr
2615 // 66: ctr
2616 // 67: ap
2617 // 68-75 cr0-7
2618 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002619 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002620
2621 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002622 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002623
2624 // 109: vrsave
2625 // 110: vscr
2626 // 111: spe_acc
2627 // 112: spefscr
2628 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002629 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002630
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002631 return false;
John McCallec853ba2010-03-11 00:10:12 +00002632}
2633
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002634// PowerPC-64
2635
2636namespace {
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002637/// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
2638class PPC64_SVR4_ABIInfo : public DefaultABIInfo {
2639
2640public:
2641 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
2642
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00002643 // TODO: We can add more logic to computeInfo to improve performance.
2644 // Example: For aggregate arguments that fit in a register, we could
2645 // use getDirectInReg (as is done below for structs containing a single
2646 // floating-point value) to avoid pushing them to memory on function
2647 // entry. This would require changing the logic in PPCISelLowering
2648 // when lowering the parameters in the caller and args in the callee.
2649 virtual void computeInfo(CGFunctionInfo &FI) const {
2650 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2651 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2652 it != ie; ++it) {
2653 // We rely on the default argument classification for the most part.
2654 // One exception: An aggregate containing a single floating-point
2655 // item must be passed in a register if one is available.
2656 const Type *T = isSingleElementStruct(it->type, getContext());
2657 if (T) {
2658 const BuiltinType *BT = T->getAs<BuiltinType>();
2659 if (BT && BT->isFloatingPoint()) {
2660 QualType QT(T, 0);
2661 it->info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
2662 continue;
2663 }
2664 }
2665 it->info = classifyArgumentType(it->type);
2666 }
2667 }
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002668
2669 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr,
2670 QualType Ty,
2671 CodeGenFunction &CGF) const;
2672};
2673
2674class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
2675public:
2676 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT)
2677 : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT)) {}
2678
2679 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2680 // This is recovered from gcc output.
2681 return 1; // r1 is the dedicated stack pointer
2682 }
2683
2684 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2685 llvm::Value *Address) const;
2686};
2687
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002688class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2689public:
2690 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
2691
2692 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2693 // This is recovered from gcc output.
2694 return 1; // r1 is the dedicated stack pointer
2695 }
2696
2697 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2698 llvm::Value *Address) const;
2699};
2700
2701}
2702
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002703// Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
2704llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr,
2705 QualType Ty,
2706 CodeGenFunction &CGF) const {
2707 llvm::Type *BP = CGF.Int8PtrTy;
2708 llvm::Type *BPP = CGF.Int8PtrPtrTy;
2709
2710 CGBuilderTy &Builder = CGF.Builder;
2711 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
2712 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2713
2714 // Handle address alignment for type alignment > 64 bits. Although
2715 // long double normally requires 16-byte alignment, this is not the
2716 // case when it is passed as an argument; so handle that special case.
2717 const BuiltinType *BT = Ty->getAs<BuiltinType>();
2718 unsigned TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
2719
2720 if (TyAlign > 8 && (!BT || !BT->isFloatingPoint())) {
2721 assert((TyAlign & (TyAlign - 1)) == 0 &&
2722 "Alignment is not power of 2!");
2723 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2724 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(TyAlign - 1));
2725 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt64(~(TyAlign - 1)));
2726 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2727 }
2728
2729 // Update the va_list pointer.
2730 unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8;
2731 unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8);
2732 llvm::Value *NextAddr =
2733 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset),
2734 "ap.next");
2735 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2736
2737 // If the argument is smaller than 8 bytes, it is right-adjusted in
2738 // its doubleword slot. Adjust the pointer to pick it up from the
2739 // correct offset.
2740 if (SizeInBytes < 8) {
2741 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2742 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes));
2743 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2744 }
2745
2746 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2747 return Builder.CreateBitCast(Addr, PTy);
2748}
2749
2750static bool
2751PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2752 llvm::Value *Address) {
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002753 // This is calculated from the LLVM and GCC tables and verified
2754 // against gcc output. AFAIK all ABIs use the same encoding.
2755
2756 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2757
2758 llvm::IntegerType *i8 = CGF.Int8Ty;
2759 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2760 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2761 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2762
2763 // 0-31: r0-31, the 8-byte general-purpose registers
2764 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
2765
2766 // 32-63: fp0-31, the 8-byte floating-point registers
2767 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
2768
2769 // 64-76 are various 4-byte special-purpose registers:
2770 // 64: mq
2771 // 65: lr
2772 // 66: ctr
2773 // 67: ap
2774 // 68-75 cr0-7
2775 // 76: xer
2776 AssignToArrayRange(Builder, Address, Four8, 64, 76);
2777
2778 // 77-108: v0-31, the 16-byte vector registers
2779 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
2780
2781 // 109: vrsave
2782 // 110: vscr
2783 // 111: spe_acc
2784 // 112: spefscr
2785 // 113: sfp
2786 AssignToArrayRange(Builder, Address, Four8, 109, 113);
2787
2788 return false;
2789}
John McCallec853ba2010-03-11 00:10:12 +00002790
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002791bool
2792PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
2793 CodeGen::CodeGenFunction &CGF,
2794 llvm::Value *Address) const {
2795
2796 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
2797}
2798
2799bool
2800PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2801 llvm::Value *Address) const {
2802
2803 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
2804}
2805
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002806//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002807// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002808//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002809
2810namespace {
2811
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002812class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002813public:
2814 enum ABIKind {
2815 APCS = 0,
2816 AAPCS = 1,
2817 AAPCS_VFP
2818 };
2819
2820private:
2821 ABIKind Kind;
2822
2823public:
Chris Lattnerea044322010-07-29 02:01:43 +00002824 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002825
John McCall49e34be2011-08-30 01:42:09 +00002826 bool isEABI() const {
Eli Friedman55fc7e22012-01-25 22:46:34 +00002827 StringRef Env =
2828 getContext().getTargetInfo().getTriple().getEnvironmentName();
Logan Chien94a71422012-09-02 09:30:11 +00002829 return (Env == "gnueabi" || Env == "eabi" ||
2830 Env == "android" || Env == "androideabi");
John McCall49e34be2011-08-30 01:42:09 +00002831 }
2832
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002833private:
2834 ABIKind getABIKind() const { return Kind; }
2835
Chris Lattnera3c109b2010-07-29 02:16:43 +00002836 ABIArgInfo classifyReturnType(QualType RetTy) const;
2837 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Manman Ren97f81572012-10-16 19:18:39 +00002838 bool isIllegalVectorType(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002839
Chris Lattneree5dcd02010-07-29 02:31:05 +00002840 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002841
2842 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2843 CodeGenFunction &CGF) const;
2844};
2845
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002846class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2847public:
Chris Lattnerea044322010-07-29 02:01:43 +00002848 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2849 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00002850
John McCall49e34be2011-08-30 01:42:09 +00002851 const ARMABIInfo &getABIInfo() const {
2852 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
2853 }
2854
John McCall6374c332010-03-06 00:35:14 +00002855 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2856 return 13;
2857 }
Roman Divacky09345d12011-05-18 19:36:54 +00002858
Chris Lattner5f9e2722011-07-23 10:55:15 +00002859 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCallf85e1932011-06-15 23:02:42 +00002860 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2861 }
2862
Roman Divacky09345d12011-05-18 19:36:54 +00002863 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2864 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002865 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divacky09345d12011-05-18 19:36:54 +00002866
2867 // 0-15 are the 16 integer registers.
Chris Lattner8b418682012-02-07 00:39:47 +00002868 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divacky09345d12011-05-18 19:36:54 +00002869 return false;
2870 }
John McCall49e34be2011-08-30 01:42:09 +00002871
2872 unsigned getSizeOfUnwindException() const {
2873 if (getABIInfo().isEABI()) return 88;
2874 return TargetCodeGenInfo::getSizeOfUnwindException();
2875 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002876};
2877
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002878}
2879
Chris Lattneree5dcd02010-07-29 02:31:05 +00002880void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002881 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002882 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattnera3c109b2010-07-29 02:16:43 +00002883 it != ie; ++it)
2884 it->info = classifyArgumentType(it->type);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002885
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002886 // Always honor user-specified calling convention.
2887 if (FI.getCallingConvention() != llvm::CallingConv::C)
2888 return;
2889
2890 // Calling convention as default by an ABI.
Rafael Espindola25117ab2010-06-16 16:13:39 +00002891 llvm::CallingConv::ID DefaultCC;
John McCall49e34be2011-08-30 01:42:09 +00002892 if (isEABI())
Rafael Espindola25117ab2010-06-16 16:13:39 +00002893 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00002894 else
2895 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00002896
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002897 // If user did not ask for specific calling convention explicitly (e.g. via
2898 // pcs attribute), set effective calling convention if it's different than ABI
2899 // default.
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002900 switch (getABIKind()) {
2901 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002902 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2903 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002904 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002905 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002906 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2907 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002908 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002909 case AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002910 if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
2911 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002912 break;
2913 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002914}
2915
Bob Wilson194f06a2011-08-03 05:58:22 +00002916/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
2917/// aggregate. If HAMembers is non-null, the number of base elements
2918/// contained in the type is returned through it; this is used for the
2919/// recursive calls that check aggregate component types.
2920static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
2921 ASTContext &Context,
2922 uint64_t *HAMembers = 0) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002923 uint64_t Members = 0;
Bob Wilson194f06a2011-08-03 05:58:22 +00002924 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2925 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
2926 return false;
2927 Members *= AT->getSize().getZExtValue();
2928 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
2929 const RecordDecl *RD = RT->getDecl();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002930 if (RD->hasFlexibleArrayMember())
Bob Wilson194f06a2011-08-03 05:58:22 +00002931 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002932
Bob Wilson194f06a2011-08-03 05:58:22 +00002933 Members = 0;
2934 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2935 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00002936 const FieldDecl *FD = *i;
Bob Wilson194f06a2011-08-03 05:58:22 +00002937 uint64_t FldMembers;
2938 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
2939 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002940
2941 Members = (RD->isUnion() ?
2942 std::max(Members, FldMembers) : Members + FldMembers);
Bob Wilson194f06a2011-08-03 05:58:22 +00002943 }
2944 } else {
2945 Members = 1;
2946 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2947 Members = 2;
2948 Ty = CT->getElementType();
2949 }
2950
2951 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
2952 // double, or 64-bit or 128-bit vectors.
2953 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
2954 if (BT->getKind() != BuiltinType::Float &&
Tim Northoveradfa45f2012-07-20 22:29:29 +00002955 BT->getKind() != BuiltinType::Double &&
2956 BT->getKind() != BuiltinType::LongDouble)
Bob Wilson194f06a2011-08-03 05:58:22 +00002957 return false;
2958 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
2959 unsigned VecSize = Context.getTypeSize(VT);
2960 if (VecSize != 64 && VecSize != 128)
2961 return false;
2962 } else {
2963 return false;
2964 }
2965
2966 // The base type must be the same for all members. Vector types of the
2967 // same total size are treated as being equivalent here.
2968 const Type *TyPtr = Ty.getTypePtr();
2969 if (!Base)
2970 Base = TyPtr;
2971 if (Base != TyPtr &&
2972 (!Base->isVectorType() || !TyPtr->isVectorType() ||
2973 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
2974 return false;
2975 }
2976
2977 // Homogeneous Aggregates can have at most 4 members of the base type.
2978 if (HAMembers)
2979 *HAMembers = Members;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002980
2981 return (Members > 0 && Members <= 4);
Bob Wilson194f06a2011-08-03 05:58:22 +00002982}
2983
Chris Lattnera3c109b2010-07-29 02:16:43 +00002984ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
Manman Ren97f81572012-10-16 19:18:39 +00002985 // Handle illegal vector types here.
2986 if (isIllegalVectorType(Ty)) {
2987 uint64_t Size = getContext().getTypeSize(Ty);
2988 if (Size <= 32) {
2989 llvm::Type *ResType =
2990 llvm::Type::getInt32Ty(getVMContext());
2991 return ABIArgInfo::getDirect(ResType);
2992 }
2993 if (Size == 64) {
2994 llvm::Type *ResType = llvm::VectorType::get(
2995 llvm::Type::getInt32Ty(getVMContext()), 2);
2996 return ABIArgInfo::getDirect(ResType);
2997 }
2998 if (Size == 128) {
2999 llvm::Type *ResType = llvm::VectorType::get(
3000 llvm::Type::getInt32Ty(getVMContext()), 4);
3001 return ABIArgInfo::getDirect(ResType);
3002 }
3003 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3004 }
3005
John McCalld608cdb2010-08-22 10:59:02 +00003006 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003007 // Treat an enum type as its underlying type.
3008 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3009 Ty = EnumTy->getDecl()->getIntegerType();
3010
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003011 return (Ty->isPromotableIntegerType() ?
3012 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003013 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003014
Daniel Dunbar42025572009-09-14 21:54:03 +00003015 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003016 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00003017 return ABIArgInfo::getIgnore();
3018
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003019 // Structures with either a non-trivial destructor or a non-trivial
3020 // copy constructor are always indirect.
3021 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
3022 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3023
Bob Wilson194f06a2011-08-03 05:58:22 +00003024 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
3025 // Homogeneous Aggregates need to be expanded.
3026 const Type *Base = 0;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003027 if (isHomogeneousAggregate(Ty, Base, getContext())) {
3028 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson194f06a2011-08-03 05:58:22 +00003029 return ABIArgInfo::getExpand();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003030 }
Bob Wilson194f06a2011-08-03 05:58:22 +00003031 }
3032
Manman Ren634b3d22012-08-13 21:23:55 +00003033 // Support byval for ARM.
3034 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64) ||
3035 getContext().getTypeAlign(Ty) > 64) {
3036 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
Eli Friedman79f30982012-08-09 00:31:40 +00003037 }
3038
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00003039 // Otherwise, pass by coercing to a structure of the appropriate size.
Chris Lattner2acc6e32011-07-18 04:24:23 +00003040 llvm::Type* ElemTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003041 unsigned SizeRegs;
Eli Friedman79f30982012-08-09 00:31:40 +00003042 // FIXME: Try to match the types of the arguments more accurately where
3043 // we can.
3044 if (getContext().getTypeAlign(Ty) <= 32) {
Bob Wilson53fc1a62011-08-01 23:39:04 +00003045 ElemTy = llvm::Type::getInt32Ty(getVMContext());
3046 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Manman Ren78eb76e2012-06-25 22:04:00 +00003047 } else {
Manman Ren78eb76e2012-06-25 22:04:00 +00003048 ElemTy = llvm::Type::getInt64Ty(getVMContext());
3049 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastings67d097e2011-04-27 17:24:02 +00003050 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003051
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003052 llvm::Type *STy =
Chris Lattner7650d952011-06-18 22:49:11 +00003053 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003054 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003055}
3056
Chris Lattnera3c109b2010-07-29 02:16:43 +00003057static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00003058 llvm::LLVMContext &VMContext) {
3059 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
3060 // is called integer-like if its size is less than or equal to one word, and
3061 // the offset of each of its addressable sub-fields is zero.
3062
3063 uint64_t Size = Context.getTypeSize(Ty);
3064
3065 // Check that the type fits in a word.
3066 if (Size > 32)
3067 return false;
3068
3069 // FIXME: Handle vector types!
3070 if (Ty->isVectorType())
3071 return false;
3072
Daniel Dunbarb0d58192009-09-14 02:20:34 +00003073 // Float types are never treated as "integer like".
3074 if (Ty->isRealFloatingType())
3075 return false;
3076
Daniel Dunbar98303b92009-09-13 08:03:58 +00003077 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00003078 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00003079 return true;
3080
Daniel Dunbar45815812010-02-01 23:31:26 +00003081 // Small complex integer types are "integer like".
3082 if (const ComplexType *CT = Ty->getAs<ComplexType>())
3083 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003084
3085 // Single element and zero sized arrays should be allowed, by the definition
3086 // above, but they are not.
3087
3088 // Otherwise, it must be a record type.
3089 const RecordType *RT = Ty->getAs<RecordType>();
3090 if (!RT) return false;
3091
3092 // Ignore records with flexible arrays.
3093 const RecordDecl *RD = RT->getDecl();
3094 if (RD->hasFlexibleArrayMember())
3095 return false;
3096
3097 // Check that all sub-fields are at offset 0, and are themselves "integer
3098 // like".
3099 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3100
3101 bool HadField = false;
3102 unsigned idx = 0;
3103 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3104 i != e; ++i, ++idx) {
David Blaikie581deb32012-06-06 20:45:41 +00003105 const FieldDecl *FD = *i;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003106
Daniel Dunbar679855a2010-01-29 03:22:29 +00003107 // Bit-fields are not addressable, we only need to verify they are "integer
3108 // like". We still have to disallow a subsequent non-bitfield, for example:
3109 // struct { int : 0; int x }
3110 // is non-integer like according to gcc.
3111 if (FD->isBitField()) {
3112 if (!RD->isUnion())
3113 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003114
Daniel Dunbar679855a2010-01-29 03:22:29 +00003115 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3116 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003117
Daniel Dunbar679855a2010-01-29 03:22:29 +00003118 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003119 }
3120
Daniel Dunbar679855a2010-01-29 03:22:29 +00003121 // Check if this field is at offset 0.
3122 if (Layout.getFieldOffset(idx) != 0)
3123 return false;
3124
Daniel Dunbar98303b92009-09-13 08:03:58 +00003125 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3126 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003127
Daniel Dunbar679855a2010-01-29 03:22:29 +00003128 // Only allow at most one field in a structure. This doesn't match the
3129 // wording above, but follows gcc in situations with a field following an
3130 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00003131 if (!RD->isUnion()) {
3132 if (HadField)
3133 return false;
3134
3135 HadField = true;
3136 }
3137 }
3138
3139 return true;
3140}
3141
Chris Lattnera3c109b2010-07-29 02:16:43 +00003142ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003143 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003144 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00003145
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00003146 // Large vector types should be returned via memory.
3147 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
3148 return ABIArgInfo::getIndirect(0);
3149
John McCalld608cdb2010-08-22 10:59:02 +00003150 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003151 // Treat an enum type as its underlying type.
3152 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3153 RetTy = EnumTy->getDecl()->getIntegerType();
3154
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003155 return (RetTy->isPromotableIntegerType() ?
3156 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003157 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003158
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003159 // Structures with either a non-trivial destructor or a non-trivial
3160 // copy constructor are always indirect.
3161 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
3162 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3163
Daniel Dunbar98303b92009-09-13 08:03:58 +00003164 // Are we following APCS?
3165 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00003166 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00003167 return ABIArgInfo::getIgnore();
3168
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003169 // Complex types are all returned as packed integers.
3170 //
3171 // FIXME: Consider using 2 x vector types if the back end handles them
3172 // correctly.
3173 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00003174 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00003175 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003176
Daniel Dunbar98303b92009-09-13 08:03:58 +00003177 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003178 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003179 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003180 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003181 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003182 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003183 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003184 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3185 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003186 }
3187
3188 // Otherwise return in memory.
3189 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003190 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003191
3192 // Otherwise this is an AAPCS variant.
3193
Chris Lattnera3c109b2010-07-29 02:16:43 +00003194 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00003195 return ABIArgInfo::getIgnore();
3196
Bob Wilson3b694fa2011-11-02 04:51:36 +00003197 // Check for homogeneous aggregates with AAPCS-VFP.
3198 if (getABIKind() == AAPCS_VFP) {
3199 const Type *Base = 0;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003200 if (isHomogeneousAggregate(RetTy, Base, getContext())) {
3201 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson3b694fa2011-11-02 04:51:36 +00003202 // Homogeneous Aggregates are returned directly.
3203 return ABIArgInfo::getDirect();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003204 }
Bob Wilson3b694fa2011-11-02 04:51:36 +00003205 }
3206
Daniel Dunbar98303b92009-09-13 08:03:58 +00003207 // Aggregates <= 4 bytes are returned in r0; other aggregates
3208 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003209 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00003210 if (Size <= 32) {
3211 // Return in the smallest viable integer type.
3212 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003213 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003214 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003215 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3216 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003217 }
3218
Daniel Dunbar98303b92009-09-13 08:03:58 +00003219 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003220}
3221
Manman Ren97f81572012-10-16 19:18:39 +00003222/// isIllegalVector - check whether Ty is an illegal vector type.
3223bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
3224 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3225 // Check whether VT is legal.
3226 unsigned NumElements = VT->getNumElements();
3227 uint64_t Size = getContext().getTypeSize(VT);
3228 // NumElements should be power of 2.
3229 if ((NumElements & (NumElements - 1)) != 0)
3230 return true;
3231 // Size should be greater than 32 bits.
3232 return Size <= 32;
3233 }
3234 return false;
3235}
3236
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003237llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00003238 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003239 llvm::Type *BP = CGF.Int8PtrTy;
3240 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003241
3242 CGBuilderTy &Builder = CGF.Builder;
Chris Lattner8b418682012-02-07 00:39:47 +00003243 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003244 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Manman Rend105e732012-10-16 19:01:37 +00003245
3246 uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8;
Rafael Espindolae164c182011-08-02 22:33:37 +00003247 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
Manman Ren97f81572012-10-16 19:18:39 +00003248 bool IsIndirect = false;
Manman Rend105e732012-10-16 19:01:37 +00003249
3250 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
3251 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
Manman Ren93371022012-10-16 19:51:48 +00003252 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3253 getABIKind() == ARMABIInfo::AAPCS)
3254 TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
3255 else
3256 TyAlign = 4;
Manman Ren97f81572012-10-16 19:18:39 +00003257 // Use indirect if size of the illegal vector is bigger than 16 bytes.
3258 if (isIllegalVectorType(Ty) && Size > 16) {
3259 IsIndirect = true;
3260 Size = 4;
3261 TyAlign = 4;
3262 }
Manman Rend105e732012-10-16 19:01:37 +00003263
3264 // Handle address alignment for ABI alignment > 4 bytes.
Rafael Espindolae164c182011-08-02 22:33:37 +00003265 if (TyAlign > 4) {
3266 assert((TyAlign & (TyAlign - 1)) == 0 &&
3267 "Alignment is not power of 2!");
3268 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3269 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
3270 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
Manman Rend105e732012-10-16 19:01:37 +00003271 Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align");
Rafael Espindolae164c182011-08-02 22:33:37 +00003272 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003273
3274 uint64_t Offset =
Manman Rend105e732012-10-16 19:01:37 +00003275 llvm::RoundUpToAlignment(Size, 4);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003276 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00003277 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003278 "ap.next");
3279 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3280
Manman Ren97f81572012-10-16 19:18:39 +00003281 if (IsIndirect)
3282 Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP));
Manman Ren93371022012-10-16 19:51:48 +00003283 else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) {
Manman Rend105e732012-10-16 19:01:37 +00003284 // We can't directly cast ap.cur to pointer to a vector type, since ap.cur
3285 // may not be correctly aligned for the vector type. We create an aligned
3286 // temporary space and copy the content over from ap.cur to the temporary
3287 // space. This is necessary if the natural alignment of the type is greater
3288 // than the ABI alignment.
3289 llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
3290 CharUnits CharSize = getContext().getTypeSizeInChars(Ty);
3291 llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty),
3292 "var.align");
3293 llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
3294 llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy);
3295 Builder.CreateMemCpy(Dst, Src,
3296 llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()),
3297 TyAlign, false);
3298 Addr = AlignedTemp; //The content is in aligned location.
3299 }
3300 llvm::Type *PTy =
3301 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3302 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3303
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003304 return AddrTyped;
3305}
3306
Derek Schuff263366f2012-10-16 22:30:41 +00003307class NaClARMABIInfo : public ABIInfo {
3308 public:
3309 NaClARMABIInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3310 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, Kind) {}
3311 virtual void computeInfo(CGFunctionInfo &FI) const;
3312 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3313 CodeGenFunction &CGF) const;
3314 private:
3315 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
3316 ARMABIInfo NInfo; // Used for everything else.
3317};
3318
3319class NaClARMTargetCodeGenInfo : public TargetCodeGenInfo {
3320 public:
3321 NaClARMTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3322 : TargetCodeGenInfo(new NaClARMABIInfo(CGT, Kind)) {}
3323};
3324
3325void NaClARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
3326 if (FI.getASTCallingConvention() == CC_PnaclCall)
3327 PInfo.computeInfo(FI);
3328 else
3329 static_cast<const ABIInfo&>(NInfo).computeInfo(FI);
3330}
3331
3332llvm::Value *NaClARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3333 CodeGenFunction &CGF) const {
3334 // Always use the native convention; calling pnacl-style varargs functions
3335 // is unsupported.
3336 return static_cast<const ABIInfo&>(NInfo).EmitVAArg(VAListAddr, Ty, CGF);
3337}
3338
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003339//===----------------------------------------------------------------------===//
Justin Holewinski2c585b92012-05-24 17:43:12 +00003340// NVPTX ABI Implementation
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003341//===----------------------------------------------------------------------===//
3342
3343namespace {
3344
Justin Holewinski2c585b92012-05-24 17:43:12 +00003345class NVPTXABIInfo : public ABIInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003346public:
Justin Holewinski2c585b92012-05-24 17:43:12 +00003347 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003348
3349 ABIArgInfo classifyReturnType(QualType RetTy) const;
3350 ABIArgInfo classifyArgumentType(QualType Ty) const;
3351
3352 virtual void computeInfo(CGFunctionInfo &FI) const;
3353 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3354 CodeGenFunction &CFG) const;
3355};
3356
Justin Holewinski2c585b92012-05-24 17:43:12 +00003357class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003358public:
Justin Holewinski2c585b92012-05-24 17:43:12 +00003359 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
3360 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
Justin Holewinski818eafb2011-10-05 17:58:44 +00003361
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003362 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3363 CodeGen::CodeGenModule &M) const;
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003364};
3365
Justin Holewinski2c585b92012-05-24 17:43:12 +00003366ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003367 if (RetTy->isVoidType())
3368 return ABIArgInfo::getIgnore();
3369 if (isAggregateTypeForABI(RetTy))
3370 return ABIArgInfo::getIndirect(0);
3371 return ABIArgInfo::getDirect();
3372}
3373
Justin Holewinski2c585b92012-05-24 17:43:12 +00003374ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003375 if (isAggregateTypeForABI(Ty))
3376 return ABIArgInfo::getIndirect(0);
3377
3378 return ABIArgInfo::getDirect();
3379}
3380
Justin Holewinski2c585b92012-05-24 17:43:12 +00003381void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003382 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3383 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3384 it != ie; ++it)
3385 it->info = classifyArgumentType(it->type);
3386
3387 // Always honor user-specified calling convention.
3388 if (FI.getCallingConvention() != llvm::CallingConv::C)
3389 return;
3390
3391 // Calling convention as default by an ABI.
Justin Holewinski2c585b92012-05-24 17:43:12 +00003392 // We're still using the PTX_Kernel/PTX_Device calling conventions here,
3393 // but we should switch to NVVM metadata later on.
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003394 llvm::CallingConv::ID DefaultCC;
David Blaikie4e4d0842012-03-11 07:00:24 +00003395 const LangOptions &LangOpts = getContext().getLangOpts();
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003396 if (LangOpts.OpenCL || LangOpts.CUDA) {
3397 // If we are in OpenCL or CUDA mode, then default to device functions
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003398 DefaultCC = llvm::CallingConv::PTX_Device;
Justin Holewinski818eafb2011-10-05 17:58:44 +00003399 } else {
3400 // If we are in standard C/C++ mode, use the triple to decide on the default
3401 StringRef Env =
3402 getContext().getTargetInfo().getTriple().getEnvironmentName();
3403 if (Env == "device")
3404 DefaultCC = llvm::CallingConv::PTX_Device;
3405 else
3406 DefaultCC = llvm::CallingConv::PTX_Kernel;
3407 }
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003408 FI.setEffectiveCallingConvention(DefaultCC);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003409
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003410}
3411
Justin Holewinski2c585b92012-05-24 17:43:12 +00003412llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3413 CodeGenFunction &CFG) const {
3414 llvm_unreachable("NVPTX does not support varargs");
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003415}
3416
Justin Holewinski2c585b92012-05-24 17:43:12 +00003417void NVPTXTargetCodeGenInfo::
3418SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3419 CodeGen::CodeGenModule &M) const{
Justin Holewinski818eafb2011-10-05 17:58:44 +00003420 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3421 if (!FD) return;
3422
3423 llvm::Function *F = cast<llvm::Function>(GV);
3424
3425 // Perform special handling in OpenCL mode
David Blaikie4e4d0842012-03-11 07:00:24 +00003426 if (M.getLangOpts().OpenCL) {
Justin Holewinski818eafb2011-10-05 17:58:44 +00003427 // Use OpenCL function attributes to set proper calling conventions
3428 // By default, all functions are device functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00003429 if (FD->hasAttr<OpenCLKernelAttr>()) {
3430 // OpenCL __kernel functions get a kernel calling convention
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003431 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003432 // And kernel functions are not subject to inlining
Bill Wendlingfac63102012-10-10 03:13:20 +00003433 F->addFnAttr(llvm::Attributes::NoInline);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003434 }
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003435 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00003436
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003437 // Perform special handling in CUDA mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00003438 if (M.getLangOpts().CUDA) {
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003439 // CUDA __global__ functions get a kernel calling convention. Since
3440 // __global__ functions cannot be called from the device, we do not
3441 // need to set the noinline attribute.
3442 if (FD->getAttr<CUDAGlobalAttr>())
3443 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003444 }
3445}
3446
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003447}
3448
3449//===----------------------------------------------------------------------===//
Wesley Peck276fdf42010-12-19 19:57:51 +00003450// MBlaze ABI Implementation
3451//===----------------------------------------------------------------------===//
3452
3453namespace {
3454
3455class MBlazeABIInfo : public ABIInfo {
3456public:
3457 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3458
3459 bool isPromotableIntegerType(QualType Ty) const;
3460
3461 ABIArgInfo classifyReturnType(QualType RetTy) const;
3462 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3463
3464 virtual void computeInfo(CGFunctionInfo &FI) const {
3465 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3466 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3467 it != ie; ++it)
3468 it->info = classifyArgumentType(it->type);
3469 }
3470
3471 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3472 CodeGenFunction &CGF) const;
3473};
3474
3475class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
3476public:
3477 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
3478 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
3479 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3480 CodeGen::CodeGenModule &M) const;
3481};
3482
3483}
3484
3485bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
3486 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
3487 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
3488 switch (BT->getKind()) {
3489 case BuiltinType::Bool:
3490 case BuiltinType::Char_S:
3491 case BuiltinType::Char_U:
3492 case BuiltinType::SChar:
3493 case BuiltinType::UChar:
3494 case BuiltinType::Short:
3495 case BuiltinType::UShort:
3496 return true;
3497 default:
3498 return false;
3499 }
3500 return false;
3501}
3502
3503llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3504 CodeGenFunction &CGF) const {
3505 // FIXME: Implement
3506 return 0;
3507}
3508
3509
3510ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
3511 if (RetTy->isVoidType())
3512 return ABIArgInfo::getIgnore();
3513 if (isAggregateTypeForABI(RetTy))
3514 return ABIArgInfo::getIndirect(0);
3515
3516 return (isPromotableIntegerType(RetTy) ?
3517 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3518}
3519
3520ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
3521 if (isAggregateTypeForABI(Ty))
3522 return ABIArgInfo::getIndirect(0);
3523
3524 return (isPromotableIntegerType(Ty) ?
3525 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3526}
3527
3528void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3529 llvm::GlobalValue *GV,
3530 CodeGen::CodeGenModule &M)
3531 const {
3532 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3533 if (!FD) return;
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00003534
Wesley Peck276fdf42010-12-19 19:57:51 +00003535 llvm::CallingConv::ID CC = llvm::CallingConv::C;
3536 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
3537 CC = llvm::CallingConv::MBLAZE_INTR;
3538 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
3539 CC = llvm::CallingConv::MBLAZE_SVOL;
3540
3541 if (CC != llvm::CallingConv::C) {
3542 // Handle 'interrupt_handler' attribute:
3543 llvm::Function *F = cast<llvm::Function>(GV);
3544
3545 // Step 1: Set ISR calling convention.
3546 F->setCallingConv(CC);
3547
3548 // Step 2: Add attributes goodness.
Bill Wendlingfac63102012-10-10 03:13:20 +00003549 F->addFnAttr(llvm::Attributes::NoInline);
Wesley Peck276fdf42010-12-19 19:57:51 +00003550 }
3551
3552 // Step 3: Emit _interrupt_handler alias.
3553 if (CC == llvm::CallingConv::MBLAZE_INTR)
3554 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
3555 "_interrupt_handler", GV, &M.getModule());
3556}
3557
3558
3559//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003560// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003561//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003562
3563namespace {
3564
3565class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
3566public:
Chris Lattnerea044322010-07-29 02:01:43 +00003567 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
3568 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003569 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3570 CodeGen::CodeGenModule &M) const;
3571};
3572
3573}
3574
3575void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3576 llvm::GlobalValue *GV,
3577 CodeGen::CodeGenModule &M) const {
3578 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
3579 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
3580 // Handle 'interrupt' attribute:
3581 llvm::Function *F = cast<llvm::Function>(GV);
3582
3583 // Step 1: Set ISR calling convention.
3584 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
3585
3586 // Step 2: Add attributes goodness.
Bill Wendlingfac63102012-10-10 03:13:20 +00003587 F->addFnAttr(llvm::Attributes::NoInline);
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003588
3589 // Step 3: Emit ISR vector alias.
3590 unsigned Num = attr->getNumber() + 0xffe0;
3591 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003592 "vector_" + Twine::utohexstr(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003593 GV, &M.getModule());
3594 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003595 }
3596}
3597
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003598//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00003599// MIPS ABI Implementation. This works for both little-endian and
3600// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003601//===----------------------------------------------------------------------===//
3602
John McCallaeeb7012010-05-27 06:19:26 +00003603namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00003604class MipsABIInfo : public ABIInfo {
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003605 bool IsO32;
Akira Hatanakac359f202012-07-03 19:24:06 +00003606 unsigned MinABIStackAlignInBytes, StackAlignInBytes;
3607 void CoerceToIntArgs(uint64_t TySize,
3608 SmallVector<llvm::Type*, 8> &ArgList) const;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003609 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003610 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003611 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003612public:
Akira Hatanakab551dd32011-11-03 00:05:50 +00003613 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
Akira Hatanakac359f202012-07-03 19:24:06 +00003614 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
3615 StackAlignInBytes(IsO32 ? 8 : 16) {}
Akira Hatanaka619e8872011-06-02 00:09:17 +00003616
3617 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003618 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003619 virtual void computeInfo(CGFunctionInfo &FI) const;
3620 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3621 CodeGenFunction &CGF) const;
3622};
3623
John McCallaeeb7012010-05-27 06:19:26 +00003624class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003625 unsigned SizeOfUnwindException;
John McCallaeeb7012010-05-27 06:19:26 +00003626public:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003627 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
3628 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
3629 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCallaeeb7012010-05-27 06:19:26 +00003630
3631 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
3632 return 29;
3633 }
3634
3635 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003636 llvm::Value *Address) const;
John McCall49e34be2011-08-30 01:42:09 +00003637
3638 unsigned getSizeOfUnwindException() const {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003639 return SizeOfUnwindException;
John McCall49e34be2011-08-30 01:42:09 +00003640 }
John McCallaeeb7012010-05-27 06:19:26 +00003641};
3642}
3643
Akira Hatanakac359f202012-07-03 19:24:06 +00003644void MipsABIInfo::CoerceToIntArgs(uint64_t TySize,
3645 SmallVector<llvm::Type*, 8> &ArgList) const {
3646 llvm::IntegerType *IntTy =
3647 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003648
3649 // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
3650 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
3651 ArgList.push_back(IntTy);
3652
3653 // If necessary, add one more integer type to ArgList.
3654 unsigned R = TySize % (MinABIStackAlignInBytes * 8);
3655
3656 if (R)
3657 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003658}
3659
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003660// In N32/64, an aligned double precision floating point field is passed in
3661// a register.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003662llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
Akira Hatanakac359f202012-07-03 19:24:06 +00003663 SmallVector<llvm::Type*, 8> ArgList, IntArgList;
3664
3665 if (IsO32) {
3666 CoerceToIntArgs(TySize, ArgList);
3667 return llvm::StructType::get(getVMContext(), ArgList);
3668 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003669
Akira Hatanaka2afd23d2012-01-12 00:52:17 +00003670 if (Ty->isComplexType())
3671 return CGT.ConvertType(Ty);
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00003672
Akira Hatanakaa34e9212012-02-09 19:54:16 +00003673 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003674
Akira Hatanakac359f202012-07-03 19:24:06 +00003675 // Unions/vectors are passed in integer registers.
3676 if (!RT || !RT->isStructureOrClassType()) {
3677 CoerceToIntArgs(TySize, ArgList);
3678 return llvm::StructType::get(getVMContext(), ArgList);
3679 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003680
3681 const RecordDecl *RD = RT->getDecl();
3682 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003683 assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003684
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003685 uint64_t LastOffset = 0;
3686 unsigned idx = 0;
3687 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
3688
Akira Hatanakaa34e9212012-02-09 19:54:16 +00003689 // Iterate over fields in the struct/class and check if there are any aligned
3690 // double fields.
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003691 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3692 i != e; ++i, ++idx) {
David Blaikie262bc182012-04-30 02:36:29 +00003693 const QualType Ty = i->getType();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003694 const BuiltinType *BT = Ty->getAs<BuiltinType>();
3695
3696 if (!BT || BT->getKind() != BuiltinType::Double)
3697 continue;
3698
3699 uint64_t Offset = Layout.getFieldOffset(idx);
3700 if (Offset % 64) // Ignore doubles that are not aligned.
3701 continue;
3702
3703 // Add ((Offset - LastOffset) / 64) args of type i64.
3704 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
3705 ArgList.push_back(I64);
3706
3707 // Add double type.
3708 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
3709 LastOffset = Offset + 64;
3710 }
3711
Akira Hatanakac359f202012-07-03 19:24:06 +00003712 CoerceToIntArgs(TySize - LastOffset, IntArgList);
3713 ArgList.append(IntArgList.begin(), IntArgList.end());
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003714
3715 return llvm::StructType::get(getVMContext(), ArgList);
3716}
3717
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003718llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003719 assert((Offset % MinABIStackAlignInBytes) == 0);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003720
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003721 if ((Align - 1) & Offset)
3722 return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
3723
3724 return 0;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003725}
Akira Hatanaka9659d592012-01-10 22:44:52 +00003726
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003727ABIArgInfo
3728MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003729 uint64_t OrigOffset = Offset;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003730 uint64_t TySize = getContext().getTypeSize(Ty);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003731 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003732
Akira Hatanakac359f202012-07-03 19:24:06 +00003733 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
3734 (uint64_t)StackAlignInBytes);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003735 Offset = llvm::RoundUpToAlignment(Offset, Align);
3736 Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003737
Akira Hatanakac359f202012-07-03 19:24:06 +00003738 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
Akira Hatanaka619e8872011-06-02 00:09:17 +00003739 // Ignore empty aggregates.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003740 if (TySize == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00003741 return ABIArgInfo::getIgnore();
3742
Akira Hatanaka511949b2011-08-01 18:09:58 +00003743 // Records with non trivial destructors/constructors should not be passed
3744 // by value.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003745 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003746 Offset = OrigOffset + MinABIStackAlignInBytes;
Akira Hatanaka511949b2011-08-01 18:09:58 +00003747 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003748 }
Akira Hatanaka511949b2011-08-01 18:09:58 +00003749
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003750 // If we have reached here, aggregates are passed directly by coercing to
3751 // another structure type. Padding is inserted if the offset of the
3752 // aggregate is unaligned.
3753 return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
3754 getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00003755 }
3756
3757 // Treat an enum type as its underlying type.
3758 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3759 Ty = EnumTy->getDecl()->getIntegerType();
3760
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003761 if (Ty->isPromotableIntegerType())
3762 return ABIArgInfo::getExtend();
3763
3764 return ABIArgInfo::getDirect(0, 0, getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00003765}
3766
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003767llvm::Type*
3768MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakada54ff32012-02-09 18:49:26 +00003769 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakac359f202012-07-03 19:24:06 +00003770 SmallVector<llvm::Type*, 8> RTList;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003771
Akira Hatanakada54ff32012-02-09 18:49:26 +00003772 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003773 const RecordDecl *RD = RT->getDecl();
Akira Hatanakada54ff32012-02-09 18:49:26 +00003774 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3775 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003776
Akira Hatanakada54ff32012-02-09 18:49:26 +00003777 // N32/64 returns struct/classes in floating point registers if the
3778 // following conditions are met:
3779 // 1. The size of the struct/class is no larger than 128-bit.
3780 // 2. The struct/class has one or two fields all of which are floating
3781 // point types.
3782 // 3. The offset of the first field is zero (this follows what gcc does).
3783 //
3784 // Any other composite results are returned in integer registers.
3785 //
3786 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
3787 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
3788 for (; b != e; ++b) {
David Blaikie262bc182012-04-30 02:36:29 +00003789 const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003790
Akira Hatanakada54ff32012-02-09 18:49:26 +00003791 if (!BT || !BT->isFloatingPoint())
3792 break;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003793
David Blaikie262bc182012-04-30 02:36:29 +00003794 RTList.push_back(CGT.ConvertType(b->getType()));
Akira Hatanakada54ff32012-02-09 18:49:26 +00003795 }
3796
3797 if (b == e)
3798 return llvm::StructType::get(getVMContext(), RTList,
3799 RD->hasAttr<PackedAttr>());
3800
3801 RTList.clear();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003802 }
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003803 }
3804
Akira Hatanakac359f202012-07-03 19:24:06 +00003805 CoerceToIntArgs(Size, RTList);
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003806 return llvm::StructType::get(getVMContext(), RTList);
3807}
3808
Akira Hatanaka619e8872011-06-02 00:09:17 +00003809ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanakaa8536c02012-01-23 23:18:57 +00003810 uint64_t Size = getContext().getTypeSize(RetTy);
3811
3812 if (RetTy->isVoidType() || Size == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00003813 return ABIArgInfo::getIgnore();
3814
Akira Hatanaka8aeb1472012-05-11 21:01:17 +00003815 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003816 if (Size <= 128) {
3817 if (RetTy->isAnyComplexType())
3818 return ABIArgInfo::getDirect();
3819
Akira Hatanakac359f202012-07-03 19:24:06 +00003820 // O32 returns integer vectors in registers.
3821 if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())
3822 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
3823
Akira Hatanaka526cdfb2012-02-08 01:31:22 +00003824 if (!IsO32 && !isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003825 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
3826 }
Akira Hatanaka619e8872011-06-02 00:09:17 +00003827
3828 return ABIArgInfo::getIndirect(0);
3829 }
3830
3831 // Treat an enum type as its underlying type.
3832 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3833 RetTy = EnumTy->getDecl()->getIntegerType();
3834
3835 return (RetTy->isPromotableIntegerType() ?
3836 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3837}
3838
3839void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanakacc662542012-01-12 01:10:09 +00003840 ABIArgInfo &RetInfo = FI.getReturnInfo();
3841 RetInfo = classifyReturnType(FI.getReturnType());
3842
3843 // Check if a pointer to an aggregate is passed as a hidden argument.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003844 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
Akira Hatanakacc662542012-01-12 01:10:09 +00003845
Akira Hatanaka619e8872011-06-02 00:09:17 +00003846 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3847 it != ie; ++it)
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003848 it->info = classifyArgumentType(it->type, Offset);
Akira Hatanaka619e8872011-06-02 00:09:17 +00003849}
3850
3851llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3852 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003853 llvm::Type *BP = CGF.Int8PtrTy;
3854 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003855
3856 CGBuilderTy &Builder = CGF.Builder;
3857 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3858 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003859 int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003860 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3861 llvm::Value *AddrTyped;
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003862 unsigned PtrWidth = getContext().getTargetInfo().getPointerWidth(0);
3863 llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003864
3865 if (TypeAlign > MinABIStackAlignInBytes) {
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003866 llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
3867 llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
3868 llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
3869 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003870 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
3871 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
3872 }
3873 else
3874 AddrTyped = Builder.CreateBitCast(Addr, PTy);
3875
3876 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003877 TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003878 uint64_t Offset =
3879 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
3880 llvm::Value *NextAddr =
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003881 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003882 "ap.next");
3883 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3884
3885 return AddrTyped;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003886}
3887
John McCallaeeb7012010-05-27 06:19:26 +00003888bool
3889MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3890 llvm::Value *Address) const {
3891 // This information comes from gcc's implementation, which seems to
3892 // as canonical as it gets.
3893
John McCallaeeb7012010-05-27 06:19:26 +00003894 // Everything on MIPS is 4 bytes. Double-precision FP registers
3895 // are aliased to pairs of single-precision FP registers.
Chris Lattner8b418682012-02-07 00:39:47 +00003896 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCallaeeb7012010-05-27 06:19:26 +00003897
3898 // 0-31 are the general purpose registers, $0 - $31.
3899 // 32-63 are the floating-point registers, $f0 - $f31.
3900 // 64 and 65 are the multiply/divide registers, $hi and $lo.
3901 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattner8b418682012-02-07 00:39:47 +00003902 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCallaeeb7012010-05-27 06:19:26 +00003903
3904 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
3905 // They are one bit wide and ignored here.
3906
3907 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
3908 // (coprocessor 1 is the FP unit)
3909 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
3910 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
3911 // 176-181 are the DSP accumulator registers.
Chris Lattner8b418682012-02-07 00:39:47 +00003912 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCallaeeb7012010-05-27 06:19:26 +00003913 return false;
3914}
3915
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003916//===----------------------------------------------------------------------===//
3917// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
3918// Currently subclassed only to implement custom OpenCL C function attribute
3919// handling.
3920//===----------------------------------------------------------------------===//
3921
3922namespace {
3923
3924class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
3925public:
3926 TCETargetCodeGenInfo(CodeGenTypes &CGT)
3927 : DefaultTargetCodeGenInfo(CGT) {}
3928
3929 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3930 CodeGen::CodeGenModule &M) const;
3931};
3932
3933void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3934 llvm::GlobalValue *GV,
3935 CodeGen::CodeGenModule &M) const {
3936 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3937 if (!FD) return;
3938
3939 llvm::Function *F = cast<llvm::Function>(GV);
3940
David Blaikie4e4d0842012-03-11 07:00:24 +00003941 if (M.getLangOpts().OpenCL) {
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003942 if (FD->hasAttr<OpenCLKernelAttr>()) {
3943 // OpenCL C Kernel functions are not subject to inlining
Bill Wendlingfac63102012-10-10 03:13:20 +00003944 F->addFnAttr(llvm::Attributes::NoInline);
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003945
3946 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
3947
3948 // Convert the reqd_work_group_size() attributes to metadata.
3949 llvm::LLVMContext &Context = F->getContext();
3950 llvm::NamedMDNode *OpenCLMetadata =
3951 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
3952
3953 SmallVector<llvm::Value*, 5> Operands;
3954 Operands.push_back(F);
3955
Chris Lattner8b418682012-02-07 00:39:47 +00003956 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
3957 llvm::APInt(32,
3958 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
3959 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
3960 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003961 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
Chris Lattner8b418682012-02-07 00:39:47 +00003962 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
3963 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003964 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
3965
3966 // Add a boolean constant operand for "required" (true) or "hint" (false)
3967 // for implementing the work_group_size_hint attr later. Currently
3968 // always true as the hint is not yet implemented.
Chris Lattner8b418682012-02-07 00:39:47 +00003969 Operands.push_back(llvm::ConstantInt::getTrue(Context));
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003970 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
3971 }
3972 }
3973 }
3974}
3975
3976}
John McCallaeeb7012010-05-27 06:19:26 +00003977
Tony Linthicum96319392011-12-12 21:14:55 +00003978//===----------------------------------------------------------------------===//
3979// Hexagon ABI Implementation
3980//===----------------------------------------------------------------------===//
3981
3982namespace {
3983
3984class HexagonABIInfo : public ABIInfo {
3985
3986
3987public:
3988 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3989
3990private:
3991
3992 ABIArgInfo classifyReturnType(QualType RetTy) const;
3993 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3994
3995 virtual void computeInfo(CGFunctionInfo &FI) const;
3996
3997 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3998 CodeGenFunction &CGF) const;
3999};
4000
4001class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
4002public:
4003 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
4004 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
4005
4006 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
4007 return 29;
4008 }
4009};
4010
4011}
4012
4013void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
4014 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4015 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4016 it != ie; ++it)
4017 it->info = classifyArgumentType(it->type);
4018}
4019
4020ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
4021 if (!isAggregateTypeForABI(Ty)) {
4022 // Treat an enum type as its underlying type.
4023 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4024 Ty = EnumTy->getDecl()->getIntegerType();
4025
4026 return (Ty->isPromotableIntegerType() ?
4027 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4028 }
4029
4030 // Ignore empty records.
4031 if (isEmptyRecord(getContext(), Ty, true))
4032 return ABIArgInfo::getIgnore();
4033
4034 // Structures with either a non-trivial destructor or a non-trivial
4035 // copy constructor are always indirect.
4036 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
4037 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
4038
4039 uint64_t Size = getContext().getTypeSize(Ty);
4040 if (Size > 64)
4041 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
4042 // Pass in the smallest viable integer type.
4043 else if (Size > 32)
4044 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
4045 else if (Size > 16)
4046 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
4047 else if (Size > 8)
4048 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
4049 else
4050 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
4051}
4052
4053ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
4054 if (RetTy->isVoidType())
4055 return ABIArgInfo::getIgnore();
4056
4057 // Large vector types should be returned via memory.
4058 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
4059 return ABIArgInfo::getIndirect(0);
4060
4061 if (!isAggregateTypeForABI(RetTy)) {
4062 // Treat an enum type as its underlying type.
4063 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4064 RetTy = EnumTy->getDecl()->getIntegerType();
4065
4066 return (RetTy->isPromotableIntegerType() ?
4067 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4068 }
4069
4070 // Structures with either a non-trivial destructor or a non-trivial
4071 // copy constructor are always indirect.
4072 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
4073 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
4074
4075 if (isEmptyRecord(getContext(), RetTy, true))
4076 return ABIArgInfo::getIgnore();
4077
4078 // Aggregates <= 8 bytes are returned in r0; other aggregates
4079 // are returned indirectly.
4080 uint64_t Size = getContext().getTypeSize(RetTy);
4081 if (Size <= 64) {
4082 // Return in the smallest viable integer type.
4083 if (Size <= 8)
4084 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
4085 if (Size <= 16)
4086 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
4087 if (Size <= 32)
4088 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
4089 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
4090 }
4091
4092 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
4093}
4094
4095llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner8b418682012-02-07 00:39:47 +00004096 CodeGenFunction &CGF) const {
Tony Linthicum96319392011-12-12 21:14:55 +00004097 // FIXME: Need to handle alignment
Chris Lattner8b418682012-02-07 00:39:47 +00004098 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Tony Linthicum96319392011-12-12 21:14:55 +00004099
4100 CGBuilderTy &Builder = CGF.Builder;
4101 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
4102 "ap");
4103 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
4104 llvm::Type *PTy =
4105 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
4106 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
4107
4108 uint64_t Offset =
4109 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
4110 llvm::Value *NextAddr =
4111 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
4112 "ap.next");
4113 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
4114
4115 return AddrTyped;
4116}
4117
4118
Chris Lattnerea044322010-07-29 02:01:43 +00004119const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004120 if (TheTargetCodeGenInfo)
4121 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004122
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004123 const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00004124 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004125 default:
Chris Lattnerea044322010-07-29 02:01:43 +00004126 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004127
Derek Schuff9ed63f82012-09-06 17:37:28 +00004128 case llvm::Triple::le32:
4129 return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
John McCallaeeb7012010-05-27 06:19:26 +00004130 case llvm::Triple::mips:
4131 case llvm::Triple::mipsel:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004132 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCallaeeb7012010-05-27 06:19:26 +00004133
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00004134 case llvm::Triple::mips64:
4135 case llvm::Triple::mips64el:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004136 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00004137
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004138 case llvm::Triple::arm:
4139 case llvm::Triple::thumb:
Sandeep Patel34c1af82011-04-05 00:23:47 +00004140 {
4141 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00004142
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004143 if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
Sandeep Patel34c1af82011-04-05 00:23:47 +00004144 Kind = ARMABIInfo::APCS;
4145 else if (CodeGenOpts.FloatABI == "hard")
4146 Kind = ARMABIInfo::AAPCS_VFP;
4147
Derek Schuff263366f2012-10-16 22:30:41 +00004148 switch (Triple.getOS()) {
4149 case llvm::Triple::NativeClient:
4150 return *(TheTargetCodeGenInfo =
4151 new NaClARMTargetCodeGenInfo(Types, Kind));
4152 default:
4153 return *(TheTargetCodeGenInfo =
4154 new ARMTargetCodeGenInfo(Types, Kind));
4155 }
Sandeep Patel34c1af82011-04-05 00:23:47 +00004156 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004157
John McCallec853ba2010-03-11 00:10:12 +00004158 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00004159 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
Roman Divacky0fbc4b92012-05-09 18:22:46 +00004160 case llvm::Triple::ppc64:
Bill Schmidt2fc107f2012-10-03 19:18:57 +00004161 if (Triple.isOSBinFormatELF())
4162 return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types));
4163 else
4164 return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00004165
Peter Collingbourneedb66f32012-05-20 23:28:41 +00004166 case llvm::Triple::nvptx:
4167 case llvm::Triple::nvptx64:
Justin Holewinski2c585b92012-05-24 17:43:12 +00004168 return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types));
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004169
Wesley Peck276fdf42010-12-19 19:57:51 +00004170 case llvm::Triple::mblaze:
4171 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
4172
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004173 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00004174 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004175
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004176 case llvm::Triple::tce:
4177 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
4178
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004179 case llvm::Triple::x86: {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004180 bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004181
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00004182 if (Triple.isOSDarwin())
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004183 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004184 new X86_32TargetCodeGenInfo(Types, true, true, DisableMMX, false,
4185 CodeGenOpts.NumRegisterParameters));
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00004186
4187 switch (Triple.getOS()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004188 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004189 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00004190 case llvm::Triple::AuroraUX:
4191 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00004192 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004193 case llvm::Triple::OpenBSD:
Eli Friedman42f74f22012-08-08 23:57:20 +00004194 case llvm::Triple::Bitrig:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004195 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004196 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX,
4197 false,
4198 CodeGenOpts.NumRegisterParameters));
Eli Friedman55fc7e22012-01-25 22:46:34 +00004199
4200 case llvm::Triple::Win32:
4201 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004202 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX, true,
4203 CodeGenOpts.NumRegisterParameters));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004204
4205 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004206 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004207 new X86_32TargetCodeGenInfo(Types, false, false, DisableMMX,
4208 false,
4209 CodeGenOpts.NumRegisterParameters));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004210 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004211 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004212
Eli Friedmanee1ad992011-12-02 00:11:43 +00004213 case llvm::Triple::x86_64: {
4214 bool HasAVX = strcmp(getContext().getTargetInfo().getABI(), "avx") == 0;
4215
Chris Lattnerf13721d2010-08-31 16:44:54 +00004216 switch (Triple.getOS()) {
4217 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00004218 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00004219 case llvm::Triple::Cygwin:
4220 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
Derek Schuff263366f2012-10-16 22:30:41 +00004221 case llvm::Triple::NativeClient:
4222 return *(TheTargetCodeGenInfo = new NaClX86_64TargetCodeGenInfo(Types, HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00004223 default:
Eli Friedmanee1ad992011-12-02 00:11:43 +00004224 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
4225 HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00004226 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004227 }
Tony Linthicum96319392011-12-12 21:14:55 +00004228 case llvm::Triple::hexagon:
4229 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
Eli Friedmanee1ad992011-12-02 00:11:43 +00004230 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004231}